Author: sevein
Date: Tue Mar 27 16:00:33 2012
New Revision: 11291

Log:
Add new dominion.js including Autocomplete class

Added:
   trunk/plugins/qtDominionPlugin/js/dominion.js   (contents, props changed)

Added: trunk/plugins/qtDominionPlugin/js/dominion.js
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/plugins/qtDominionPlugin/js/dominion.js       Tue Mar 27 16:00:33 
2012        (r11291)
@@ -0,0 +1,220 @@
+!function ($) {
+
+  "use strict";
+
+  /****
+   ****
+   ****  Autocomplete plugin
+   ****
+   ****/
+
+  var Autocomplete = function (element)
+    {
+      this.$element = element;
+      this.$menu = $('<div id="search-suggestions"></div>').appendTo('body');
+      this.shown = false;
+      this.source = element.closest('form').data('autocomplete');
+
+      this.listen();
+    };
+
+  Autocomplete.prototype = {
+
+    constructor: Autocomplete,
+
+    select: function() { },
+
+    show: function()
+      {
+        var pos = $.extend({}, this.$element.offset(), {
+          height: this.$element[0].offsetHeight
+        });
+
+        this.$menu.css(
+          {
+            top: pos.top + pos.height,
+            left: pos.left
+          });
+
+        this.$menu.show();
+        this.shown = true;
+        return this;
+      },
+
+    hide: function()
+      {
+        this.$menu.hide();
+        this.shown = false;
+        return this;
+      },
+
+    lookup: function (e)
+      {
+        this.query = this.$element.val();
+
+        $.ajax(this.source,
+          {
+            context: this,
+            data: { query: this.query },
+            dataType: 'html'
+          })
+          .done(function(html)
+            {
+              if (html)
+              {
+                this.render(html).show();
+              }
+              else
+              {
+                this.hide();
+              }
+            });
+
+        // return this.render(html).show();
+      },
+
+    // matcher
+    // sorter
+    // highlighter
+
+    render: function (html)
+      {
+        this.$menu.html(html);
+
+        return this;
+      },
+
+    next: function(event) { },
+    prev: function(event) { },
+
+    listen: function()
+      {
+        this.$element
+          .on('blur', $.proxy(this.blur, this))
+          .on('keypress', $.proxy(this.keypress, this))
+          .on('keyup', $.proxy(this.keyup, this));
+
+        if ($.browser.webkit || $.browser.msie)
+        {
+          this.$element.on('keydown', $.proxy(this.keypress, this));
+        }
+
+        this.$menu
+          .on('click', $.proxy(this.click, this))
+          .on('mouseenter', 'li', $.proxy(this.mouseenter, this))
+      },
+
+    keyup: function (e)
+      {
+        switch (e.keyCode)
+        {
+          case 40: // Down arrow
+          case 38: // Up arrow
+            break;
+
+          case 9: // Tab
+          case 13: // Enter
+            if (!this.shown)
+            {
+              return;
+            }
+            this.select();
+            break;
+
+          case 27: // Escape
+            if (!this.shown)
+            {
+              return;
+            }
+            this.hide();
+            break;
+
+          default:
+            this.lookup();
+        }
+
+        e.stopPropagation()
+        e.preventDefault()
+      },
+
+    keypress: function (e)
+      {
+        if (!this.shown)
+        {
+          return;
+        }
+
+        switch (e.keyCode)
+        {
+          case 9: // Tab
+          case 13: // Enter
+          case 27: // Escape
+            e.preventDefault();
+            break;
+
+          case 38: // Up arrow
+            e.preventDefault();
+            this.prev();
+            break;
+
+          case 40: // Down arrow
+            e.preventDefault();
+            this.next();
+            break;
+        }
+
+        e.stopPropagation();
+      },
+
+    blur: function(e)
+      {
+        var self = this;
+        setTimeout(function ()
+          {
+            // self.hide();
+          }, 150);
+      },
+
+    click: function(e)
+      {
+        e.stopPropagation();
+        e.preventDefault();
+        this.select();
+      },
+
+    mouseenter: function(e)
+      {
+        this.$menu.find('active').removeClass('active');
+        $(e.currentTarget).addClass('active');
+      }
+  };
+
+  $.fn.autocomplete = function()
+    {
+      var $this = this;
+      var data = $this.data('autocomplete');
+      if (!data)
+      {
+        $this.data('autocomplete', (data = new Autocomplete(this)));
+      }
+    };
+
+  $.fn.autocomplete.Constructor = Autocomplete;
+
+  $(function ()
+    {
+      $('body').on('focus', '#main-search input[name="query"]', function(e)
+        {
+          var $this = $(this);
+
+          if ($this.data('autocomplete'))
+          {
+            return;
+          }
+
+          e.preventDefault();
+          $this.autocomplete();
+        });
+    });
+
+}(window.jQuery);

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to