summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/resources/ext.srf.util.js
blob: a9c687cafeff5103c4dbaf5e213e8f125fffd636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*!
 * This file is part of the Semantic Result Formats Extension
 * @see https://www.semantic-mediawiki.org/wiki/SRF
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 *
 * @since 1.8
 * @ingroup SRF
 *
 * @licence GNU GPL v2+
 * @author mwjames
 */
( function( $, mw, srf ) {
 'use strict';

	////////////////////////// PRIVATE METHODS //////////////////////////

	var html = mw.html;

	var _cacheTime = 1000 * 60 * 60 * 24; // 24 hours

	var _CL_mwspinner   = 'mw-small-spinner';
	var _CL_srfIspinner = 'srf-spinner-img';
	var _CL_srfspinner  = 'srf-spinner';

	////////////////////////// PUBLIC METHODS /////////////////////////

	/**
	 * Module for formats utilities namespace
	 * @since 1.8
	 * @type Object
	 */
	srf.util = srf.util || {};

	/**
	 * Constructor
	 * @var Object
	 */
	srf.util = function( settings ) {
		$.extend( this, settings );
	};

	srf.util.prototype = {
		/**
		 * Get image url
		 * @since 1.8
		 * @param options
		 * @param callback
		 * @return string
		 */
		getImageURL: function( options, callback ) {
			var title = options.title,
				cacheTime = options.cachetime;

			// Get cache time
			cacheTime = cacheTime === undefined ? _cacheTime : cacheTime;

			// Get url from cache otherwise do an ajax call
			var url = $.jStorage.get( title );

			if ( url !== null ) {
				if ( typeof callback === 'function' ) { // make sure the callback is a function
					callback.call( this, url ); // brings the scope to the callback
				}
				return;
			}

			// Get url via ajax
			$.getJSON(
			mw.config.get( 'wgScriptPath' ) + '/api.php',
			{
				'action': 'query',
				'format': 'json',
				'prop'  : 'imageinfo',
				'iiprop': 'url',
				'titles': title
			},
			function( data ) {
				if ( data.query && data.query.pages ) {
					var pages = data.query.pages;
					for ( var p in pages ) {
						if ( pages.hasOwnProperty( p ) ) {
							var info = pages[p].imageinfo;
							for ( var i in info ) {
								if ( info.hasOwnProperty( i ) ) {
									$.jStorage.set( title , info[i].url, { TTL: cacheTime } );
									if ( typeof callback === 'function' ) { // make sure the callback is a function
										callback.call( this, info[i].url ); // brings the scope to the callback
									}
									return;
								}
							}
						}
					}
				}
				if ( typeof callback === 'function' ) { // make sure the callback is a function
					callback.call( this, false ); // brings the scope to the callback
				}
				}
			);
		},

		/**
		 * Get title url
		 * @since 1.8
		 * @param options
		 * @param callback
		 * @return string
		 */
		getTitleURL: function( options, callback ) {
			var title = options.title,
				cacheTime = options.cachetime;

			// Get cache time
			cacheTime = cacheTime === undefined ? _cacheTime : cacheTime;

			// Get url from cache otherwise do an ajax call
			var url = $.jStorage.get( title );
			if ( url !== null ) {
				if ( typeof callback === 'function' ) { // make sure the callback is a function
					callback.call( this, url ); // brings the scope to the callback
				}
				return;
			}

			// Get url via ajax
			$.getJSON(
				mw.config.get( 'wgScriptPath' ) + '/api.php',
				{
					'action': 'query',
					'format': 'json',
					'prop'  : 'info',
					'inprop': 'url',
					'titles': title
				},
				function( data ) {
					if ( data.query && data.query.pages ) {
						var pages = data.query.pages;
						for ( var p in pages ) {
							if ( pages.hasOwnProperty( p ) ) {
								var info = pages[p];
									$.jStorage.set( title, info.fullurl, { TTL: cacheTime } );
									if ( typeof callback === 'function' ) { // make sure the callback is a function
										callback.call( this, info.fullurl ); // brings the scope to the callback
									}
									return;
							}
						}
					}
				if ( typeof callback === 'function' ) { // make sure the callback is a function
					callback.call( this, false ); // brings the scope to the callback
				}
				}
			);
		},

		/**
		 * Get spinner for a local element
		 * @since 1.8
		 * @param options
		 * @return object
		 */
		spinner: {
			create: function( options ) {

				// Select the object from its context and determine height and width
				var obj = options.context.find( options.selector ),
					h = mw.html,
					width  = obj.width(),
					height = obj.height();

				// Add spinner to target object
				obj.after( h.element( 'span', { 'class' : _CL_srfIspinner + ' ' + _CL_mwspinner }, null ) );

				// Adopt height and width to avoid clutter
				options.context.find( '.' + _CL_srfIspinner + '.' + _CL_mwspinner )
					.css( { width: width, height: height } )
					.data ( 'spinner', obj ); // Attach the original object as data element
				obj.remove(); // Could just hide the element instead of removing it

			},
			replace: function ( options ){
				// Replace spinner and restore original instance
				options.context.find( '.' + _CL_srfIspinner + '.' + _CL_mwspinner )
					.replaceWith( options.context.find( '.' + _CL_srfIspinner ).data( 'spinner' ) ) ;
			},
			hide: function ( options ){
				var c = options.length === undefined ? options.context : options;
				c.find( '.' + _CL_srfspinner ).hide();
			}
		},

		/**
		 * Convenience method to check if some options are supported
		 *
		 * @since 1.9
		 *
		 * @param {string} option
		 *
		 * @return boolean
		 */
		assert: function( option ) {
			switch ( option ){
				case 'canvas':
					// Checks if the current browser supports canvas
					// @see http://goo.gl/9PYP3
					return !!window.HTMLCanvasElement;
				case 'svg':
					// Checks if the current browser supports svg
					return !!window.SVGSVGElement;
				default:
					return false;
			}
		},

		/**
		 * Convenience method for growl-like notifications using the blockUI plugin
		 *
		 * @since 1.9
		 * @var options
		 * @return object
		 */
		notification: {
			create: function( options ) {
				return $.blockUI( {
					message: html.element( 'span', { 'class' : 'srf-notification-content' }, new html.Raw( options.content ) ),
					fadeIn: 700,
					fadeOut: 700,
					timeout: 2000,
					showOverlay: false,
					centerY: false,
					css: {
						'width': '235px',
						'line-height': '1.35',
						'z-index': '10000',
						'top': '10px',
						'left': '',
						'right': '15px',
						'padding': '0.25em 1em',
						'margin-bottom': '0.5em',
						'border': '0px solid #fff',
						'background-color': options.color || '#000',
						'opacity': '.6',
						'cursor': 'pointer',
						'-webkit-border-radius': '5px',
						'-moz-border-radius': '5px',
						'border-radius': '5px',
						'-webkit-box-shadow': '0 2px 10px 0 rgba(0,0,0,0.125)',
						'-moz-box-shadow': '0 2px 10px 0 rgba(0,0,0,0.125)',
						'box-shadow': '0 2px 10px 0 rgba(0,0,0,0.125)'
					}
				} );
			}
		},

		/**
		 * Convenience method for ui-widget like error/info messages
		 *
		 * @since 1.9
		 * @var options
		 * @return object
		 */
		message:{
			set: function( options ){
				var type = options.type === 'error' ? 'ui-state-error' : 'ui-state-highlight';
				options.context.prepend( html.element( 'div', {
					'class': 'ui-widget' }, new html.Raw( html.element( 'div', {
						'class': type + ' ui-corner-all','style': 'padding-left: 0.5em' }, new html.Raw( html.element( 'p', { }, new html.Raw( html.element( 'span', { 'class': 'ui-icon ui-icon-alert', 'style': 'float: left; margin-right: 0.7em;' }, '' ) + options.message ) ) ) ) ) ) );
			},

			exception: function( options ){
				this.set( $.extend( {}, { type: 'error' }, options ) );
				throw new Error( options.message );
			},

			remove:function( context ){
				context.children().fadeOut( 'slow' ).remove();
			}
		},

		/**
		 *
		 *
		 *
		 */
		 image: {

			/**
			 * Returns image information including thumbnail
			 *
			 * @since  1.9
			 *
			 * @param options
			 * @param callback
			 *
			 * @return object
			 */
			imageInfo: function( options, callback ){
				var isCached = true;

				// Get cache otherwise do an Ajax call
				if ( options.cache ) {
					var imageInfo = $.jStorage.get( options.title + '-' + options.width );

					if ( imageInfo !== null ) {
						if ( typeof callback === 'function' ) {
							callback.call( this, isCached, imageInfo );
						}
						return;
					}
				}

				$.getJSON( mw.config.get( 'wgScriptPath' ) + '/api.php', {
						'action': 'query',
						'format': 'json',
						'prop'  : 'imageinfo',
						'iiprop': 'url',
						'iiurlwidth': options.width,
						'iiurlheight': options.height,
						'titles': options.title
					},
					function( data ) {
						if ( data.query && data.query.pages ) {
							var pages = data.query.pages;
							for ( var p in pages ) {
								if ( pages.hasOwnProperty( p ) ) {
									var info = pages[p];
									if ( options.cache !== undefined ) {
										$.jStorage.set( options.title + '-' + options.width , info, { TTL: options.cache } );
									}
									if ( typeof callback === 'function' ) {
										callback.call( this, !isCached, info );
									}
									return;
								}
							}
						}
					if ( typeof callback === 'function' ) {
						callback.call( this, !isCached, false );
					}
					}
				);
			}

		 }
	};

} )( jQuery, mediaWiki, semanticFormats );