Commits

A S committed 2496522

Faster delimit.lookup method using a callback func

Comments (0)

Files changed (1)

pypysite/static/js/script.js

         });
     },
 
-    lookup: function (query) {
+    lookup: function (query, callback) {
         var self = this,
             data = self.datalist,
             found = self.found = [],
             datalen = data.length,
             i;
 
+        if (typeof callback !== 'function') {
+            callback = false;
+        }
+
         for (i = 0; i < datalen; i += 1) {
             if (data[i][0].indexOf(query) >= 0) {
                 found.push([data[i][0], i]);
+
+                if (callback) {
+                    callback.apply(delimit, [i, 'show']);
+                }
+            } else {
+                if (callback) {
+                    callback.apply(delimit, [i, 'hide']);
+                }
             }
         }
 
         return found;
     },
 
+    // lookup callback
+    setstate: function (itemid, state) {
+        var self = this,
+            item = $(self.elems.items[itemid]);
+        
+        if (state === 'show') {
+            item.addClass('active');
+        } else {
+            item.removeClass('active');
+        }
+    },
+
     view: function (query) {
         var self = this,
             mouthopen = mouth.elems.contents.hasClass('active'),
         } else {
             self.elems.list.addClass('searching');
 
-            result = self.lookup(query);
+            result = self.lookup(query, self.setstate);
             resultlen = result.length;
 
             if (resultlen > 0) {
 
     draw: function (result) {
         var self = this,
-            resultlen = result.length,
-            i;
-
-        self.elems.items.removeClass('active');
+            resultlen = result.length;
 
         if (resultlen === 0) {
             self.elems.supported.addClass('none');
             self.elems.list.show();
             self.elems.result.hide();
         }
-
-        for (i = 0; i < resultlen; i += 1) {
-            $(self.elems.items[result[i][1]]).addClass('active');
-        }
     }
 
 };
     osdetect();
     subnav.init();
     donate.init();
+    mouth.init();
     delimit.init();
-    mouth.init();
 
     // Twitter
     $('#twitter-controls').delegate('a', 'click', function (e) {