Browse Source

updated bootstrap-typahead

php2python
Holger Frey 7 years ago
parent
commit
bc78ffcdde
  1. 168
      ordr2/static/js/bootstrap-typeahead.js

168
ordr2/static/js/bootstrap-typeahead.js vendored

@ -1,8 +1,8 @@
/* ============================================================= /* =============================================================
* bootstrap-typeahead.js v2.0.0 * bootstrap-typeahead.js v2.3.2
* http://twitter.github.com/bootstrap/javascript.html#typeahead * http://getbootstrap.com/2.3.2/javascript.html#typeahead
* ============================================================= * =============================================================
* Copyright 2012 Twitter, Inc. * Copyright 2013 Twitter, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,18 +17,24 @@
* limitations under the License. * limitations under the License.
* ============================================================ */ * ============================================================ */
!function( $ ){
"use strict" !function($){
var Typeahead = function ( element, options ) { "use strict"; // jshint ;_;
/* TYPEAHEAD PUBLIC CLASS DEFINITION
* ================================= */
var Typeahead = function (element, options) {
this.$element = $(element) this.$element = $(element)
this.options = $.extend({}, $.fn.typeahead.defaults, options) this.options = $.extend({}, $.fn.typeahead.defaults, options)
this.matcher = this.options.matcher || this.matcher this.matcher = this.options.matcher || this.matcher
this.sorter = this.options.sorter || this.sorter this.sorter = this.options.sorter || this.sorter
this.highlighter = this.options.highlighter || this.highlighter this.highlighter = this.options.highlighter || this.highlighter
this.$menu = $(this.options.menu).appendTo('body') this.updater = this.options.updater || this.updater
this.source = this.options.source this.source = this.options.source
this.$menu = $(this.options.menu)
this.shown = false this.shown = false
this.listen() this.listen()
} }
@ -39,21 +45,29 @@
, select: function () { , select: function () {
var val = this.$menu.find('.active').attr('data-value') var val = this.$menu.find('.active').attr('data-value')
this.$element.val(val) this.$element
.val(this.updater(val))
.change()
return this.hide() return this.hide()
} }
, updater: function (item) {
return item
}
, show: function () { , show: function () {
var pos = $.extend({}, this.$element.offset(), { var pos = $.extend({}, this.$element.position(), {
height: this.$element[0].offsetHeight height: this.$element[0].offsetHeight
}) })
this.$menu.css({ this.$menu
.insertAfter(this.$element)
.css({
top: pos.top + pos.height top: pos.top + pos.height
, left: pos.left , left: pos.left
}) })
.show()
this.$menu.show()
this.shown = true this.shown = true
return this return this
} }
@ -65,18 +79,24 @@
} }
, lookup: function (event) { , lookup: function (event) {
var that = this var items
, items
, q
this.query = this.$element.val() this.query = this.$element.val()
if (!this.query) { if (!this.query || this.query.length < this.options.minLength) {
return this.shown ? this.hide() : this return this.shown ? this.hide() : this
} }
items = $.grep(this.source, function (item) { items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source
if (that.matcher(item)) return item
return items ? this.process(items) : this
}
, process: function (items) {
var that = this
items = $.grep(items, function (item) {
return that.matcher(item)
}) })
items = this.sorter(items) items = this.sorter(items)
@ -108,7 +128,8 @@
} }
, highlighter: function (item) { , highlighter: function (item) {
return item.replace(new RegExp('(' + this.query + ')', 'ig'), function ($1, match) { var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>' return '<strong>' + match + '</strong>'
}) })
} }
@ -151,26 +172,71 @@
, listen: function () { , listen: function () {
this.$element this.$element
.on('focus', $.proxy(this.focus, this))
.on('blur', $.proxy(this.blur, this)) .on('blur', $.proxy(this.blur, this))
.on('keypress', $.proxy(this.keypress, this)) .on('keypress', $.proxy(this.keypress, this))
.on('keyup', $.proxy(this.keyup, this)) .on('keyup', $.proxy(this.keyup, this))
if ($.browser.webkit || $.browser.msie) { if (this.eventSupported('keydown')) {
this.$element.on('keydown', $.proxy(this.keypress, this)) this.$element.on('keydown', $.proxy(this.keydown, this))
} }
this.$menu this.$menu
.on('click', $.proxy(this.click, this)) .on('click', $.proxy(this.click, this))
.on('mouseenter', 'li', $.proxy(this.mouseenter, this)) .on('mouseenter', 'li', $.proxy(this.mouseenter, this))
.on('mouseleave', 'li', $.proxy(this.mouseleave, this))
} }
, keyup: function (e) { , eventSupported: function(eventName) {
e.stopPropagation() var isSupported = eventName in this.$element
if (!isSupported) {
this.$element.setAttribute(eventName, 'return;')
isSupported = typeof this.$element[eventName] === 'function'
}
return isSupported
}
, move: function (e) {
if (!this.shown) return
switch(e.keyCode) {
case 9: // tab
case 13: // enter
case 27: // escape
e.preventDefault()
break
case 38: // up arrow
e.preventDefault()
this.prev()
break
case 40: // down arrow
e.preventDefault() e.preventDefault()
this.next()
break
}
e.stopPropagation()
}
, keydown: function (e) {
this.suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27])
this.move(e)
}
, keypress: function (e) {
if (this.suppressKeyPressRepeat) return
this.move(e)
}
, keyup: function (e) {
switch(e.keyCode) { switch(e.keyCode) {
case 40: // down arrow case 40: // down arrow
case 38: // up arrow case 38: // up arrow
case 16: // shift
case 17: // ctrl
case 18: // alt
break break
case 9: // tab case 9: // tab
@ -180,6 +246,7 @@
break break
case 27: // escape case 27: // escape
if (!this.shown) return
this.hide() this.hide()
break break
@ -187,56 +254,46 @@
this.lookup() this.lookup()
} }
}
, keypress: function (e) {
e.stopPropagation() e.stopPropagation()
if (!this.shown) return
switch(e.keyCode) {
case 9: // tab
case 13: // enter
case 27: // escape
e.preventDefault()
break
case 38: // up arrow
e.preventDefault()
this.prev()
break
case 40: // down arrow
e.preventDefault() e.preventDefault()
this.next()
break
} }
, focus: function (e) {
this.focused = true
} }
, blur: function (e) { , blur: function (e) {
var that = this this.focused = false
e.stopPropagation() if (!this.mousedover && this.shown) this.hide()
e.preventDefault()
setTimeout(function () { that.hide() }, 150)
} }
, click: function (e) { , click: function (e) {
e.stopPropagation() e.stopPropagation()
e.preventDefault() e.preventDefault()
this.select() this.select()
this.$element.focus()
} }
, mouseenter: function (e) { , mouseenter: function (e) {
this.mousedover = true
this.$menu.find('.active').removeClass('active') this.$menu.find('.active').removeClass('active')
$(e.currentTarget).addClass('active') $(e.currentTarget).addClass('active')
} }
, mouseleave: function (e) {
this.mousedover = false
if (!this.focused && this.shown) this.hide()
}
} }
/* TYPEAHEAD PLUGIN DEFINITION /* TYPEAHEAD PLUGIN DEFINITION
* =========================== */ * =========================== */
$.fn.typeahead = function ( option ) { var old = $.fn.typeahead
$.fn.typeahead = function (option) {
return this.each(function () { return this.each(function () {
var $this = $(this) var $this = $(this)
, data = $this.data('typeahead') , data = $this.data('typeahead')
@ -251,21 +308,28 @@
, items: 8 , items: 8
, menu: '<ul class="typeahead dropdown-menu"></ul>' , menu: '<ul class="typeahead dropdown-menu"></ul>'
, item: '<li><a href="#"></a></li>' , item: '<li><a href="#"></a></li>'
, minLength: 1
} }
$.fn.typeahead.Constructor = Typeahead $.fn.typeahead.Constructor = Typeahead
/* TYPEAHEAD NO CONFLICT
* =================== */
$.fn.typeahead.noConflict = function () {
$.fn.typeahead = old
return this
}
/* TYPEAHEAD DATA-API /* TYPEAHEAD DATA-API
* ================== */ * ================== */
$(function () { $(document).on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) {
$('body').on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) {
var $this = $(this) var $this = $(this)
if ($this.data('typeahead')) return if ($this.data('typeahead')) return
e.preventDefault()
$this.typeahead($this.data()) $this.typeahead($this.data())
}) })
})
}( window.jQuery ) }(window.jQuery);