src/js/model.js
branchnew-model
changeset 900 7673d645a8e0
parent 887 6a04bd37da0a
child 902 14022f1d49ab
equal deleted inserted replaced
898:d05b8920dbaa 900:7673d645a8e0
    64 IriSP.Model.List = function(_directory) {
    64 IriSP.Model.List = function(_directory) {
    65     Array.call(this);
    65     Array.call(this);
    66     this.directory = _directory;
    66     this.directory = _directory;
    67     this.idIndex = [];
    67     this.idIndex = [];
    68     if (typeof _directory == "undefined") {
    68     if (typeof _directory == "undefined") {
       
    69         console.trace();
    69         throw "Error : new IriSP.Model.List(directory): directory is undefined";
    70         throw "Error : new IriSP.Model.List(directory): directory is undefined";
    70     }
    71     }
    71 }
    72 }
    72 
    73 
    73 IriSP.Model.List.prototype = new Array();
    74 IriSP.Model.List.prototype = new Array();
    74 
    75 
    75 IriSP.Model.List.prototype.getElement = function(_id) {
    76 IriSP.Model.List.prototype.getElement = function(_id) {
    76     var _index = IriSP._(this.idIndex).indexOf(_id);
    77     return this[_id];
    77     if (_index !== -1) {
       
    78         return this[_index];
       
    79     } else {
       
    80         var _un = _id.replace(/.*:/);
       
    81         return IriSP._(this.idIndex).find(function(_i) {
       
    82             return _i.replace(/.*:/) === _un;
       
    83         });
       
    84     }
       
    85 }
    78 }
    86 
    79 
    87 IriSP.Model.List.prototype.hasId = function(_id) {
    80 IriSP.Model.List.prototype.hasId = function(_id) {
    88     return (IriSP._(this.idIndex).indexOf(_id) !== -1);
    81     return (IriSP._(this.idIndex).indexOf(_id) !== -1);
    89 }
    82 }
   105         var _this = this;
    98         var _this = this;
   106         return IriSP._(this).map(function(_value, _key) {
    99         return IriSP._(this).map(function(_value, _key) {
   107             return _callback(_value, _key, _this);
   100             return _callback(_value, _key, _this);
   108         });
   101         });
   109     }
   102     }
       
   103 }
       
   104 
       
   105 IriSP.Model.List.prototype.pluck = function(_key) {
       
   106     return this.map(function(_value) {
       
   107         return _value[_key];
       
   108     });
   110 }
   109 }
   111 
   110 
   112 /* We override Array's filter function because it doesn't return an IriSP.Model.List
   111 /* We override Array's filter function because it doesn't return an IriSP.Model.List
   113  */
   112  */
   114 IriSP.Model.List.prototype.filter = function(_callback) {
   113 IriSP.Model.List.prototype.filter = function(_callback) {
   239 /* A simple time management object, that helps converting millisecs to seconds and strings,
   238 /* A simple time management object, that helps converting millisecs to seconds and strings,
   240  * without the clumsiness of the original Date object.
   239  * without the clumsiness of the original Date object.
   241  */
   240  */
   242 
   241 
   243 IriSP.Model.Time = function(_milliseconds) {
   242 IriSP.Model.Time = function(_milliseconds) {
       
   243     this.milliseconds = 0;
       
   244     this.setMilliseconds(_milliseconds);
       
   245 }
       
   246 
       
   247 IriSP.Model.Time.prototype.setMilliseconds = function(_milliseconds) {
       
   248     var _ante = _milliseconds;
   244     switch(typeof _milliseconds) {
   249     switch(typeof _milliseconds) {
   245         case "string":
   250         case "string":
   246             this.milliseconds = parseFloat(_milliseconds);
   251             this.milliseconds = parseFloat(_milliseconds);
   247             break;
   252             break;
   248         case "number":
   253         case "number":
   253             break;
   258             break;
   254         default:
   259         default:
   255             this.milliseconds = 0;
   260             this.milliseconds = 0;
   256     }
   261     }
   257     if (this.milliseconds === NaN) {
   262     if (this.milliseconds === NaN) {
   258         this.milliseconds = 0;
   263         this.milliseconds = _ante;
   259     }
   264     }
   260 }
   265 }
   261 
   266 
   262 IriSP.Model.Time.prototype.setSeconds = function(_seconds) {
   267 IriSP.Model.Time.prototype.setSeconds = function(_seconds) {
   263     this.milliseconds = 1000 * _seconds;
   268     this.milliseconds = 1000 * _seconds;
   394 }
   399 }
   395 
   400 
   396 IriSP.Model.Media.prototype = new IriSP.Model.Element();
   401 IriSP.Model.Media.prototype = new IriSP.Model.Element();
   397 
   402 
   398 IriSP.Model.Media.prototype.setDuration = function(_durationMs) {
   403 IriSP.Model.Media.prototype.setDuration = function(_durationMs) {
   399     this.duration.milliseconds = _durationMs;
   404     this.duration.setMilliseconds(_durationMs);
   400 }
   405 }
   401 
   406 
   402 IriSP.Model.Media.prototype.getAnnotations = function() {
   407 IriSP.Model.Media.prototype.getAnnotations = function() {
   403     return this.getRelated("annotation");
   408     return this.getRelated("annotation");
       
   409 }
       
   410 
       
   411 IriSP.Model.Media.prototype.getAnnotationsByTypeTitle = function(_title) {
       
   412     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
       
   413     if (_annTypes.length) {
       
   414         return this.getAnnotations().filter(function(_annotation) {
       
   415             return IriSP._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
       
   416         });
       
   417     } else {
       
   418         return new IriSP.Model.List(this.source.directory)
       
   419     }
   404 }
   420 }
   405 
   421 
   406 /* */
   422 /* */
   407 
   423 
   408 IriSP.Model.Tag = function(_id, _source) {
   424 IriSP.Model.Tag = function(_id, _source) {
   440 }
   456 }
   441 
   457 
   442 IriSP.Model.Annotation.prototype = new IriSP.Model.Element(null);
   458 IriSP.Model.Annotation.prototype = new IriSP.Model.Element(null);
   443 
   459 
   444 IriSP.Model.Annotation.prototype.setBegin = function(_beginMs) {
   460 IriSP.Model.Annotation.prototype.setBegin = function(_beginMs) {
   445     this.begin.milliseconds = _beginMs;
   461     this.begin.setMilliseconds(_beginMs);
   446 }
   462 }
   447 
   463 
   448 IriSP.Model.Annotation.prototype.setEnd = function(_beginMs) {
   464 IriSP.Model.Annotation.prototype.setEnd = function(_beginMs) {
   449     this.end.milliseconds = _beginMs;
   465     this.end.setMilliseconds(_beginMs);
   450 }
   466 }
   451 
   467 
   452 IriSP.Model.Annotation.prototype.setMedia = function(_idRef) {
   468 IriSP.Model.Annotation.prototype.setMedia = function(_idRef) {
   453     this.setReference("media", _idRef);
   469     this.setReference("media", _idRef);
   454 }
   470 }
   473     return this.getReference("tag");
   489     return this.getReference("tag");
   474 }
   490 }
   475 
   491 
   476 IriSP.Model.Annotation.prototype.getTagTexts = function() {
   492 IriSP.Model.Annotation.prototype.getTagTexts = function() {
   477     return this.getTags().getTitles();
   493     return this.getTags().getTitles();
       
   494 }
       
   495 
       
   496 IriSP.Model.Annotation.prototype.getDuration = function() {
       
   497     return new IriSP.Model.Time(this.end.milliseconds - this.begin.milliseconds)
   478 }
   498 }
   479 
   499 
   480 /* */
   500 /* */
   481 
   501 
   482 IriSP.Model.MashedAnnotation = function(_annotation, _offset) {
   502 IriSP.Model.MashedAnnotation = function(_annotation, _offset) {
   483     IriSP.Model.Element.call(this, IriSP.Model.getUID(), _annotation.source);
   503     IriSP.Model.Element.call(this, IriSP.Model.getUID(), _annotation.source);
   484     this.elementType = 'mashedAnnotation';
   504     this.elementType = 'mashedAnnotation';
   485     this.annotation = _annotation;
   505     this.annotation = _annotation;
   486     this.begin = new IriSP.Model.Time(_offset);
   506     this.begin = new IriSP.Model.Time(_offset);
   487     var _duration = (this.annotation.end - this.annotation.begin);
   507     this.end = new IriSP.Model.Time(_offset + _annotation.getDuration());
   488     this.end = new IriSP.Model.Time(_offset + _duration)
       
   489     this.title = this.annotation.title;
   508     this.title = this.annotation.title;
   490     this.description = this.annotation.description;
   509     this.description = this.annotation.description;
       
   510     this.color = this.annotation.color;
   491 }
   511 }
   492 
   512 
   493 IriSP.Model.MashedAnnotation.prototype = new IriSP.Model.Element(null);
   513 IriSP.Model.MashedAnnotation.prototype = new IriSP.Model.Element(null);
   494 
   514 
   495 IriSP.Model.MashedAnnotation.prototype.getMedia = function() {
   515 IriSP.Model.MashedAnnotation.prototype.getMedia = function() {
   512 
   532 
   513 IriSP.Model.Mashup = function(_id, _source) {
   533 IriSP.Model.Mashup = function(_id, _source) {
   514     IriSP.Model.Element.call(this, _id, _source);
   534     IriSP.Model.Element.call(this, _id, _source);
   515     this.elementType = 'mashup';
   535     this.elementType = 'mashup';
   516     this.duration = new IriSP.Model.Time();
   536     this.duration = new IriSP.Model.Time();
   517     this.segments = new IriSP.Model.List();
   537     this.segments = new IriSP.Model.List(_source.directory);
   518     this.medias = new IriSP.Model.List();
   538     this.medias = new IriSP.Model.List(_source.directory);
   519 }
   539 }
   520 
   540 
   521 IriSP.Model.Mashup.prototype = new IriSP.Model.Element();
   541 IriSP.Model.Mashup.prototype = new IriSP.Model.Element();
   522 
   542 
   523 IriSP.Model.Mashup.prototype.addSegment = function(_annotation) {
   543 IriSP.Model.Mashup.prototype.addSegment = function(_annotation) {
   524     this.segments.push(new IriSP.Model.MashedAnnotation(_annotation));
   544     var _mashedAnnotation = new IriSP.Model.MashedAnnotation(_annotation, this.duration);
   525     this.medias.addElement(_annotation.getMedia());
   545     this.duration.setMilliseconds(_mashedAnnotation.end);
       
   546     this.segments.push(_mashedAnnotation);
       
   547     this.medias.push(_annotation.getMedia());
       
   548 }
       
   549 
       
   550 IriSP.Model.Mashup.prototype.addSegmentById = function(_elId) {
       
   551     var _annotation = this.source.getElement(_elId);
       
   552     if (typeof _annotation !== "undefined") {
       
   553         this.addSegment(_annotation);
       
   554     }
   526 }
   555 }
   527 
   556 
   528 IriSP.Model.Mashup.prototype.getAnnotations = function() {
   557 IriSP.Model.Mashup.prototype.getAnnotations = function() {
   529     return this.segments;
   558     return this.segments;
   530 }
   559 }
   531 
   560 
   532 IriSP.Model.Mashup.prototype.getMedias = function() {
   561 IriSP.Model.Mashup.prototype.getMedias = function() {
   533     return this.medias;
   562     return this.medias;
   534 }
   563 }
       
   564 
       
   565 IriSP.Model.Mashup.prototype.getAnnotationsByTypeTitle = function(_title) {
       
   566     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
       
   567     if (_annTypes.length) {
       
   568         return this.getAnnotations().filter(function(_annotation) {
       
   569             return IriSP._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
       
   570         });
       
   571     } else {
       
   572         return new IriSP.Model.List(this.source.directory)
       
   573     }
       
   574 }
       
   575 
   535 /* */
   576 /* */
   536 
   577 
   537 IriSP.Model.Source = function(_config) {
   578 IriSP.Model.Source = function(_config) {
   538     this.status = IriSP.Model._SOURCE_STATUS_EMPTY;
   579     this.status = IriSP.Model._SOURCE_STATUS_EMPTY;
   539     if (typeof _config !== "undefined") {
   580     if (typeof _config !== "undefined") {
   620     return this.directory.getElement(this.getNamespaced(_elId).fullname);
   661     return this.directory.getElement(this.getNamespaced(_elId).fullname);
   621 }
   662 }
   622 
   663 
   623 IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) {
   664 IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) {
   624     if (typeof _idRef !== "undefined") {
   665     if (typeof _idRef !== "undefined") {
   625         this.currentMedia = this.getMedias().getElement(this.getNamespaced(_idRef).fullname);
   666         this.currentMedia = this.getElement(_idRef);
   626     }
   667     }
   627 }
   668 }
   628 
   669 
   629 IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() {
   670 IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() {
   630     if (typeof this.currentMedia === "undefined" && this.getMedias().length) {
   671     if (typeof this.currentMedia === "undefined" && this.getMedias().length) {
   664     this.status = IriSP.Model._SOURCE_STATUS_READY;
   705     this.status = IriSP.Model._SOURCE_STATUS_READY;
   665     while (this.callbackQueue.length) {
   706     while (this.callbackQueue.length) {
   666         this.deferCallback(this.callbackQueue.splice(0,1)[0]);
   707         this.deferCallback(this.callbackQueue.splice(0,1)[0]);
   667     }
   708     }
   668 }
   709 }
   669 
       
   670 IriSP.Model.Source.prototype.onLoad = function(_callback) {
   710 IriSP.Model.Source.prototype.onLoad = function(_callback) {
   671     if (this.status === IriSP.Model._SOURCE_STATUS_READY) {
   711     if (this.status === IriSP.Model._SOURCE_STATUS_READY) {
   672         this.deferCallback(_callback);
   712         this.deferCallback(_callback);
   673     } else {
   713     } else {
   674         this.callbackQueue.push(_callback);
   714         this.callbackQueue.push(_callback);
   689 }
   729 }
   690 
   730 
   691 IriSP.Model.Source.prototype.getMedias = function(_global) {
   731 IriSP.Model.Source.prototype.getMedias = function(_global) {
   692     _global = (typeof _global !== "undefined" && _global);
   732     _global = (typeof _global !== "undefined" && _global);
   693     return this.getList("media", _global);
   733     return this.getList("media", _global);
       
   734 }
       
   735 
       
   736 IriSP.Model.Source.prototype.getMashups = function(_global) {
       
   737     _global = (typeof _global !== "undefined" && _global);
       
   738     return this.getList("mashup", _global);
   694 }
   739 }
   695 
   740 
   696 IriSP.Model.Source.prototype.getAnnotationTypes = function(_global) {
   741 IriSP.Model.Source.prototype.getAnnotationTypes = function(_global) {
   697     _global = (typeof _global !== "undefined" && _global);
   742     _global = (typeof _global !== "undefined" && _global);
   698     return this.getList("annotationType", _global);
   743     return this.getList("annotationType", _global);
   757         _res = new IriSP.Model.Source(_config);
   802         _res = new IriSP.Model.Source(_config);
   758     return _res;
   803     return _res;
   759 }
   804 }
   760 
   805 
   761 IriSP.Model.Directory.prototype.getElement = function(_id) {
   806 IriSP.Model.Directory.prototype.getElement = function(_id) {
   762     var _res = this.elements[_id];
   807     return this.elements[_id];
   763     if (typeof _res === "undefined") {
       
   764         var _un = _id.replace(/.*:/),
       
   765             _keys = IriSP._(this.elements).keys();
       
   766             _key = IriSP._(_keys).find(function(_i) {
       
   767                 return _i.replace(/.*:/) === _un;
       
   768             });
       
   769         if (typeof _key !== "undefined") {
       
   770             _res = this.elements[_key];
       
   771         }
       
   772     }
       
   773     return _res;
       
   774 }
   808 }
   775 
   809 
   776 IriSP.Model.Directory.prototype.addElement = function(_element) {
   810 IriSP.Model.Directory.prototype.addElement = function(_element) {
   777     this.elements[_element.id] = _element;
   811     this.elements[_element.id] = _element;
   778 }
   812 }