integration/js/model.js
changeset 141 f1b68efb360a
parent 123 679809037606
child 143 3a2f9993c6cb
--- a/integration/js/model.js	Thu Dec 06 17:07:49 2012 +0100
+++ b/integration/js/model.js	Thu Jan 17 17:20:23 2013 +0100
@@ -17,7 +17,21 @@
     }
     
     var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000);
-
+    
+    var charsub = [
+        [ 'a', 'á', 'à', 'â', 'ä' ],
+        [ 'c', 'ç' ],
+        [ 'e', 'é', 'è', 'ê', 'ë' ],
+        [ 'i', 'í', 'ì', 'î', 'ï' ],
+        [ 'o', 'ó', 'ò', 'ô', 'ö' ]
+    ];
+    
+    var removeChars = [
+        String.fromCharCode(768), String.fromCharCode(769), String.fromCharCode(770), String.fromCharCode(771), String.fromCharCode(807),
+        "{", "}", "(", ")", "[", "]", "【", "】", "、", "・", "‥", "。", "「", "」", "『", "』", "〜", ":", "!", "?", " ",
+        ",", " ", ";", "(", ")", ".", "*", "+", "\\", "?", "|", "{", "}", "[", "]", "^", "#", "/"
+    ]
+    
 var Model = {
     _SOURCE_STATUS_EMPTY : 0,
     _SOURCE_STATUS_WAITING : 1,
@@ -52,6 +66,30 @@
         }
         return new RegExp( _source, _flags);
     },
+    fullTextRegexps: function(_text) {
+        var remsrc = "[\\" + removeChars.join("\\") + "]",
+            remrx = new RegExp(remsrc,"gm"),
+            txt = _text.toLowerCase().replace(remrx,"")
+            res = [],
+            charsrc = ns._(charsub).map(function(c) {
+                return "(" + c.join("|") + ")";
+            }),
+            charsrx = ns._(charsrc).map(function(c) {
+                return new RegExp(c);
+            }),
+            src = "";
+        for (var j = 0; j < txt.length; j++) {
+            if (j) {
+                src += remsrc + "*";
+            }
+            var l = txt[j];
+            ns._(charsrc).each(function(v, k) {
+                l = l.replace(charsrx[k], v);
+            });
+            src += l;
+        }
+        return "(" + src + ")";
+    },
     isoToDate : function(_str) {
         // http://delete.me.uk/2005/03/iso8601.html
         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})))?)?)?)?";
@@ -100,6 +138,15 @@
         console.trace();
         throw "Error : new Model.List(directory): directory is undefined";
     }
+    var _this =  this;
+    this.on("clear-search", function() {
+        _this.searching = false;
+        _this.regexp = undefined;
+        _this.forEach(function(_element) {
+            _element.found = undefined;
+        });
+        _this.trigger("search-cleared");
+    })
 }
 
 Model.List.prototype = new Array();
@@ -199,6 +246,28 @@
     });
 }
 
+Model.List.prototype.search = function(_text) {
+    if (!_text) {
+        this.trigger("clear-search");
+        return this;
+    }
+    this.searching = true;
+    this.trigger("search", _text);
+    var rxsource = Model.fullTextRegexps(_text)
+        rgxp = new RegExp(rxsource,"im"),
+        this.regexp = new RegExp(rxsource,"gim");
+    var res = this.filter(function(_element, _k) {
+        var titlematch = rgxp.test(_element.title),
+            descmatch = rgxp.test(_element.description),
+            _isfound = !!(titlematch || descmatch);
+        _element.found = _isfound;
+        _element.trigger(_isfound ? "found" : "not-found");
+        return _isfound;
+    });
+    this.trigger(res.length ? "found" : "not-found",res);
+    return res;
+}
+
 Model.List.prototype.getTitles = function() {
     return this.map(function(_el) {
         return _el.title;
@@ -358,7 +427,7 @@
     }
     _res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds);
     if (showCs) {
-        _res += "." + Math.round(_hms.milliseconds / 100)
+        _res += "." + Math.floor(_hms.milliseconds / 100)
     }
     return _res;
 }
@@ -934,12 +1003,12 @@
 
 Model.Source.prototype.getList = function(_listId, _global) {
     _global = (typeof _global !== "undefined" && _global);
-    if (_global || typeof this.contents[_listId] === "undefined") {
+    if (_global) {
         return this.directory.getGlobalList().filter(function(_e) {
             return (_e.elementType === _listId);
         });
     } else {
-        return this.contents[_listId];
+        return this.contents[_listId] || new IriSP.Model.List(this.directory);
     }
 }