AdminLTE/build/js/SiteSearch.js

124 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-01-03 16:42:20 +01:00
/**
* --------------------------------------------
* AdminLTE SiteSearch.js
* License MIT
* --------------------------------------------
*/
2020-05-30 15:44:20 +02:00
const SiteSearch = ($ => {
2017-01-03 16:42:20 +01:00
/**
* Constants
* ====================================================
*/
2020-05-30 15:44:20 +02:00
const NAME = 'SiteSearch'
const DATA_KEY = 'lte.site-search'
2017-01-03 16:42:20 +01:00
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Selector = {
2020-05-30 15:44:20 +02:00
TOGGLE_BUTTON: '[data-widget="site-search"]',
SEARCH_BLOCK: '.site-search-block',
2017-01-03 16:42:20 +01:00
SEARCH_BACKDROP: '.site-search-backdrop',
2020-05-30 15:44:20 +02:00
SEARCH_INPUT: '.site-search-block .form-control'
2017-01-03 16:42:20 +01:00
}
const ClassName = {
OPEN: 'site-search-open'
}
const Default = {
transitionSpeed: 300
}
/**
* Class Definition
* ====================================================
*/
class SiteSearch {
constructor(_element, _options) {
this.element = _element
2018-02-04 00:45:19 +01:00
this.options = $.extend({}, Default, _options)
2017-01-03 16:42:20 +01:00
}
// Public
open() {
$(Selector.SEARCH_BLOCK).slideDown(this.options.transitionSpeed)
$(Selector.SEARCH_BACKDROP).show(0)
2018-02-04 00:45:19 +01:00
$(Selector.SEARCH_INPUT).focus()
2017-01-03 16:42:20 +01:00
$(Selector.SEARCH_BLOCK).addClass(ClassName.OPEN)
}
close() {
$(Selector.SEARCH_BLOCK).slideUp(this.options.transitionSpeed)
$(Selector.SEARCH_BACKDROP).hide(0)
$(Selector.SEARCH_BLOCK).removeClass(ClassName.OPEN)
}
toggle() {
if ($(Selector.SEARCH_BLOCK).hasClass(ClassName.OPEN)) {
this.close()
} else {
this.open()
}
}
// Static
static _jQueryInterface(options) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
if (!data) {
data = new SiteSearch(this, options)
$(this).data(DATA_KEY, data)
}
2018-02-04 00:45:19 +01:00
if (!/toggle|close/.test(options)) {
2020-05-30 15:44:20 +02:00
throw new Error(`Undefined method ${options}`)
2017-01-03 16:42:20 +01:00
}
data[options]()
})
}
}
/**
* Data API
* ====================================================
*/
2020-05-30 15:44:20 +02:00
$(document).on('click', Selector.TOGGLE_BUTTON, event => {
2018-02-04 00:45:19 +01:00
event.preventDefault()
2017-01-03 16:42:20 +01:00
let button = $(event.currentTarget)
if (button.data('widget') !== 'site-search') {
button = button.closest(Selector.TOGGLE_BUTTON)
}
SiteSearch._jQueryInterface.call(button, 'toggle')
2018-02-04 00:45:19 +01:00
})
2017-01-03 16:42:20 +01:00
2020-05-30 15:44:20 +02:00
$(document).on('click', Selector.SEARCH_BACKDROP, event => {
2018-02-04 00:45:19 +01:00
const backdrop = $(event.currentTarget)
2017-01-03 16:42:20 +01:00
SiteSearch._jQueryInterface.call(backdrop, 'close')
2018-02-04 00:45:19 +01:00
})
2017-01-03 16:42:20 +01:00
/**
* jQuery API
* ====================================================
*/
$.fn[NAME] = SiteSearch._jQueryInterface
$.fn[NAME].Constructor = SiteSearch
2020-05-30 15:44:20 +02:00
$.fn[NAME].noConflict = function () {
2017-01-03 16:42:20 +01:00
$.fn[NAME] = JQUERY_NO_CONFLICT
return SiteSearch._jQueryInterface
}
return SiteSearch
})(jQuery)
2018-02-04 00:45:19 +01:00
export default SiteSearch