diff --git a/app/assets/javascripts/app/lib/base/jquery.textmodule.js b/app/assets/javascripts/app/lib/base/jquery.textmodule.js index 2764f5657..6836f47d2 100644 --- a/app/assets/javascripts/app/lib/base/jquery.textmodule.js +++ b/app/assets/javascripts/app/lib/base/jquery.textmodule.js @@ -38,7 +38,7 @@ } Plugin.prototype.init = function () { - this.baseTemplate() + this.renderBase() var _this = this this.$element.on('keydown', function (e) { @@ -75,41 +75,29 @@ return } - // up - if ( e.keyCode === 38 ) { + // up or down + if ( e.keyCode === 38 || e.keyCode === 40 ) { e.preventDefault() - if ( !_this.$widget.find('.dropdown-menu li.is-active')[0] ) { - var top = _this.$widget.find('.dropdown-menu li').last().addClass('is-active').position().top - _this.$widget.find('.dropdown-menu').scrollTop( top ); - return - } - else { - var prev = _this.$widget.find('.dropdown-menu li.is-active').removeClass('is-active').prev() - var top = 300 - if ( prev[0] ) { - top = prev.addClass('is-active').position().top - } - _this.$widget.find('.dropdown-menu').scrollTop( top ); - return - } - } + var active = _this.$widget.find('.dropdown-menu li.is-active') + active.removeClass('is-active') - // down - if ( e.keyCode === 40 ) { - e.preventDefault() - if ( !_this.$widget.find('.dropdown-menu li.is-active')[0] ) { - var top = _this.$widget.find('.dropdown-menu li').first().addClass('is-active').position().top - _this.$widget.find('.dropdown-menu').scrollTop( top ); - return + if ( e.keyCode == 38 && active.prev().size() ) { + active = active.prev() + } else if ( e.keyCode == 40 && active.next().size() ) { + active = active.next() } - else { - var next = _this.$widget.find('.dropdown-menu li.is-active').removeClass('is-active').next() - var top = 300 - if ( next[0] ) { - top = next.addClass('is-active').position().top - } - _this.$widget.find('.dropdown-menu').scrollTop( top ); - return + + active.addClass('is-active') + + var menu = _this.$widget.find('.dropdown-menu') + + if ( active.position().top < 0 ) { + // scroll up + menu.scrollTop( menu.scrollTop() + active.position().top ) + } else if ( active.position().top + active.height() > menu.height() ) { + // scroll down + var invisibleHeight = active.position().top + active.height() - menu.height() + menu.scrollTop( menu.scrollTop() + invisibleHeight ) } } @@ -174,15 +162,11 @@ } _this.log('BUFF HINT', _this.buffer, _this.buffer.length, e.which, String.fromCharCode(e.which)) - b = $.proxy(function() { - this.result( this.buffer.substr(2,this.buffer.length) ) - }, _this) - setTimeout(b, 400); - if (!_this.isActive()) { _this.open() } + _this.result( _this.buffer.substr(2, _this.buffer.length) ) } }).on('focus', function (e) { @@ -191,11 +175,11 @@ }; // create base template - Plugin.prototype.baseTemplate = function() { - this.$element.after('