AdminLTE/build/js/NavbarSearch.js

114 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-01-03 15:42:20 +00:00
/**
* --------------------------------------------
2020-12-02 08:34:04 +00:00
* AdminLTE NavbarSearch.js
2017-01-03 15:42:20 +00:00
* 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-12-02 08:34:04 +00:00
const NAME = 'NavbarSearch'
const DATA_KEY = 'lte.navbar-search'
2020-06-02 13:23:22 +00:00
const JQUERY_NO_CONFLICT = $.fn[NAME]
2017-01-03 15:42:20 +00:00
2020-12-02 08:34:04 +00:00
const SELECTOR_TOGGLE_BUTTON = '[data-widget="navbar-search"]'
const SELECTOR_SEARCH_BLOCK = '.navbar-search-block'
2021-02-02 07:15:50 +00:00
const SELECTOR_SEARCH_INPUT = '.form-control'
2017-01-03 15:42:20 +00:00
2020-12-02 08:34:04 +00:00
const CLASS_NAME_OPEN = 'navbar-search-open'
2017-01-03 15:42:20 +00:00
2020-06-02 13:23:22 +00:00
const Default = {
2021-02-02 07:15:50 +00:00
resetOnClose: true,
target: SELECTOR_SEARCH_BLOCK
2020-06-02 13:23:22 +00:00
}
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-12-02 08:34:04 +00:00
class NavbarSearch {
2020-06-02 13:23:22 +00:00
constructor(_element, _options) {
2020-12-02 08:24:43 +00:00
this._element = _element
this._config = $.extend({}, Default, _options)
2020-06-02 13:23:22 +00:00
}
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() {
2021-02-02 07:15:50 +00:00
$(this._config.target).css('display', 'flex').hide().fadeIn().addClass(CLASS_NAME_OPEN)
$(`${this._config.target} ${SELECTOR_SEARCH_INPUT}`).focus()
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() {
2021-02-02 07:15:50 +00:00
$(this._config.target).fadeOut().removeClass(CLASS_NAME_OPEN)
2020-12-02 08:24:43 +00:00
if (this._config.resetOnClose) {
2021-02-02 07:15:50 +00:00
$(`${this._config.target} ${SELECTOR_SEARCH_INPUT}`).val('')
2020-12-02 08:24:43 +00:00
}
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() {
2021-02-02 07:15:50 +00:00
if ($(this._config.target).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)
2021-02-02 07:15:50 +00:00
const _options = $.extend({}, Default, $(this).data())
2017-01-03 15:42:20 +00:00
2020-06-02 13:23:22 +00:00
if (!data) {
2021-02-02 07:15:50 +00:00
data = new NavbarSearch(this, _options)
2020-06-02 13:23:22 +00:00
$(this).data(DATA_KEY, data)
}
2017-01-03 15:42:20 +00:00
2020-12-02 08:24:43 +00:00
if (!/toggle|close|open/.test(options)) {
2020-06-02 13:23:22 +00:00
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 => {
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-12-02 08:34:04 +00:00
if (button.data('widget') !== 'navbar-search') {
button = button.closest(SELECTOR_TOGGLE_BUTTON)
2017-01-03 15:42:20 +00:00
}
2020-12-02 08:34:04 +00:00
NavbarSearch._jQueryInterface.call(button, 'toggle')
2020-06-02 13:23:22 +00:00
})
/**
* jQuery API
* ====================================================
*/
2020-12-02 08:34:04 +00:00
$.fn[NAME] = NavbarSearch._jQueryInterface
$.fn[NAME].Constructor = NavbarSearch
2020-06-02 13:23:22 +00:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
2020-12-02 08:34:04 +00:00
return NavbarSearch._jQueryInterface
2020-06-02 13:23:22 +00:00
}
2018-02-03 23:45:19 +00:00
2020-12-02 08:34:04 +00:00
export default NavbarSearch