Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #8502 from ipaksc/rsscors
Browse files Browse the repository at this point in the history
Feeds plugins: CORS enabled pulling
  • Loading branch information
duboisp committed Dec 13, 2018
2 parents 9e85c8b + 29c138f commit 2c5fdc3
Show file tree
Hide file tree
Showing 7 changed files with 1,045 additions and 16 deletions.
12 changes: 12 additions & 0 deletions site/pages/docs/ref/feeds/feeds-en.hbs
Expand Up @@ -237,6 +237,18 @@
</dl>
</td>
</tr>
<tr>
<td><code>data-cors="true"</code></td>
<td>Allows cross-site requests to fetch ATOM feed without using the Yahoo API services</td>
<td>Add the attribute <code>data-cors="true"</code></td>
<td>
<dl>
<dt>undefined (default)</dt>
<dd>Use the Yahoo API service</dd>
<dt><code>true</code></dt><dd>Retreive directly the ATOM feed</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</section>
Expand Down
13 changes: 13 additions & 0 deletions site/pages/docs/ref/feeds/feeds-fr.hbs
Expand Up @@ -239,6 +239,19 @@
</dl>
</td>
</tr>
<tr>
<td><code>data-cors="true"</code></td>
<td>Permet des requêtes inter-sites (CORS) afin d’obtenir le fil de syndication ATOM sans à devoir utiliser le service web de Yahoo.</td>
<td>Ajouter l’attribut <code>data-cors="true"</code></td>
<td>
<dl>
<dt>non-définie (défaut)</dt>
<dd>Utilise le service web de Yahoo </dd>
<dt><code>true</code></dt>
<dd>Obtient directement le fil de syndication ATOM </dd>
</dl>
</td>
</tr>
</tbody>
</table>
</section>
Expand Down
449 changes: 449 additions & 0 deletions src/plugins/feeds/demo/manitoba-en.atom.xml

Large diffs are not rendered by default.

429 changes: 429 additions & 0 deletions src/plugins/feeds/demo/manitoba-fr.atom.xml

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions src/plugins/feeds/feeds-en.hbs
Expand Up @@ -150,6 +150,28 @@
</details>
</section>
</section>

<section class="wb-feeds limit-3" data-cors="true">
<h3>CORS enabled pulling - Canada News Centre - Manitoba</h3>
<p>This method fetch the ATOM feed directly and don't use a third party service.</p>
<ul class="feeds-cont list-unstyled lst-spcd">
<li><a href="demo/manitoba-en.atom.xml" rel="external">Government of Canada News Releases</a></li>
</ul>
<section>
<h4>Code</h4>
<details>
<summary>View code</summary>
<pre><code>&lt;section class="wb-feeds limit-3" data-cors="true"&gt;
&lt;h3&gt;Canada News Centre - Manitoba&lt;/h3&gt;
&lt;ul class="feeds-cont list-unstyled lst-spcd"&gt;
&lt;li&gt;
&lt;a href="demo/manitoba-en.atom.xml"&gt;Government of Canada News Releases&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;</code></pre>
</details>
</section>
</section>
</section>

<section>
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/feeds/feeds-fr.hbs
Expand Up @@ -148,6 +148,28 @@
</details>
</section>
</section>

<section class="wb-feeds limit-3" data-cors="true">
<h3>Affichage du fils de syndication via CORS - Centre des nouvelles du Canada - Manitoba</h3>
<p>Cette méthode télécharge le fil de syndication ATOM directement sans dépendre d’un service tierce.</p>
<ul class="feeds-cont list-unstyled lst-spcd">
<li><a href="demo/manitoba-fr.atom.xml">Nouvelles du Gouvernement du Canada</a></li>
</ul>
<section>
<h4>Code</h4>
<details>
<summary>View code</summary>
<pre><code>&lt;section class="wb-feeds limit-3 data-cors="true"&gt;
&lt;h3&gt;Centre des nouvelles du Canada - Manitoba&lt;/h3&gt;
&lt;ul class="feeds-cont list-unstyled lst-spcd"&gt;
&lt;li&gt;
&lt;a href="demo/manitoba-fr.atom.xml"&gt;Nouvelles du Gouvernement du Canada&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;</code></pre>
</details>
</section>
</section>
</section>

<section>
Expand Down
114 changes: 98 additions & 16 deletions src/plugins/feeds/feeds.js
Expand Up @@ -162,10 +162,11 @@ var componentName = "wb-feeds",
// returns DOM object = proceed with init
// returns undefined = do not proceed with init (e.g., already initialized)
var elm = wb.init( event, componentName, selector ),
fetch, url, $content, limit, feeds, fType, last, i, callback, fElem, fIcon, youtubeData;
fetch, url, $content, limit, feeds, fType, last, i, callback, fElem, fIcon, youtubeData, $elm;

if ( elm ) {
$content = $( elm ).find( ".feeds-cont" );
$elm = $( elm );
$content = $elm.find( ".feeds-cont" );
limit = getLimit( elm );
feeds = $content.find( feedLinkSelector );
last = feeds.length - 1;
Expand Down Expand Up @@ -216,7 +217,14 @@ var componentName = "wb-feeds",
}

} else {
url = jsonRequest( fElem.attr( "href" ), limit );

// Detect if CORS request
if ( $elm.data( "cors" ) === true ) {
url = fElem.attr( "href" );
fetch.dataType = "xml";
} else {
url = jsonRequest( fElem.attr( "href" ), limit );
}
fetch.url = url;

// Let's bind the template to the Entries
Expand All @@ -243,11 +251,79 @@ var componentName = "wb-feeds",
}
},

/**
* Process Feed/JSON Entries for CORS Enabled
* @method corsEntry
*/
corsEntry = function( xmlDoc, limit ) {
var arr_entry = [],
corsObj = {},
limit = limit,
jsonString = JSON.stringify( xmlToJson( xmlDoc ) ),
jsonObj = JSON.parse( jsonString ),
i, iCache;
for ( i = 0; i < limit; i++ ) {
iCache = jsonObj.feed.entry[ i ];
corsObj = {
title: iCache.title[ "#text" ],
link: iCache.id[ "#text" ],
updated: iCache.updated[ "#text" ]
};
arr_entry.push( corsObj );
}
return arr_entry;
},

/**
* Process XML to JSON
* @method xmlToJson
* @param {xml}
*/
xmlToJson = function( xml ) {

var obj = {},
i, iCache, nodeName, old,
xmlAttributes, xmlChildNodes,
xmlNodeType = xml.nodeType;

if ( xmlNodeType === 1 ) {
xmlAttributes = xml.attributes;
if ( xmlAttributes.length ) {
obj[ "@attributes" ] = {};
for ( i = 0; i < xmlAttributes.length; i++ ) {
iCache = xmlAttributes.item( i );
obj[ "@attributes" ][ iCache.nodeName ] = iCache.nodeValue;
}
}
} else if ( xmlNodeType === 3 ) {
obj = xml.nodeValue;
}

if ( xml.hasChildNodes() ) {
xmlChildNodes = xml.childNodes;
for ( i = 0; i < xmlChildNodes.length; i++ ) {
iCache = xmlChildNodes.item( i );
nodeName = iCache.nodeName;
if ( typeof( obj[ nodeName ] ) === "undefined" ) {
obj[ nodeName ] = xmlToJson( iCache );
} else {
if ( typeof( obj[ nodeName ].push ) === "undefined" ) {
old = obj[ nodeName ];
obj[ nodeName ] = [];
obj[ nodeName ].push( old );
}
obj[ nodeName ].push( xmlToJson( iCache ) );
}
}
}
return obj;
},

/**
* Process Feed/JSON Entries
* @method processEntries
* @param {data} JSON formatted data to process
* @return {string} of HTML output
* @return {string} of HTML output
*/
processEntries = function( data ) {
var items = data,
Expand Down Expand Up @@ -390,30 +466,36 @@ var componentName = "wb-feeds",

$document.on( "ajax-fetched.wb data-ready.wb-feeds", selector + " " + feedLinkSelector, function( event, context ) {
var eventTarget = event.target,
data, response;
data, response, $emlRss, limit, results;

// Filter out any events triggered by descendants
if ( event.currentTarget === eventTarget ) {
$emlRss = $( eventTarget ).parentsUntil( selector ).parent();
switch ( event.type ) {
case "ajax-fetched":
response = event.fetch.response;

if ( response.query ) {
var results = response.query.results;

if ( results ) {
data = results.entry ? results.entry : results.item;
// if CORS -> transform the xml response into a JSON
if ( $emlRss.data( "cors" ) === true ) {
limit = $emlRss.attr( "class" ).match( /\blimit-\d+/ );
limit = Number( limit[ 0 ].replace( /limit-/i, "" ) );
data = corsEntry( response, limit );

if ( !Array.isArray( data ) ) {
data = [ data ];
} else {
if ( response.query ) {
results = response.query.results;
if ( results ) {
data = results.entry ? results.entry : results.item;
if ( !Array.isArray( data ) ) {
data = [ data ];
}
} else {
data = [];
}
} else {
data = [];
data = ( response.responseData ) ? response.responseData.feed.entries : response.items || response.feed.entry;
}
} else {
data = ( response.responseData ) ? response.responseData.feed.entries : response.items || response.feed.entry;
}

break;
default:
data = event.feedsData;
Expand Down

0 comments on commit 2c5fdc3

Please sign in to comment.