Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 5d79a3c3d05f9eb6a107d507f256dbc9ac23d79e @wycats committed Sep 16, 2008
@@ -0,0 +1,17 @@
+jQuery(function($) {
+ $("input.autocomplete").autocomplete({
+ getList: function() {
+ var self = $(this);
+
+ $.getJSON("/simple/list", function(json) {
+ self.fn("setList", json.array);
+ });
+ }
+ })
+ .bind("autocomplete.activate", function(e, d) {
+ console.log(d);
+ })
+ .bind("autocomplete.cancel", function(e, d) {
+ console.log("Cancelling");
+ });
+});
@@ -0,0 +1,17 @@
+(function($) {
+
+ $.delayedInterval = function(fn, time, halted) {
+ var interval;
+
+ this.reset = function() {
+ clearInterval(interval);
+ interval = setInterval(fn, time);
+ };
+
+ this.pause = function() { clearInterval(interval); };
+ this.start = function() { return setInterval(fn, time); };
+
+ if(!halted) interval = this.start();
+ };
+
+})(jQuery);
@@ -0,0 +1,130 @@
+$.fn.autocomplete = function(opts) {
+ opts.list = opts.list || function(arr) {
+ ret = "<ul class='complete'><li>";
+ ret += arr.join("</li><li>");
+ return ret + "</li></ul>";
+ };
+
+ opts.match = opts.match || function(element, typed) {
+ return element.match(new RegExp(typed, "i"));
+ };
+
+ // Low-level events
+
+ // Main keyboard event catcher. In general, you don't want to be
+ // modifying this later.
+ this.keyup(function(e) {
+ if($.isTypedChar(e.keyCode)) $(this).trigger("autocomplete.typed");
+ else if(e.keyCode == 38) $(this).trigger("autocomplete.uparrow");
+ else if(e.keyCode == 40) $(this).trigger("autocomplete.downarrow");
+ else if(e.keyCode == 9) $(this).trigger("autocomplete.tab");
+ else if(e.keyCode == 27) $(this).trigger("autocomplete.escape");
+ else if(e.keyCode == 8) $(this).trigger("autocomplete.backspace");
+ else if(e.keyCode == 13)
+ $(this).trigger("autocomplete.activate", [$(this).val()]);
+ });
+
+ // manage state and catch high-level events
+
+ var state = {
+ toSelectState: function(arr) {
+
+ $.currentState("autocomplete", "select");
+ var self = $(this), original = self.val();
+
+ arr = $(arr).filter(function() {
+ return opts.match(this, original);
+ }).get();
+
+ if(!arr.length) return self.fn("toTypeState");
+
+ var list = $(opts.list.call(self, arr));
+ var size = $("li", list).length;
+ var selected = null;
+
+ list.appendTo("body").css({left: self.offset().left,
+ top: self.offset().top + self.outerHeight});
+
+ var updateSelected = function() {
+ if(selected < 0) selected = size + (selected % size);
+ var el = $("li", list)
+ .removeClass("selected")
+ .eq(selected % size)
+ .addClass("selected");
+
+ self.val(el.html());
+ };
+
+ $(this)
+ .unbind("autocomplete:select")
+ .bind("autocomplete:select.typed", function() {
+ list.remove();
+ self.fn("toTypeState").trigger("autocomplete.typed");
+
+ }).bind("autocomplete:select.escape", function() {
+
+ list.remove();
+ self.val(original).trigger("autocomplete.cancel").fn("toTypeState");
+
+ }).bind("autocomplete:select.backspace", function() {
+
+ list.remove();
+ self.fn("toTypeState").trigger("autocomplete.backspace");
+
+ })
+ .bind("autocomplete:select.downarrow", function() {
+
+ selected = selected != null ? selected + 1 : 0;
+ updateSelected();
+
+ }).bind("autocomplete:select.uparrow", function() {
+
+ selected = selected != null ? selected - 1 : -1;
+ updateSelected();
+
+ }).bind("autocomplete:select.activate", function() {
+
+ updateSelected();
+ list.remove();
+ self.fn("toTypeState");
+
+ });
+
+ }, toTypeState: function() {
+
+ $.currentState("autocomplete", "type");
+ var countup = function() {
+ var self = $(this);
+
+ var interval = self.data("interval");
+
+ if(!interval) {
+ interval = new $.delayedInterval(function() {
+ interval.pause();
+ opts.getList.call(self);
+ }, 1000, true);
+ self.data("interval", interval);
+ }
+
+ interval.reset();
+ };
+
+ $(this)
+ .unbind("autocomplete:type")
+ .bind("autocomplete:type.escape", function() {
+ var interval = $(this).data("interval");
+ if(interval) interval.pause();
+ })
+ .bind("autocomplete:type.typed", countup)
+ .bind("autocomplete:type.downarrow", countup)
+ .bind("autocomplete:type.uparrow", countup)
+ .bind("autocomplete:type.backspace", countup);
+ },
+
+ setList: function(list) { $(this).fn("toSelectState", list); }
+
+ };
+
+ return this.fn(state).fn("toTypeState");
+
+};
@@ -0,0 +1,37 @@
+(function($) {
+
+ $.fn.fn = function() {
+ var self = this;
+ var extension = arguments[0], name = arguments[0];
+ if (typeof name == "string") {
+ return apply(self, name, $.makeArray(arguments).slice(1, arguments.length));
+ } else {
+ $.each(extension, function(key, value) {
+ define(self, key, value);
+ });
+ return self;
+ }
+ };
+
+ function define(self, name, fn) {
+ self.data(namespacedName(name), fn);
+ };
+
+ function apply(self, name, args) {
+ var result;
+ self.each(function(i, item) {
+ var fn = $(item).data(namespacedName(name));
+ if (fn)
+ result = fn.apply(item, args);
+ else
+ throw(name + " is not defined");
+ });
+ if(typeof result == "undefined") return self;
+ else return result;
+ };
+
+ function namespacedName(name) {
+ return 'fn.' + name;
+ }
+
+})(jQuery);
Oops, something went wrong.

0 comments on commit 5d79a3c

Please sign in to comment.