src/js/model.js
branchnew-model
changeset 866 3bf7aa8216e5
parent 864 5e76a06b961c
child 868 a525cc2214e7
equal deleted inserted replaced
864:5e76a06b961c 866:3bf7aa8216e5
    45 }
    45 }
    46 
    46 
    47 /* */
    47 /* */
    48 
    48 
    49 IriSP.Model.List = function(_directory) {
    49 IriSP.Model.List = function(_directory) {
    50     this.contents = [];
    50     Array.call(this);
    51     this.directory = _directory;
    51     this.directory = _directory;
       
    52     this.idIndex = [];
    52     if (typeof _directory == "undefined") {
    53     if (typeof _directory == "undefined") {
    53         throw("Error : new IriSP.Model.List(directory): directory is undefined");
    54         throw("Error : new IriSP.Model.List(directory): directory is undefined");
    54     }
    55     }
    55 }
    56 }
    56 
    57 
    57 IriSP.Model.List.prototype.toString = function() {
    58 IriSP.Model.List.prototype = new Array();
    58     return 'List of Elements, length=' + this.length();
       
    59 }
       
    60 
       
    61 IriSP.Model.List.prototype.length = function() {
       
    62     return this.contents.length;
       
    63 }
       
    64 
    59 
    65 IriSP.Model.List.prototype.getElement = function(_id) {
    60 IriSP.Model.List.prototype.getElement = function(_id) {
    66     return this.directory.getElement(_id);
    61     if (this.hasId(_id)) {
    67 }
    62         return this;
    68 
       
    69 IriSP.Model.List.prototype.getElementAt = function(_pos) {
       
    70     if (_pos >= 0 && _pos < this.length()) {
       
    71         return this.getElement(this.contents[_pos]);
       
    72     }
    63     }
    73 }
    64 }
    74 
    65 
    75 IriSP.Model.List.prototype.hasId = function(_id) {
    66 IriSP.Model.List.prototype.hasId = function(_id) {
    76     return (IriSP._(this.contents).indexOf(_id) !== -1);
    67     return (IriSP._(this.idIndex).indexOf(_id) !== -1);
    77 }
    68 }
    78 
    69 
    79 IriSP.Model.List.prototype.each = function(_callback) {
    70 if (typeof Array.prototype.forEach === "undefined") {
    80     var _this = this;
    71     IriSP.Model.List.prototype.forEach = function(_callback) {
    81     IriSP._(this.contents).each(function(_id) {
    72         var _this = this;
    82         _callback.call(_this, _this.getElement(_id), _id);
    73         IriSP._(this).forEach(function(_value, _key) {
    83     });
    74             _callback(_value, _key, _this);
    84 }
    75         });
    85 
    76     }
    86 IriSP.Model.List.prototype.map = function(_callback) {
    77 }
    87     var _this = this;
    78 
    88     return IriSP._(this.contents).map(function(_id) {
    79 if (typeof Array.prototype.map === "undefined") {
    89         return _callback.call(_this, _this.getElement(_id), _id);
    80     IriSP.Model.List.prototype.map = function(_callback) {
    90     });
    81         var _this = this;
    91 }
    82         return IriSP._(this).map(function(_value, _key) {
    92 
    83             return _callback(_value, _key, _this);
       
    84         });
       
    85     }
       
    86 }
       
    87 
       
    88 /* We override Array's filter function because it doesn't return an IriSP.Model.List */
    93 IriSP.Model.List.prototype.filter = function(_callback) {
    89 IriSP.Model.List.prototype.filter = function(_callback) {
    94     var _this = this,
    90     var _this = this,
    95         _res = new IriSP.Model.List(this.directory);
    91         _res = new IriSP.Model.List(this.directory);
    96     _res.contents = IriSP._(this.contents).filter(function(_id) {
    92     _res.addElements(IriSP._(this).filter(function(_value, _key) {
    97         return _callback.call(_this, _this.getElement(_id), _id);
    93         return _callback(_value, _key, _this);
    98     });
    94     }));
    99     return _res;
    95     return _res;
   100 }
    96 }
   101 
    97 
   102 IriSP.Model.List.prototype.sortBy = function(_callback) {
    98 IriSP.Model.List.prototype.sortBy = function(_callback) {
   103     var _this = this,
    99     var _this = this,
   104         _res = new IriSP.Model.List(this.directory);
   100         _res = new IriSP.Model.List(this.directory);
   105     _res.contents = IriSP._(this.contents).sortBy(function(_id) {
   101     _res.contents = IriSP._(this).sortBy(function(_value, _key) {
   106         return _callback.call(_this, _this.getElement(_id), _id);
   102         return _callback(_value, _key, _this);
   107     });
   103     });
   108     return _res;
   104     return _res;
   109 }
   105 }
   110 
   106 
   111 IriSP.Model.List.prototype.searchByTitle = function(_text) {
   107 IriSP.Model.List.prototype.searchByTitle = function(_text) {
   128         return _rgxp.test(_element.description) || _rgxp.test(_element.title);
   124         return _rgxp.test(_element.description) || _rgxp.test(_element.title);
   129     });
   125     });
   130 }
   126 }
   131 
   127 
   132 IriSP.Model.List.prototype.addId = function(_id) {
   128 IriSP.Model.List.prototype.addId = function(_id) {
   133     if (!this.hasId(_id)) {
   129     var _el = this.directory.getElement(_id)
   134         this.contents.push(_id);
   130     if (!this.hasId(_id) && typeof _el !== "undefined") {
   135     }
   131         this.idIndex.push(_id);
   136 }
   132         Array.prototype.push.call(this, _el);
   137 
   133     }
   138 IriSP.Model.List.prototype.addElement = function(_el) {
   134 }
   139     this.addId(_el.id);
   135 
   140 }
   136 IriSP.Model.List.prototype.push = function(_el) {
   141 
   137     if (typeof this.directory.getElement(_el.id) === "undefined") {
   142 IriSP.Model.List.prototype.addIdsFromArray = function(_array) {
   138         this.directory.addElement(_el);
   143     var _l = _array.length;
   139     }
   144     for (var _i = 0; _i < _l; _i++) {
   140     this.idIndex.push(_el.id);
   145         this.addId(_array[_i]);
   141     Array.prototype.push.call(this, _el);
   146     }
   142 }
   147 }
   143 
   148 
   144 IriSP.Model.List.prototype.addIds = function(_array) {
   149 IriSP.Model.List.prototype.addIdsFromList = function(_list) {
   145     var _l = _array.length,
   150     this.addIdsFromArray(_list.contents);
   146         _this = this;
       
   147     IriSP._(_array).forEach(function(_id) {
       
   148         _this.addId(_id);
       
   149     });
       
   150 }
       
   151 
       
   152 IriSP.Model.List.prototype.addElements = function(_array) {
       
   153     var _l = _array.length,
       
   154         _this = this;
       
   155     IriSP._(_array).forEach(function(_el) {
       
   156         _this.push(_el);
       
   157     });
   151 }
   158 }
   152 
   159 
   153 /* */
   160 /* */
   154 
   161 
   155 IriSP.Model.Time = function(_milliseconds) {
   162 IriSP.Model.Time = function(_milliseconds) {
   195 IriSP.Model.Reference = function(_source, _idRef) {
   202 IriSP.Model.Reference = function(_source, _idRef) {
   196     this.source = _source;
   203     this.source = _source;
   197     if (typeof _idRef === "object") {
   204     if (typeof _idRef === "object") {
   198         this.isList = true;
   205         this.isList = true;
   199         this.contents = new IriSP.Model.List(this.source.directory);
   206         this.contents = new IriSP.Model.List(this.source.directory);
   200         this.contents.addIdsFromArray(IriSP._(_idRef).map(function(_id) {
   207         this.contents.addIds(IriSP._(_idRef).map(function(_id) {
   201             return _source.getNamespaced(_id).fullname;
   208             return _source.getNamespaced(_id).fullname;
   202         }));
   209         }));
   203     } else {
   210     } else {
   204         this.isList = false;
   211         this.isList = false;
   205         this.contents = _source.getNamespaced(_idRef).fullname;
   212         this.contents = _source.getNamespaced(_idRef).fullname;
   257 
   264 
   258 IriSP.Model.Media = function(_id, _source) {
   265 IriSP.Model.Media = function(_id, _source) {
   259     IriSP.Model.Element.call(this, _id, _source);
   266     IriSP.Model.Element.call(this, _id, _source);
   260     this.elementType = 'media';
   267     this.elementType = 'media';
   261     this.duration = new IriSP.Model.Time();
   268     this.duration = new IriSP.Model.Time();
   262     this.url = '';
   269     this.video = '';
   263 }
   270 }
   264 
   271 
   265 IriSP.Model.Media.prototype = new IriSP.Model.Element();
   272 IriSP.Model.Media.prototype = new IriSP.Model.Element();
   266 
   273 
   267 IriSP.Model.Media.prototype.setDuration = function(_durationMs) {
   274 IriSP.Model.Media.prototype.setDuration = function(_durationMs) {
   346 
   353 
   347 IriSP.Model.Source = function(_config) {
   354 IriSP.Model.Source = function(_config) {
   348     this.status = IriSP.Model.SOURCE_STATUS_EMPTY;
   355     this.status = IriSP.Model.SOURCE_STATUS_EMPTY;
   349     if (typeof _config !== "undefined") {
   356     if (typeof _config !== "undefined") {
   350         var _this = this;
   357         var _this = this;
   351         IriSP._(_config).each(function(_v, _k) {
   358         IriSP._(_config).forEach(function(_v, _k) {
   352             _this[_k] = _v;
   359             _this[_k] = _v;
   353         })
   360         })
   354         this.callbackQueue = [];
   361         this.callbackQueue = [];
   355         this.contents = {};
   362         this.contents = {};
   356         if (typeof this.namespace === "undefined") {
   363         if (typeof this.namespace === "undefined") {
   387 
   394 
   388 IriSP.Model.Source.prototype.addList = function(_listId, _contents) {
   395 IriSP.Model.Source.prototype.addList = function(_listId, _contents) {
   389     if (typeof this.contents[_listId] === "undefined") {
   396     if (typeof this.contents[_listId] === "undefined") {
   390         this.contents[_listId] = new IriSP.Model.List(this.directory);
   397         this.contents[_listId] = new IriSP.Model.List(this.directory);
   391     }
   398     }
   392     this.contents[_listId].addIdsFromList(_contents);
   399     this.contents[_listId].addElements(_contents);
   393 }
   400 }
   394 
   401 
   395 IriSP.Model.Source.prototype.getList = function(_listId) {
   402 IriSP.Model.Source.prototype.getList = function(_listId) {
   396     if (typeof this.contents[_listId] === "undefined") {
   403     if (typeof this.contents[_listId] === "undefined") {
   397         return this.directory.getGlobalList().filter(function(_e) {
   404         return this.directory.getGlobalList().filter(function(_e) {
   400     } else {
   407     } else {
   401         return this.contents[_listId];
   408         return this.contents[_listId];
   402     }
   409     }
   403 }
   410 }
   404 
   411 
   405 IriSP.Model.Source.prototype.each = function(_callback) {
   412 IriSP.Model.Source.prototype.forEach = function(_callback) {
   406     var _this = this;
   413     var _this = this;
   407     IriSP._(this.contents).each(function(_value, _key) {
   414     IriSP._(this.contents).forEach(function(_value, _key) {
   408         _callback.call(_this, _value, _key);
   415         _callback.call(_this, _value, _key);
   409     })
   416     })
   410 }
   417 }
   411 
   418 
   412 IriSP.Model.Source.prototype.getElement = function(_listId, _elId) {
   419 IriSP.Model.Source.prototype.getElement = function(_elId) {
   413     var _list = this.getList(_listId);
   420     return this.directory.getElement(_elId);
   414     return (typeof _list !== "undefined" ? _list.getElement(_elId) : undefined);
       
   415 }
   421 }
   416 
   422 
   417 IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) {
   423 IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) {
   418     if (typeof _idRef !== "undefined") {
   424     if (typeof _idRef !== "undefined") {
   419         this.currentMedia = this.getNamespaced(_idRef).fullname;
   425         this.currentMedia = this.getMedias().getElement(this.getNamespaced(_idRef).fullname);
   420     }
   426     }
   421 }
   427 }
   422 
   428 
   423 IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() {
   429 IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() {
   424     if (typeof this.currentMedia === "undefined") {
   430     if (typeof this.currentMedia === "undefined" && this.getMedias().length) {
   425         this.currentMedia = this.getList("media").getElementAt(0);
   431         this.currentMedia = this.getMedias()[0];
   426     }
   432     }
   427 }
   433 }
   428 
   434 
   429 IriSP.Model.Source.prototype.listNamespaces = function(_excludeSelf) {
   435 IriSP.Model.Source.prototype.listNamespaces = function(_excludeSelf) {
   430     var _this = this,
   436     var _this = this,
   431         _nsls = [],
   437         _nsls = [],
   432         _excludeSelf = (typeof _excludeSelf !== "undefined" && _excludeSelf);
   438         _excludeSelf = (typeof _excludeSelf !== "undefined" && _excludeSelf);
   433     this.each(function(_list) {
   439     this.forEach(function(_list) {
   434         IriSP._(_list.contents).each(function(_id) {
   440         IriSP._(_list).forEach(function(_el) {
   435             var _ns = _id.replace(/:.*$/,'');
   441             var _ns = _el.id.replace(/:.*$/,'');
   436             if (IriSP._(_nsls).indexOf(_ns) === -1 && (!_excludeSelf || _ns !== _this.namespace)) {
   442             if (IriSP._(_nsls).indexOf(_ns) === -1 && (!_excludeSelf || _ns !== _this.namespace)) {
   437                 _nsls.push(_ns);
   443                 _nsls.push(_ns);
   438             }
   444             }
   439         })
   445         })
   440     });
   446     });
   443 
   449 
   444 IriSP.Model.Source.prototype.get = function() {
   450 IriSP.Model.Source.prototype.get = function() {
   445     this.status = IriSP.Model.SOURCE_STATUS_READY;
   451     this.status = IriSP.Model.SOURCE_STATUS_READY;
   446     var _this = this;
   452     var _this = this;
   447     if (_this.callbackQueue.length) {
   453     if (_this.callbackQueue.length) {
   448         IriSP._.each(_this.callbackQueue, function(_callback) {
   454         IriSP._(_this.callbackQueue).forEach(function(_callback) {
   449             _callback.call(_this);
   455             _callback.call(_this);
   450         });
   456         });
   451     }
   457     }
   452     _this.callbackQueue = [];
   458     _this.callbackQueue = [];
   453 }
   459 }
   454 
   460 
   455 IriSP.Model.Source.prototype.serialize = function() {
   461 IriSP.Model.Source.prototype.serialize = function() {
   456     return this.serializer.serialize(this);
   462     return this.serializer.serialize(this);
   457 }
   463 }
   458 
   464 
   459 IriSP.Model.Source.prototype.addCallback = function(_callback) {
   465 IriSP.Model.Source.prototype.onLoad = function(_callback) {
   460     if (this.status === IriSP.Model.SOURCE_STATUS_READY) {
   466     if (this.status === IriSP.Model.SOURCE_STATUS_READY) {
   461         callback.call(this);
   467         callback.call(this);
   462     } else {
   468     } else {
   463         this.callbackQueue.push(_callback);
   469         this.callbackQueue.push(_callback);
   464     }
   470     }
   476     return this.getList("annotationType");
   482     return this.getList("annotationType");
   477 }
   483 }
   478 
   484 
   479 IriSP.Model.Source.prototype.getAnnotationTypeByTitle = function(_title) {
   485 IriSP.Model.Source.prototype.getAnnotationTypeByTitle = function(_title) {
   480     var _res = this.getAnnotationTypes().searchByTitle(_title);
   486     var _res = this.getAnnotationTypes().searchByTitle(_title);
   481     if (_res.length() > 0) {
   487     if (_res.length) {
   482         return _res.getElementAt(0);
   488         return _res[0];
   483     }
   489     }
   484 }
   490 }
   485 
   491 
   486 IriSP.Model.Source.prototype.getDuration = function() {
   492 IriSP.Model.Source.prototype.getDuration = function() {
   487     return this.currentMedia.duration;
   493     var _m = this.currentMedia;
       
   494     if (typeof _m !== "undefined") {
       
   495         return this.currentMedia.duration;
       
   496     }
   488 }
   497 }
   489 
   498 
   490 /* */
   499 /* */
   491 
   500 
   492 IriSP.Model.RemoteSource = function(_config) {
   501 IriSP.Model.RemoteSource = function(_config) {
   499     this.status = IriSP.Model.SOURCE_STATUS_WAITING;
   508     this.status = IriSP.Model.SOURCE_STATUS_WAITING;
   500     var _this = this;
   509     var _this = this;
   501     IriSP.jQuery.getJSON(this.url, function(_result) {
   510     IriSP.jQuery.getJSON(this.url, function(_result) {
   502         _this.serializer.deSerialize(_result, _this);
   511         _this.serializer.deSerialize(_result, _this);
   503         if (_this.callbackQueue.length) {
   512         if (_this.callbackQueue.length) {
   504             IriSP._.each(_this.callbackQueue, function(_callback) {
   513             IriSP._(_this.callbackQueue).forEach(function(_callback) {
   505                 _callback.call(_this);
   514                 _callback.call(_this);
   506             });
   515             });
   507         }
   516         }
   508         _this.callbackQueue = [];
   517         _this.callbackQueue = [];
   509         _this.status = IriSP.Model.SOURCE_STATUS_READY;
   518         _this.status = IriSP.Model.SOURCE_STATUS_READY;
   540     this.elements[_element.id] = _element;
   549     this.elements[_element.id] = _element;
   541 }
   550 }
   542 
   551 
   543 IriSP.Model.Directory.prototype.getGlobalList = function() {
   552 IriSP.Model.Directory.prototype.getGlobalList = function() {
   544     var _res = new IriSP.Model.List(this);
   553     var _res = new IriSP.Model.List(this);
   545     _res.addIdsFromArray(IriSP._(this.elements).keys());
   554     _res.addIds(IriSP._(this.elements).keys());
   546     return _res;
   555     return _res;
   547 }
   556 }
   548 
   557 
   549 /* */
   558 /* */