client/js/main.js
changeset 196 2a2fcec209d0
parent 195 15e048e00002
child 208 0e464687eaee
--- a/client/js/main.js	Wed Jul 03 13:42:59 2013 +0200
+++ b/client/js/main.js	Thu Jul 04 12:43:30 2013 +0200
@@ -125,7 +125,7 @@
     
     this.options = _.defaults(_opts, Rkns.defaults);
         
-    Rkns._(this.options.property_files).each(function(f) {
+    _(this.options.property_files).each(function(f) {
         Rkns.$.getJSON(f, function(data) {
             _this.options.properties = _this.options.properties.concat(data);
         });
@@ -166,17 +166,17 @@
     if (!this.options.search.length) {
         this.$.find(".Rk-Web-Search-Form").detach();
     } else {
-        var _tmpl = Rkns._.template('<li class="<%= className %>" data-key="<%= key %>"><%= title %></li>'),
+        var _tmpl = _.template('<li class="<%= className %>" data-key="<%= key %>"><%= title %></li>'),
             _select = this.$.find(".Rk-Search-List"),
             _input = this.$.find(".Rk-Web-Search-Input"),
             _form = this.$.find(".Rk-Web-Search-Form");
-        Rkns._(this.options.search).each(function(_search, _key) {
+        _(this.options.search).each(function(_search, _key) {
             if (Rkns[_search.type] && Rkns[_search.type].Search) {
                 _this.search_engines.push(new Rkns[_search.type].Search(_this, _search));
             }
         });
         _select.html(
-            Rkns._(this.search_engines).map(function(_search, _key) {
+            _(this.search_engines).map(function(_search, _key) {
                 return _tmpl({
                     key: _key,
                     title: _search.getSearchTitle(),
@@ -204,7 +204,7 @@
         );
         this.setSearchEngine(0);
     }
-    Rkns._(this.options.bins).each(function(_bin) {
+    _(this.options.bins).each(function(_bin) {
         if (Rkns[_bin.type] && Rkns[_bin.type].Bin) {
             _this.tabs.push(new Rkns[_bin.type].Bin(_this, _bin));
         }
@@ -229,7 +229,7 @@
                 var _models = _this.project.get("nodes").where({
                     uri: $(_t).attr("data-uri")
                 });
-                Rkns._(_models).each(function(_model) {
+                _(_models).each(function(_model) {
                     _this.renderer.highlightModel(_model);
                 });
             }
@@ -281,11 +281,22 @@
         _this.resizeBins();
     });
     
+    var lastsearch = false, lastval = '';
+    
     this.$.find(".Rk-Bins-Search-Input").on("change keyup paste input", function() {
-       var val = Rkns.$(this).val();
-       Rkns._(_this.tabs).each(function(tab) {
-           tab.render(val);
-       });
+        var val = Rkns.$(this).val();
+        if (val === lastval) {
+            return;
+        }
+        var search = Rkns.Utils.regexpFromTextOrArray(val.length > 1 ? val: null);
+        if (search.source === lastsearch) {
+            return;
+        }
+        lastsearch = search.source;
+        _(_this.tabs).each(function(tab) {
+            tab.render(search);
+        });
+        
     });
     this.$.find(".Rk-Bins-Search-Form").submit(function() {
         return false;
@@ -293,7 +304,7 @@
     
 };
 
-Renkan.prototype.template = Rkns._.template(
+Renkan.prototype.template = _.template(
     '<% if (options.show_bins) { %><div class="Rk-Bins"><div class="Rk-Bins-Head"><h2 class="Rk-Bins-Title"><%- translate("Select contents:")%></h2>'
     + '<form class="Rk-Web-Search-Form Rk-Search-Form"><input class="Rk-Web-Search-Input Rk-Search-Input" type="search" placeholder="<%- translate("Search the Web") %>" />'
     + '<div class="Rk-Search-Select"><div class="Rk-Search-Current"></div><ul class="Rk-Search-List"></ul></div>'
@@ -336,10 +347,10 @@
 /* Utility functions */
 
 Rkns.Utils = {
-    _ID_AUTO_INCREMENT : 0,
-    _ID_BASE : (function(_d) {
-        
-        function pad(n){return n<10 ? '0'+n : n;}
+    getUID : (function() {
+        function pad(n){
+            return n<10 ? '0'+n : n;
+        }
         function fillrand(n) {
             var _res = '';
             for (var i=0; i<n; i++) {
@@ -347,22 +358,19 @@
             }
             return _res;
         }
-        return _d.getUTCFullYear() + '-'  
+        var _d = new Date(),
+            ID_AUTO_INCREMENT = 0,
+            ID_BASE = _d.getUTCFullYear() + '-'  
             + pad(_d.getUTCMonth()+1) + '-'  
             + pad(_d.getUTCDate()) + '-'
             + fillrand(16);
-        
-    })(new Date()),
-    getUID : function(_base) {
-        
-        var _n = (++this._ID_AUTO_INCREMENT).toString(16),
-            _base = (typeof _base === "undefined" ? "" : _base + "-" );
-        while (_n.length < 4) {
-            _n = '0' + _n;
+        return function(_base) {
+            var _n = (++ID_AUTO_INCREMENT).toString(16),
+                _base = (typeof _base === "undefined" ? "" : _base + "-" );
+            while (_n.length < 4) { _n = '0' + _n; }
+            return _base + this._ID_BASE + '-' + _n;
         }
-        return _base + this._ID_BASE + '-' + _n;
-        
-    },
+    })(),
     getFullURL : function(url) {
         
         if(typeof(url) == 'undefined' || url == null ) {
@@ -390,10 +398,86 @@
                 this._initialized = true;
             }
         };
-        Rkns._(_class.prototype).extend(_baseClass.prototype);
+        _(_class.prototype).extend(_baseClass.prototype);
         return _class;
         
-    }
+    },
+    regexpFromTextOrArray: (function() {
+        var charsub = [
+                '[aáàâä]',
+                '[cç]',
+                '[eéèêë]',
+                '[iíìîï]',
+                '[oóòôö]',
+                '[uùûü]'
+            ],
+            removeChars = [
+                String.fromCharCode(768), String.fromCharCode(769), String.fromCharCode(770), String.fromCharCode(771), String.fromCharCode(807),
+                "{", "}", "(", ")", "[", "]", "【", "】", "、", "・", "‥", "。", "「", "」", "『", "』", "〜", ":", "!", "?", " ",
+                ",", " ", ";", "(", ")", ".", "*", "+", "\\", "?", "|", "{", "}", "[", "]", "^", "#", "/"
+            ],
+            remsrc = "[\\" + removeChars.join("\\") + "]",
+            remrx = new RegExp(remsrc, "gm"),
+            charsrx = _(charsub).map(function(c) {
+                return new RegExp(c);
+            });
+        
+        function replaceText(_text) {
+            var txt = _text.toLowerCase().replace(remrx,""), src = "";
+            for (var j = 0; j < txt.length; j++) {
+                if (j) {
+                    src += remsrc + "*";
+                }
+                var l = txt[j];
+                _(charsub).each(function(v, k) {
+                    l = l.replace(charsrx[k], v);
+                });
+                src += l;
+            }
+            return src;
+        }
+        
+        function getSource(inp) {
+            switch (typeof inp) {
+                case "string":
+                    return replaceText(inp);
+                case "object":
+                    var src = '';
+                    _(inp).each(function(v) {
+                        var res = getSource(v);
+                        if (res) {
+                            if (src) {
+                                src += '|';
+                            }
+                            src += res;
+                        }
+                    });
+                    return src;
+            }
+            return '';
+        }
+        
+        return function(_textOrArray) {
+            var source = getSource(_textOrArray);
+            if (source) {
+                var testrx = new RegExp( source, "im"),
+                    replacerx = new RegExp( '(' + source + ')', "igm")
+                return {
+                    isempty: false,
+                    source: source,
+                    test: function(_t) { return testrx.test(_t) },
+                    replace: function(_text, _replace) { return _text.replace(replacerx, _replace); }
+                }
+            } else {
+                return {
+                    isempty: true,
+                    source: '',
+                    test: function() { return true },
+                    replace: function(_text) { return text }
+                }
+            }
+        }
+    })()
 };
 })(window);