textmodule: simplify template, reverse order, auto-scroll to bottom

This commit is contained in:
Felix Niklas 2016-02-02 16:42:11 +01:00
parent 031f60d3aa
commit fe40ac6020

View file

@ -54,16 +54,16 @@
// enter // enter
if ( e.keyCode === 13 ) { if ( e.keyCode === 13 ) {
e.preventDefault() e.preventDefault()
var id = _this.$widget.find('.dropdown-menu li.is-active a').data('id') var id = _this.$widget.find('.dropdown-menu li.is-active').data('id')
// as fallback use hovered element // as fallback use hovered element
if (!id) { if (!id) {
id = _this.$widget.find('.dropdown-menu li:hover a').data('id') id = _this.$widget.find('.dropdown-menu li:hover').data('id')
} }
// as fallback first element // as fallback first element
if (!id) { if (!id) {
id = _this.$widget.find('.dropdown-menu li:first-child a').data('id') id = _this.$widget.find('.dropdown-menu li:first-child').data('id')
} }
_this.take(id) _this.take(id)
return return
@ -194,6 +194,7 @@
Plugin.prototype.baseTemplate = function() { Plugin.prototype.baseTemplate = function() {
this.$element.after('<div class="shortcut dropdown"><ul class="dropdown-menu" style="max-height: 200px;"><li><a>-</a></li></ul></div>') this.$element.after('<div class="shortcut dropdown"><ul class="dropdown-menu" style="max-height: 200px;"><li><a>-</a></li></ul></div>')
this.$widget = this.$element.next() this.$widget = this.$element.next()
this.$widget.on('click', 'li', $.proxy(this.onEntryClick, this))
} }
// set height of widget // set height of widget
@ -318,6 +319,11 @@
} }
} }
Plugin.prototype.onEntryClick = function(event) {
var id = $(event.target).data('id')
this.take(id)
}
// select text module and insert into text // select text module and insert into text
Plugin.prototype.take = function(id) { Plugin.prototype.take = function(id) {
if (!id) { if (!id) {
@ -364,32 +370,32 @@
return return
}) })
result.reverse()
this.$widget.find('ul').html('') this.$widget.find('ul').html('')
this.log('result', term, result) this.log('result', term, result)
if (!result[0]) {
return this.close()
}
if (!this.active) {
this.open()
}
for (var i = 0; i < result.length; i++) { for (var i = 0; i < result.length; i++) {
var item = result[i] var item = result[i]
var template = "<li><a href=\"#\" class=\"u-textTruncate\" data-id=" + item.id + ">" + App.Utils.htmlEscape(item.name) var element = $('<li>')
if (item.keywords) { element.attr('data-id', item.id)
template = template + " (" + App.Utils.htmlEscape(item.keywords) + ")" element.text(App.Utils.htmlEscape(item.name))
} element.addClass('u-clickable u-textTruncate')
template = template + "</a></li>" if (i == result.length-1) {
var element = $(template)
if (i == 0) {
element.addClass('is-active') element.addClass('is-active')
} }
this.$widget.find('ul').append(element) this.$widget.find('ul').append(element)
} }
// if ( !result[0] ) {
// this.$widget.find('ul').append("<li><a href='#'>-</a></li>") this.$widget.find('ul').scrollTop(9999)
// }
this.$widget.find('ul li').on(
'click',
function(e) {
e.preventDefault()
var id = $(e.target).data('id')
_this.take(id)
}
)
this.movePosition() this.movePosition()
} }