2 |
2 |
3 IriSP.Model = { |
3 IriSP.Model = { |
4 SOURCE_STATUS_EMPTY : 0, |
4 SOURCE_STATUS_EMPTY : 0, |
5 SOURCE_STATUS_WAITING : 1, |
5 SOURCE_STATUS_WAITING : 1, |
6 SOURCE_STATUS_READY : 2, |
6 SOURCE_STATUS_READY : 2, |
7 ID_AUTO_INCREMENT : 0 |
7 ID_AUTO_INCREMENT : 0, |
|
8 getAI : function() { |
|
9 return "autoid-" + (++this.ID_AUTO_INCREMENT); |
|
10 }, |
|
11 isoToDate : function(_str) { |
|
12 // http://delete.me.uk/2005/03/iso8601.html |
|
13 var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"; |
|
14 var d = _str.match(new RegExp(regexp)); |
|
15 |
|
16 var offset = 0; |
|
17 var date = new Date(d[1], 0, 1); |
|
18 |
|
19 if (d[3]) { date.setMonth(d[3] - 1); } |
|
20 if (d[5]) { date.setDate(d[5]); } |
|
21 if (d[7]) { date.setHours(d[7]); } |
|
22 if (d[8]) { date.setMinutes(d[8]); } |
|
23 if (d[10]) { date.setSeconds(d[10]); } |
|
24 if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } |
|
25 if (d[14]) { |
|
26 offset = (Number(d[16]) * 60) + Number(d[17]); |
|
27 offset *= ((d[15] == '-') ? 1 : -1); |
|
28 } |
|
29 |
|
30 offset -= date.getTimezoneOffset(); |
|
31 time = (Number(date) + (offset * 60 * 1000)); |
|
32 var _res = new Date(); |
|
33 _res.setTime(Number(time)); |
|
34 return _res; |
|
35 }, |
|
36 dateToIso : function(d) { |
|
37 function pad(n){return n<10 ? '0'+n : n} |
|
38 return d.getUTCFullYear()+'-' |
|
39 + pad(d.getUTCMonth()+1)+'-' |
|
40 + pad(d.getUTCDate())+'T' |
|
41 + pad(d.getUTCHours())+':' |
|
42 + pad(d.getUTCMinutes())+':' |
|
43 + pad(d.getUTCSeconds())+'Z' |
|
44 } |
8 } |
45 } |
9 |
46 |
10 /* */ |
47 /* */ |
11 |
48 |
12 IriSP.Model.List = function(_directory) { |
49 IriSP.Model.List = function(_directory) { |
13 this.contents = []; |
50 this.contents = []; |
14 this.directory = _directory; |
51 this.directory = _directory; |
|
52 if (typeof _directory == "undefined") { |
|
53 throw("Error : new IriSP.Model.List(directory): directory is undefined"); |
|
54 } |
15 } |
55 } |
16 |
56 |
17 IriSP.Model.List.prototype.toString = function() { |
57 IriSP.Model.List.prototype.toString = function() { |
18 return 'List of Elements, length=' + this.length(); |
58 return 'List of Elements, length=' + this.length(); |
19 } |
59 } |
137 return _res; |
177 return _res; |
138 } |
178 } |
139 |
179 |
140 /* */ |
180 /* */ |
141 |
181 |
142 IriSP.Model.Reference = function(_directory, _idRef) { |
182 IriSP.Model.Reference = function(_source, _idRef) { |
143 this.directory = _directory; |
183 this.source = _source; |
144 if (typeof _idRef === "object") { |
184 if (typeof _idRef === "object") { |
145 this.isList = true; |
185 this.isList = true; |
146 this.contents = new IriSP.Model.List(this.directory); |
186 this.contents = new IriSP.Model.List(this.source.directory); |
147 this.contents.addIdsFromArray(_idRef); |
187 this.contents.addIdsFromArray(IriSP._(_idRef).map(function(_id) { |
|
188 return _source.getNamespaced(_id).fullname; |
|
189 })); |
148 } else { |
190 } else { |
149 this.isList = false; |
191 this.isList = false; |
150 this.contents = _idRef; |
192 this.contents = _source.getNamespaced(_idRef).fullname; |
151 } |
193 } |
152 } |
194 } |
153 |
195 |
154 IriSP.Model.Reference.prototype.getContents = function() { |
196 IriSP.Model.Reference.prototype.getContents = function() { |
155 return (this.isList ? this.contents : this.directory.getElement(this.contents)); |
197 return (this.isList ? this.contents : this.source.directory.getElement(this.contents)); |
156 } |
198 } |
157 |
199 |
158 /* */ |
200 /* */ |
159 |
201 |
160 IriSP.Model.Element = function(_id, _source) { |
202 IriSP.Model.Element = function(_id, _source) { |
161 this.elementType = 'element'; |
203 this.elementType = 'element'; |
162 if (typeof _id !== "undefined" && typeof _source !== "undefined") { |
204 if (typeof _source !== "undefined") { |
|
205 if (typeof _id === "undefined" || !_id) { |
|
206 _id = IriSP.Model.getAI(); |
|
207 } |
163 this.source = _source; |
208 this.source = _source; |
164 this.id = _id; |
209 this.id = _source.getNamespaced(_id).fullname; |
165 this.title = ""; |
210 this.title = ""; |
166 this.description = ""; |
211 this.description = ""; |
167 this.source.directory.addElement(this); |
212 this.source.directory.addElement(this); |
168 } |
213 } |
169 } |
214 } |
171 IriSP.Model.Element.prototype.toString = function() { |
216 IriSP.Model.Element.prototype.toString = function() { |
172 return this.elementType + (this.elementType !== 'element' ? ', id=' + this.id + ', title="' + this.title + '"' : ''); |
217 return this.elementType + (this.elementType !== 'element' ? ', id=' + this.id + ', title="' + this.title + '"' : ''); |
173 } |
218 } |
174 |
219 |
175 IriSP.Model.Element.prototype.setReference = function(_elementType, _idRef) { |
220 IriSP.Model.Element.prototype.setReference = function(_elementType, _idRef) { |
176 this[_elementType] = new IriSP.Model.Reference(this.source.directory, _idRef); |
221 this[_elementType] = new IriSP.Model.Reference(this.source, _idRef); |
177 } |
222 } |
178 |
223 |
179 IriSP.Model.Element.prototype.getReference = function(_elementType) { |
224 IriSP.Model.Element.prototype.getReference = function(_elementType) { |
180 if (typeof this[_elementType] !== "undefined") { |
225 if (typeof this[_elementType] !== "undefined") { |
181 return this[_elementType].getContents(); |
226 return this[_elementType].getContents(); |
259 return this.getReference("tag"); |
304 return this.getReference("tag"); |
260 } |
305 } |
261 |
306 |
262 /* */ |
307 /* */ |
263 |
308 |
264 IriSP.Model.Source = function(_properties) { |
309 IriSP.Model.Source = function(_config) { |
265 this.status = IriSP.Model.SOURCE_STATUS_EMPTY; |
310 this.status = IriSP.Model.SOURCE_STATUS_EMPTY; |
266 this.config = _properties; |
311 if (typeof _config !== "undefined") { |
267 this.callbackQueue = []; |
312 var _this = this; |
268 this.contents = {}; |
313 IriSP._(_config).each(function(_v, _k) { |
269 this.get(); |
314 _this[_k] = _v; |
|
315 }) |
|
316 this.callbackQueue = []; |
|
317 this.contents = {}; |
|
318 if (typeof this.namespace === "undefined") { |
|
319 this.namespace = IriSP.Model.getAI(); |
|
320 } |
|
321 if (typeof this.namespaceUrl === "undefined") { |
|
322 this.namespaceUrl = (typeof this.url !== "undefined" ? this.url : this.namespaceUrl); |
|
323 } |
|
324 this.directory.namespaces[this.namespace] = this.namespaceUrl; |
|
325 this.get(); |
|
326 } |
|
327 } |
|
328 |
|
329 IriSP.Model.Source.prototype.getNamespaced = function(_id) { |
|
330 var _tab = _id.split(':'); |
|
331 if (_tab.length > 1) { |
|
332 return { |
|
333 namespace : _tab[0], |
|
334 name : _tab[1], |
|
335 fullname : _id |
|
336 } |
|
337 } else { |
|
338 return { |
|
339 namespace : this.namespace, |
|
340 name : _id, |
|
341 fullname : this.namespace + ':' + _id |
|
342 } |
|
343 } |
|
344 } |
|
345 |
|
346 IriSP.Model.Source.prototype.unNamespace = function(_id) { |
|
347 return _id.replace(this.namespace + ':', ''); |
270 } |
348 } |
271 |
349 |
272 IriSP.Model.Source.prototype.addList = function(_listId, _contents) { |
350 IriSP.Model.Source.prototype.addList = function(_listId, _contents) { |
273 if (typeof this.contents[_listId] === "undefined") { |
351 if (typeof this.contents[_listId] === "undefined") { |
274 this.contents[_listId] = new IriSP.Model.List(this.config.directory); |
352 this.contents[_listId] = new IriSP.Model.List(this.directory); |
275 } |
353 } |
276 this.contents[_listId].addIdsFromList(_contents); |
354 this.contents[_listId].addIdsFromList(_contents); |
277 } |
355 } |
278 |
356 |
279 IriSP.Model.Source.prototype.getList = function(_listId) { |
357 IriSP.Model.Source.prototype.getList = function(_listId) { |
280 if (typeof this.contents[_listId] === "undefined") { |
358 if (typeof this.contents[_listId] === "undefined") { |
281 return this.config.directory.getGlobalList.filter(function(_e) { |
359 return this.directory.getGlobalList().filter(function(_e) { |
282 return (_e.elType === _listId); |
360 return (_e.elType === _listId); |
283 }); |
361 }); |
284 } else { |
362 } else { |
285 return this.contents[_listId]; |
363 return this.contents[_listId]; |
286 } |
364 } |
287 } |
365 } |
288 |
366 |
|
367 IriSP.Model.Source.prototype.each = function(_callback) { |
|
368 var _this = this; |
|
369 IriSP._(this.contents).each(function(_value, _key) { |
|
370 _callback.call(_this, _value, _key); |
|
371 }) |
|
372 } |
|
373 |
289 IriSP.Model.Source.prototype.getElement = function(_listId, _elId) { |
374 IriSP.Model.Source.prototype.getElement = function(_listId, _elId) { |
290 var _list = this.getList(_listId); |
375 var _list = this.getList(_listId); |
291 return (typeof _list !== "undefined" ? _list.getElement(_elId) : undefined); |
376 return (typeof _list !== "undefined" ? _list.getElement(_elId) : undefined); |
292 } |
377 } |
293 |
378 |
294 IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) { |
379 IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) { |
295 if (typeof _idRef !== "undefined") { |
380 if (typeof _idRef !== "undefined") { |
296 this.currentMedia = _idRef; |
381 this.currentMedia = this.getNamespaced(_idRef).fullname; |
297 } |
382 } |
298 } |
383 } |
299 |
384 |
300 IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() { |
385 IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() { |
301 if (typeof this.currentMedia === "undefined") { |
386 if (typeof this.currentMedia === "undefined") { |
302 this.currentMedia = this.getList("media")[0]; |
387 this.currentMedia = this.getList("media")[0]; |
303 } |
388 } |
304 } |
389 } |
305 |
390 |
|
391 IriSP.Model.Source.prototype.listNamespaces = function() { |
|
392 var _this = this, |
|
393 _nsls = []; |
|
394 this.each(function(_list) { |
|
395 IriSP._(_list.contents).each(function(_id) { |
|
396 var _ns = _id.replace(/:.*$/,''); |
|
397 if (_nsls.indexOf(_ns) === -1) { |
|
398 _nsls.push(_ns); |
|
399 } |
|
400 }) |
|
401 }); |
|
402 return _nsls; |
|
403 } |
|
404 |
306 IriSP.Model.Source.prototype.get = function() { |
405 IriSP.Model.Source.prototype.get = function() { |
307 this.status = IriSP.Model.SOURCE_STATUS_WAITING; |
|
308 this.status = IriSP.Model.SOURCE_STATUS_READY; |
406 this.status = IriSP.Model.SOURCE_STATUS_READY; |
309 var _this = this; |
407 var _this = this; |
310 if (_this.callbackQueue.length) { |
408 if (_this.callbackQueue.length) { |
311 IriSP._.each(_this.callbackQueue, function(_callback) { |
409 IriSP._.each(_this.callbackQueue, function(_callback) { |
312 _callback.call(_this); |
410 _callback.call(_this); |
313 }); |
411 }); |
314 } |
412 } |
315 _this.callbackQueue = []; |
413 _this.callbackQueue = []; |
316 } |
414 } |
317 |
415 |
|
416 IriSP.Model.Source.prototype.serialize = function() { |
|
417 return this.serializer.serialize(this); |
|
418 } |
|
419 |
318 IriSP.Model.Source.prototype.addCallback = function(_callback) { |
420 IriSP.Model.Source.prototype.addCallback = function(_callback) { |
319 if (this.status === IriSP.Model.SOURCE_STATUS_READY) { |
421 if (this.status === IriSP.Model.SOURCE_STATUS_READY) { |
320 callback.call(this); |
422 callback.call(this); |
321 } else { |
423 } else { |
322 this.callbackQueue.push(_callback); |
424 this.callbackQueue.push(_callback); |
361 |
462 |
362 /* */ |
463 /* */ |
363 |
464 |
364 IriSP.Model.Directory = function() { |
465 IriSP.Model.Directory = function() { |
365 this.remoteSources = {}; |
466 this.remoteSources = {}; |
366 this.localSource = []; |
|
367 this.elements = {}; |
467 this.elements = {}; |
368 this.nameSpaces = {}; |
468 this.namespaces = {}; |
369 } |
469 } |
370 |
470 |
371 IriSP.Model.Directory.prototype.remoteSource = function(_properties) { |
471 IriSP.Model.Directory.prototype.remoteSource = function(_properties) { |
|
472 var _config = IriSP._({ directory: this }).extend(_properties); |
372 if (typeof this.remoteSources[_properties.url] === "undefined") { |
473 if (typeof this.remoteSources[_properties.url] === "undefined") { |
373 this.remoteSources[_properties.url] = new IriSP.Model.RemoteSource(_properties); |
474 this.remoteSources[_properties.url] = new IriSP.Model.RemoteSource(_config); |
374 } |
475 } |
375 return this.remoteSources[_properties.url]; |
476 return this.remoteSources[_properties.url]; |
|
477 } |
|
478 |
|
479 IriSP.Model.Directory.prototype.newLocalSource = function(_properties) { |
|
480 var _config = IriSP._({ directory: this }).extend(_properties), |
|
481 _res = new IriSP.Model.Source(_config); |
|
482 return _res; |
376 } |
483 } |
377 |
484 |
378 IriSP.Model.Directory.prototype.getElement = function(_id) { |
485 IriSP.Model.Directory.prototype.getElement = function(_id) { |
379 return this.elements[_id]; |
486 return this.elements[_id]; |
380 } |
487 } |