2014-07-06 15:58:57 +00:00
|
|
|
/*!
|
|
|
|
* inputhistory
|
|
|
|
* https://github.com/erming/inputhistory
|
2014-08-05 09:47:52 +00:00
|
|
|
* v0.3.1
|
2014-07-06 15:58:57 +00:00
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
$.inputhistory = {};
|
|
|
|
$.inputhistory.defaultOptions = {
|
|
|
|
history: [],
|
|
|
|
preventSubmit: false
|
|
|
|
};
|
|
|
|
|
|
|
|
$.fn.history = // Alias
|
|
|
|
$.fn.inputhistory = function(options) {
|
|
|
|
options = $.extend(
|
|
|
|
$.inputhistory.defaultOptions,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
if (self.size() > 1) {
|
|
|
|
return self.each(function() {
|
|
|
|
$(this).history(options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var history = options.history;
|
|
|
|
history.push("");
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
self.on("keydown", function(e) {
|
|
|
|
var key = e.which;
|
|
|
|
switch (key) {
|
|
|
|
case 13: // Enter
|
2016-06-05 02:48:41 +00:00
|
|
|
if (e.shiftKey) {
|
|
|
|
return; // multiline input
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:58:57 +00:00
|
|
|
if (self.val() != "") {
|
|
|
|
i = history.length;
|
|
|
|
history[i - 1] = self.val();
|
|
|
|
history.push("");
|
|
|
|
if (history[i - 1] == history[i - 2]) {
|
|
|
|
history.splice(-2, 1);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!options.preventSubmit) {
|
|
|
|
self.parents("form").eq(0).submit();
|
|
|
|
}
|
|
|
|
self.val("");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 38: // Up
|
|
|
|
case 40: // Down
|
2016-02-10 05:45:21 +00:00
|
|
|
// NOTICE: This is specific to The Lounge.
|
2014-08-14 19:26:36 +00:00
|
|
|
if (e.ctrlKey || e.metaKey) {
|
2014-08-05 09:47:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-06-05 02:48:41 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
this.value.indexOf("\n") >= 0
|
|
|
|
&&
|
|
|
|
(key === 38 && this.selectionStart > 0)
|
|
|
|
||
|
|
|
|
(key === 40 && this.selectionStart < this.value.length))
|
|
|
|
{
|
|
|
|
return; // don't prevent default
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:58:57 +00:00
|
|
|
history[i] = self.val();
|
|
|
|
if (key == 38 && i != 0) {
|
|
|
|
i--;
|
|
|
|
} else if (key == 40 && i < history.length - 1) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
self.val(history[i]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
})(jQuery);
|