NavbarSearch.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE NavbarSearch.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'NavbarSearch'
  13. const DATA_KEY = 'lte.navbar-search'
  14. const JQUERY_NO_CONFLICT = $.fn[NAME]
  15. const SELECTOR_TOGGLE_BUTTON = '[data-widget="navbar-search"]'
  16. const SELECTOR_SEARCH_BLOCK = '.navbar-search-block'
  17. const SELECTOR_SEARCH_INPUT = '.form-control'
  18. const CLASS_NAME_OPEN = 'navbar-search-open'
  19. const Default = {
  20. resetOnClose: true,
  21. target: SELECTOR_SEARCH_BLOCK
  22. }
  23. /**
  24. * Class Definition
  25. * ====================================================
  26. */
  27. class NavbarSearch {
  28. constructor(_element, _options) {
  29. this._element = _element
  30. this._config = _options
  31. }
  32. // Public
  33. open() {
  34. $(this._config.target).css('display', 'flex').hide().fadeIn().addClass(CLASS_NAME_OPEN)
  35. $(`${this._config.target} ${SELECTOR_SEARCH_INPUT}`).focus()
  36. }
  37. close() {
  38. $(this._config.target).fadeOut().removeClass(CLASS_NAME_OPEN)
  39. if (this._config.resetOnClose) {
  40. $(`${this._config.target} ${SELECTOR_SEARCH_INPUT}`).val('')
  41. }
  42. }
  43. toggle() {
  44. if ($(this._config.target).hasClass(CLASS_NAME_OPEN)) {
  45. this.close()
  46. } else {
  47. this.open()
  48. }
  49. }
  50. // Static
  51. static _jQueryInterface(config) {
  52. return this.each(function () {
  53. let data = $(this).data(DATA_KEY)
  54. const _config = $.extend({}, Default, typeof config === 'object' ? config : $(this).data())
  55. if (!data) {
  56. data = new NavbarSearch($(this), _config)
  57. $(this).data(DATA_KEY, data)
  58. } else if (typeof config === 'string') {
  59. if (typeof data[config] === 'undefined') {
  60. throw new TypeError(`No method named "${config}"`)
  61. }
  62. data[config]()
  63. }
  64. })
  65. }
  66. }
  67. /**
  68. * Data API
  69. * ====================================================
  70. */
  71. $(document).on('click', SELECTOR_TOGGLE_BUTTON, event => {
  72. event.preventDefault()
  73. let button = $(event.currentTarget)
  74. if (button.data('widget') !== 'navbar-search') {
  75. button = button.closest(SELECTOR_TOGGLE_BUTTON)
  76. }
  77. NavbarSearch._jQueryInterface.call(button, 'toggle')
  78. })
  79. /**
  80. * jQuery API
  81. * ====================================================
  82. */
  83. $.fn[NAME] = NavbarSearch._jQueryInterface
  84. $.fn[NAME].Constructor = NavbarSearch
  85. $.fn[NAME].noConflict = function () {
  86. $.fn[NAME] = JQUERY_NO_CONFLICT
  87. return NavbarSearch._jQueryInterface
  88. }
  89. export default NavbarSearch