Sparkline correction
authorveltr
Fri, 09 Nov 2012 14:47:56 +0100
changeset 918 c5d890348839
parent 917 9251909ce0f2
child 919 11650ed8126e
Sparkline correction
src/ldt/ldt/static/ldt/metadataplayer/LdtPlayer-core.js
src/ldt/ldt/static/ldt/metadataplayer/Sparkline.js
--- a/src/ldt/ldt/static/ldt/metadataplayer/LdtPlayer-core.js	Thu Nov 08 17:57:31 2012 +0100
+++ b/src/ldt/ldt/static/ldt/metadataplayer/LdtPlayer-core.js	Fri Nov 09 14:47:56 2012 +0100
@@ -235,33 +235,30 @@
 /* TODO: Separate Project-specific data from Source */
 
 /* 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,
@@ -309,13 +306,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'  
     }
 }
 
@@ -580,19 +576,12 @@
 }
 
 Model.Time.prototype.toString = function() {
-    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);
     return _res;
 }
 
--- a/src/ldt/ldt/static/ldt/metadataplayer/Sparkline.js	Thu Nov 08 17:57:31 2012 +0100
+++ b/src/ldt/ldt/static/ldt/metadataplayer/Sparkline.js	Fri Nov 09 14:47:56 2012 +0100
@@ -20,11 +20,20 @@
         _list = this.getWidgetAnnotations();
     
     for (var _i = 0; _i < this.slice_count; _i++) {
-        var _begin = new IriSP.Model.Time(_i*_duration/this.slice_count),
-            _end = new IriSP.Model.Time((_i+1)*_duration/this.slice_count),
-            _annotations = _list.filter(function(_annotation) {
-                return _annotation.begin >= _begin && _annotation.end < _end;
-            }).length;
+        var _begin = (_i*_duration/this.slice_count),
+            _end = ((_i+1)*_duration/this.slice_count),
+            _annotations = _list.reduce(function(_m, _annotation) {
+                if (_annotation.begin < _end && _annotation.begin >= _begin) {
+                    var _d = _annotation.getDuration().milliseconds;
+                    if (!_d) {
+                        return _m + 1;
+                    } else {
+                        return _m + (Math.min(_annotation.end, _end) - Math.max(_annotation.begin, _begin)) / _d;
+                    }
+                } else {
+                    return _m
+                }
+            },0);
         _max = Math.max(_max, _annotations);
         _slices.push(_annotations);
     }