Dropdown.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE Dropdown.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'Dropdown'
  13. const DATA_KEY = 'lte.dropdown'
  14. const JQUERY_NO_CONFLICT = $.fn[NAME]
  15. const Selector = {
  16. NAVBAR: '.navbar',
  17. DROPDOWN_MENU: '.dropdown-menu',
  18. DROPDOWN_MENU_ACTIVE: '.dropdown-menu.show',
  19. DROPDOWN_TOGGLE: '[data-toggle="dropdown"]'
  20. }
  21. const ClassName = {
  22. DROPDOWN_RIGHT: 'dropdown-menu-right'
  23. }
  24. const Default = {
  25. }
  26. /**
  27. * Class Definition
  28. * ====================================================
  29. */
  30. class Dropdown {
  31. constructor(element, config) {
  32. this._config = config
  33. this._element = element
  34. }
  35. // Public
  36. toggleSubmenu() {
  37. this._element.siblings().show().toggleClass('show')
  38. if (!this._element.next().hasClass('show')) {
  39. this._element.parents(Selector.DROPDOWN_MENU).first().find('.show').removeClass('show').hide()
  40. }
  41. this._element.parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', () => {
  42. $('.dropdown-submenu .show').removeClass('show').hide()
  43. })
  44. }
  45. fixPosition() {
  46. const elm = $(Selector.DROPDOWN_MENU_ACTIVE)
  47. if (elm.length !== 0) {
  48. if (elm.hasClass(ClassName.DROPDOWN_RIGHT)) {
  49. elm.css('left', 'inherit')
  50. elm.css('right', 0)
  51. } else {
  52. elm.css('left', 0)
  53. elm.css('right', 'inherit')
  54. }
  55. const offset = elm.offset()
  56. const width = elm.width()
  57. const windowWidth = $(window).width()
  58. const visiblePart = windowWidth - offset.left
  59. if (offset.left < 0) {
  60. elm.css('left', 'inherit')
  61. elm.css('right', (offset.left - 5))
  62. } else if (visiblePart < width) {
  63. elm.css('left', 'inherit')
  64. elm.css('right', 0)
  65. }
  66. }
  67. }
  68. // Static
  69. static _jQueryInterface(config) {
  70. return this.each(function () {
  71. let data = $(this).data(DATA_KEY)
  72. const _config = $.extend({}, Default, $(this).data())
  73. if (!data) {
  74. data = new Dropdown($(this), _config)
  75. $(this).data(DATA_KEY, data)
  76. }
  77. if (config === 'toggleSubmenu' || config === 'fixPosition') {
  78. data[config]()
  79. }
  80. })
  81. }
  82. }
  83. /**
  84. * Data API
  85. * ====================================================
  86. */
  87. $(Selector.DROPDOWN_MENU + ' ' + Selector.DROPDOWN_TOGGLE).on('click', function (event) {
  88. event.preventDefault()
  89. event.stopPropagation()
  90. Dropdown._jQueryInterface.call($(this), 'toggleSubmenu')
  91. })
  92. $(Selector.NAVBAR + ' ' + Selector.DROPDOWN_TOGGLE).on('click', event => {
  93. event.preventDefault()
  94. setTimeout(function () {
  95. Dropdown._jQueryInterface.call($(this), 'fixPosition')
  96. }, 1)
  97. })
  98. /**
  99. * jQuery API
  100. * ====================================================
  101. */
  102. $.fn[NAME] = Dropdown._jQueryInterface
  103. $.fn[NAME].Constructor = Dropdown
  104. $.fn[NAME].noConflict = function () {
  105. $.fn[NAME] = JQUERY_NO_CONFLICT
  106. return Dropdown._jQueryInterface
  107. }
  108. export default Dropdown