2011-09-30 08:00:12 +00:00
|
|
|
/*
|
|
|
|
Spent hours debugging opera, turns out they reserve the global variable autocomplete. Bitches.
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
var autocomp = {
|
|
|
|
id: "",
|
|
|
|
value: "",
|
2012-06-13 08:00:16 +00:00
|
|
|
artistid: null,
|
2011-09-30 08:00:12 +00:00
|
|
|
timer: null,
|
|
|
|
input: null,
|
|
|
|
list: null,
|
|
|
|
pos: -1,
|
|
|
|
cache: [],
|
|
|
|
start: function (id) {
|
|
|
|
this.id = id;
|
|
|
|
this.cache[id] = ["",[],[],[]];
|
|
|
|
this.input = document.getElementById(id + "search");
|
|
|
|
this.list = document.getElementById(id + "complete");
|
2013-05-04 08:00:48 +00:00
|
|
|
listener.set(document.body,'click',function() {
|
2011-09-30 08:00:12 +00:00
|
|
|
autocomp.value = autocomp.input.value;
|
|
|
|
autocomp.end();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
end: function () {
|
|
|
|
//this.input.value = this.value;
|
2012-06-13 08:00:16 +00:00
|
|
|
this.artistid = null;
|
2011-09-30 08:00:12 +00:00
|
|
|
this.highlight(-1);
|
|
|
|
this.list.style.visibility = 'hidden';
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
},
|
|
|
|
keyup: function (e) {
|
|
|
|
clearTimeout(this.timer);
|
2013-05-04 08:00:48 +00:00
|
|
|
var key = (window.event) ? window.event.keyCode : e.keyCode;
|
2011-09-30 08:00:12 +00:00
|
|
|
switch (key) {
|
|
|
|
case 27: //esc
|
|
|
|
break;
|
|
|
|
case 8: //backspace
|
2012-06-13 08:00:16 +00:00
|
|
|
this.artistid = null;
|
2011-09-30 08:00:12 +00:00
|
|
|
this.list.style.visibility = 'hidden';
|
|
|
|
this.timer = setTimeout("autocomp.get('" + escape(this.input.value) + "');",500);
|
|
|
|
break;
|
|
|
|
case 38: //up
|
|
|
|
case 40: //down
|
|
|
|
this.highlight(key);
|
|
|
|
if(this.pos !== -1) {
|
2012-06-13 08:00:16 +00:00
|
|
|
this.artistid = this.list.children[this.pos].artistid;
|
2011-09-30 08:00:12 +00:00
|
|
|
this.input.value = this.list.children[this.pos].textContent || this.list.children[this.pos].value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 13:
|
2012-06-13 08:00:16 +00:00
|
|
|
if(this.artistid != null) {
|
|
|
|
window.location = this.id + '.php?id='+this.artistid;
|
2011-09-30 08:00:12 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
2012-06-13 08:00:16 +00:00
|
|
|
this.artistid = null;
|
2011-09-30 08:00:12 +00:00
|
|
|
this.timer = setTimeout("autocomp.get('" + escape(this.input.value) + "');",300);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
keydown: function (e) {
|
|
|
|
switch ((window.event)?window.event.keyCode:e.keyCode) {
|
|
|
|
case 9: //tab
|
|
|
|
this.value = this.input.value;
|
|
|
|
case 27: //esc
|
|
|
|
this.end();
|
|
|
|
break;
|
|
|
|
case 38:
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
case 13: //enter
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
highlight: function(change) {
|
|
|
|
//No highlights on no list
|
|
|
|
if (this.list.children.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Show me the
|
|
|
|
this.list.style.visibility = 'visible';
|
|
|
|
|
|
|
|
//Remove the previous highlight
|
|
|
|
if (this.pos !== -1) {
|
|
|
|
this.list.children[this.pos].className = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//Change position
|
|
|
|
if (change === 40) {
|
|
|
|
++this.pos;
|
|
|
|
} else if (change === 38) {
|
|
|
|
--this.pos;
|
|
|
|
} else {
|
|
|
|
this.pos = change;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Wrap arounds
|
|
|
|
if (this.pos >= this.list.children.length) {
|
|
|
|
this.pos = -1;
|
|
|
|
} else if (this.pos < -1) {
|
2013-05-04 08:00:48 +00:00
|
|
|
this.pos = this.list.children.length - 1;
|
2011-09-30 08:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pos !== -1) {
|
|
|
|
this.list.children[this.pos].className = "highlight";
|
|
|
|
} else {
|
2012-06-13 08:00:16 +00:00
|
|
|
this.artistid = null;
|
2011-09-30 08:00:12 +00:00
|
|
|
this.input.value = this.value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
get: function (value) {
|
|
|
|
this.pos = -1;
|
|
|
|
this.value = unescape(value);
|
|
|
|
|
2013-05-04 08:00:48 +00:00
|
|
|
if (typeof this.cache[this.id + value] === 'object') {
|
|
|
|
this.display(this.cache[this.id + value]);
|
2011-09-30 08:00:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-04 08:00:48 +00:00
|
|
|
ajax.get(this.id+'.php?action=autocomplete&name='+this.input.value,function(jstr) {
|
2011-09-30 08:00:12 +00:00
|
|
|
var data = json.decode(jstr);
|
|
|
|
autocomp.cache[autocomp.id+data[0]] = data;
|
|
|
|
autocomp.display(data);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
display: function (data) {
|
2013-05-04 08:00:48 +00:00
|
|
|
var i, il, li;
|
2011-09-30 08:00:12 +00:00
|
|
|
this.list.innerHTML = '';
|
2013-05-04 08:00:48 +00:00
|
|
|
for (i = 0, il = data[1].length; i < il; ++i) {
|
2011-09-30 08:00:12 +00:00
|
|
|
li = document.createElement('li');
|
|
|
|
li.innerHTML = data[1][i];
|
|
|
|
li.i = i;
|
2012-06-13 08:00:16 +00:00
|
|
|
li.artistid = data[3][i];
|
2013-05-04 08:00:48 +00:00
|
|
|
listener.set(li,'mouseover',function() {
|
2011-09-30 08:00:12 +00:00
|
|
|
autocomp.highlight(this.i);
|
|
|
|
});
|
2013-05-04 08:00:48 +00:00
|
|
|
listener.set(li,'click',function() {
|
2012-06-13 08:00:16 +00:00
|
|
|
window.location = autocomp.id + '.php?id='+this.artistid;
|
2011-09-30 08:00:12 +00:00
|
|
|
});
|
|
|
|
this.list.appendChild(li);
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
this.list.style.visibility = 'visible';
|
|
|
|
} else {
|
|
|
|
this.list.style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|