integration/js/model.js
changeset 41 3ec2343f2b85
parent 27 b2d068afdbd8
child 42 40909e8d6855
--- a/integration/js/model.js	Tue Nov 06 18:49:13 2012 +0100
+++ b/integration/js/model.js	Thu Nov 08 18:24:47 2012 +0100
@@ -2,32 +2,28 @@
 
 /* model.js is where data is stored in a standard form, whatever the serializer */
 IriSP.Model = (function (ns) {
+    
+    function pad(n, x, b) {
+        b = b || 10;
+        var s = (x).toString(b);
+        while (s.length < n) {
+            s = "0" + s;
+        }
+        return s;
+    }
+    
+    function rand16(n) {
+        return pad(n, Math.floor(Math.random()*Math.pow(16,n)), 16);
+    }
+    
+    var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000);
 
 var Model = {
     _SOURCE_STATUS_EMPTY : 0,
     _SOURCE_STATUS_WAITING : 1,
     _SOURCE_STATUS_READY : 2,
-    _ID_AUTO_INCREMENT : 0,
-    _ID_BASE : (function(_d) {
-        function pad(n){return n<10 ? '0'+n : n}
-        function fillrand(n) {
-            var _res = ''
-            for (var i=0; i<n; i++) {
-                _res += Math.floor(16*Math.random()).toString(16);
-            }
-            return _res;
-        }
-        return _d.getUTCFullYear() + '-'  
-            + pad(_d.getUTCMonth()+1) + '-'  
-            + pad(_d.getUTCDate()) + '-'
-            + fillrand(16);
-    })(new Date()),
     getUID : function() {
-        var _n = (++this._ID_AUTO_INCREMENT).toString();
-        while (_n.length < 4) {
-            _n = '0' + _n
-        }
-        return "autoid-" + this._ID_BASE + '-' + _n;
+        return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6);
     },
     regexpFromTextOrArray : function(_textOrArray, _testOnly, _iexact) {
         var _testOnly = _testOnly || false,
@@ -75,13 +71,12 @@
         return _res;
     },
     dateToIso : function(d) {
-        function pad(n){return n<10 ? '0'+n : n}  
         return d.getUTCFullYear()+'-'  
-            + pad(d.getUTCMonth()+1)+'-'  
-            + pad(d.getUTCDate())+'T'  
-            + pad(d.getUTCHours())+':'  
-            + pad(d.getUTCMinutes())+':'  
-            + pad(d.getUTCSeconds())+'Z'  
+            + pad(2, d.getUTCMonth()+1)+'-'  
+            + pad(2, d.getUTCDate())+'T'  
+            + pad(2, d.getUTCHours())+':'  
+            + pad(2, d.getUTCMinutes())+':'  
+            + pad(2, d.getUTCSeconds())+'Z'  
     }
 }
 
@@ -347,19 +342,12 @@
 }
 
 Model.Time.prototype.toString = function(showCs) {
-    function pad(_n) {
-        var _res = _n.toString();
-        while (_res.length < 2) {
-            _res = '0' + _res;
-        }
-        return _res;
-    }
     var _hms = this.getHMS(),
         _res = '';
     if (_hms.hours) {
         _res += _hms.hours + ':'
     }
-    _res += pad(_hms.minutes) + ':' + pad(_hms.seconds);
+    _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
     if (showCs) {
         _res += "." + Math.round(_hms.milliseconds / 100)
     }
@@ -409,6 +397,9 @@
 
 Model.Element = function(_id, _source) {
     this.elementType = 'element';
+    this.title = "";
+    this.description = "";
+    this.__events = {}
     if (typeof _source === "undefined") {
         return;
     }
@@ -417,9 +408,6 @@
     }
     this.source = _source;
     this.id = _id;
-    this.title = "";
-    this.description = "";
-    this.__events = {}
     this.source.directory.addElement(this);
 }
 
@@ -489,6 +477,20 @@
     });
     this.on("timeupdate", function(_time) {
         _this.currentTime = _time;
+        _this.getAnnotations().filter(function(_a) {
+            return (_a.end <= _time || _a.begin > _time) && _a.playing
+        }).forEach(function(_a) {
+            _a.playing = false;
+            _a.trigger("leave");
+            _this.trigger("leave-annotation",_a);
+        });
+        _this.getAnnotations().filter(function(_a) {
+            return _a.begin <= _time && _a.end > _time && !_a.playing
+        }).forEach(function(_a) {
+            _a.playing = true;
+            _a.trigger("enter");
+            _this.trigger("enter-annotation",_a);
+        });
     });
 }
 
@@ -541,22 +543,7 @@
     this.elementType = 'media';
     this.duration = new Model.Time();
     this.video = '';
-    
     var _this = this;
-    this.on("timeupdate", function(_time) {
-        _this.getAnnotations().filter(function(_a) {
-            return (_a.end <= _time || _a.begin > _time) && _a.playing
-        }).forEach(function(_a) {
-            _a.playing = false;
-            _a.trigger("leave");
-        });
-        _this.getAnnotations().filter(function(_a) {
-            return _a.begin <= _time && _a.end > _time && !_a.playing
-        }).forEach(function(_a) {
-            _a.playing = true;
-            _a.trigger("enter");
-        });
-    });
 }
 
 Model.Media.prototype = new Model.Playable();
@@ -737,30 +724,7 @@
     this.duration = new Model.Time();
     this.segments = new Model.List(_source.directory);
     this.loaded = false;
-    var _currentMedia = null;
     var _this = this;
-    this.on("timeupdate", function(_time) {
-        _this.getSegments().filter(function(_a) {
-            return (_a.end <= _time || _a.begin > _time) && _a.playing
-        }).forEach(function(_a) {
-            _a.playing = false;
-            _a.trigger("leave");
-        });
-        _this.getSegments().filter(function(_a) {
-            return _a.begin <= _time && _a.end > _time && !_a.playing
-        }).forEach(function(_a) {
-            _a.playing = true;
-            _a.trigger("enter");
-            var _m = _a.getMedia();
-            if (_m !== _currentMedia) {
-                if (_currentMedia) {
-                    _currentMedia.trigger("leave");
-                }
-                _m.trigger("enter");
-                _currentMedia = _m;
-            }
-        });
-    });
     this._updateTimes = function() {
         _this.updateTimes();
         _this.trigger("change");
@@ -880,7 +844,7 @@
     });
 }
 
-Model.Mashup.prototype.getSegments = function() {
+Model.Mashup.prototype.getAnnotations = function() {
     return this.segments;
 }