From 2e511378ad399ba544ec395106233aaf6d2c4416 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Fri, 13 Jun 2014 00:41:23 +0200 Subject: [PATCH] Added inputhistory --- client/js/chat.js | 4 ++- client/js/jquery.plugins.js | 63 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/client/js/chat.js b/client/js/chat.js index 0437e0b..00ceec4 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -65,7 +65,8 @@ $(function() { .sticky() .end() .find(".input") - .tabcomplete(commands, {hint: false}); + .tabcomplete(commands, {hint: false}) + .history(); $("#network-" + data.id) .append(render("channels", {channels: [data.chan]})) @@ -97,6 +98,7 @@ $(function() { chat.html(render("windows", {windows: channels})) .find(".input") .tabcomplete(commands, {hint: false}) + .history() .end() .find(".hidden") .prev(".show-more") diff --git a/client/js/jquery.plugins.js b/client/js/jquery.plugins.js index 7db9e0e..dac8212 100644 --- a/client/js/jquery.plugins.js +++ b/client/js/jquery.plugins.js @@ -351,6 +351,69 @@ } })(jQuery); +/*! + * inputhistory + * https://github.com/erming/inputhistory + * v0.2.1 + */ +(function($) { + $.fn.history = // Alias + $.fn.inputhistory = function(options) { + var settings = $.extend({ + history: [], + submit: true, + }, options); + + var self = this; + if (self.size() > 1) { + return self.each(function() { + $(this).history(options); + }); + } + + self.data('history', settings.history.concat([''])); + + var i = 0; + self.on('keydown', function(e) { + var history = self.data('history'); + var key = e.which; + switch (key) { + + case 13: // Enter + if (self.val() != '') { + i = history.length; + history[i - 1] = self.val(); + history.push(''); + } + if (settings.submit) { + 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; + + } + + return false; + }); + + return this; + } +})(jQuery); + /*! * jQuery Cookie Plugin v1.4.0 * https://github.com/carhartl/jquery-cookie