SiteSearch.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE SiteSearch.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. const SiteSearch = (($) => {
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'SiteSearch'
  13. const DATA_KEY = 'lte.site-search'
  14. const EVENT_KEY = `.${DATA_KEY}`
  15. const JQUERY_NO_CONFLICT = $.fn[NAME]
  16. const Event = {}
  17. const Selector = {
  18. TOGGLE_BUTTON : '[data-widget="site-search"]',
  19. SEARCH_BLOCK : '.site-search-block',
  20. SEARCH_BACKDROP: '.site-search-backdrop',
  21. SEARCH_INPUT : '.site-search-block .form-control'
  22. }
  23. const ClassName = {
  24. OPEN: 'site-search-open'
  25. }
  26. const Default = {
  27. transitionSpeed: 300
  28. }
  29. /**
  30. * Class Definition
  31. * ====================================================
  32. */
  33. class SiteSearch {
  34. constructor(_element, _options) {
  35. this.element = _element
  36. this.options = $.extend({}, Default, _options)
  37. }
  38. // Public
  39. open() {
  40. $(Selector.SEARCH_BLOCK).slideDown(this.options.transitionSpeed)
  41. $(Selector.SEARCH_BACKDROP).show(0)
  42. $(Selector.SEARCH_INPUT).focus()
  43. $(Selector.SEARCH_BLOCK).addClass(ClassName.OPEN)
  44. }
  45. close() {
  46. $(Selector.SEARCH_BLOCK).slideUp(this.options.transitionSpeed)
  47. $(Selector.SEARCH_BACKDROP).hide(0)
  48. $(Selector.SEARCH_BLOCK).removeClass(ClassName.OPEN)
  49. }
  50. toggle() {
  51. if ($(Selector.SEARCH_BLOCK).hasClass(ClassName.OPEN)) {
  52. this.close()
  53. } else {
  54. this.open()
  55. }
  56. }
  57. // Static
  58. static _jQueryInterface(options) {
  59. return this.each(function () {
  60. let data = $(this).data(DATA_KEY)
  61. if (!data) {
  62. data = new SiteSearch(this, options)
  63. $(this).data(DATA_KEY, data)
  64. }
  65. if (!/toggle|close/.test(options)) {
  66. throw Error(`Undefined method ${options}`)
  67. }
  68. data[options]()
  69. })
  70. }
  71. }
  72. /**
  73. * Data API
  74. * ====================================================
  75. */
  76. $(document).on('click', Selector.TOGGLE_BUTTON, (event) => {
  77. event.preventDefault()
  78. let button = $(event.currentTarget)
  79. if (button.data('widget') !== 'site-search') {
  80. button = button.closest(Selector.TOGGLE_BUTTON)
  81. }
  82. SiteSearch._jQueryInterface.call(button, 'toggle')
  83. })
  84. $(document).on('click', Selector.SEARCH_BACKDROP, (event) => {
  85. const backdrop = $(event.currentTarget)
  86. SiteSearch._jQueryInterface.call(backdrop, 'close')
  87. })
  88. /**
  89. * jQuery API
  90. * ====================================================
  91. */
  92. $.fn[NAME] = SiteSearch._jQueryInterface
  93. $.fn[NAME].Constructor = SiteSearch
  94. $.fn[NAME].noConflict = function () {
  95. $.fn[NAME] = JQUERY_NO_CONFLICT
  96. return SiteSearch._jQueryInterface
  97. }
  98. return SiteSearch
  99. })(jQuery)
  100. export default SiteSearch