Added inputhistory

This commit is contained in:
Mattias Erming 2014-07-06 17:58:57 +02:00
parent ce792d1a60
commit a1d5f52875
4 changed files with 75 additions and 5 deletions

View File

@ -0,0 +1,70 @@
/*!
* inputhistory
* https://github.com/erming/inputhistory
* v0.3.0
*/
(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
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
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);

View File

@ -6,7 +6,7 @@
(function($) { (function($) {
$.fn.sticky = function() { $.fn.sticky = function() {
if (this.size() > 1) { if (this.size() > 1) {
return self.each(function() { return this.each(function() {
$(this).sticky(options); $(this).sticky(options);
}); });
} }

View File

@ -134,9 +134,9 @@ $(function() {
} }
}); });
var input = $("#input").tab(complete, { var input = $("#input")
hint: false .history()
}); .tab(complete, {hint: false});
var form = $("#form").on("submit", function(e) { var form = $("#form").on("submit", function(e) {
e.preventDefault(); e.preventDefault();

File diff suppressed because one or more lines are too long