2005-08-11 13:00:17 +00:00
|
|
|
// $Id$
|
|
|
|
|
2005-05-24 06:00:22 +00:00
|
|
|
// Global Killswitch
|
|
|
|
if (isJsEnabled()) {
|
|
|
|
addLoadEvent(autocompleteAutoAttach);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches the autocomplete behaviour to all required fields
|
|
|
|
*/
|
|
|
|
function autocompleteAutoAttach() {
|
|
|
|
var acdb = [];
|
|
|
|
var inputs = document.getElementsByTagName('input');
|
|
|
|
for (i = 0; input = inputs[i]; i++) {
|
|
|
|
if (input && hasClass(input, 'autocomplete')) {
|
|
|
|
uri = input.value;
|
|
|
|
if (!acdb[uri]) {
|
|
|
|
acdb[uri] = new ACDB(uri);
|
|
|
|
}
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
input = $(input.id.substr(0, input.id.length - 13));
|
2005-05-24 06:00:22 +00:00
|
|
|
input.setAttribute('autocomplete', 'OFF');
|
- Patch #28483 by Steven: JavaScript enabled uploading.
Comment from Steven: It does this by redirecting the submission of the form to a hidden <iframe> when you click "Attach" (we cannot submit data through Ajax directly because you cannot read file contents from JS for security reasons). Once the file is submitted, the upload-section of the form is updated. Things to note:
* The feature degrades back to the current behaviour without JS.
* If there are errors with the uploaded file (disallowed type, too big, ...), they are displayed at the top of the file attachments fieldset.
* Though the hidden-iframe method sounds dirty, it's quite compact and is 100% implemented in .js files. The drupal.js api makes it a snap to use.
* I included some minor improvements to the Drupal JS API and code.
* I added an API drupal_call_js() to bridge the PHP/JS gap: it takes a function name and arguments, and outputs a <script> tag. The kicker is that it preserves the structure and type of arguments, so e.g. PHP associative arrays end up as objects in JS.
* I also included a progressbar widget that I wrote for drumm's ongoing update.php work. It includes Ajax status updating/monitoring, but it is only used as a pure throbber in this patch. But as the code was already written and is going to be used in the near future, I left that part in. It's pretty small ;). If PHP supports ad-hoc upload info in the future like Ruby on Rails, we can implement that in 5 minutes.
2005-08-31 18:37:30 +00:00
|
|
|
addSubmitEvent(input.form, autocompleteSubmit);
|
2005-05-24 06:00:22 +00:00
|
|
|
new jsAC(input, acdb[uri]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevents the form from submitting if the suggestions popup is open
|
|
|
|
*/
|
|
|
|
function autocompleteSubmit() {
|
|
|
|
var popup = document.getElementById('autocomplete');
|
|
|
|
if (popup) {
|
|
|
|
popup.owner.hidePopup();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An AutoComplete object
|
|
|
|
*/
|
|
|
|
function jsAC(input, db) {
|
|
|
|
var ac = this;
|
|
|
|
this.input = input;
|
|
|
|
this.db = db;
|
|
|
|
this.input.onkeydown = function (event) { return ac.onkeydown(this, event); };
|
|
|
|
this.input.onkeyup = function (event) { ac.onkeyup(this, event) };
|
2006-04-05 00:53:16 +00:00
|
|
|
this.input.onblur = function () { ac.hidePopup(); ac.db.cancel(); };
|
2005-05-24 06:00:22 +00:00
|
|
|
this.popup = document.createElement('div');
|
|
|
|
this.popup.id = 'autocomplete';
|
|
|
|
this.popup.owner = this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides the autocomplete suggestions
|
|
|
|
*/
|
|
|
|
jsAC.prototype.hidePopup = function (keycode) {
|
|
|
|
if (this.selected && ((keycode && keycode != 46 && keycode != 8 && keycode != 27) || !keycode)) {
|
|
|
|
this.input.value = this.selected.autocompleteValue;
|
|
|
|
}
|
|
|
|
if (this.popup.parentNode && this.popup.parentNode.tagName) {
|
|
|
|
removeNode(this.popup);
|
|
|
|
}
|
|
|
|
this.selected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the "keydown" event
|
|
|
|
*/
|
|
|
|
jsAC.prototype.onkeydown = function (input, e) {
|
|
|
|
if (!e) {
|
|
|
|
e = window.event;
|
|
|
|
}
|
|
|
|
switch (e.keyCode) {
|
|
|
|
case 40: // down arrow
|
|
|
|
this.selectDown();
|
|
|
|
return false;
|
|
|
|
case 38: // up arrow
|
|
|
|
this.selectUp();
|
|
|
|
return false;
|
|
|
|
default: // all other keys
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the "keyup" event
|
|
|
|
*/
|
|
|
|
jsAC.prototype.onkeyup = function (input, e) {
|
|
|
|
if (!e) {
|
|
|
|
e = window.event;
|
|
|
|
}
|
|
|
|
switch (e.keyCode) {
|
|
|
|
case 16: // shift
|
|
|
|
case 17: // ctrl
|
|
|
|
case 18: // alt
|
|
|
|
case 20: // caps lock
|
|
|
|
case 33: // page up
|
|
|
|
case 34: // page down
|
|
|
|
case 35: // end
|
|
|
|
case 36: // home
|
|
|
|
case 37: // left arrow
|
|
|
|
case 38: // up arrow
|
|
|
|
case 39: // right arrow
|
|
|
|
case 40: // down arrow
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 9: // tab
|
|
|
|
case 13: // enter
|
|
|
|
case 27: // esc
|
|
|
|
this.hidePopup(e.keyCode);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default: // all other keys
|
|
|
|
if (input.value.length > 0)
|
|
|
|
this.populatePopup();
|
|
|
|
else
|
|
|
|
this.hidePopup(e.keyCode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Puts the currently highlighted suggestion into the autocomplete field
|
|
|
|
*/
|
|
|
|
jsAC.prototype.select = function (node) {
|
|
|
|
this.input.value = node.autocompleteValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlights the next suggestion
|
|
|
|
*/
|
|
|
|
jsAC.prototype.selectDown = function () {
|
|
|
|
if (this.selected && this.selected.nextSibling) {
|
|
|
|
this.highlight(this.selected.nextSibling);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var lis = this.popup.getElementsByTagName('li');
|
|
|
|
if (lis.length > 0) {
|
|
|
|
this.highlight(lis[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlights the previous suggestion
|
|
|
|
*/
|
|
|
|
jsAC.prototype.selectUp = function () {
|
|
|
|
if (this.selected && this.selected.previousSibling) {
|
|
|
|
this.highlight(this.selected.previousSibling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlights a suggestion
|
|
|
|
*/
|
|
|
|
jsAC.prototype.highlight = function (node) {
|
|
|
|
removeClass(this.selected, 'selected');
|
|
|
|
addClass(node, 'selected');
|
|
|
|
this.selected = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unhighlights a suggestion
|
|
|
|
*/
|
|
|
|
jsAC.prototype.unhighlight = function (node) {
|
|
|
|
removeClass(node, 'selected');
|
|
|
|
this.selected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Positions the suggestions popup and starts a search
|
|
|
|
*/
|
|
|
|
jsAC.prototype.populatePopup = function () {
|
|
|
|
var ac = this;
|
|
|
|
var pos = absolutePosition(this.input);
|
|
|
|
this.selected = false;
|
|
|
|
this.popup.style.top = (pos.y + this.input.offsetHeight) +'px';
|
|
|
|
this.popup.style.left = pos.x +'px';
|
|
|
|
this.popup.style.width = (this.input.offsetWidth - 4) +'px';
|
2005-05-25 00:14:42 +00:00
|
|
|
this.db.owner = this;
|
2005-05-24 06:00:22 +00:00
|
|
|
this.db.search(this.input.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fills the suggestion popup with any matches received
|
|
|
|
*/
|
|
|
|
jsAC.prototype.found = function (matches) {
|
|
|
|
while (this.popup.hasChildNodes()) {
|
|
|
|
this.popup.removeChild(this.popup.childNodes[0]);
|
|
|
|
}
|
|
|
|
if (!this.popup.parentNode || !this.popup.parentNode.tagName) {
|
|
|
|
document.getElementsByTagName('body')[0].appendChild(this.popup);
|
|
|
|
}
|
|
|
|
var ul = document.createElement('ul');
|
|
|
|
var ac = this;
|
2006-04-06 02:41:48 +00:00
|
|
|
|
|
|
|
for (key in matches) {
|
|
|
|
var li = document.createElement('li');
|
|
|
|
var div = document.createElement('div');
|
|
|
|
div.innerHTML = matches[key];
|
|
|
|
li.appendChild(div);
|
|
|
|
li.autocompleteValue = key;
|
|
|
|
li.onmousedown = function() { ac.select(this); };
|
|
|
|
li.onmouseover = function() { ac.highlight(this); };
|
|
|
|
li.onmouseout = function() { ac.unhighlight(this); };
|
|
|
|
ul.appendChild(li);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ul.childNodes.length > 0) {
|
2005-05-24 06:00:22 +00:00
|
|
|
this.popup.appendChild(ul);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.hidePopup();
|
|
|
|
}
|
|
|
|
removeClass(this.input, 'throbbing');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An AutoComplete DataBase object
|
|
|
|
*/
|
|
|
|
function ACDB(uri) {
|
|
|
|
this.uri = uri;
|
|
|
|
this.delay = 300;
|
|
|
|
this.cache = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a cached and delayed search
|
|
|
|
*/
|
|
|
|
ACDB.prototype.search = function(searchString) {
|
2005-07-03 16:12:55 +00:00
|
|
|
this.searchString = searchString;
|
|
|
|
if (this.cache[searchString]) {
|
|
|
|
return this.owner.found(this.cache[searchString]);
|
2005-05-24 06:00:22 +00:00
|
|
|
}
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
}
|
|
|
|
var db = this;
|
2005-05-25 00:14:42 +00:00
|
|
|
this.timer = setTimeout(function() {
|
|
|
|
addClass(db.owner.input, 'throbbing');
|
2006-04-05 00:53:16 +00:00
|
|
|
db.transport = HTTPGet(db.uri +'/'+ encodeURIComponent(searchString), db.receive, db);
|
2005-05-25 00:14:42 +00:00
|
|
|
}, this.delay);
|
2005-05-24 06:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP callback function. Passes suggestions to the autocomplete object
|
|
|
|
*/
|
|
|
|
ACDB.prototype.receive = function(string, xmlhttp, acdb) {
|
2006-01-16 16:31:49 +00:00
|
|
|
// Note: Safari returns 'undefined' status if the request returns no data.
|
|
|
|
if (xmlhttp.status != 200 && typeof xmlhttp.status != 'undefined') {
|
2005-05-25 00:14:42 +00:00
|
|
|
removeClass(acdb.owner.input, 'throbbing');
|
2005-05-24 06:00:22 +00:00
|
|
|
return alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ acdb.uri);
|
|
|
|
}
|
2006-04-06 02:41:48 +00:00
|
|
|
// Parse back result
|
|
|
|
var matches = parseJson(string);
|
|
|
|
if (typeof matches['status'] == 'undefined' || matches['status'] != 0) {
|
|
|
|
acdb.cache[acdb.searchString] = matches;
|
2006-04-17 20:48:26 +00:00
|
|
|
acdb.owner.found(matches);
|
2005-05-24 06:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-05 00:53:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels the current autocomplete request
|
|
|
|
*/
|
|
|
|
ACDB.prototype.cancel = function() {
|
|
|
|
if (this.owner) removeClass(this.owner.input, 'throbbing');
|
|
|
|
if (this.timer) clearTimeout(this.timer);
|
|
|
|
if (this.transport) {
|
|
|
|
this.transport.onreadystatechange = function() {};
|
|
|
|
this.transport.abort();
|
|
|
|
}
|
|
|
|
}
|