integration/js/model.js
changeset 141 f1b68efb360a
parent 123 679809037606
child 143 3a2f9993c6cb
equal deleted inserted replaced
138:729a6ee4ea1c 141:f1b68efb360a
    15     function rand16(n) {
    15     function rand16(n) {
    16         return pad(n, Math.floor(Math.random()*Math.pow(16,n)), 16);
    16         return pad(n, Math.floor(Math.random()*Math.pow(16,n)), 16);
    17     }
    17     }
    18     
    18     
    19     var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000);
    19     var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000);
    20 
    20     
       
    21     var charsub = [
       
    22         [ 'a', 'á', 'à', 'â', 'ä' ],
       
    23         [ 'c', 'ç' ],
       
    24         [ 'e', 'é', 'è', 'ê', 'ë' ],
       
    25         [ 'i', 'í', 'ì', 'î', 'ï' ],
       
    26         [ 'o', 'ó', 'ò', 'ô', 'ö' ]
       
    27     ];
       
    28     
       
    29     var removeChars = [
       
    30         String.fromCharCode(768), String.fromCharCode(769), String.fromCharCode(770), String.fromCharCode(771), String.fromCharCode(807),
       
    31         "{", "}", "(", ")", "[", "]", "【", "】", "、", "・", "‥", "。", "「", "」", "『", "』", "〜", ":", "!", "?", " ",
       
    32         ",", " ", ";", "(", ")", ".", "*", "+", "\\", "?", "|", "{", "}", "[", "]", "^", "#", "/"
       
    33     ]
       
    34     
    21 var Model = {
    35 var Model = {
    22     _SOURCE_STATUS_EMPTY : 0,
    36     _SOURCE_STATUS_EMPTY : 0,
    23     _SOURCE_STATUS_WAITING : 1,
    37     _SOURCE_STATUS_WAITING : 1,
    24     _SOURCE_STATUS_READY : 2,
    38     _SOURCE_STATUS_READY : 2,
    25     getUID : function() {
    39     getUID : function() {
    50         if (_iexact) {
    64         if (_iexact) {
    51             _source = '^' + _source + '$';
    65             _source = '^' + _source + '$';
    52         }
    66         }
    53         return new RegExp( _source, _flags);
    67         return new RegExp( _source, _flags);
    54     },
    68     },
       
    69     fullTextRegexps: function(_text) {
       
    70         var remsrc = "[\\" + removeChars.join("\\") + "]",
       
    71             remrx = new RegExp(remsrc,"gm"),
       
    72             txt = _text.toLowerCase().replace(remrx,"")
       
    73             res = [],
       
    74             charsrc = ns._(charsub).map(function(c) {
       
    75                 return "(" + c.join("|") + ")";
       
    76             }),
       
    77             charsrx = ns._(charsrc).map(function(c) {
       
    78                 return new RegExp(c);
       
    79             }),
       
    80             src = "";
       
    81         for (var j = 0; j < txt.length; j++) {
       
    82             if (j) {
       
    83                 src += remsrc + "*";
       
    84             }
       
    85             var l = txt[j];
       
    86             ns._(charsrc).each(function(v, k) {
       
    87                 l = l.replace(charsrx[k], v);
       
    88             });
       
    89             src += l;
       
    90         }
       
    91         return "(" + src + ")";
       
    92     },
    55     isoToDate : function(_str) {
    93     isoToDate : function(_str) {
    56         // http://delete.me.uk/2005/03/iso8601.html
    94         // http://delete.me.uk/2005/03/iso8601.html
    57         var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    95         var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    58         var d = _str.match(new RegExp(regexp));
    96         var d = _str.match(new RegExp(regexp));
    59     
    97     
    98     this.__events = {};
   136     this.__events = {};
    99     if (typeof _directory == "undefined") {
   137     if (typeof _directory == "undefined") {
   100         console.trace();
   138         console.trace();
   101         throw "Error : new Model.List(directory): directory is undefined";
   139         throw "Error : new Model.List(directory): directory is undefined";
   102     }
   140     }
       
   141     var _this =  this;
       
   142     this.on("clear-search", function() {
       
   143         _this.searching = false;
       
   144         _this.regexp = undefined;
       
   145         _this.forEach(function(_element) {
       
   146             _element.found = undefined;
       
   147         });
       
   148         _this.trigger("search-cleared");
       
   149     })
   103 }
   150 }
   104 
   151 
   105 Model.List.prototype = new Array();
   152 Model.List.prototype = new Array();
   106 
   153 
   107 Model.List.prototype.hasId = function(_id) {
   154 Model.List.prototype.hasId = function(_id) {
   195         _rgxp =  Model.regexpFromTextOrArray(_text, true);
   242         _rgxp =  Model.regexpFromTextOrArray(_text, true);
   196     return this.filter(function(_element) {
   243     return this.filter(function(_element) {
   197         var keywords = (_element.keywords || _element.getTagTexts() || []).join(", ");
   244         var keywords = (_element.keywords || _element.getTagTexts() || []).join(", ");
   198         return _rgxp.test(_element.description) || _rgxp.test(_element.title) || _rgxp.test(keywords);
   245         return _rgxp.test(_element.description) || _rgxp.test(_element.title) || _rgxp.test(keywords);
   199     });
   246     });
       
   247 }
       
   248 
       
   249 Model.List.prototype.search = function(_text) {
       
   250     if (!_text) {
       
   251         this.trigger("clear-search");
       
   252         return this;
       
   253     }
       
   254     this.searching = true;
       
   255     this.trigger("search", _text);
       
   256     var rxsource = Model.fullTextRegexps(_text)
       
   257         rgxp = new RegExp(rxsource,"im"),
       
   258         this.regexp = new RegExp(rxsource,"gim");
       
   259     var res = this.filter(function(_element, _k) {
       
   260         var titlematch = rgxp.test(_element.title),
       
   261             descmatch = rgxp.test(_element.description),
       
   262             _isfound = !!(titlematch || descmatch);
       
   263         _element.found = _isfound;
       
   264         _element.trigger(_isfound ? "found" : "not-found");
       
   265         return _isfound;
       
   266     });
       
   267     this.trigger(res.length ? "found" : "not-found",res);
       
   268     return res;
   200 }
   269 }
   201 
   270 
   202 Model.List.prototype.getTitles = function() {
   271 Model.List.prototype.getTitles = function() {
   203     return this.map(function(_el) {
   272     return this.map(function(_el) {
   204         return _el.title;
   273         return _el.title;
   356     if (_hms.hours) {
   425     if (_hms.hours) {
   357         _res += _hms.hours + ':'
   426         _res += _hms.hours + ':'
   358     }
   427     }
   359     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
   428     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
   360     if (showCs) {
   429     if (showCs) {
   361         _res += "." + Math.round(_hms.milliseconds / 100)
   430         _res += "." + Math.floor(_hms.milliseconds / 100)
   362     }
   431     }
   363     return _res;
   432     return _res;
   364 }
   433 }
   365 
   434 
   366 /* Model.Reference handles references between elements
   435 /* Model.Reference handles references between elements
   932     this.contents[_listId].addElements(_contents);
  1001     this.contents[_listId].addElements(_contents);
   933 }
  1002 }
   934 
  1003 
   935 Model.Source.prototype.getList = function(_listId, _global) {
  1004 Model.Source.prototype.getList = function(_listId, _global) {
   936     _global = (typeof _global !== "undefined" && _global);
  1005     _global = (typeof _global !== "undefined" && _global);
   937     if (_global || typeof this.contents[_listId] === "undefined") {
  1006     if (_global) {
   938         return this.directory.getGlobalList().filter(function(_e) {
  1007         return this.directory.getGlobalList().filter(function(_e) {
   939             return (_e.elementType === _listId);
  1008             return (_e.elementType === _listId);
   940         });
  1009         });
   941     } else {
  1010     } else {
   942         return this.contents[_listId];
  1011         return this.contents[_listId] || new IriSP.Model.List(this.directory);
   943     }
  1012     }
   944 }
  1013 }
   945 
  1014 
   946 Model.Source.prototype.forEach = function(_callback) {
  1015 Model.Source.prototype.forEach = function(_callback) {
   947     var _this = this;
  1016     var _this = this;