lib/jschat/http/public/javascripts/app/ui/tab_completion.js in jschat-0.3.3 vs lib/jschat/http/public/javascripts/app/ui/tab_completion.js in jschat-0.3.5
- old
+ new
@@ -1,12 +1,52 @@
+var History = Class.create({
+ initialize: function() {
+ this.messages = [];
+ this.index = 0;
+ this.limit = 100;
+ },
+
+ prev: function() {
+ this.index = this.index <= 0 ? this.messages.length - 1 : this.index - 1;
+ },
+
+ next: function() {
+ this.index = this.index >= this.messages.length - 1 ? 0 : this.index + 1;
+ },
+
+ reset: function() {
+ this.index = this.messages.length;
+ },
+
+ value: function() {
+ if (this.messages.length == 0) return '';
+ return this.messages[this.index];
+ },
+
+ add: function(value) {
+ if (!value || value.length == 0) return;
+
+ this.messages.push(value);
+ if (this.messages.length > this.limit) {
+ this.messages = this.messages.slice(-this.limit);
+ }
+ this.index = this.messages.length;
+ },
+
+ atTop: function() {
+ return this.index === this.messages.length;
+ }
+});
+
var TabCompletion = Class.create({
initialize: function(element) {
this.element = $(element);
this.matches = [];
this.match_offset = 0;
this.cycling = false;
this.has_focus = true;
+ this.history = new History();
document.observe('keydown', this.keyboardEvents.bindAsEventListener(this));
this.element.observe('focus', this.onFocus.bindAsEventListener(this));
this.element.observe('blur', this.onBlur.bindAsEventListener(this));
this.element.observe('click', this.onFocus.bindAsEventListener(this));
@@ -21,11 +61,11 @@
this.has_focus = true;
this.reset();
},
tabSearch: function(input) {
- var names = $$('#names li').collect(function(element) { return element.innerHTML });
+ var names = $$('#names li').collect(function(element) { return element.innerHTML }).sort();
return names.findAll(function(name) { return name.toLowerCase().match(input.toLowerCase()) });
},
textToLeft: function() {
var text = this.element.value;
@@ -100,9 +140,29 @@
this.element.value = '#{search_result}: '.interpolate({ search_result: search_result });
}
}
}
+ Event.stop(e);
+ return false;
+ break;
+
+ case Event.KEY_UP:
+ if (this.history.atTop()) {
+ this.history.add(this.element.value);
+ }
+
+ this.history.prev();
+ this.element.value = this.history.value();
+ FormHelpers.setCaretPosition(this.element, this.element.value.length + 1);
+ Event.stop(e);
+ return false;
+ break;
+
+ case Event.KEY_DOWN:
+ this.history.next();
+ this.element.value = this.history.value();
+ FormHelpers.setCaretPosition(this.element, this.element.value.length + 1);
Event.stop(e);
return false;
break;
default: