AdminLTE/build/js/SiteSearch.js

122 lines
2.5 KiB
JavaScript
Raw Normal View History

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