Patch to allow generic Ajax.Autocompleter-style textbox watchers.

Forwarded for "[Rails-spinoffs] Delayed input posting"

-Rob
--- Begin Message ---
Hey. Below is a patch to allow generic Ajax.Autocompleters. Basically
it's for people who wanna be able to watch an input for changes, but
don't want it to pop up an autocompleter box below. Useful for live
previews, that kind of thing.

Someone's gonna have to fix the tabstops. I couldn't be arse to work out
how to make vim do the softtabs properly. Spaces for indentation is
stupid anyway.

-Rob

--- js/controls.js.old  2006-02-13 16:48:19.000000000 +0000
+++ js/controls.js      2006-02-13 14:48:28.000000000 +0000
@@ -336,6 +336,45 @@

 });

+Ajax.Watcher = Class.create();
+Ajax.Watcher.prototype = {
+       initialize: function(element, url, callback, options) {
+               this.options = Object.extend({
+                       waitTime: 3,
+                       frequency: 0.4
+               }, options);
+               this.element = $(element);
+               this.url = url;
+               this.callback = callback;
+               this.observer = null;
+               this.active = false;
+               this.changed = false;
+               Event.observe(this.element, "keypress",
this.onKeyPress.bindAsEventListener(this));
+               if (this.options.firstRun) {
+                       this.onObserverEvent();
+               }
+       },
+  onKeyPress: function(event) {
+    this.changed = true;
+    this.hasFocus = true;
+
+    if(this.observer) clearTimeout(this.observer);
+      this.observer =
+        setTimeout(this.onObserverEvent.bind(this),
this.options.frequency*1000);
+  },
+  onObserverEvent: function() {
+       this.changed = false;
+       data = escape(this.element.value);
+               new Ajax.Request(this.url, Object.extend(this.options, {
+                       onSuccess:this.callback,
+                       method:"post",
+                       postBody:"value="+data}));
+  },
+
+};
+
+
+
 // The local array autocompleter. Used when you'd prefer to
 // inject an array of autocompletion options into the page, rather
 // than sending out Ajax queries, which can be quite slow sometimes.


--- End Message ---
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to