dropdown.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui',
  8. 'mage/translate'
  9. ], function ($) {
  10. 'use strict';
  11. var timer = null;
  12. /**
  13. * Dropdown Widget - this widget is a wrapper for the jQuery UI Dialog
  14. */
  15. $.widget('mage.dropdownDialog', $.ui.dialog, {
  16. options: {
  17. triggerEvent: 'click',
  18. triggerClass: null,
  19. parentClass: null,
  20. triggerTarget: null,
  21. defaultDialogClass: 'mage-dropdown-dialog',
  22. dialogContentClass: null,
  23. shadowHinter: null,
  24. closeOnMouseLeave: true,
  25. closeOnClickOutside: true,
  26. minHeight: null,
  27. minWidth: null,
  28. width: null,
  29. modal: false,
  30. timeout: null,
  31. autoOpen: false,
  32. createTitleBar: false,
  33. autoPosition: false,
  34. autoSize: false,
  35. draggable: false,
  36. resizable: false,
  37. bodyClass: '',
  38. buttons: [
  39. {
  40. 'class': 'action close',
  41. 'text': $.mage.__('Close'),
  42. /**
  43. * Click action.
  44. */
  45. 'click': function () {
  46. $(this).dropdownDialog('close');
  47. }
  48. }
  49. ]
  50. },
  51. /**
  52. * extend default functionality to bind the opener for dropdown
  53. * @private
  54. */
  55. _create: function () {
  56. var _self = this;
  57. this._super();
  58. this.uiDialog.addClass(this.options.defaultDialogClass);
  59. if (_self.options.triggerTarget) {
  60. $(_self.options.triggerTarget).on(_self.options.triggerEvent, function (event) {
  61. event.preventDefault();
  62. event.stopPropagation();
  63. if (!_self._isOpen) {
  64. $('.' + _self.options.defaultDialogClass + ' > .ui-dialog-content').dropdownDialog('close');
  65. _self.open();
  66. } else {
  67. _self.close(event);
  68. }
  69. });
  70. }
  71. if (_self.options.shadowHinter) {
  72. _self.hinter = $('<div class="' + _self.options.shadowHinter + '"/>');
  73. _self.element.append(_self.hinter);
  74. }
  75. },
  76. /**
  77. * Extend default functionality to close the dropdown
  78. * with custom delay on mouse out and also to close when clicking outside
  79. */
  80. open: function () {
  81. var _self = this;
  82. this._super();
  83. if (_self.options.dialogContentClass) {
  84. _self.element.addClass(_self.options.dialogContentClass);
  85. }
  86. if (_self.options.closeOnMouseLeave) {
  87. this._mouseEnter(_self.uiDialog);
  88. this._mouseLeave(_self.uiDialog);
  89. if (_self.options.triggerTarget) {
  90. this._mouseLeave($(_self.options.triggerTarget));
  91. }
  92. }
  93. if (_self.options.closeOnClickOutside) {
  94. $('body').on('click.outsideDropdown', function (event) {
  95. if (_self._isOpen && !$(event.target).closest('.ui-dialog').length) {
  96. if (timer) {
  97. clearTimeout(timer);
  98. }
  99. _self.close(event);
  100. }
  101. });
  102. }
  103. // adding the class on the opener and parent element for dropdown
  104. if (_self.options.triggerClass) {
  105. $(_self.options.triggerTarget).addClass(_self.options.triggerClass);
  106. }
  107. if (_self.options.parentClass) {
  108. $(_self.options.appendTo).addClass(_self.options.parentClass);
  109. }
  110. if (_self.options.bodyClass) {
  111. $('body').addClass(_self.options.bodyClass);
  112. }
  113. if (_self.options.shadowHinter) {
  114. _self._setShadowHinterPosition();
  115. }
  116. },
  117. /**
  118. * extend default functionality to reset the timer and remove the active class for opener
  119. */
  120. close: function () {
  121. this._super();
  122. if (this.options.dialogContentClass) {
  123. this.element.removeClass(this.options.dialogContentClass);
  124. }
  125. if (this.options.triggerClass) {
  126. $(this.options.triggerTarget).removeClass(this.options.triggerClass);
  127. }
  128. if (this.options.parentClass) {
  129. $(this.options.appendTo).removeClass(this.options.parentClass);
  130. }
  131. if (this.options.bodyClass) {
  132. $('body').removeClass(this.options.bodyClass);
  133. }
  134. if (timer) {
  135. clearTimeout(timer);
  136. }
  137. if (this.options.triggerTarget) {
  138. $(this.options.triggerTarget).off('mouseleave');
  139. }
  140. this.uiDialog.off('mouseenter');
  141. this.uiDialog.off('mouseleave');
  142. $('body').off('click.outsideDropdown');
  143. },
  144. /**
  145. * _setShadowHinterPosition
  146. * @private
  147. */
  148. _setShadowHinterPosition: function () {
  149. var _self = this,
  150. offset;
  151. offset = _self.options.position.of.offset().left -
  152. _self.element.offset().left +
  153. _self.options.position.of.outerWidth() / 2;
  154. offset = isNaN(offset) ? 0 : Math.floor(offset);
  155. _self.hinter.css('left', offset);
  156. },
  157. /**
  158. * @private
  159. */
  160. _position: function () {
  161. if (this.options.autoPosition) {
  162. this._super();
  163. }
  164. },
  165. /**
  166. * @private
  167. */
  168. _createTitlebar: function () {
  169. if (this.options.createTitleBar) {
  170. this._super();
  171. } else {
  172. // the title bar close button is referenced
  173. // in _focusTabbable function, so to prevent errors it must be declared
  174. this.uiDialogTitlebarClose = $('<div>');
  175. }
  176. },
  177. /**
  178. * @private
  179. */
  180. _size: function () {
  181. if (this.options.autoSize) {
  182. this._super();
  183. }
  184. },
  185. /**
  186. * @param {Object} handler
  187. * @private
  188. */
  189. _mouseLeave: function (handler) {
  190. var _self = this;
  191. handler.on('mouseleave', function (event) {
  192. event.stopPropagation();
  193. if (_self._isOpen) {
  194. if (timer) {
  195. clearTimeout(timer);
  196. }
  197. timer = setTimeout(function (e) {
  198. _self.close(e);
  199. }, _self.options.timeout);
  200. }
  201. });
  202. },
  203. /**
  204. * @param {Object} handler
  205. * @private
  206. */
  207. _mouseEnter: function (handler) {
  208. handler.on('mouseenter', function (event) {
  209. event.stopPropagation();
  210. if (timer) {
  211. clearTimeout(timer);
  212. }
  213. });
  214. },
  215. /**
  216. * @param {String} key
  217. * @param {*} value
  218. * @private
  219. */
  220. _setOption: function (key, value) {
  221. this._super(key, value);
  222. if (key === 'triggerTarget') {
  223. this.options.triggerTarget = value;
  224. }
  225. }
  226. });
  227. return $.mage.dropdownDialog;
  228. });