diff options
Diffstat (limited to 'misc/autocomplete.js')
-rw-r--r-- | misc/autocomplete.js | 78 |
1 files changed, 38 insertions, 40 deletions
diff --git a/misc/autocomplete.js b/misc/autocomplete.js index 920865b4b..85469cc95 100644 --- a/misc/autocomplete.js +++ b/misc/autocomplete.js @@ -7,7 +7,7 @@ Drupal.behaviors.autocomplete = { attach: function(context, settings) { var acdb = []; - $('input.autocomplete:not(.autocomplete-processed)', context).each(function () { + $('input.autocomplete:not(.autocomplete-processed)', context).each(function() { var uri = this.value; if (!acdb[uri]) { acdb[uri] = new Drupal.ACDB(uri); @@ -25,8 +25,8 @@ Drupal.behaviors.autocomplete = { * Prevents the form from submitting if the suggestions popup is open * and closes the suggestions popup when doing so. */ -Drupal.autocompleteSubmit = function () { - return $('#autocomplete').each(function () { +Drupal.autocompleteSubmit = function() { + return $('#autocomplete').each(function() { this.owner.hidePopup(); }).size() == 0; }; @@ -34,22 +34,22 @@ Drupal.autocompleteSubmit = function () { /** * An AutoComplete object. */ -Drupal.jsAC = function (input, db) { +Drupal.jsAC = function(input, db) { var ac = this; this.input = input; this.db = db; $(this.input) - .keydown(function (event) { return ac.onkeydown(this, event); }) - .keyup(function (event) { ac.onkeyup(this, event); }) - .blur(function () { ac.hidePopup(); ac.db.cancel(); }); + .keydown(function(event) { return ac.onkeydown(this, event); }) + .keyup(function(event) { ac.onkeyup(this, event); }) + .blur(function() { ac.hidePopup(); ac.db.cancel(); }); }; /** * Handler for the "keydown" event. */ -Drupal.jsAC.prototype.onkeydown = function (input, e) { +Drupal.jsAC.prototype.onkeydown = function(input, e) { if (!e) { e = window.event; } @@ -68,7 +68,7 @@ Drupal.jsAC.prototype.onkeydown = function (input, e) { /** * Handler for the "keyup" event. */ -Drupal.jsAC.prototype.onkeyup = function (input, e) { +Drupal.jsAC.prototype.onkeyup = function(input, e) { if (!e) { e = window.event; } @@ -105,14 +105,14 @@ Drupal.jsAC.prototype.onkeyup = function (input, e) { /** * Puts the currently highlighted suggestion into the autocomplete field. */ -Drupal.jsAC.prototype.select = function (node) { +Drupal.jsAC.prototype.select = function(node) { this.input.value = node.autocompleteValue; }; /** * Highlights the next suggestion. */ -Drupal.jsAC.prototype.selectDown = function () { +Drupal.jsAC.prototype.selectDown = function() { if (this.selected && this.selected.nextSibling) { this.highlight(this.selected.nextSibling); } @@ -127,7 +127,7 @@ Drupal.jsAC.prototype.selectDown = function () { /** * Highlights the previous suggestion. */ -Drupal.jsAC.prototype.selectUp = function () { +Drupal.jsAC.prototype.selectUp = function() { if (this.selected && this.selected.previousSibling) { this.highlight(this.selected.previousSibling); } @@ -136,7 +136,7 @@ Drupal.jsAC.prototype.selectUp = function () { /** * Highlights a suggestion. */ -Drupal.jsAC.prototype.highlight = function (node) { +Drupal.jsAC.prototype.highlight = function(node) { if (this.selected) { $(this.selected).removeClass('selected'); } @@ -147,7 +147,7 @@ Drupal.jsAC.prototype.highlight = function (node) { /** * Unhighlights a suggestion. */ -Drupal.jsAC.prototype.unhighlight = function (node) { +Drupal.jsAC.prototype.unhighlight = function(node) { $(node).removeClass('selected'); this.selected = false; }; @@ -155,7 +155,7 @@ Drupal.jsAC.prototype.unhighlight = function (node) { /** * Hides the autocomplete suggestions. */ -Drupal.jsAC.prototype.hidePopup = function (keycode) { +Drupal.jsAC.prototype.hidePopup = function(keycode) { // Select item if the right key or mousebutton was pressed. if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) { this.input.value = this.selected.autocompleteValue; @@ -172,18 +172,17 @@ Drupal.jsAC.prototype.hidePopup = function (keycode) { /** * Positions the suggestions popup and starts a search. */ -Drupal.jsAC.prototype.populatePopup = function () { +Drupal.jsAC.prototype.populatePopup = function() { // Show popup. if (this.popup) { $(this.popup).remove(); } this.selected = false; - this.popup = document.createElement('div'); - this.popup.id = 'autocomplete'; + this.popup = $('<div id="autocomplete"></div>')[0]; this.popup.owner = this; $(this.popup).css({ - marginTop: this.input.offsetHeight +'px', - width: (this.input.offsetWidth - 4) +'px', + marginTop: this.input.offsetHeight + 'px', + width: (this.input.offsetWidth - 4) + 'px', display: 'none' }); $(this.input).before(this.popup); @@ -196,39 +195,38 @@ Drupal.jsAC.prototype.populatePopup = function () { /** * Fills the suggestion popup with any matches received. */ -Drupal.jsAC.prototype.found = function (matches) { +Drupal.jsAC.prototype.found = function(matches) { // If no value in the textfield, do not show the popup. if (!this.input.value.length) { return false; } // Prepare matches. - var ul = document.createElement('ul'); + var ul = $('<ul></ul>'); var ac = this; for (key in matches) { - var li = document.createElement('li'); - $(li) - .html('<div>'+ matches[key] +'</div>') - .mousedown(function () { ac.select(this); }) - .mouseover(function () { ac.highlight(this); }) - .mouseout(function () { ac.unhighlight(this); }); - li.autocompleteValue = key; - $(ul).append(li); + $('<li></li>') + .html($('<div></div>').html(matches[key])) + .mousedown(function() { ac.select(this); }) + .mouseover(function() { ac.highlight(this); }) + .mouseout(function() { ac.unhighlight(this); }) + .attr('autocompleteValue', key) + .appendTo(ul); } // Show popup with matches, if any. if (this.popup) { - if (ul.childNodes.length > 0) { + if (ul.children().size()) { $(this.popup).empty().append(ul).show(); } else { - $(this.popup).css({visibility: 'hidden'}); + $(this.popup).css({ visibility: 'hidden' }); this.hidePopup(); } } }; -Drupal.jsAC.prototype.setStatus = function (status) { +Drupal.jsAC.prototype.setStatus = function(status) { switch (status) { case 'begin': $(this.input).addClass('throbbing'); @@ -244,7 +242,7 @@ Drupal.jsAC.prototype.setStatus = function (status) { /** * An AutoComplete DataBase object. */ -Drupal.ACDB = function (uri) { +Drupal.ACDB = function(uri) { this.uri = uri; this.delay = 300; this.cache = {}; @@ -253,7 +251,7 @@ Drupal.ACDB = function (uri) { /** * Performs a cached and delayed search. */ -Drupal.ACDB.prototype.search = function (searchString) { +Drupal.ACDB.prototype.search = function(searchString) { var db = this; this.searchString = searchString; @@ -271,11 +269,11 @@ Drupal.ACDB.prototype.search = function (searchString) { // Ajax GET request for autocompletion. $.ajax({ - type: "GET", - url: db.uri +'/'+ Drupal.encodeURIComponent(searchString), + type: 'GET', + url: db.uri + '/' + Drupal.encodeURIComponent(searchString), dataType: 'json', - success: function (matches) { - if (typeof matches['status'] == 'undefined' || matches['status'] != 0) { + success: function(matches) { + if (typeof matches.status == 'undefined' || matches.status != 0) { db.cache[searchString] = matches; // Verify if these are still the matches the user wants to see. if (db.searchString == searchString) { @@ -284,7 +282,7 @@ Drupal.ACDB.prototype.search = function (searchString) { db.owner.setStatus('found'); } }, - error: function (xmlhttp) { + error: function(xmlhttp) { alert(Drupal.ahahError(xmlhttp, db.uri)); } }); |