src/js/model.js
changeset 980 9ee8c00ae5b7
parent 976 4b9ec475026a
child 983 97fef7a4b189
equal deleted inserted replaced
979:ff62016e051d 980:9ee8c00ae5b7
     1 /* TODO: Separate Project-specific data from Source */
     1 /* TODO: Separate Project-specific data from Source */
     2 
     2 
     3 /* model.js is where data is stored in a standard form, whatever the serializer */
     3 /* model.js is where data is stored in a standard form, whatever the serializer */
       
     4 
     4 IriSP.Model = (function (ns) {
     5 IriSP.Model = (function (ns) {
       
     6     
       
     7     function pad(n, x, b) {
       
     8         b = b || 10;
       
     9         var s = (x).toString(b);
       
    10         while (s.length < n) {
       
    11             s = "0" + s;
       
    12         }
       
    13         return s;
       
    14     }
       
    15     
       
    16     function rand16(n) {
       
    17         return pad(n, Math.floor(Math.random()*Math.pow(16,n)), 16);
       
    18     }
       
    19     
       
    20     var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000);
     5 
    21 
     6 var Model = {
    22 var Model = {
     7     _SOURCE_STATUS_EMPTY : 0,
    23     _SOURCE_STATUS_EMPTY : 0,
     8     _SOURCE_STATUS_WAITING : 1,
    24     _SOURCE_STATUS_WAITING : 1,
     9     _SOURCE_STATUS_READY : 2,
    25     _SOURCE_STATUS_READY : 2,
    10     _ID_AUTO_INCREMENT : 0,
       
    11     _ID_BASE : (function(_d) {
       
    12         function pad(n){return n<10 ? '0'+n : n}
       
    13         function fillrand(n) {
       
    14             var _res = ''
       
    15             for (var i=0; i<n; i++) {
       
    16                 _res += Math.floor(16*Math.random()).toString(16);
       
    17             }
       
    18             return _res;
       
    19         }
       
    20         return _d.getUTCFullYear() + '-'  
       
    21             + pad(_d.getUTCMonth()+1) + '-'  
       
    22             + pad(_d.getUTCDate()) + '-'
       
    23             + fillrand(16);
       
    24     })(new Date()),
       
    25     getUID : function() {
    26     getUID : function() {
    26         var _n = (++this._ID_AUTO_INCREMENT).toString();
    27         return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6);
    27         while (_n.length < 4) {
       
    28             _n = '0' + _n
       
    29         }
       
    30         return "autoid-" + this._ID_BASE + '-' + _n;
       
    31     },
    28     },
    32     regexpFromTextOrArray : function(_textOrArray, _testOnly, _iexact) {
    29     regexpFromTextOrArray : function(_textOrArray, _testOnly, _iexact) {
    33         var _testOnly = _testOnly || false,
    30         var _testOnly = _testOnly || false,
    34             _iexact = _iexact || false;
    31             _iexact = _iexact || false;
    35         function escapeText(_text) {
    32         function escapeText(_text) {
    73         var _res = new Date();
    70         var _res = new Date();
    74         _res.setTime(Number(time));
    71         _res.setTime(Number(time));
    75         return _res;
    72         return _res;
    76     },
    73     },
    77     dateToIso : function(d) {
    74     dateToIso : function(d) {
    78         function pad(n){return n<10 ? '0'+n : n}  
       
    79         return d.getUTCFullYear()+'-'  
    75         return d.getUTCFullYear()+'-'  
    80             + pad(d.getUTCMonth()+1)+'-'  
    76             + pad(2, d.getUTCMonth()+1)+'-'  
    81             + pad(d.getUTCDate())+'T'  
    77             + pad(2, d.getUTCDate())+'T'  
    82             + pad(d.getUTCHours())+':'  
    78             + pad(2, d.getUTCHours())+':'  
    83             + pad(d.getUTCMinutes())+':'  
    79             + pad(2, d.getUTCMinutes())+':'  
    84             + pad(d.getUTCSeconds())+'Z'  
    80             + pad(2, d.getUTCSeconds())+'Z'  
    85     }
    81     }
    86 }
    82 }
    87 
    83 
    88 /*
    84 /*
    89  * Model.List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID)
    85  * Model.List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID)
   344 Model.Time.prototype.valueOf = function() {
   340 Model.Time.prototype.valueOf = function() {
   345     return this.milliseconds;
   341     return this.milliseconds;
   346 }
   342 }
   347 
   343 
   348 Model.Time.prototype.toString = function() {
   344 Model.Time.prototype.toString = function() {
   349     function pad(_n) {
       
   350         var _res = _n.toString();
       
   351         while (_res.length < 2) {
       
   352             _res = '0' + _res;
       
   353         }
       
   354         return _res;
       
   355     }
       
   356     var _hms = this.getHMS(),
   345     var _hms = this.getHMS(),
   357         _res = '';
   346         _res = '';
   358     if (_hms.hours) {
   347     if (_hms.hours) {
   359         _res += _hms.hours + ':'
   348         _res += _hms.hours + ':'
   360     }
   349     }
   361     _res += pad(_hms.minutes) + ':' + pad(_hms.seconds);
   350     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
   362     return _res;
   351     return _res;
   363 }
   352 }
   364 
   353 
   365 /* Model.Reference handles references between elements
   354 /* Model.Reference handles references between elements
   366  */
   355  */