Support removing SimpleAutocomplete instances
parent
8f2a400174
commit
5104cfaf82
75
hinter.js
75
hinter.js
|
@ -57,6 +57,7 @@ var SimpleAutocomplete = function(input, dataLoader, multipleDelimiter, onChange
|
||||||
this.allowHTML = allowHTML;
|
this.allowHTML = allowHTML;
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
|
this.closure = [];
|
||||||
this.items = [];
|
this.items = [];
|
||||||
this.skipHideCounter = 0;
|
this.skipHideCounter = 0;
|
||||||
this.selectedIndex = -1;
|
this.selectedIndex = -1;
|
||||||
|
@ -102,16 +103,16 @@ SimpleAutocomplete.prototype.init = function()
|
||||||
var self = this;
|
var self = this;
|
||||||
var ie_opera = navigator.userAgent.match('MSIE') || navigator.userAgent.match('Opera');
|
var ie_opera = navigator.userAgent.match('MSIE') || navigator.userAgent.match('Opera');
|
||||||
if (ie_opera)
|
if (ie_opera)
|
||||||
addListener(e, 'keydown', function(ev) { return self.onKeyPress(ev); });
|
this.addRmListener('keydown', function(ev) { return self.onKeyPress(ev); });
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
addListener(e, 'keydown', function(ev) { return self.onKeyDown(ev); });
|
this.addRmListener('keydown', function(ev) { return self.onKeyDown(ev); });
|
||||||
addListener(e, 'keypress', function(ev) { return self.onKeyPress(ev); });
|
this.addRmListener('keypress', function(ev) { return self.onKeyPress(ev); });
|
||||||
}
|
}
|
||||||
addListener(e, 'keyup', function(ev) { return self.onKeyUp(ev); });
|
this.addRmListener('keyup', function(ev) { return self.onKeyUp(ev); });
|
||||||
addListener(e, 'change', function() { return self.onChange(); });
|
this.addRmListener('change', function() { return self.onChange(); });
|
||||||
addListener(e, 'focus', function() { return self.onInputFocus(); });
|
this.addRmListener('focus', function() { return self.onInputFocus(); });
|
||||||
addListener(e, 'blur', function() { return self.onInputBlur(); });
|
this.addRmListener('blur', function() { return self.onInputBlur(); });
|
||||||
this.onChange();
|
this.onChange();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -152,6 +153,37 @@ SimpleAutocomplete.prototype.replaceItems = function(items)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add removable listener (remember the function)
|
||||||
|
SimpleAutocomplete.prototype.addRmListener = function(n, f)
|
||||||
|
{
|
||||||
|
this.closure[n] = f;
|
||||||
|
addListener(this.input, n, f);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove instance ("destructor")
|
||||||
|
SimpleAutocomplete.prototype.remove = function()
|
||||||
|
{
|
||||||
|
if (!this.hintLayer)
|
||||||
|
return;
|
||||||
|
this.hintLayer.parentNode.removeChild(this.hintLayer);
|
||||||
|
for (var i in this.closure)
|
||||||
|
{
|
||||||
|
removeListener(this.input, i, this.closure[i]);
|
||||||
|
}
|
||||||
|
for (var i = 0; i < SimpleAutocomplete.SimpleAutocompletes.length; i++)
|
||||||
|
{
|
||||||
|
if (SimpleAutocomplete.SimpleAutocompletes[i] == this)
|
||||||
|
{
|
||||||
|
SimpleAutocomplete.SimpleAutocompletes.splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.closure = {};
|
||||||
|
this.input = null;
|
||||||
|
this.hintLayer = null;
|
||||||
|
this.items = null;
|
||||||
|
};
|
||||||
|
|
||||||
// Create a drop-down list item, include checkbox if this.multipleDelimiter is true
|
// Create a drop-down list item, include checkbox if this.multipleDelimiter is true
|
||||||
SimpleAutocomplete.prototype.makeItem = function(name, value, checked)
|
SimpleAutocomplete.prototype.makeItem = function(name, value, checked)
|
||||||
{
|
{
|
||||||
|
@ -454,26 +486,21 @@ SimpleAutocomplete.GlobalMouseDown = function(ev)
|
||||||
};
|
};
|
||||||
|
|
||||||
// *** UTILITY FUNCTIONS ***
|
// *** UTILITY FUNCTIONS ***
|
||||||
// Remove this section if you already have these functions somewhere else included
|
// Remove this section if you already have these functions defined somewhere else
|
||||||
|
|
||||||
// Cross-browser adding of event listeners
|
// Cross-browser add/remove event listeners
|
||||||
var addListener = function()
|
var addListener = function()
|
||||||
{
|
{
|
||||||
if (window.addEventListener)
|
return window.addEventListener
|
||||||
{
|
? function(el, type, fn) { el.addEventListener(type, fn, false); }
|
||||||
return function(el, type, fn) { el.addEventListener(type, fn, false); };
|
: function(el, type, fn) { el.attachEvent('on'+type, fn); };
|
||||||
}
|
}();
|
||||||
else if (window.attachEvent)
|
|
||||||
{
|
var removeListener = function()
|
||||||
return function(el, type, fn) {
|
{
|
||||||
var f = function() { return fn.call(el, window.event); };
|
return window.removeEventListener
|
||||||
el.attachEvent('on'+type, f);
|
? function(el, type, fn) { el.removeEventListener(type, fn, false); }
|
||||||
};
|
: function(el, type, fn) { el.detachEvent('on'+type, fn); };
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return function(el, type, fn) { element['on'+type] = fn; }
|
|
||||||
}
|
|
||||||
}();
|
}();
|
||||||
|
|
||||||
// Cancel event bubbling and/or default action
|
// Cancel event bubbling and/or default action
|
||||||
|
|
Loading…
Reference in New Issue