git Merge branch 'master' of vcs.fsf.org:libreplanet-static
[libreplanet-static.git] / 2019 / assets / js / jquery.feeds.js
1 /*!
2 * jQuery Feeds v0.5
3 * https://camagu.github.com/jquery-feeds
4 *
5 * Copyright (c) 2013, Camilo Aguilar
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
9 *
10 * Includes a modified version of Simple JavaScript Templating
11 * http://ejohn.org/blog/javascript-micro-templating/
12 * Copyright (c) John Resig (http://ejohn.org)
13 * MIT licensed
14 *
15 * Date: 2013-02-18
16 */
17
18 /*jshint evil: true */
19 ( function( $ ) {
20
21 var cache = {};
22
23 $.fn.feeds = function( options ) {
24
25 var engine = {
26 service: '//ajax.googleapis.com/ajax/services/feed/load?v=1.0',
27
28 settings: {
29 loadingTemplate: '<div class="feeds-loader">Loading entries ...</div>',
30 entryTemplate: '<div class="feeds-entry feeds-source-<!=source!>">' +
31 '<a class="feeds-entry-title" target="_blank" href="<!=link!>" title="<!=title!>"><!=title!></a>' +
32 '<div class="feeds-entry-date"><!=publishedDate!></div>' +
33 '<div class="feeds-entry-contentSnippet"><!=contentSnippet!></div>' +
34 '</div>',
35 feeds: {},
36 max: -1,
37 xml: false,
38 ssl: 'auto',
39 onComplete: function( entries ) {
40
41 },
42 preprocess: function( feed ) {
43
44 }
45 },
46
47 feeds: { },
48 entries: [ ],
49
50 feedsLength: 0,
51 feedsLoaded: 0,
52
53 $element: null,
54 $loader: null,
55
56 init: function( element, options ) {
57 this.settings = $.extend( this.settings, options );
58 this.feeds = this.settings.feeds;
59
60 for ( var i in this.feeds ) {
61 if ( this.feeds.hasOwnProperty( i ) ) {
62 this.feedsLength++;
63 }
64 }
65
66 var protocol = this.settings.ssl === 'auto' ? document.location.protocol : this.settings.ssl ? 'https:' : 'http:';
67 if ( $.inArray( protocol, [ 'http:', 'https' ]) === -1 ) {
68 protocol = 'https:';
69 }
70
71 this.service = protocol + this.service;
72
73 this.$element = $( element );
74
75 var render = typeof this.settings.loadingTemplate === 'function' ? this.settings.loadingTemplate : this.tmpl( this.settings.loadingTemplate );
76 this.$loader = $( render.call( this, { } ) );
77 this.$element.html( this.$loader );
78
79 var output = this.settings.xml ? 'json_xml' : 'json';
80
81 for ( var j in this.feeds ) {
82 this.fetchFeed( j, this.feeds[ j ], this.settings.max, output );
83 }
84 },
85
86 fetchFeed: function( key, feed, max, output ) {
87 var self = this;
88
89 var cacheKey = feed + '**' + max + '**' + output;
90 if ( typeof cache[ cacheKey ] !== 'undefined' ) {
91 self.processResponse( cache[ cacheKey ], key, feed );
92 return;
93 }
94
95 $.ajax( {
96 url: this.service,
97 dataType: 'jsonp',
98 data: {
99 q: feed,
100 num: max,
101 output: output
102 },
103 beforeSend: function( ) {
104 this.feed = feed;
105 this.key = key;
106 },
107 success: function( data ) {
108 cache[ cacheKey ] = data;
109 self.processResponse( data, this.key, this.feed );
110 }
111 } );
112 },
113
114 processResponse: function( data, key, feed ) {
115 if ( data.responseStatus !== 200 ) {
116 if ( window.console && window.console.log ) {
117 console.log( 'Unable to load feed ' + feed + ': (' + data.responseStatus + ') ' + data.responseDetails );
118 }
119 } else {
120 var currentFeed = data.responseData.feed;
121 var feedEntries = currentFeed.entries;
122
123 var type = data.responseData.feed.type;
124
125 if ( this.settings.xml ) {
126 var $xml = $( data.responseData.xmlString );
127
128 if ( type.match( /^rss.*/ ) ) {
129 $xml = $xml.filter( 'rss' ).find( 'channel' );
130 } else if ( type.match( /^atom.*/ ) ) {
131 $xml = $xml.filter( 'feed' );
132 }
133
134 currentFeed.xml = $xml;
135 }
136
137 for ( var i in feedEntries ) {
138 var entry = $.extend( {}, feedEntries[ i ] );
139 entry.source = key;
140 entry.publishedDateRaw = entry.publishedDate;
141
142 entry.feedUrl = currentFeed.feedUrl;
143 entry.feedTitle = currentFeed.title;
144 entry.feedLink = currentFeed.link;
145 entry.feedDescription = currentFeed.description;
146 entry.feedAuthor = currentFeed.author;
147
148 if ( this.settings.xml ) {
149 if ( type.match( /^rss.*/ ) ) {
150 entry.xml = currentFeed.xml.find( 'item' ).eq( i );
151 } else if ( type.match( /^atom.*/ ) ) {
152 entry.xml = currentFeed.xml.find( 'entry' ).eq( i );
153 } else {
154 entry.xml = { };
155 }
156 }
157
158 if ( this.settings.preprocess.call( entry, currentFeed ) !== false ) {
159 this.entries.push( entry );
160 }
161 }
162 }
163
164 this.feedsLoaded++;
165 this.checkComplete();
166 },
167
168 checkComplete: function( ) {
169 if ( this.feedsLoaded === this.feedsLength ) {
170 this.$loader.remove( );
171
172 this.entries.sort( function( a, b) {
173 var aDate = new Date( a.publishedDateRaw ).getTime( );
174 var bDate = new Date( b.publishedDateRaw ).getTime( );
175
176 return bDate - aDate;
177 } );
178
179 var render = typeof this.settings.entryTemplate === 'function' ? this.settings.entryTemplate : this.tmpl( this.settings.entryTemplate );
180
181 for ( var i in this.entries ) {
182 var entry = this.entries[ i ];
183
184 var html = render.call( this, entry );
185
186 this.$element.append( html );
187 }
188
189 this.settings.onComplete.call( this.$element[ 0 ], this.entries );
190 }
191 },
192
193
194 // Simple JavaScript Templating (modified)
195 // John Resig - http://ejohn.org/ - MIT Licensed
196 // @see http://ejohn.org/blog/javascript-micro-templating/
197 tmplCache: {},
198 tmpl: function tmpl( str, data ) {
199
200 var fn = !/\W/.test( str ) ? this.tmplCache[ str ] = this.tmplCache[ str ] || this.tmpl( document.getElementById( str ).innerHTML ) :
201
202 new Function( "obj",
203 "var p=[],print=function(){p.push.apply(p,arguments);};" +
204
205 "with(obj){p.push('" +
206 str
207 .replace( /[\r\t\n]/g, " " )
208 .split( "<!" ).join( "\t" )
209 .replace( /((^|!>)[^\t]*)'/g, "$1\r" )
210 .replace( /\t=(.*?)!>/g, "',typeof $1 != 'undefined' ? $1 : '','" )
211 .split( "\t" ).join( "');" )
212 .split( "!>" ).join( "p.push('" )
213 .split( "\r" ).join( "\\'" ) +
214 "');}return p.join('');"
215 );
216
217 return data ? fn( data ) : fn;
218 }
219 };
220
221 return $( this ).each( function( ) {
222 engine.init( this, options );
223 });
224 };
225 }( jQuery ) );