summaryrefslogtreecommitdiff
path: root/javascript/libs
diff options
context:
space:
mode:
authorMax Kremmel <xing@synapse.plus.com>2006-08-07 13:52:28 +0000
committerMax Kremmel <xing@synapse.plus.com>2006-08-07 13:52:28 +0000
commit1bf231c4b4f26fac30f3381c38c4948e5c667de3 (patch)
tree8bbbfd2925305f2aa32e50f33a2b95534bc1862f /javascript/libs
parent06be48884e115a34e5b40f92bd015fe80c614554 (diff)
downloadutil-1bf231c4b4f26fac30f3381c38c4948e5c667de3.tar.gz
util-1bf231c4b4f26fac30f3381c38c4948e5c667de3.tar.bz2
util-1bf231c4b4f26fac30f3381c38c4948e5c667de3.zip
add framework for ajax search. not working yet, due to current module limitations
Diffstat (limited to 'javascript/libs')
-rw-r--r--javascript/libs/live_search.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/javascript/libs/live_search.js b/javascript/libs/live_search.js
new file mode 100644
index 0000000..42ebfbc
--- /dev/null
+++ b/javascript/libs/live_search.js
@@ -0,0 +1,70 @@
+/*
+Written by: Adam Crownoble (adam@bryan.edu)
+Date: April 3 2006
+License: LGPL (http://www.gnu.org/copyleft/lesser.html)
+*/
+
+var LiveSearch = Class.create();
+LiveSearch.prototype = {
+ initialize: function(searchBox, results) {
+ this.form = searchBox.form;
+ this.searchBox = searchBox;
+ this.results = results;
+ this.url = this.form.action;
+ this.method = this.form.method;
+ this.stopSubmit = true;
+ this.searchString = this.searchBox.value;
+ this.starterText = this.searchString;
+ this.searchingMessage = 'Searching...';
+ this.minLength = 3;
+ this.timeoutID = 0;
+ this.delay = 500;
+
+ Event.observe(this.form, 'submit', this.submit.bind(this));
+ Event.observe(this.searchBox, 'keyup', this.update.bind(this));
+ Event.observe(this.searchBox, 'focus', this.handleStarterText.bind(this));
+ Event.observe(this.searchBox, 'blur', this.handleStarterText.bind(this));
+
+ this.form.reset();
+ },
+
+ handleStarterText: function(event) {
+ switch(event.type) {
+ case 'focus':
+ if(this.searchBox.value == this.starterText) {
+ Field.clear(this.searchBox);
+ }
+ break;
+
+ case 'blur':
+ if(this.searchBox.value == '') {
+ this.searchBox.value = this.starterText;
+ }
+ break;
+ }
+ },
+
+ update: function() {
+ if(this.searchString != this.searchBox.value && this.searchBox.value != this.starterText) {
+
+ this.searchString = this.searchBox.value;
+ clearTimeout(this.timeoutID);
+
+ if(this.searchString.length >= this.minLength) {
+ this.results.innerHTML = this.searchingMessage;
+ this.timeoutID = setTimeout(this.search.bind(this), this.delay);
+ } else {
+ this.results.innerHTML = '';
+ }
+ }
+ },
+
+ search: function() {
+ var parameters = Form.serialize(this.form);
+ var ajax = new Ajax.Updater(this.results, this.url, { method: this.method, parameters: parameters });
+ },
+
+ submit: function(event) {
+ if(this.stopSubmit) { Event.stop(event); }
+ }
+};