summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/resources/ext.srf.api.results.js
blob: f6c1f04b87c66affa82b24c040f02f3ac5bd3494 (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
/**
 * SRF JavaScript for the api/results
 *
 * @since 1.9
 * @release 0.1
 *
 * @file
 * @ingroup SRF
 *
 * @licence GNU GPL v2 or later
 * @author mwjames
 */
/* global wgMonthNames:true */
( function( $, mw, srf ) {
 'use strict';

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

	var timeLocales = {
		monthNames: ['January','February','March','April','May','June','July','August','September','October','November','December'],
		monthNamesShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
		dayNames: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
		dayNamesShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
		amDesignator: 'AM',
		pmDesignator: 'PM'
	};

	// Map wgMonthNames and create an indexed array
	var monthNames = [];
	$.map ( mw.config.get( 'wgMonthNames' ), function( value, key ) {
		if( value !== '' ){
			monthNames.push( value );
		}
	} );

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

	/**
	 * API namespace declaration
	 *
	 * @since 1.9
	 * @type Object
	 */
	srf.api = srf.api || {};

	/**
	 * Base constructor for objects representing a api.results instance
	 *
	 * @since 1.9
	 */
	srf.api.results = function() {};
	srf.api.util = function() {};

	/**
	 * Public to access results information retrieved through the SMWAPI
	 *
	 * @since 1.9
	 * @type Object
	 */
	srf.api.util.prototype = {

		/**
		 * Array helper functions
		 *
		 * @since 1.9
		 */
		array:{
			/**
			 * Array unique function
			 *
			 * $.unique only allows to be an array of DOM elements therefore
			 * this returns a nromal "array" with no duplicates
			 *
			 * @credits http://paulirish.com/2010/duck-punching-with-jquery/
			 *
			 * @since 1.9
			 */
			unique: function( arr ){
				if ( !!arr[0].nodeType ){
					return $.unique.apply( this, arguments );
				} else {
					return $.grep(arr,function(v,k){ return $.inArray(v,arr) === k; } );
				}
			}
		}
	};

	/**
	 * Public to access results information retrieved through the SMWAPI
	 *
	 * @since 1.9
	 * @type Object
	 */
	srf.api.results.prototype = {

		/**
		 * Collection related to printrequests and properties
		 *
		 * @since 1.9
		 */
		printrequests: function( printrequests ){
			var list = printrequests;

			return {
				list: list,

				/**
				 * Returns a key reference array
				 *
				 * Transforms printrequest objects into an accessible flat array
				 * allowing a key reference
				 *
				 * Call as class instance via printrequests( [...] ).toArray()
				 * or as operational method via printrequests().toArray( [...] )
				 *
				 * @since 1.9
				 * @type Object
				 */
				toArray: function ( printrequests ){
					var tList = {};
					printrequests = printrequests || list;

					if ( printrequests !== undefined ){
						$.map( printrequests, function( key, index ) {
							tList[key.label] = { typeid: key.typeid, position: index, meta: key.meta };
						} );
					}
					return list = tList;
				},

				/**
				 * Returns typeid for a property
				 *
				 * @since 1.9
				 * @type Object
				 */
				getTypeId: function ( property ){
					return list[property].typeid || null;
				},

				/**
				 * Returns some meta data for a property
				 *
				 * @since 1.9
				 * @type Object
				 */
				getMetaData: function ( property ){
					return list[property].meta || null;
				},

				/**
				 * Returns the position for a property
				 *
				 * Printouts in the result object are not sorted
				 * therefore this returns its position in accordance with the query
				 *
				 * @since 1.9
				 */
				getPosition: function ( property ){
					return list[property].position || null;
				}
			};
		},

		/**
		 * Collection related to data values
		 *
		 * @since 1.9
		 */
		dataValues: {

			/**
			 * Methods related to time data value
			 *
			 * @since 1.9
			 */
			time: {

				/**
				 * Returns normalized timestamp as JS date object
				 *
				 * @since 1.9
				 * @type Object
				 */
				parseDate: function( d ) {
					if ( typeof d === 'object') {
						return d;
					}
					if ( typeof d === 'number' ) {
						return new Date( d * 1000 );
					}
					if ( typeof d === 'string' ) {
						if ( d.match(/^\d+(\.\d+)?$/) ) {
							return new Date( parseFloat( d ) * 1000 );
						}
					}
					return null;
				},

				/**
				 * Create a new JavasScript date object based on the timestamp and return
				 * an ISO string
				 *
				 * @see SMWTimeValue::getISO8601Date()
				 *
				 * @since 1.9
				 * @type Object
				 */
				getISO8601Date: function( timestamp ) {
					var d = this.parseDate( timestamp );
					return d !== null ? d.toISOString() : null;
				},

				/**
				 * Returns a formatted time (HH:MM:SS)
				 *
				 * @param string|Date time
				 * @return string
				 */
				getTime: function( time ) {
					var d = typeof time === 'object' ? time : this.parseDate( time );
					return ( d.getHours() < 10 ? '0' + d.getHours() : d.getHours() ) +
						':' + ( d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes() ) +
						':' + ( d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() );
				},

				/**
				 * Returns a formatted date
				 *
				 * @param string|Date
				 * @param string format
				 * @return string
				 */
				getDate: function( date, format ) {
					var d = typeof date === 'object' ? date : this.parseDate( date ),
						formatDate = '';

					switch( format ) {
						case 'dmY':
							formatDate = d.getDate() + '.' + ( '' + d.getMonth() + 1 ) + '.' + d.getFullYear();
							break;
						case 'Ymd':
							formatDate = d.getFullYear() + '.' + ( '' + d.getMonth() + 1 ) + '.' + d.getDate();
							break;
						default:
							formatDate = d.getDate() + ' ' + monthNames[d.getMonth()] + ' ' + d.getFullYear();
					}

					return formatDate;
				},

				/**
				 * Returns date
				 *
				 * @param string timestamp
				 * @param string format
				 * @return string
				 */
				getMediaWikiDate: function( timestamp, format ) {
					var date = this.parseDate( timestamp );
					return this.getDate( date, format ) + ' ' + this.getTime( date );
				}
			}
		}
	};

} )( jQuery, mediaWiki, semanticFormats );