integration/js/model.js
changeset 27 b2d068afdbd8
parent 25 eea45f9b124b
child 41 3ec2343f2b85
equal deleted inserted replaced
26:7c394ea40f28 27:b2d068afdbd8
   299     this.milliseconds = 0;
   299     this.milliseconds = 0;
   300     this.setMilliseconds(_milliseconds);
   300     this.setMilliseconds(_milliseconds);
   301 }
   301 }
   302 
   302 
   303 Model.Time.prototype.setMilliseconds = function(_milliseconds) {
   303 Model.Time.prototype.setMilliseconds = function(_milliseconds) {
   304     var _ante = _milliseconds;
   304     var _ante = this.milliseconds;
   305     switch(typeof _milliseconds) {
   305     switch(typeof _milliseconds) {
   306         case "string":
   306         case "string":
   307             this.milliseconds = parseFloat(_milliseconds);
   307             this.milliseconds = parseInt(_milliseconds);
   308             break;
   308             break;
   309         case "number":
   309         case "number":
   310             this.milliseconds = _milliseconds;
   310             this.milliseconds = Math.floor(_milliseconds);
   311             break;
   311             break;
   312         case "object":
   312         case "object":
   313             this.milliseconds = parseFloat(_milliseconds.valueOf());
   313             this.milliseconds = parseInt(_milliseconds.valueOf());
   314             break;
   314             break;
   315         default:
   315         default:
   316             this.milliseconds = 0;
   316             this.milliseconds = 0;
   317     }
   317     }
   318     if (this.milliseconds === NaN) {
   318     if (this.milliseconds === NaN) {
   331 Model.Time.prototype.getHMS = function() {
   331 Model.Time.prototype.getHMS = function() {
   332     var _totalSeconds = Math.abs(Math.floor(this.getSeconds()));
   332     var _totalSeconds = Math.abs(Math.floor(this.getSeconds()));
   333     return {
   333     return {
   334         hours : Math.floor(_totalSeconds / 3600),
   334         hours : Math.floor(_totalSeconds / 3600),
   335         minutes : (Math.floor(_totalSeconds / 60) % 60),
   335         minutes : (Math.floor(_totalSeconds / 60) % 60),
   336         seconds : _totalSeconds % 60
   336         seconds : _totalSeconds % 60,
       
   337         milliseconds: this.milliseconds % 1000
   337     } 
   338     } 
   338 }
   339 }
   339 
   340 
   340 Model.Time.prototype.add = function(_milliseconds) {
   341 Model.Time.prototype.add = function(_milliseconds) {
   341     this.milliseconds += new Model.Time(_milliseconds).milliseconds;
   342     this.milliseconds += new Model.Time(_milliseconds).milliseconds;
   343 
   344 
   344 Model.Time.prototype.valueOf = function() {
   345 Model.Time.prototype.valueOf = function() {
   345     return this.milliseconds;
   346     return this.milliseconds;
   346 }
   347 }
   347 
   348 
   348 Model.Time.prototype.toString = function() {
   349 Model.Time.prototype.toString = function(showCs) {
   349     function pad(_n) {
   350     function pad(_n) {
   350         var _res = _n.toString();
   351         var _res = _n.toString();
   351         while (_res.length < 2) {
   352         while (_res.length < 2) {
   352             _res = '0' + _res;
   353             _res = '0' + _res;
   353         }
   354         }
   357         _res = '';
   358         _res = '';
   358     if (_hms.hours) {
   359     if (_hms.hours) {
   359         _res += _hms.hours + ':'
   360         _res += _hms.hours + ':'
   360     }
   361     }
   361     _res += pad(_hms.minutes) + ':' + pad(_hms.seconds);
   362     _res += pad(_hms.minutes) + ':' + pad(_hms.seconds);
       
   363     if (showCs) {
       
   364         _res += "." + Math.round(_hms.milliseconds / 100)
       
   365     }
   362     return _res;
   366     return _res;
   363 }
   367 }
   364 
   368 
   365 /* Model.Reference handles references between elements
   369 /* Model.Reference handles references between elements
   366  */
   370  */
   524 
   528 
   525 Model.Playable.prototype.pause = function() {
   529 Model.Playable.prototype.pause = function() {
   526     this.trigger("setpause");
   530     this.trigger("setpause");
   527 }
   531 }
   528 
   532 
       
   533 Model.Playable.prototype.show = function() {}
       
   534 
       
   535 Model.Playable.prototype.hide = function() {}
   529 
   536 
   530 /* */
   537 /* */
   531 
   538 
   532 Model.Media = function(_id, _source) {
   539 Model.Media = function(_id, _source) {
   533     Model.Playable.call(this, _id, _source);
   540     Model.Playable.call(this, _id, _source);
   678     this.annotation = _annotation;
   685     this.annotation = _annotation;
   679     this.begin = new Model.Time();
   686     this.begin = new Model.Time();
   680     this.end = new Model.Time();
   687     this.end = new Model.Time();
   681     this.duration = new Model.Time();
   688     this.duration = new Model.Time();
   682     this.title = this.annotation.title;
   689     this.title = this.annotation.title;
   683     this.media_title = this.getMedia().title;
       
   684     this.description = this.annotation.description;
   690     this.description = this.annotation.description;
   685     this.color = this.annotation.color;
   691     this.color = this.annotation.color;
   686     var _this = this;
   692     var _this = this;
   687     this.on("click", function() {
   693     this.on("click", function() {
   688         _mashup.setCurrentTime(_this.begin);
   694         _mashup.setCurrentTime(_this.begin);
       
   695     });
       
   696     this.on("enter", function() {
       
   697         _this.annotation.trigger("enter");
       
   698     });
       
   699     this.on("leave", function() {
       
   700         _this.annotation.trigger("leave");
   689     });
   701     });
   690 }
   702 }
   691 
   703 
   692 Model.MashedAnnotation.prototype = new Model.Element(null);
   704 Model.MashedAnnotation.prototype = new Model.Element(null);
   693 
   705 
   722 Model.Mashup = function(_id, _source) {
   734 Model.Mashup = function(_id, _source) {
   723     Model.Playable.call(this, _id, _source);
   735     Model.Playable.call(this, _id, _source);
   724     this.elementType = 'mashup';
   736     this.elementType = 'mashup';
   725     this.duration = new Model.Time();
   737     this.duration = new Model.Time();
   726     this.segments = new Model.List(_source.directory);
   738     this.segments = new Model.List(_source.directory);
       
   739     this.loaded = false;
   727     var _currentMedia = null;
   740     var _currentMedia = null;
   728     var _this = this;
   741     var _this = this;
   729     this.on("timeupdate", function(_time) {
   742     this.on("timeupdate", function(_time) {
   730         _this.getAnnotations().filter(function(_a) {
   743         _this.getSegments().filter(function(_a) {
   731             return (_a.end <= _time || _a.begin > _time) && _a.playing
   744             return (_a.end <= _time || _a.begin > _time) && _a.playing
   732         }).forEach(function(_a) {
   745         }).forEach(function(_a) {
   733             _a.playing = false;
   746             _a.playing = false;
   734             _a.trigger("leave");
   747             _a.trigger("leave");
   735         });
   748         });
   736         _this.getAnnotations().filter(function(_a) {
   749         _this.getSegments().filter(function(_a) {
   737             return _a.begin <= _time && _a.end > _time && !_a.playing
   750             return _a.begin <= _time && _a.end > _time && !_a.playing
   738         }).forEach(function(_a) {
   751         }).forEach(function(_a) {
   739             _a.playing = true;
   752             _a.playing = true;
   740             _a.trigger("enter");
   753             _a.trigger("enter");
   741             var _m = _a.getMedia();
   754             var _m = _a.getMedia();
   748             }
   761             }
   749         });
   762         });
   750     });
   763     });
   751     this._updateTimes = function() {
   764     this._updateTimes = function() {
   752         _this.updateTimes();
   765         _this.updateTimes();
   753     }
   766         _this.trigger("change");
   754     this.on("add-segments", this._updateTimes);
   767     }
   755     this.on("remove-segments", this._updateTimes);
   768     this.on("add", this._updateTimes);
       
   769     this.on("remove", this._updateTimes);
   756 }
   770 }
   757 
   771 
   758 Model.Mashup.prototype = new Model.Playable();
   772 Model.Mashup.prototype = new Model.Playable();
       
   773 
       
   774 Model.Mashup.prototype.checkLoaded = function() {
       
   775     var loaded = !!this.segments.length;
       
   776     this.getMedias().forEach(function(_m) {
       
   777         loaded = loaded && _m.loaded;
       
   778     });
       
   779     this.loaded = loaded;
       
   780     if (loaded) {
       
   781         this.trigger("loadedmetadata");
       
   782     }
       
   783 }
   759 
   784 
   760 Model.Mashup.prototype.updateTimes = function() {
   785 Model.Mashup.prototype.updateTimes = function() {
   761     var _time = 0;
   786     var _time = 0;
   762     this.segments.forEach(function(_segment) {
   787     this.segments.forEach(function(_segment) {
   763         _segment.setBegin(_time);
   788         _segment.setBegin(_time);
   764         _time = _segment.end;
   789         _time = _segment.end;
   765     });
   790     });
   766     this.duration.setMilliseconds(_time);
   791     this.duration.setMilliseconds(_time);
   767 }
   792 }
   768 
   793 
   769 Model.Mashup.prototype.addSegment = function(_annotation, _defer) {
   794 Model.Mashup.prototype.addAnnotation = function(_annotation, _defer) {
   770     var _mashedAnnotation = new Model.MashedAnnotation(this, _annotation),
   795     var _mashedAnnotation = new Model.MashedAnnotation(this, _annotation),
   771         _defer = _defer || false;
   796         _defer = _defer || false;
   772     this.segments.push(_mashedAnnotation);
   797     this.segments.push(_mashedAnnotation);
   773     _annotation.on("change-begin", this._updateTimes);
   798     _annotation.on("change-begin", this._updateTimes);
   774     _annotation.on("change-end", this._updateTimes);
   799     _annotation.on("change-end", this._updateTimes);
   775     if (!_defer) {
   800     if (!_defer) {
   776         this.trigger("add-segments");
   801         this.trigger("add");
   777     }
   802     }
   778 }
   803 }
   779 
   804 
   780 Model.Mashup.prototype.addSegmentById = function(_elId, _defer) {
   805 Model.Mashup.prototype.addAnnotationById = function(_elId, _defer) {
   781     var _annotation = this.source.getElement(_elId),
   806     var _annotation = this.source.getElement(_elId),
   782         _defer = _defer || false;
   807         _defer = _defer || false;
   783     if (typeof _annotation !== "undefined") {
   808     if (typeof _annotation !== "undefined") {
   784         this.addSegment(_annotation, _defer);
   809         this.addAnnotation(_annotation, _defer);
   785     }
   810     }
   786 }
   811 }
   787 
   812 
   788 Model.Mashup.prototype.addSegments = function(_segments) {
   813 Model.Mashup.prototype.addAnnotations = function(_segments) {
   789     var _this = this;
   814     var _this = this;
   790     ns._(_segments).forEach(function(_segment) {
   815     ns._(_segments).forEach(function(_segment) {
   791         _this.addSegment(_segment, true);
   816         _this.addAnnotation(_segment, true);
   792     });
   817     });
   793     this.trigger("add-segments");
   818     this.trigger("add");
   794 }
   819 }
   795 
   820 
   796 Model.Mashup.prototype.addSegmentsById = function(_segments) {
   821 Model.Mashup.prototype.addAnnotationsById = function(_segments) {
   797     var _this = this;
   822     var _this = this;
   798     ns._(_segments).forEach(function(_segment) {
   823     ns._(_segments).forEach(function(_segment) {
   799         _this.addSegmentById(_segment, true);
   824         _this.addAnnotationById(_segment, true);
   800     });
   825     });
   801     this.trigger("add-segments");
   826     this.trigger("add");
   802 }
   827 }
   803 
   828 
   804 Model.Mashup.prototype.removeSegment = function(_annotation, _defer) {
   829 Model.Mashup.prototype.removeAnnotation = function(_annotation, _defer) {
   805     var _defer = _defer || false;
   830     var _defer = _defer || false;
   806     _annotation.off("change-begin", this._updateTimes);
   831     _annotation.off("change-begin", this._updateTimes);
   807     _annotation.off("change-end", this._updateTimes);
   832     _annotation.off("change-end", this._updateTimes);
   808     this.segments.removeElement(_annotation);
   833     this.segments.removeId(this.id + "_" + _annotation.id);
   809     if (!_defer) {
   834     if (!_defer) {
   810         this.trigger("remove-segments");
   835         this.trigger("remove");
   811     }
   836     }
   812 }
   837 }
   813 
   838 
   814 Model.Mashup.prototype.removeSegmentById = function(_annId, _defer) {
   839 Model.Mashup.prototype.removeAnnotationById = function(_annId, _defer) {
   815     var _defer = _defer || false;
   840     var _defer = _defer || false;
   816     var _annotation = this.source.getElementById(_annId);
   841     var _annotation = this.source.getElement(_annId);
       
   842 
   817     if (_annotation) {
   843     if (_annotation) {
   818         this.removeSegment(_annotation, _defer);
   844         this.removeAnnotation(_annotation, _defer);
   819     }
   845     }
   820     this.segments.removeElement(_annotation);
       
   821     if (!_defer) {
   846     if (!_defer) {
   822         this.trigger("remove-segments");
   847         this.trigger("remove");
   823     }
   848     }
   824 }
   849 }
   825 
   850 
   826 Model.Mashup.prototype.getAnnotations = function() {
   851 Model.Mashup.prototype.setAnnotations = function(_segments) {
       
   852     while (this.segments.length) {
       
   853         this.removeAnnotation(this.segments[0].annotation, true);
       
   854     }
       
   855     this.addAnnotations(_segments);
       
   856 }
       
   857 
       
   858 Model.Mashup.prototype.setAnnotationsById = function(_segments) {
       
   859     while (this.segments.length) {
       
   860         this.removeAnnotation(this.segments[0].annotation, true);
       
   861     }
       
   862     this.addAnnotationsById(_segments);
       
   863 }
       
   864 
       
   865 Model.Mashup.prototype.hasAnnotation = function(_annotation) {
       
   866     return !!ns._(this.segments).find(function(_s) {
       
   867         return _s.annotation === _annotation
       
   868     });
       
   869 }
       
   870 
       
   871 Model.Mashup.prototype.getAnnotation = function(_annotation) {
       
   872     return ns._(this.segments).find(function(_s) {
       
   873         return _s.annotation === _annotation
       
   874     });
       
   875 }
       
   876 
       
   877 Model.Mashup.prototype.getAnnotationById = function(_id) {
       
   878     return ns._(this.segments).find(function(_s) {
       
   879         return _s.annotation.id === _id
       
   880     });
       
   881 }
       
   882 
       
   883 Model.Mashup.prototype.getSegments = function() {
   827     return this.segments;
   884     return this.segments;
   828 }
   885 }
   829 
   886 
   830 Model.Mashup.prototype.getMedias = function() {
   887 Model.Mashup.prototype.getMedias = function() {
   831     var medias = new Model.List(_source.directory);
   888     var medias = new Model.List(this.source.directory);
   832     this.segments.each(function(_annotation) {
   889     this.segments.forEach(function(_annotation) {
   833         medias.push(_annotation.getMedia())
   890         medias.push(_annotation.getMedia())
   834     })
   891     })
   835     return medias;
   892     return medias;
   836 }
   893 }
   837 
   894