3
0
Fork 0
fsim/WebRoot/js/fsim.js

253 lines
6.3 KiB
JavaScript
Raw Normal View History

2022-11-13 13:46:54 +00:00
//if(window.url.indexOf("divv")!=-1) {
// alert("turning on divv devv mode");
//div,span {
//border: 1px solid #f00;
//margin: 1px;
//}
//}
var IE = document.all?true:false;
var req = null;
if (!IE) {
document.captureEvents(Event.KEYBOARD);
}
// full autocomplete off AND XHTML compatible
for(i=0;i<document.forms.length;i++)void(document.forms[i].setAttribute('autocomplete', 'off'));
function debug(text) {
var divObject = document.getElementById('debug');
divObject.innerHTML = divObject.innerHTML+text+'<br/>';
}
function initRequest() {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
IE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function sendRequest(url,oDiv) {
if(req.readyState==1) {
// not while loading.
return;
}
req.table = oDiv;
req.onreadystatechange=callBack;
req.open("GET",url,true);
req.send(null);
}
function callBack()
{
// if xmlhttp shows "loaded"
if(req && req.readyState==4)
{
// if "OK"
if (req.status==200)
{
//alert("just got: "+xmlhttp.responseText);
//document.write(xmlhttp.responseText);
ac = req.table;
oDiv = ac.oDiv;
// clear the popup div.
while ( oDiv.hasChildNodes() )
{
oDiv.removeChild(oDiv.firstChild);
}
var items = req.responseXML.getElementsByTagName("item");
//alert("just got items:"+items);
var to = "";
for (loop = 0; loop < items.length; loop++)
{
var item = items.item(loop).firstChild.nodeValue;
var hits = items.item(loop).getAttribute('hits');
var oDivBinder = document.createElement('div');
oDivBinder.id = loop;
oDivBinder.className = 'fsimCompleteBinderNormal';
var oDivItem = document.createElement('div');
oDivItem.innerHTML = item;
oDivItem.className = 'fsimCompleteWords';
oDivBinder.onmousedown = AutoComplete.prototype.onDivMouseDown;
oDivBinder.onmouseover = AutoComplete.prototype.onDivMouseOver;
oDivBinder.onmouseout = AutoComplete.prototype.onDivMouseOut;
//var oSpanHits = document.createElement('div');
//oSpanHits.className = 'fsimCompleteHits';
//oSpanHits.innerHTML = 'hits: '+hits;
oDivBinder.appendChild(oDivItem);
//oDivBinder.appendChild(oSpanHits);
oDiv.AutoComplete = ac;
oDiv.appendChild(oDivBinder);
}
//alert("got: "+to);
oDiv.style.visibility = "visible";
//oDiv.stlyeClass = "pop";
oDiv.className = "pop";
}
else
{
alert("Problem retrieving XML data status:"+req.statusText);
}
}
}
function AutoComplete(url, oText, oDiv)
{
// initialize member variables
this.oText = oText; // the text box
this.oDiv = oDiv; // a hidden <div> for the popup auto-complete
this.url=url;
this.selectedItem = null;
this.preKeyword = "";
initRequest();
//if(IE) {
// turn of autocompletion in IE for this field !
oText.autocomplete = 'off';
//}
// attach handlers to the text-box
oText.AutoComplete = this;
oText.onkeyup = AutoComplete.prototype.onTextChange;
oText.onblur = AutoComplete.prototype.onTextBlur;
}
AutoComplete.prototype.onTextBlur = function()
{
this.AutoComplete.onblur();
}
AutoComplete.prototype.onblur = function()
{
this.oDiv.style.visibility = "hidden";
}
AutoComplete.prototype.onTextChange = function(e)
{
if(IE) {
var e = window.event;
}
//debug('keycode='+e.keyCode);
var keyCode = e.keyCode;
if(32 === keyCode) {
this.AutoComplete.preKeyword = this.AutoComplete.oText.value;
return;
}
if(40 === keyCode || 38 === keyCode) {
if(this.AutoComplete.selectedItem !== null) {
this.AutoComplete.selectedItem.className = "fsimCompleteBinderNormal";
var currentID = this.AutoComplete.selectedItem.id;
//debug("curID="+currentID);
}
var nextItem = null;
if(40 === keyCode) {
if(!currentID) {
var currentID = -1;
}
currentID++;
nextItem = this.AutoComplete.oDiv.childNodes.item(currentID);
nextItem.className = "fsimCompleteBinderHighlight";
this.AutoComplete.selectedItem = nextItem;
this.AutoComplete.oText.value = this.AutoComplete.preKeyword+nextItem.firstChild.innerHTML;
}
if(38 === keyCode) {
if(!currentID) {
var currentID = this.AutoComplete.oDiv.childNodes.lenght+1;
}
currentID--;
nextItem = this.AutoComplete.oDiv.childNodes.item(currentID);
nextItem.className = "fsimCompleteBinderHighlight";
this.AutoComplete.selectedItem = nextItem;
this.AutoComplete.oText.value = this.parentNode.AutoComplete.preKeyword+nextItem.firstChild.innerHTML;
}
} else {
this.AutoComplete.onchange();
}
}
AutoComplete.prototype.selectRange = function (iStart, iLength)
{
if (this.oText.createTextRange) {
var oRange = this.oText.createTextRange();
oRange.moveStart("character", iStart);
oRange.moveEnd("character", iLength - this.textbox.value.length);
oRange.select();
} else if (this.oText.setSelectionRange) {
this.oText.setSelectionRange(iStart, iLength);
}
this.oText.focus();
}
AutoComplete.prototype.onchange = function()
{
var txt = this.oText.value;
if(""!=txt) {
this.selectedItem = null;
sendRequest(this.url+txt,this);
} else {
// hide !
this.oDiv.style.visibility = "hidden";
this.oText.focus();
}
}
AutoComplete.prototype.onDivMouseDown = function()
{
this.parentNode.AutoComplete.oText.value = this.parentNode.AutoComplete.preKeyword+this.firstChild.innerHTML;
this.parentNode.AutoComplete.selectedItem = null;
}
AutoComplete.prototype.onDivMouseOver = function()
{
if(this.parentNode.AutoComplete.selectedItem !== null) {
this.parentNode.AutoComplete.selectedItem.className = "fsimCompleteBinderNormal";
}
this.className = 'fsimCompleteBinderHighlight';
this.parentNode.AutoComplete.selectedItem = this;
this.parentNode.AutoComplete.oText.value = this.parentNode.AutoComplete.preKeyword+this.firstChild.innerHTML;
}
AutoComplete.prototype.onDivMouseOut = function()
{
this.className = 'fsimCompleteBinderNormal';
}
function createAutoComplete()
{
new AutoComplete('/search?m=1&l=',
document.getElementById('q'),
document.getElementById('fsimKeywordsPopup'));
}