integration/js/metadataplayer-core.js
changeset 187 38ce0eace032
parent 131 6ca3d309d1da
equal deleted inserted replaced
186:6357243985a9 187:38ce0eace032
    97     if (_regexp) {
    97     if (_regexp) {
    98         addToList(_regexp, '<span class="Ldt-Highlight">', '</span>');
    98         addToList(_regexp, '<span class="Ldt-Highlight">', '</span>');
    99     }
    99     }
   100     
   100     
   101     addToList(/(https?:\/\/)?[\w\d\-]+\.[\w\d\-]+\S+/gm, function(matches) {
   101     addToList(/(https?:\/\/)?[\w\d\-]+\.[\w\d\-]+\S+/gm, function(matches) {
   102         return '<a href="' + (matches[1] ? '' : 'http://') + matches[0] + '" target="_blank">'
   102         return '<a href="' + (matches[1] ? '' : 'http://') + matches[0] + '" target="_blank">';
   103     }, '</a>');
   103     }, '</a>');
   104     addToList(/@([\d\w]{1,15})/gm, function(matches) {
   104     addToList(/@([\d\w]{1,15})/gm, function(matches) {
   105         return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">'
   105         return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">';
   106     }, '</a>');
   106     }, '</a>');
   107     addToList(/\*[^*]+\*/gm, '<b>', '</b>');
   107     addToList(/\*[^*]+\*/gm, '<b>', '</b>');
   108     addToList(/[\n\r]+/gm, '', '<br />');
   108     addToList(/[\n\r]+/gm, '', '<br />');
   109     
   109     
   110     IriSP._(_extend).each(function(x) {
   110     IriSP._(_extend).each(function(x) {
   112     });
   112     });
   113     
   113     
   114     positions = IriSP._(positions)
   114     positions = IriSP._(positions)
   115         .chain()
   115         .chain()
   116         .uniq()
   116         .uniq()
   117         .sortBy(function(p) { return parseInt(p) })
   117         .sortBy(function(p) { return parseInt(p); })
   118         .value();
   118         .value();
   119     
   119     
   120     var res = "", lastIndex = 0;
   120     var res = "", lastIndex = 0;
   121     
   121     
   122     for (var i = 0; i < positions.length; i++) {
   122     for (var i = 0; i < positions.length; i++) {
   166 
   166 
   167 IriSP.FakeClass = function(properties) {
   167 IriSP.FakeClass = function(properties) {
   168     var _this = this,
   168     var _this = this,
   169         noop = (function() {});
   169         noop = (function() {});
   170     IriSP._(properties).each(function(p) {
   170     IriSP._(properties).each(function(p) {
   171         _this[p] = noop
   171         _this[p] = noop;
   172     });
   172     });
   173 }
   173 };
   174 
   174 
   175 /* js is where data is stored in a standard form, whatever the serializer */
   175 /* js is where data is stored in a standard form, whatever the serializer */
   176 
   176 
   177 //TODO: Separate Project-specific data from Source
   177 //TODO: Separate Project-specific data from Source
   178 
   178 
   225         return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6);
   225         return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6);
   226     },
   226     },
   227     isLocalURL = Model.isLocalURL = function(url) {
   227     isLocalURL = Model.isLocalURL = function(url) {
   228         var matches = url.match(/^(\w+:)\/\/([^/]+)/);
   228         var matches = url.match(/^(\w+:)\/\/([^/]+)/);
   229         if (matches) {
   229         if (matches) {
   230             return(matches[1] === document.location.protocol && matches[2] === document.location.host)
   230             return(matches[1] === document.location.protocol && matches[2] === document.location.host);
   231         }
   231         }
   232         return true;
   232         return true;
   233     },
   233     },
   234     regexpFromTextOrArray = Model.regexpFromTextOrArray = function(_textOrArray, _testOnly, _iexact) {
   234     regexpFromTextOrArray = Model.regexpFromTextOrArray = function(_textOrArray, _testOnly, _iexact) {
   235         var _testOnly = _testOnly || false,
   235         var _testOnly = _testOnly || false,
   252         return new RegExp( _source, _flags);
   252         return new RegExp( _source, _flags);
   253     },
   253     },
   254     fullTextRegexps = Model.fullTextRegexps = function(_text) {
   254     fullTextRegexps = Model.fullTextRegexps = function(_text) {
   255         var remsrc = "[\\" + removeChars.join("\\") + "]",
   255         var remsrc = "[\\" + removeChars.join("\\") + "]",
   256             remrx = new RegExp(remsrc,"gm"),
   256             remrx = new RegExp(remsrc,"gm"),
   257             txt = _text.toLowerCase().replace(remrx,"")
   257             txt = _text.toLowerCase().replace(remrx,""),
   258             res = [],
   258             res = [],
   259             charsrx = ns._(charsub).map(function(c) {
   259             charsrx = ns._(charsub).map(function(c) {
   260                 return new RegExp(c);
   260                 return new RegExp(c);
   261             }),
   261             }),
   262             src = "";
   262             src = "";
   302         return d.getUTCFullYear()+'-'  
   302         return d.getUTCFullYear()+'-'  
   303             + pad(2, d.getUTCMonth()+1)+'-'  
   303             + pad(2, d.getUTCMonth()+1)+'-'  
   304             + pad(2, d.getUTCDate())+'T'  
   304             + pad(2, d.getUTCDate())+'T'  
   305             + pad(2, d.getUTCHours())+':'  
   305             + pad(2, d.getUTCHours())+':'  
   306             + pad(2, d.getUTCMinutes())+':'  
   306             + pad(2, d.getUTCMinutes())+':'  
   307             + pad(2, d.getUTCSeconds())+'Z'  
   307             + pad(2, d.getUTCSeconds())+'Z'  ;
   308     };
   308     };
   309 
   309 
   310 /*
   310 /*
   311  * List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID)
   311  * List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID)
   312  */
   312  */
   325         _this.regexp = undefined;
   325         _this.regexp = undefined;
   326         _this.forEach(function(_element) {
   326         _this.forEach(function(_element) {
   327             _element.found = undefined;
   327             _element.found = undefined;
   328         });
   328         });
   329         _this.trigger("search-cleared");
   329         _this.trigger("search-cleared");
   330     })
   330     });
   331 };
   331 };
   332 
   332 
   333 List.prototype = new Array();
   333 List.prototype = new Array();
   334 
   334 
   335 List.prototype.hasId = function(_id) {
   335 List.prototype.hasId = function(_id) {
   343     List.prototype.forEach = function(_callback) {
   343     List.prototype.forEach = function(_callback) {
   344         var _this = this;
   344         var _this = this;
   345         ns._(this).forEach(function(_value, _key) {
   345         ns._(this).forEach(function(_value, _key) {
   346             _callback(_value, _key, _this);
   346             _callback(_value, _key, _this);
   347         });
   347         });
   348     }
   348     };
   349 };
   349 };
   350 
   350 
   351 if (typeof Array.prototype.map === "undefined") {
   351 if (typeof Array.prototype.map === "undefined") {
   352     List.prototype.map = function(_callback) {
   352     List.prototype.map = function(_callback) {
   353         var _this = this;
   353         var _this = this;
   354         return ns._(this).map(function(_value, _key) {
   354         return ns._(this).map(function(_value, _key) {
   355             return _callback(_value, _key, _this);
   355             return _callback(_value, _key, _this);
   356         });
   356         });
   357     }
   357     };
   358 };
   358 };
   359 
   359 
   360 List.prototype.pluck = function(_key) {
   360 List.prototype.pluck = function(_key) {
   361     return this.map(function(_value) {
   361     return this.map(function(_value) {
   362         return _value[_key];
   362         return _value[_key];
   432         this.trigger("clear-search");
   432         this.trigger("clear-search");
   433         return this;
   433         return this;
   434     }
   434     }
   435     this.searching = true;
   435     this.searching = true;
   436     this.trigger("search", _text);
   436     this.trigger("search", _text);
   437     var rxsource = fullTextRegexps(_text)
   437     var rxsource = fullTextRegexps(_text),
   438         rgxp = new RegExp(rxsource,"im"),
   438         rgxp = new RegExp(rxsource,"im");
   439         this.regexp = new RegExp(rxsource,"gim");
   439     this.regexp = new RegExp(rxsource,"gim");
   440     var res = this.filter(function(_element, _k) {
   440     var res = this.filter(function(_element, _k) {
   441         var titlematch = rgxp.test(_element.title),
   441         var titlematch = rgxp.test(_element.title),
   442             descmatch = rgxp.test(_element.description),
   442             descmatch = rgxp.test(_element.description),
   443             _isfound = !!(titlematch || descmatch);
   443             _isfound = !!(titlematch || descmatch);
   444         _element.found = _isfound;
   444         _element.found = _isfound;
   454         return _el.title;
   454         return _el.title;
   455     });
   455     });
   456 };
   456 };
   457 
   457 
   458 List.prototype.addId = function(_id) {
   458 List.prototype.addId = function(_id) {
   459     var _el = this.directory.getElement(_id)
   459     var _el = this.directory.getElement(_id);
   460     if (!this.hasId(_id) && typeof _el !== "undefined") {
   460     if (!this.hasId(_id) && typeof _el !== "undefined") {
   461         this.idIndex.push(_id);
   461         this.idIndex.push(_id);
   462         Array.prototype.push.call(this, _el);
   462         Array.prototype.push.call(this, _el);
   463     }
   463     }
   464 };
   464 };
   587     return {
   587     return {
   588         hours : Math.floor(_totalSeconds / 3600),
   588         hours : Math.floor(_totalSeconds / 3600),
   589         minutes : (Math.floor(_totalSeconds / 60) % 60),
   589         minutes : (Math.floor(_totalSeconds / 60) % 60),
   590         seconds : _totalSeconds % 60,
   590         seconds : _totalSeconds % 60,
   591         milliseconds: this.milliseconds % 1000
   591         milliseconds: this.milliseconds % 1000
   592     } 
   592     };
   593 };
   593 };
   594 
   594 
   595 Time.prototype.add = function(_milliseconds) {
   595 Time.prototype.add = function(_milliseconds) {
   596     this.milliseconds += new Time(_milliseconds).milliseconds;
   596     this.milliseconds += new Time(_milliseconds).milliseconds;
   597 };
   597 };
   602 
   602 
   603 Time.prototype.toString = function(showCs) {
   603 Time.prototype.toString = function(showCs) {
   604     var _hms = this.getHMS(),
   604     var _hms = this.getHMS(),
   605         _res = '';
   605         _res = '';
   606     if (_hms.hours) {
   606     if (_hms.hours) {
   607         _res += _hms.hours + ':'
   607         _res += _hms.hours + ':';
   608     }
   608     }
   609     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
   609     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
   610     if (showCs) {
   610     if (showCs) {
   611         _res += "." + Math.floor(_hms.milliseconds / 100)
   611         _res += "." + Math.floor(_hms.milliseconds / 100);
   612     }
   612     }
   613     return _res;
   613     return _res;
   614 };
   614 };
   615 
   615 
   616 /* Reference handles references between elements
   616 /* Reference handles references between elements
   644     return this.contents;
   644     return this.contents;
   645 };
   645 };
   646 
   646 
   647 Reference.prototype.isOrHasId = function(_idRef) {
   647 Reference.prototype.isOrHasId = function(_idRef) {
   648     if (this.isList) {
   648     if (this.isList) {
   649         return (ns._(this.id).indexOf(_idRef) !== -1)
   649         return (ns._(this.id).indexOf(_idRef) !== -1);
   650     } else {
   650     } else {
   651         return (this.id == _idRef);
   651         return (this.id == _idRef);
   652     }
   652     }
   653 };
   653 };
   654 
   654 
   656 
   656 
   657 var BaseElement = Model.Element = function(_id, _source) {
   657 var BaseElement = Model.Element = function(_id, _source) {
   658     this.elementType = 'element';
   658     this.elementType = 'element';
   659     this.title = "";
   659     this.title = "";
   660     this.description = "";
   660     this.description = "";
   661     this.__events = {}
   661     this.__events = {};
   662     if (typeof _source === "undefined") {
   662     if (typeof _source === "undefined") {
   663         return;
   663         return;
   664     }
   664     }
   665     if (typeof _id === "undefined" || !_id) {
   665     if (typeof _id === "undefined" || !_id) {
   666         _id = getUID();
   666         _id = getUID();
   738         _this.paused = true;
   738         _this.paused = true;
   739     });
   739     });
   740     this.on("timeupdate", function(_time) {
   740     this.on("timeupdate", function(_time) {
   741         _this.currentTime = _time;
   741         _this.currentTime = _time;
   742         _this.getAnnotations().filter(function(_a) {
   742         _this.getAnnotations().filter(function(_a) {
   743             return (_a.end <= _time || _a.begin > _time) && _a.playing
   743             return (_a.end <= _time || _a.begin > _time) && _a.playing;
   744         }).forEach(function(_a) {
   744         }).forEach(function(_a) {
   745             _a.playing = false;
   745             _a.playing = false;
   746             _a.trigger("leave");
   746             _a.trigger("leave");
   747             _this.trigger("leave-annotation",_a);
   747             _this.trigger("leave-annotation",_a);
   748         });
   748         });
   749         _this.getAnnotations().filter(function(_a) {
   749         _this.getAnnotations().filter(function(_a) {
   750             return _a.begin <= _time && _a.end > _time && !_a.playing
   750             return _a.begin <= _time && _a.end > _time && !_a.playing;
   751         }).forEach(function(_a) {
   751         }).forEach(function(_a) {
   752             _a.playing = true;
   752             _a.playing = true;
   753             _a.trigger("enter");
   753             _a.trigger("enter");
   754             _this.trigger("enter-annotation",_a);
   754             _this.trigger("enter-annotation",_a);
   755         });
   755         });
   828     if (_annTypes.length) {
   828     if (_annTypes.length) {
   829         return this.getAnnotations().filter(function(_annotation) {
   829         return this.getAnnotations().filter(function(_annotation) {
   830             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
   830             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
   831         });
   831         });
   832     } else {
   832     } else {
   833         return new List(this.source.directory)
   833         return new List(this.source.directory);
   834     }
   834     }
   835 };
   835 };
   836 
   836 
   837 /* */
   837 /* */
   838 
   838 
   924 Annotation.prototype.getTagTexts = function() {
   924 Annotation.prototype.getTagTexts = function() {
   925     return this.getTags().getTitles();
   925     return this.getTags().getTitles();
   926 };
   926 };
   927 
   927 
   928 Annotation.prototype.getDuration = function() {
   928 Annotation.prototype.getDuration = function() {
   929     return new Time(this.end.milliseconds - this.begin.milliseconds)
   929     return new Time(this.end.milliseconds - this.begin.milliseconds);
   930 };
   930 };
   931 
   931 
   932 /* */
   932 /* */
   933 
   933 
   934 var MashedAnnotation = Model.MashedAnnotation = function(_mashup, _annotation) {
   934 var MashedAnnotation = Model.MashedAnnotation = function(_mashup, _annotation) {
   991     this.loaded = false;
   991     this.loaded = false;
   992     var _this = this;
   992     var _this = this;
   993     this._updateTimes = function() {
   993     this._updateTimes = function() {
   994         _this.updateTimes();
   994         _this.updateTimes();
   995         _this.trigger("change");
   995         _this.trigger("change");
   996     }
   996     };
   997     this.on("add", this._updateTimes);
   997     this.on("add", this._updateTimes);
   998     this.on("remove", this._updateTimes);
   998     this.on("remove", this._updateTimes);
   999 };
   999 };
  1000 
  1000 
  1001 extendPrototype(Mashup, Playable);
  1001 extendPrototype(Mashup, Playable);
  1080     this.addAnnotationsById(_segments);
  1080     this.addAnnotationsById(_segments);
  1081 };
  1081 };
  1082 
  1082 
  1083 Mashup.prototype.hasAnnotation = function(_annotation) {
  1083 Mashup.prototype.hasAnnotation = function(_annotation) {
  1084     return !!ns._(this.segments).find(function(_s) {
  1084     return !!ns._(this.segments).find(function(_s) {
  1085         return _s.annotation === _annotation
  1085         return _s.annotation === _annotation;
  1086     });
  1086     });
  1087 };
  1087 };
  1088 
  1088 
  1089 Mashup.prototype.getAnnotation = function(_annotation) {
  1089 Mashup.prototype.getAnnotation = function(_annotation) {
  1090     return ns._(this.segments).find(function(_s) {
  1090     return ns._(this.segments).find(function(_s) {
  1091         return _s.annotation === _annotation
  1091         return _s.annotation === _annotation;
  1092     });
  1092     });
  1093 };
  1093 };
  1094 
  1094 
  1095 Mashup.prototype.getAnnotationById = function(_id) {
  1095 Mashup.prototype.getAnnotationById = function(_id) {
  1096     return ns._(this.segments).find(function(_s) {
  1096     return ns._(this.segments).find(function(_s) {
  1097         return _s.annotation.id === _id
  1097         return _s.annotation.id === _id;
  1098     });
  1098     });
  1099 };
  1099 };
  1100 
  1100 
  1101 Mashup.prototype.getAnnotations = function() {
  1101 Mashup.prototype.getAnnotations = function() {
  1102     return this.segments;
  1102     return this.segments;
  1111 };
  1111 };
  1112 
  1112 
  1113 Mashup.prototype.getMedias = function() {
  1113 Mashup.prototype.getMedias = function() {
  1114     var medias = new List(this.source.directory);
  1114     var medias = new List(this.source.directory);
  1115     this.segments.forEach(function(_annotation) {
  1115     this.segments.forEach(function(_annotation) {
  1116         medias.push(_annotation.getMedia())
  1116         medias.push(_annotation.getMedia());
  1117     })
  1117     });
  1118     return medias;
  1118     return medias;
  1119 };
  1119 };
  1120 
  1120 
  1121 Mashup.prototype.getAnnotationsByTypeTitle = function(_title) {
  1121 Mashup.prototype.getAnnotationsByTypeTitle = function(_title) {
  1122     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
  1122     var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id");
  1123     if (_annTypes.length) {
  1123     if (_annTypes.length) {
  1124         return this.getAnnotations().filter(function(_annotation) {
  1124         return this.getAnnotations().filter(function(_annotation) {
  1125             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
  1125             return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1;
  1126         });
  1126         });
  1127     } else {
  1127     } else {
  1128         return new List(this.source.directory)
  1128         return new List(this.source.directory);
  1129     }
  1129     }
  1130 };
  1130 };
  1131 
  1131 
  1132 Mashup.prototype.getAnnotationAtTime = function(_time) {
  1132 Mashup.prototype.getAnnotationAtTime = function(_time) {
  1133     var _list = this.segments.filter(function(_annotation) {
  1133     var _list = this.segments.filter(function(_annotation) {
  1157     this.elementType = "source";
  1157     this.elementType = "source";
  1158     if (typeof _config !== "undefined") {
  1158     if (typeof _config !== "undefined") {
  1159         var _this = this;
  1159         var _this = this;
  1160         ns._(_config).forEach(function(_v, _k) {
  1160         ns._(_config).forEach(function(_v, _k) {
  1161             _this[_k] = _v;
  1161             _this[_k] = _v;
  1162         })
  1162         });
  1163         this.callbackQueue = [];
  1163         this.callbackQueue = [];
  1164         this.contents = {};
  1164         this.contents = {};
  1165         this.get();
  1165         this.get();
  1166     }
  1166     }
  1167 };
  1167 };
  1181         return this.directory.getGlobalList().filter(function(_e) {
  1181         return this.directory.getGlobalList().filter(function(_e) {
  1182             return (_e.elementType === _listId);
  1182             return (_e.elementType === _listId);
  1183         });
  1183         });
  1184     } else {
  1184     } else {
  1185         if (typeof this.contents[_listId] === "undefined") {
  1185         if (typeof this.contents[_listId] === "undefined") {
  1186             this.contents[_listId] = new IriSP.List(this.directory);
  1186             this.contents[_listId] = new List(this.directory);
  1187         }
  1187         }
  1188         return this.contents[_listId];
  1188         return this.contents[_listId];
  1189     }
  1189     }
  1190 };
  1190 };
  1191 
  1191 
  1192 Source.prototype.forEach = function(_callback) {
  1192 Source.prototype.forEach = function(_callback) {
  1193     var _this = this;
  1193     var _this = this;
  1194     ns._(this.contents).forEach(function(_value, _key) {
  1194     ns._(this.contents).forEach(function(_value, _key) {
  1195         _callback.call(_this, _value, _key);
  1195         _callback.call(_this, _value, _key);
  1196     })
  1196     });
  1197 };
  1197 };
  1198 
  1198 
  1199 Source.prototype.getElement = function(_elId) {
  1199 Source.prototype.getElement = function(_elId) {
  1200     return this.directory.getElement(_elId);
  1200     return this.directory.getElement(_elId);
  1201 };
  1201 };
  1264     _global = (typeof _global !== "undefined" && _global);
  1264     _global = (typeof _global !== "undefined" && _global);
  1265     var _res = new List(this.directory),
  1265     var _res = new List(this.directory),
  1266         _annTypes = this.getAnnotationTypes(_global).searchByTitle(_title);
  1266         _annTypes = this.getAnnotationTypes(_global).searchByTitle(_title);
  1267     _annTypes.forEach(function(_annType) {
  1267     _annTypes.forEach(function(_annType) {
  1268         _res.addElements(_annType.getAnnotations(_global));
  1268         _res.addElements(_annType.getAnnotations(_global));
  1269     })
  1269     });
  1270     return _res;
  1270     return _res;
  1271 };
  1271 };
  1272 
  1272 
  1273 Source.prototype.getDuration = function() {
  1273 Source.prototype.getDuration = function() {
  1274     var _m = this.currentMedia;
  1274     var _m = this.currentMedia;
  1460     
  1460     
  1461     videoEl.on("loadedmetadata", function() {
  1461     videoEl.on("loadedmetadata", function() {
  1462         getVolume();
  1462         getVolume();
  1463         media.trigger("loadedmetadata");
  1463         media.trigger("loadedmetadata");
  1464         media.trigger("volumechange");
  1464         media.trigger("volumechange");
  1465     })
  1465     });
  1466     
  1466     
  1467     videoEl.on("timeupdate", function() {
  1467     videoEl.on("timeupdate", function() {
  1468         media.trigger("timeupdate", new IriSP.Model.Time(1000*mediaEl.currentTime));
  1468         media.trigger("timeupdate", new IriSP.Model.Time(1000*mediaEl.currentTime));
  1469     });
  1469     });
  1470     
  1470     
  1471     videoEl.on("volumechange", function() {
  1471     videoEl.on("volumechange", function() {
  1472         getVolume();
  1472         getVolume();
  1473         media.trigger("volumechange");
  1473         media.trigger("volumechange");
  1474     })
  1474     });
  1475     
  1475     
  1476     videoEl.on("play", function() {
  1476     videoEl.on("play", function() {
  1477         media.trigger("play");
  1477         media.trigger("play");
  1478     });
  1478     });
  1479     
  1479