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() { |
8 getUID : function() { |
9 return "autoid-" + (++this._ID_AUTO_INCREMENT); |
9 return "autoid-" + (++this._ID_AUTO_INCREMENT); |
10 }, |
10 }, |
11 isoToDate : function(_str) { |
11 isoToDate : function(_str) { |
12 // http://delete.me.uk/2005/03/iso8601.html |
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})))?)?)?)?"; |
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})))?)?)?)?"; |
57 } |
57 } |
58 |
58 |
59 IriSP.Model.List.prototype = new Array(); |
59 IriSP.Model.List.prototype = new Array(); |
60 |
60 |
61 IriSP.Model.List.prototype.getElement = function(_id) { |
61 IriSP.Model.List.prototype.getElement = function(_id) { |
62 if (this.hasId(_id)) { |
62 var _index = (IriSP._(this.idIndex).indexOf(_id)); |
63 return this; |
63 if (_index !== -1) { |
|
64 return this[_index]; |
64 } |
65 } |
65 } |
66 } |
66 |
67 |
67 IriSP.Model.List.prototype.hasId = function(_id) { |
68 IriSP.Model.List.prototype.hasId = function(_id) { |
68 return (IriSP._(this.idIndex).indexOf(_id) !== -1); |
69 return (IriSP._(this.idIndex).indexOf(_id) !== -1); |
98 return _callback(_value, _key, _this); |
99 return _callback(_value, _key, _this); |
99 })); |
100 })); |
100 return _res; |
101 return _res; |
101 } |
102 } |
102 |
103 |
|
104 IriSP.Model.List.prototype.slice = function(_start, _end) { |
|
105 var _res = new IriSP.Model.List(this.directory); |
|
106 _res.addElements(Array.prototype.slice.call(this, _start, _end)); |
|
107 return _res; |
|
108 } |
|
109 |
103 /* Array has a sort function, but it's not as interesting as Underscore.js's sortBy |
110 /* Array has a sort function, but it's not as interesting as Underscore.js's sortBy |
104 * and won't return a new IriSP.Model.List |
111 * and won't return a new IriSP.Model.List |
105 */ |
112 */ |
106 IriSP.Model.List.prototype.sortBy = function(_callback) { |
113 IriSP.Model.List.prototype.sortBy = function(_callback) { |
107 var _this = this, |
114 var _this = this, |
108 _res = new IriSP.Model.List(this.directory); |
115 _res = new IriSP.Model.List(this.directory); |
109 _res.contents = IriSP._(this).sortBy(function(_value, _key) { |
116 _res.addElements(IriSP._(this).sortBy(function(_value, _key) { |
110 return _callback(_value, _key, _this); |
117 return _callback(_value, _key, _this); |
111 }); |
118 })); |
112 return _res; |
119 return _res; |
113 } |
120 } |
114 |
121 |
115 /* Title and Description are basic information for (almost) all element types, |
122 /* Title and Description are basic information for (almost) all element types, |
116 * here we can search by these criteria |
123 * here we can search by these criteria |
134 return this.filter(function(_element) { |
141 return this.filter(function(_element) { |
135 return _rgxp.test(_element.description) || _rgxp.test(_element.title); |
142 return _rgxp.test(_element.description) || _rgxp.test(_element.title); |
136 }); |
143 }); |
137 } |
144 } |
138 |
145 |
|
146 IriSP.Model.List.prototype.getTitles = function() { |
|
147 return this.map(function(_el) { |
|
148 return _el.title; |
|
149 }); |
|
150 } |
|
151 |
139 IriSP.Model.List.prototype.addId = function(_id) { |
152 IriSP.Model.List.prototype.addId = function(_id) { |
140 var _el = this.directory.getElement(_id) |
153 var _el = this.directory.getElement(_id) |
141 if (!this.hasId(_id) && typeof _el !== "undefined") { |
154 if (!this.hasId(_id) && typeof _el !== "undefined") { |
142 this.idIndex.push(_id); |
155 this.idIndex.push(_id); |
143 Array.prototype.push.call(this, _el); |
156 Array.prototype.push.call(this, _el); |
236 |
248 |
237 IriSP.Model.Element = function(_id, _source) { |
249 IriSP.Model.Element = function(_id, _source) { |
238 this.elementType = 'element'; |
250 this.elementType = 'element'; |
239 if (typeof _source !== "undefined") { |
251 if (typeof _source !== "undefined") { |
240 if (typeof _id === "undefined" || !_id) { |
252 if (typeof _id === "undefined" || !_id) { |
241 _id = IriSP.Model.getAI(); |
253 _id = IriSP.Model.getUID(); |
242 } |
254 } |
243 this.source = _source; |
255 this.source = _source; |
244 this.id = _source.getNamespaced(_id).fullname; |
256 this.namespacedId = _source.getNamespaced(_id) |
|
257 this.id = this.namespacedId.fullname; |
245 this.title = ""; |
258 this.title = ""; |
246 this.description = ""; |
259 this.description = ""; |
247 this.source.directory.addElement(this); |
260 this.source.directory.addElement(this); |
248 } |
261 } |
249 } |
262 } |
360 this.setReference("tag", _idRefs); |
373 this.setReference("tag", _idRefs); |
361 } |
374 } |
362 |
375 |
363 IriSP.Model.Annotation.prototype.getTags = function() { |
376 IriSP.Model.Annotation.prototype.getTags = function() { |
364 return this.getReference("tag"); |
377 return this.getReference("tag"); |
|
378 } |
|
379 |
|
380 IriSP.Model.Annotation.prototype.getTagTexts = function() { |
|
381 return this.getTags().getTitles(); |
365 } |
382 } |
366 |
383 |
367 /* */ |
384 /* */ |
368 |
385 |
369 IriSP.Model.Source = function(_config) { |
386 IriSP.Model.Source = function(_config) { |
374 _this[_k] = _v; |
391 _this[_k] = _v; |
375 }) |
392 }) |
376 this.callbackQueue = []; |
393 this.callbackQueue = []; |
377 this.contents = {}; |
394 this.contents = {}; |
378 if (typeof this.namespace === "undefined") { |
395 if (typeof this.namespace === "undefined") { |
379 this.namespace = IriSP.Model.getAI(); |
396 this.namespace = IriSP.Model.getUID(); |
380 } |
397 } |
381 if (typeof this.namespaceUrl === "undefined") { |
398 if (typeof this.namespaceUrl === "undefined") { |
382 this.namespaceUrl = (typeof this.url !== "undefined" ? this.url : this.namespaceUrl); |
399 this.namespaceUrl = (typeof this.url !== "undefined" ? this.url : this.namespaceUrl); |
383 } |
400 } |
384 this.directory.namespaces[this.namespace] = this.namespaceUrl; |
401 this.directory.namespaces[this.namespace] = this.namespaceUrl; |
461 }); |
478 }); |
462 return _nsls; |
479 return _nsls; |
463 } |
480 } |
464 |
481 |
465 IriSP.Model.Source.prototype.get = function() { |
482 IriSP.Model.Source.prototype.get = function() { |
|
483 this.status = IriSP.Model._SOURCE_STATUS_WAITING; |
|
484 this.handleCallbacks(); |
|
485 } |
|
486 |
|
487 /* We defer the callbacks calls so they execute after the queue is cleared */ |
|
488 IriSP.Model.Source.prototype.deferCallback = function(_callback) { |
|
489 var _this = this; |
|
490 IriSP._.defer(function() { |
|
491 _callback.call(_this); |
|
492 }); |
|
493 } |
|
494 |
|
495 IriSP.Model.Source.prototype.handleCallbacks = function() { |
466 this.status = IriSP.Model._SOURCE_STATUS_READY; |
496 this.status = IriSP.Model._SOURCE_STATUS_READY; |
467 var _this = this; |
497 while (this.callbackQueue.length) { |
468 if (_this.callbackQueue.length) { |
498 this.deferCallback(this.callbackQueue.splice(0,1)[0]); |
469 IriSP._(_this.callbackQueue).forEach(function(_callback) { |
499 } |
470 _callback.call(_this); |
500 } |
471 }); |
501 |
472 } |
502 IriSP.Model.Source.prototype.onLoad = function(_callback) { |
473 _this.callbackQueue = []; |
503 if (this.status === IriSP.Model._SOURCE_STATUS_READY) { |
|
504 this.deferCallback(_callback); |
|
505 } else { |
|
506 this.callbackQueue.push(_callback); |
|
507 } |
474 } |
508 } |
475 |
509 |
476 IriSP.Model.Source.prototype.serialize = function() { |
510 IriSP.Model.Source.prototype.serialize = function() { |
477 return this.serializer.serialize(this); |
511 return this.serializer.serialize(this); |
478 } |
512 } |
479 |
513 |
480 IriSP.Model.Source.prototype.onLoad = function(_callback) { |
514 IriSP.Model.Source.prototype.deSerialize = function(_data) { |
481 if (this.status === IriSP.Model._SOURCE_STATUS_READY) { |
515 this.serializer.deSerialize(_data, this); |
482 console.log("Called on load, Ready"); |
|
483 var _this = this; |
|
484 IriSP._.defer(function() { |
|
485 _callback.call(_this); |
|
486 }); |
|
487 } else { |
|
488 console.log("Called on load, not ready"); |
|
489 this.callbackQueue.push(_callback); |
|
490 } |
|
491 } |
516 } |
492 |
517 |
493 IriSP.Model.Source.prototype.getAnnotations = function() { |
518 IriSP.Model.Source.prototype.getAnnotations = function() { |
494 return this.getList("annotation"); |
519 return this.getList("annotation"); |
495 } |
520 } |
507 if (_res.length) { |
532 if (_res.length) { |
508 return _res[0]; |
533 return _res[0]; |
509 } |
534 } |
510 } |
535 } |
511 |
536 |
|
537 IriSP.Model.Source.prototype.getAnnotationsByTypeTitle = function(_title) { |
|
538 var _annType = this.getAnnotationTypeByTitle(_title); |
|
539 if (typeof _annType !== "undefined") { |
|
540 return _annType.getAnnotations(); |
|
541 } |
|
542 } |
|
543 |
512 IriSP.Model.Source.prototype.getDuration = function() { |
544 IriSP.Model.Source.prototype.getDuration = function() { |
513 var _m = this.currentMedia; |
545 var _m = this.currentMedia; |
514 if (typeof _m !== "undefined") { |
546 if (typeof _m !== "undefined") { |
515 return this.currentMedia.duration; |
547 return this.currentMedia.duration; |
516 } |
548 } |
526 |
558 |
527 IriSP.Model.RemoteSource.prototype.get = function() { |
559 IriSP.Model.RemoteSource.prototype.get = function() { |
528 this.status = IriSP.Model._SOURCE_STATUS_WAITING; |
560 this.status = IriSP.Model._SOURCE_STATUS_WAITING; |
529 var _this = this; |
561 var _this = this; |
530 IriSP.jQuery.getJSON(this.url, function(_result) { |
562 IriSP.jQuery.getJSON(this.url, function(_result) { |
531 _this.serializer.deSerialize(_result, _this); |
563 _this.deSerialize(_result); |
532 console.log('Received data, we have '+_this.callbackQueue.length+' callbacks waiting'); |
564 _this.handleCallbacks(); |
533 if (_this.callbackQueue.length) { |
|
534 IriSP._(_this.callbackQueue).forEach(function(_callback) { |
|
535 _callback.call(_this); |
|
536 }); |
|
537 } |
|
538 _this.callbackQueue = []; |
|
539 _this.status = IriSP.Model._SOURCE_STATUS_READY; |
|
540 }); |
565 }); |
541 } |
566 } |
542 |
567 |
543 /* */ |
568 /* */ |
544 |
569 |