src/js/model.js
changeset 1013 392ddcd212d7
parent 1010 5566738cb829
child 1026 420608a77566
equal deleted inserted replaced
1012:7e18d953a1f8 1013:392ddcd212d7
    52         return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6);
    52         return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6);
    53     },
    53     },
    54     isLocalURL = Model.isLocalURL = function(url) {
    54     isLocalURL = Model.isLocalURL = function(url) {
    55         var matches = url.match(/^(\w+:)\/\/([^/]+)/);
    55         var matches = url.match(/^(\w+:)\/\/([^/]+)/);
    56         if (matches) {
    56         if (matches) {
    57             return(matches[1] === document.location.protocol && matches[2] === document.location.host)
    57             return(matches[1] === document.location.protocol && matches[2] === document.location.host);
    58         }
    58         }
    59         return true;
    59         return true;
    60     },
    60     },
    61     regexpFromTextOrArray = Model.regexpFromTextOrArray = function(_textOrArray, _testOnly, _iexact) {
    61     regexpFromTextOrArray = Model.regexpFromTextOrArray = function(_textOrArray, _testOnly, _iexact) {
    62         var _testOnly = _testOnly || false,
    62         var _testOnly = _testOnly || false,
    79         return new RegExp( _source, _flags);
    79         return new RegExp( _source, _flags);
    80     },
    80     },
    81     fullTextRegexps = Model.fullTextRegexps = function(_text) {
    81     fullTextRegexps = Model.fullTextRegexps = function(_text) {
    82         var remsrc = "[\\" + removeChars.join("\\") + "]",
    82         var remsrc = "[\\" + removeChars.join("\\") + "]",
    83             remrx = new RegExp(remsrc,"gm"),
    83             remrx = new RegExp(remsrc,"gm"),
    84             txt = _text.toLowerCase().replace(remrx,"")
    84             txt = _text.toLowerCase().replace(remrx,""),
    85             res = [],
    85             res = [],
    86             charsrx = ns._(charsub).map(function(c) {
    86             charsrx = ns._(charsub).map(function(c) {
    87                 return new RegExp(c);
    87                 return new RegExp(c);
    88             }),
    88             }),
    89             src = "";
    89             src = "";
   129         return d.getUTCFullYear()+'-'  
   129         return d.getUTCFullYear()+'-'  
   130             + pad(2, d.getUTCMonth()+1)+'-'  
   130             + pad(2, d.getUTCMonth()+1)+'-'  
   131             + pad(2, d.getUTCDate())+'T'  
   131             + pad(2, d.getUTCDate())+'T'  
   132             + pad(2, d.getUTCHours())+':'  
   132             + pad(2, d.getUTCHours())+':'  
   133             + pad(2, d.getUTCMinutes())+':'  
   133             + pad(2, d.getUTCMinutes())+':'  
   134             + pad(2, d.getUTCSeconds())+'Z'  
   134             + pad(2, d.getUTCSeconds())+'Z'  ;
   135     };
   135     };
   136 
   136 
   137 /*
   137 /*
   138  * List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID)
   138  * List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID)
   139  */
   139  */
   152         _this.regexp = undefined;
   152         _this.regexp = undefined;
   153         _this.forEach(function(_element) {
   153         _this.forEach(function(_element) {
   154             _element.found = undefined;
   154             _element.found = undefined;
   155         });
   155         });
   156         _this.trigger("search-cleared");
   156         _this.trigger("search-cleared");
   157     })
   157     });
   158 };
   158 };
   159 
   159 
   160 List.prototype = new Array();
   160 List.prototype = new Array();
   161 
   161 
   162 List.prototype.hasId = function(_id) {
   162 List.prototype.hasId = function(_id) {
   170     List.prototype.forEach = function(_callback) {
   170     List.prototype.forEach = function(_callback) {
   171         var _this = this;
   171         var _this = this;
   172         ns._(this).forEach(function(_value, _key) {
   172         ns._(this).forEach(function(_value, _key) {
   173             _callback(_value, _key, _this);
   173             _callback(_value, _key, _this);
   174         });
   174         });
   175     }
   175     };
   176 };
   176 };
   177 
   177 
   178 if (typeof Array.prototype.map === "undefined") {
   178 if (typeof Array.prototype.map === "undefined") {
   179     List.prototype.map = function(_callback) {
   179     List.prototype.map = function(_callback) {
   180         var _this = this;
   180         var _this = this;
   181         return ns._(this).map(function(_value, _key) {
   181         return ns._(this).map(function(_value, _key) {
   182             return _callback(_value, _key, _this);
   182             return _callback(_value, _key, _this);
   183         });
   183         });
   184     }
   184     };
   185 };
   185 };
   186 
   186 
   187 List.prototype.pluck = function(_key) {
   187 List.prototype.pluck = function(_key) {
   188     return this.map(function(_value) {
   188     return this.map(function(_value) {
   189         return _value[_key];
   189         return _value[_key];
   259         this.trigger("clear-search");
   259         this.trigger("clear-search");
   260         return this;
   260         return this;
   261     }
   261     }
   262     this.searching = true;
   262     this.searching = true;
   263     this.trigger("search", _text);
   263     this.trigger("search", _text);
   264     var rxsource = fullTextRegexps(_text)
   264     var rxsource = fullTextRegexps(_text),
   265         rgxp = new RegExp(rxsource,"im"),
   265         rgxp = new RegExp(rxsource,"im");
   266         this.regexp = new RegExp(rxsource,"gim");
   266     this.regexp = new RegExp(rxsource,"gim");
   267     var res = this.filter(function(_element, _k) {
   267     var res = this.filter(function(_element, _k) {
   268         var titlematch = rgxp.test(_element.title),
   268         var titlematch = rgxp.test(_element.title),
   269             descmatch = rgxp.test(_element.description),
   269             descmatch = rgxp.test(_element.description),
   270             _isfound = !!(titlematch || descmatch);
   270             _isfound = !!(titlematch || descmatch);
   271         _element.found = _isfound;
   271         _element.found = _isfound;
   281         return _el.title;
   281         return _el.title;
   282     });
   282     });
   283 };
   283 };
   284 
   284 
   285 List.prototype.addId = function(_id) {
   285 List.prototype.addId = function(_id) {
   286     var _el = this.directory.getElement(_id)
   286     var _el = this.directory.getElement(_id);
   287     if (!this.hasId(_id) && typeof _el !== "undefined") {
   287     if (!this.hasId(_id) && typeof _el !== "undefined") {
   288         this.idIndex.push(_id);
   288         this.idIndex.push(_id);
   289         Array.prototype.push.call(this, _el);
   289         Array.prototype.push.call(this, _el);
   290     }
   290     }
   291 };
   291 };
   414     return {
   414     return {
   415         hours : Math.floor(_totalSeconds / 3600),
   415         hours : Math.floor(_totalSeconds / 3600),
   416         minutes : (Math.floor(_totalSeconds / 60) % 60),
   416         minutes : (Math.floor(_totalSeconds / 60) % 60),
   417         seconds : _totalSeconds % 60,
   417         seconds : _totalSeconds % 60,
   418         milliseconds: this.milliseconds % 1000
   418         milliseconds: this.milliseconds % 1000
   419     } 
   419     };
   420 };
   420 };
   421 
   421 
   422 Time.prototype.add = function(_milliseconds) {
   422 Time.prototype.add = function(_milliseconds) {
   423     this.milliseconds += new Time(_milliseconds).milliseconds;
   423     this.milliseconds += new Time(_milliseconds).milliseconds;
   424 };
   424 };
   429 
   429 
   430 Time.prototype.toString = function(showCs) {
   430 Time.prototype.toString = function(showCs) {
   431     var _hms = this.getHMS(),
   431     var _hms = this.getHMS(),
   432         _res = '';
   432         _res = '';
   433     if (_hms.hours) {
   433     if (_hms.hours) {
   434         _res += _hms.hours + ':'
   434         _res += _hms.hours + ':';
   435     }
   435     }
   436     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
   436     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
   437     if (showCs) {
   437     if (showCs) {
   438         _res += "." + Math.floor(_hms.milliseconds / 100)
   438         _res += "." + Math.floor(_hms.milliseconds / 100);
   439     }
   439     }
   440     return _res;
   440     return _res;
   441 };
   441 };
   442 
   442 
   443 /* Reference handles references between elements
   443 /* Reference handles references between elements
   471     return this.contents;
   471     return this.contents;
   472 };
   472 };
   473 
   473 
   474 Reference.prototype.isOrHasId = function(_idRef) {
   474 Reference.prototype.isOrHasId = function(_idRef) {
   475     if (this.isList) {
   475     if (this.isList) {
   476         return (ns._(this.id).indexOf(_idRef) !== -1)
   476         return (ns._(this.id).indexOf(_idRef) !== -1);
   477     } else {
   477     } else {
   478         return (this.id == _idRef);
   478         return (this.id == _idRef);
   479     }
   479     }
   480 };
   480 };
   481 
   481 
   483 
   483 
   484 var BaseElement = Model.Element = function(_id, _source) {
   484 var BaseElement = Model.Element = function(_id, _source) {
   485     this.elementType = 'element';
   485     this.elementType = 'element';
   486     this.title = "";
   486     this.title = "";
   487     this.description = "";
   487     this.description = "";
   488     this.__events = {}
   488     this.__events = {};
   489     if (typeof _source === "undefined") {
   489     if (typeof _source === "undefined") {
   490         return;
   490         return;
   491     }
   491     }
   492     if (typeof _id === "undefined" || !_id) {
   492     if (typeof _id === "undefined" || !_id) {
   493         _id = getUID();
   493         _id = getUID();
   565         _this.paused = true;
   565         _this.paused = true;
   566     });
   566     });
   567     this.on("timeupdate", function(_time) {
   567     this.on("timeupdate", function(_time) {
   568         _this.currentTime = _time;
   568         _this.currentTime = _time;
   569         _this.getAnnotations().filter(function(_a) {
   569         _this.getAnnotations().filter(function(_a) {
   570             return (_a.end <= _time || _a.begin > _time) && _a.playing
   570             return (_a.end <= _time || _a.begin > _time) && _a.playing;
   571         }).forEach(function(_a) {
   571         }).forEach(function(_a) {
   572             _a.playing = false;
   572             _a.playing = false;
   573             _a.trigger("leave");
   573             _a.trigger("leave");
   574             _this.trigger("leave-annotation",_a);
   574             _this.trigger("leave-annotation",_a);
   575         });
   575         });
   576         _this.getAnnotations().filter(function(_a) {
   576         _this.getAnnotations().filter(function(_a) {
   577             return _a.begin <= _time && _a.end > _time && !_a.playing
   577             return _a.begin <= _time && _a.end > _time && !_a.playing;
   578         }).forEach(function(_a) {
   578         }).forEach(function(_a) {
   579             _a.playing = true;
   579             _a.playing = true;
   580             _a.trigger("enter");
   580             _a.trigger("enter");
   581             _this.trigger("enter-annotation",_a);
   581             _this.trigger("enter-annotation",_a);
   582         });
   582         });
   655     if (_annTypes.length) {
   655     if (_annTypes.length) {
   656         return this.getAnnotations().filter(function(_annotation) {
   656         return this.getAnnotations().filter(function(_annotation) {
   657             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
   657             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
   658         });
   658         });
   659     } else {
   659     } else {
   660         return new List(this.source.directory)
   660         return new List(this.source.directory);
   661     }
   661     }
   662 };
   662 };
   663 
   663 
   664 /* */
   664 /* */
   665 
   665 
   751 Annotation.prototype.getTagTexts = function() {
   751 Annotation.prototype.getTagTexts = function() {
   752     return this.getTags().getTitles();
   752     return this.getTags().getTitles();
   753 };
   753 };
   754 
   754 
   755 Annotation.prototype.getDuration = function() {
   755 Annotation.prototype.getDuration = function() {
   756     return new Time(this.end.milliseconds - this.begin.milliseconds)
   756     return new Time(this.end.milliseconds - this.begin.milliseconds);
   757 };
   757 };
   758 
   758 
   759 /* */
   759 /* */
   760 
   760 
   761 var MashedAnnotation = Model.MashedAnnotation = function(_mashup, _annotation) {
   761 var MashedAnnotation = Model.MashedAnnotation = function(_mashup, _annotation) {
   818     this.loaded = false;
   818     this.loaded = false;
   819     var _this = this;
   819     var _this = this;
   820     this._updateTimes = function() {
   820     this._updateTimes = function() {
   821         _this.updateTimes();
   821         _this.updateTimes();
   822         _this.trigger("change");
   822         _this.trigger("change");
   823     }
   823     };
   824     this.on("add", this._updateTimes);
   824     this.on("add", this._updateTimes);
   825     this.on("remove", this._updateTimes);
   825     this.on("remove", this._updateTimes);
   826 };
   826 };
   827 
   827 
   828 extendPrototype(Mashup, Playable);
   828 extendPrototype(Mashup, Playable);
   907     this.addAnnotationsById(_segments);
   907     this.addAnnotationsById(_segments);
   908 };
   908 };
   909 
   909 
   910 Mashup.prototype.hasAnnotation = function(_annotation) {
   910 Mashup.prototype.hasAnnotation = function(_annotation) {
   911     return !!ns._(this.segments).find(function(_s) {
   911     return !!ns._(this.segments).find(function(_s) {
   912         return _s.annotation === _annotation
   912         return _s.annotation === _annotation;
   913     });
   913     });
   914 };
   914 };
   915 
   915 
   916 Mashup.prototype.getAnnotation = function(_annotation) {
   916 Mashup.prototype.getAnnotation = function(_annotation) {
   917     return ns._(this.segments).find(function(_s) {
   917     return ns._(this.segments).find(function(_s) {
   918         return _s.annotation === _annotation
   918         return _s.annotation === _annotation;
   919     });
   919     });
   920 };
   920 };
   921 
   921 
   922 Mashup.prototype.getAnnotationById = function(_id) {
   922 Mashup.prototype.getAnnotationById = function(_id) {
   923     return ns._(this.segments).find(function(_s) {
   923     return ns._(this.segments).find(function(_s) {
   924         return _s.annotation.id === _id
   924         return _s.annotation.id === _id;
   925     });
   925     });
   926 };
   926 };
   927 
   927 
   928 Mashup.prototype.getAnnotations = function() {
   928 Mashup.prototype.getAnnotations = function() {
   929     return this.segments;
   929     return this.segments;
   938 };
   938 };
   939 
   939 
   940 Mashup.prototype.getMedias = function() {
   940 Mashup.prototype.getMedias = function() {
   941     var medias = new List(this.source.directory);
   941     var medias = new List(this.source.directory);
   942     this.segments.forEach(function(_annotation) {
   942     this.segments.forEach(function(_annotation) {
   943         medias.push(_annotation.getMedia())
   943         medias.push(_annotation.getMedia());
   944     })
   944     });
   945     return medias;
   945     return medias;
   946 };
   946 };
   947 
   947 
   948 Mashup.prototype.getAnnotationsByTypeTitle = function(_title) {
   948 Mashup.prototype.getAnnotationsByTypeTitle = function(_title) {
   949     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
   949     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
   950     if (_annTypes.length) {
   950     if (_annTypes.length) {
   951         return this.getAnnotations().filter(function(_annotation) {
   951         return this.getAnnotations().filter(function(_annotation) {
   952             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
   952             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
   953         });
   953         });
   954     } else {
   954     } else {
   955         return new List(this.source.directory)
   955         return new List(this.source.directory);
   956     }
   956     }
   957 };
   957 };
   958 
   958 
   959 Mashup.prototype.getAnnotationAtTime = function(_time) {
   959 Mashup.prototype.getAnnotationAtTime = function(_time) {
   960     var _list = this.segments.filter(function(_annotation) {
   960     var _list = this.segments.filter(function(_annotation) {
   984     this.elementType = "source";
   984     this.elementType = "source";
   985     if (typeof _config !== "undefined") {
   985     if (typeof _config !== "undefined") {
   986         var _this = this;
   986         var _this = this;
   987         ns._(_config).forEach(function(_v, _k) {
   987         ns._(_config).forEach(function(_v, _k) {
   988             _this[_k] = _v;
   988             _this[_k] = _v;
   989         })
   989         });
   990         this.callbackQueue = [];
   990         this.callbackQueue = [];
   991         this.contents = {};
   991         this.contents = {};
   992         this.get();
   992         this.get();
   993     }
   993     }
   994 };
   994 };
  1018 
  1018 
  1019 Source.prototype.forEach = function(_callback) {
  1019 Source.prototype.forEach = function(_callback) {
  1020     var _this = this;
  1020     var _this = this;
  1021     ns._(this.contents).forEach(function(_value, _key) {
  1021     ns._(this.contents).forEach(function(_value, _key) {
  1022         _callback.call(_this, _value, _key);
  1022         _callback.call(_this, _value, _key);
  1023     })
  1023     });
  1024 };
  1024 };
  1025 
  1025 
  1026 Source.prototype.getElement = function(_elId) {
  1026 Source.prototype.getElement = function(_elId) {
  1027     return this.directory.getElement(_elId);
  1027     return this.directory.getElement(_elId);
  1028 };
  1028 };
  1091     _global = (typeof _global !== "undefined" && _global);
  1091     _global = (typeof _global !== "undefined" && _global);
  1092     var _res = new List(this.directory),
  1092     var _res = new List(this.directory),
  1093         _annTypes = this.getAnnotationTypes(_global).searchByTitle(_title);
  1093         _annTypes = this.getAnnotationTypes(_global).searchByTitle(_title);
  1094     _annTypes.forEach(function(_annType) {
  1094     _annTypes.forEach(function(_annType) {
  1095         _res.addElements(_annType.getAnnotations(_global));
  1095         _res.addElements(_annType.getAnnotations(_global));
  1096     })
  1096     });
  1097     return _res;
  1097     return _res;
  1098 };
  1098 };
  1099 
  1099 
  1100 Source.prototype.getDuration = function() {
  1100 Source.prototype.getDuration = function() {
  1101     var _m = this.currentMedia;
  1101     var _m = this.currentMedia;