Update stickyScroll

This commit is contained in:
Mattias Erming 2014-05-24 21:45:28 +02:00
parent a516a1ac12
commit 799b10c6d9
2 changed files with 63 additions and 92 deletions

View file

@ -59,7 +59,7 @@ $(function() {
.find(".window") .find(".window")
.last() .last()
.find(".messages") .find(".messages")
.scrollGlue({speed: 400}) .sticky({speed: 400})
.end() .end()
.find(".input") .find(".input")
.tabComplete(commands); .tabComplete(commands);
@ -87,7 +87,7 @@ $(function() {
.prev(".show-more") .prev(".show-more")
.show(); .show();
chat.find(".messages") chat.find(".messages")
.scrollGlue({speed: 400}) .sticky({speed: 400})
.end(); .end();
var networks = $("#networks") var networks = $("#networks")

View file

@ -1,72 +1,6 @@
/*! /*!
* inputHistory * stickyScroll
* https://github.com/erming/inputHistory * https://github.com/erming/stickyScroll
*
* Copyright (c) 2014 Mattias Erming <mattias@mattiaserming.com>
* Licensed under the MIT License.
*
* Version 0.1.2
*/
(function($) {
$.fn.inputHistory = function(options) {
var settings = $.extend({
history: [],
submit: false,
}, options);
var self = this;
if (self.size() > 1) {
return self.each(function() {
$(this).inputHistory(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);
/*!
* scrollGlue
* https://github.com/erming/scrollGlue
* *
* Copyright (c) 2014 Mattias Erming <mattias@mattiaserming.com> * Copyright (c) 2014 Mattias Erming <mattias@mattiaserming.com>
* Licensed under the MIT License. * Licensed under the MIT License.
@ -74,7 +8,7 @@
* Version 1.2.1 * Version 1.2.1
*/ */
(function($) { (function($) {
$.fn.scrollGlue = function(options) { $.fn.sticky = function(options) {
var settings = $.extend({ var settings = $.extend({
disableManualScroll: false, disableManualScroll: false,
overflow: 'scroll', overflow: 'scroll',
@ -85,7 +19,7 @@
var self = this; var self = this;
if (self.size() > 1) { if (self.size() > 1) {
return self.each(function() { return self.each(function() {
$(this).scrollGlue(options); $(this).sticky(options);
}); });
} }
@ -156,43 +90,52 @@
/*! /*!
* tabComplete * tabComplete
* Lightweight tab completion for <input> and <textarea>
*
* Source:
* https://github.com/erming/tabComplete * https://github.com/erming/tabComplete
* *
* Copyright (c) 2014 Mattias Erming <mattias@mattiaserming.com> * Copyright (c) 2014 Mattias Erming <mattias@mattiaserming.com>
* Licensed under the MIT License. * Licensed under the MIT License.
* *
* Version 1.0.0-alpha2 * Version 1.1.1
*/ */
(function($) { (function($) {
var defaults = { var keys = {
after: "", tab: 9,
caseSensitive: false, up: 38,
hint: true, down: 40
minLength: 1,
}; };
$.fn.tabComplete = function(args, options) { $.fn.tabComplete = function(args, options) {
var self = this;
options = $.extend(
{}, defaults, options
);
if (this.length > 1) { if (this.length > 1) {
return this.each(function() { return this.each(function() {
$(this).tabComplete(args, options); $(this).tabComplete(args, options);
}); });
} }
if (options.hint) { // Only enable the plugin on <input> and <textarea> elements.
// Lets turn on hinting. var tag = this.prop("tagName");
hint.call(self, ""); if (tag != "INPUT" && tag != "TEXTAREA") {
return;
} }
// Set default options.
options = $.extend({
after: "",
arrowKeys: tag == "INPUT" ? true : false,
caseSensitive: false,
hint: true,
minLength: 1,
onTabComplete: $.noop
}, options);
// Unbind namespace. // Unbind namespace.
// This allows us to override the plugin if necessary. // This allows us to override the plugin if necessary.
this.unbind(".tabComplete"); this.unbind(".tabComplete");
var i = 0; var self = this;
var i = -1;
var words = []; var words = [];
var last = ""; var last = "";
@ -201,7 +144,7 @@
var word = input.split(/ |\n/).pop(); var word = input.split(/ |\n/).pop();
if (!word) { if (!word) {
i = 0; i = -1;
words = []; words = [];
last = ""; last = "";
} else if (typeof args === "function") { } else if (typeof args === "function") {
@ -213,6 +156,9 @@
words = match(args, word, options.caseSensitive); words = match(args, word, options.caseSensitive);
} }
// Emit the number of matching words with the 'match' event.
self.trigger("match", words.length);
if (options.hint) { if (options.hint) {
if (word.length >= options.minLength && words.length) { if (word.length >= options.minLength && words.length) {
hint.call(self, words[0]); hint.call(self, words[0]);
@ -226,12 +172,26 @@
this.on("keydown.tabComplete", function(e) { this.on("keydown.tabComplete", function(e) {
var key = e.which; var key = e.which;
if (key == 9) { if (key == keys.tab || (options.arrowKeys && (key == keys.up || key == keys.down))) {
// Don't lose focus on tab click. // Don't lose focus on tab click.
e.preventDefault(); e.preventDefault();
// Iterate the matches with tab and the up and down keys by incrementing
// or decrementing the 'i' variable.
if (key != keys.up) {
i++;
} else {
if (i == -1) return;
if (i == 0) {
// Jump to the last word.
i = words.length - 1;
} else {
i--;
}
}
// Get next match. // Get next match.
var word = words[i++ % words.length]; var word = words[i % words.length];
if (!word) { if (!word) {
return; return;
} }
@ -252,6 +212,12 @@
// Remember the word until next time. // Remember the word until next time.
last = word; last = word;
// Trigger callback.
options.onTabComplete(last);
// Trigger the 'tabComplete' event on a successful complete.
self.trigger("tabComplete", last);
if (options.hint) { if (options.hint) {
// Turn off any additional hinting. // Turn off any additional hinting.
hint.call(self, ""); hint.call(self, "");
@ -259,6 +225,11 @@
} }
}); });
if (options.hint) {
// If enabled, turn on hinting.
hint.call(this, "");
}
return this; return this;
} }
@ -277,7 +248,7 @@
); );
} }
// Add input hinting. // Input hinting.
// This works by creating a copy of the input and placing it behind // This works by creating a copy of the input and placing it behind
// the real input. // the real input.
function hint(word) { function hint(word) {
@ -297,7 +268,7 @@
); );
clone = input clone = input
.clone() .clone()
.prop("disabled", true) .attr("tabindex", -1)
.removeAttr("id name placeholder") .removeAttr("id name placeholder")
.addClass("hint") .addClass("hint")
.insertBefore(input); .insertBefore(input);