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-11 12:34:44 +00:00
|
|
|
const SELECTOR_TOGGLE_BUTTON = '[data-widget="site-search"]'
|
|
|
|
const SELECTOR_SEARCH_BLOCK = '.site-search-block'
|
|
|
|
const SELECTOR_SEARCH_BACKDROP = '.site-search-backdrop'
|
|
|
|
const SELECTOR_SEARCH_INPUT = '.site-search-block .form-control'
|
2017-01-03 15:42:20 +00:00
|
|
|
|
2020-06-11 12:34:44 +00:00
|
|
|
const CLASS_NAME_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() {
|
2020-06-11 12:34:44 +00:00
|
|
|
$(SELECTOR_SEARCH_BLOCK).slideDown(this.options.transitionSpeed)
|
|
|
|
$(SELECTOR_SEARCH_BACKDROP).show(0)
|
|
|
|
$(SELECTOR_SEARCH_INPUT).focus()
|
|
|
|
$(SELECTOR_SEARCH_BLOCK).addClass(CLASS_NAME_OPEN)
|
2020-06-02 13:23:22 +00:00
|
|
|
}
|
2017-01-03 15:42:20 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
close() {
|
2020-06-11 12:34:44 +00:00
|
|
|
$(SELECTOR_SEARCH_BLOCK).slideUp(this.options.transitionSpeed)
|
|
|
|
$(SELECTOR_SEARCH_BACKDROP).hide(0)
|
|
|
|
$(SELECTOR_SEARCH_BLOCK).removeClass(CLASS_NAME_OPEN)
|
2020-06-02 13:23:22 +00:00
|
|
|
}
|
2017-01-03 15:42:20 +00:00
|
|
|
|
2020-06-02 13:23:22 +00:00
|
|
|
toggle() {
|
2020-06-11 12:34:44 +00:00
|
|
|
if ($(SELECTOR_SEARCH_BLOCK).hasClass(CLASS_NAME_OPEN)) {
|
2020-06-02 13:23:22 +00:00
|
|
|
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
|
|
|
|
* ====================================================
|
|
|
|
*/
|
2020-06-11 12:34:44 +00:00
|
|
|
$(document).on('click', SELECTOR_TOGGLE_BUTTON, event => {
|
2020-06-02 13:23:22 +00:00
|
|
|
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') {
|
2020-06-11 12:34:44 +00:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2020-06-11 12:34:44 +00:00
|
|
|
$(document).on('click', SELECTOR_SEARCH_BACKDROP, event => {
|
2020-06-02 13:23:22 +00:00
|
|
|
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
|