summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/res/smw/data/ext.smw.data.js
blob: 75644105a5933b9f74e27c69c171fafebe1f97c0 (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
/**
 * This file is part of the Semantic MediaWiki JavaScript DataItem module
 * @see https://semantic-mediawiki.org/
 *
 * @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
 *
 * @file
 * @ignore
 *
 * @since 1.9
 * @ingroup SMW
 *
 * @licence GNU GPL v2+
 * @author mwjames
 */
( function( $, mw, smw ) {
	'use strict';

	/**
	 * Constructor to create an object to interact with data objects and Api
	 *
	 * @since 1.9
	 *
	 * @class
	 * @alias smw.data
	 * @constructor
	 */
	smw.Data = function() {};

	/* Public methods */

	smw.Data.prototype = {

		/**
		 * List of properties used
		 *
		 * @property
		 * @static
		 */
		properties: null,

		/**
		 * Factory methods that maps an JSON.parse key/value to an dataItem object
		 * This function is normally only called during smw.Api.parse/fetch()
		 *
		 * Structure will be similar to
		 *
		 * Subject (if exists is of type smw.dataItem.wikiPage otherwise a simple object)
		 * |--> property -> smw.dataItem.property
		 *         |--> smw.dataItem.wikiPage
		 *         |--> ...
		 * |--> property -> smw.dataItem.property
		 *         |--> smw.dataItem.uri
		 *         |--> ...
		 * |--> property -> smw.dataItem.property
		 *         |--> smw.dataItem.time
		 *         |--> ...
		 *
		 * @since  1.9
		 *
		 * @param {string} key
		 * @param {mixed} value
		 *
		 * @return {object}
		 */
		factory: function( key, value ) {
			var self = this;

			// Map printrequests in order to be used as key accessible reference object
			// which enables type hinting for all items that exists within in this list
			if ( key === 'printrequests' && value !== undefined ){
				var list = {};
				$.map( value, function( key, index ) {
					list[key.label] = { typeid: key.typeid, position: index };
				} );
				self.properties = list;
			}

			// Map the entire result object, for objects that have a subject as
			// full fledged head item and rebuild the entire object to ensure
			// that wikiPage is invoked at the top as well
			if ( key === 'results' ){
				var nResults = {};

				$.each( value, function( subjectName, subject ) {
					if( subject.hasOwnProperty( 'fulltext' ) ){
						var nSubject = new smw.dataItem.wikiPage( subject.fulltext, subject.fullurl, subject.namespace, subject.exists, subject.displaytitle );
						nSubject.printouts = subject.printouts;
						nResults[subjectName] = nSubject;
					} else {
						// Headless entry without a subject
						nResults = value;
					}
				} );

				return nResults;
			}

			// Map individual properties according to its type
			if ( typeof value === 'object' && self.properties !== null ){
				if ( key !== '' && value.length > 0 && self.properties.hasOwnProperty( key ) ){
					var property = new smw.dataItem.property( key ),
						typeid = self.properties[key].typeid,
						factoredValue = [];

					// Assignment of individual classes
					switch ( typeid ) {
						case '_wpg':
							$.map( value, function( w ) {
								factoredValue.push( new smw.dataItem.wikiPage( w.fulltext, w.fullurl, w.namespace, w.exists, w.displaytitle ) );
							} );
							break;
						case '_uri':
							$.map( value, function( u ) {
								factoredValue.push( new smw.dataItem.uri( u ) );
							} );
							break;
						case '_dat':
							$.map( value, function( t ) {
								// API 2.4+
								if ( t.hasOwnProperty( 'raw' ) ) {
									var time = new smw.dataItem.time( t.timestamp, t.raw );
								} else {
									var time = new smw.dataItem.time( t, undefined );
								}

								factoredValue.push( time );
							} );
							break;
						case '_num':
							$.map( value, function( n ) {
								factoredValue.push( new smw.dataItem.number( n ) );
							} );
							break;
						case '_qty':
							$.map( value, function( q ) {
								factoredValue.push( new smw.dataValue.quantity( q.value, q.unit ) );
							} );
							break;
						case '_str':
						case '_txt':
							$.map( value, function( s ) {
								factoredValue.push( new smw.dataItem.text( s, typeid ) );
							} );
							break;
						case '_geo':
							$.map( value, function( g ) {
								factoredValue.push( new smw.dataItem.geo( g, typeid ) );
							} );
							break;
						default:
							// Register all non identifiable types as unknown
							$.map( value, function( v ) {
								factoredValue.push( new smw.dataItem.unknown( v, typeid ) );
							} );
					}

					return $.extend( property, factoredValue );
				}
			}

			// Return all other values
			return value;
		}
	};

	// Alias
	smw.data = smw.Data;

} )( jQuery, mediaWiki, semanticMediaWiki );