SiteSearch.js 2.5 KB

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