toggle.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui'
  8. ], function ($) {
  9. 'use strict';
  10. $.widget('mage.toggleAdvanced', {
  11. options: {
  12. baseToggleClass: 'active' // Class used to be toggled on clicked element
  13. },
  14. /**
  15. * Toggle creation
  16. * @private
  17. */
  18. _create: function () {
  19. this.beforeCreate();
  20. this._bindCore();
  21. this.afterCreate();
  22. },
  23. /**
  24. * Core bound events & setup
  25. * @protected
  26. */
  27. _bindCore: function () {
  28. var widget = this;
  29. this.element.on('click', $.proxy(function (e) {
  30. widget._onClick();
  31. e.preventDefault();
  32. }, this));
  33. },
  34. /**
  35. * Binding Click event
  36. *
  37. * @protected
  38. */
  39. _onClick: function () {
  40. this._prepareOptions();
  41. this._toggleSelectors();
  42. },
  43. /**
  44. * Method used to look for data attributes to override default options
  45. *
  46. * @protected
  47. */
  48. _prepareOptions: function () {
  49. this.options.baseToggleClass = this.element.data('base-toggle-class') ?
  50. this.element.data('base-toggle-class') : this.options.baseToggleClass;
  51. },
  52. /**
  53. * Method responsible for hiding and revealing specified DOM elements
  54. * Toggle the class on clicked element
  55. *
  56. * @protected
  57. */
  58. _toggleSelectors: function () {
  59. this.element.toggleClass(this.options.baseToggleClass);
  60. },
  61. /**
  62. * Method used to inject 3rd party functionality before create
  63. * @public
  64. */
  65. beforeCreate: function () {},
  66. /**
  67. * Method used to inject 3rd party functionality after create
  68. * @public
  69. */
  70. afterCreate: function () {}
  71. });
  72. // Extension for mage.toggle - Adding selectors support for other DOM elements we wish to toggle
  73. $.widget('mage.toggleAdvanced', $.mage.toggleAdvanced, {
  74. options: {
  75. selectorsToggleClass: 'hidden', // Class used to be toggled on selectors DOM elements
  76. toggleContainers: null
  77. },
  78. /**
  79. * Method responsible for hiding and revealing specified DOM elements
  80. * If data-toggle-selectors attribute is present - toggle will be done on these selectors
  81. * Otherwise we toggle the class on clicked element
  82. *
  83. * @protected
  84. * @override
  85. */
  86. _toggleSelectors: function () {
  87. this._super();
  88. if (this.options.toggleContainers) {
  89. $(this.options.toggleContainers).toggleClass(this.options.selectorsToggleClass);
  90. } else {
  91. this.element.toggleClass(this.options.baseToggleClass);
  92. }
  93. },
  94. /**
  95. * Method used to look for data attributes to override default options
  96. *
  97. * @protected
  98. * @override
  99. */
  100. _prepareOptions: function () {
  101. this.options.selectorsToggleClass = this.element.data('selectors-toggle-class') ?
  102. this.element.data('selectors-toggle-class') : this.options.selectorsToggleClass;
  103. this.options.toggleContainers = this.element.data('toggle-selectors') ?
  104. this.element.data('toggle-selectors') : this.options.toggleContainers;
  105. this._super();
  106. }
  107. });
  108. // Extension for mage.toggle - Adding label toggle
  109. $.widget('mage.toggleAdvanced', $.mage.toggleAdvanced, {
  110. options: {
  111. newLabel: null, // Text of the new label to be used on toggle
  112. curLabel: null, // Text of the old label to be used on toggle
  113. currentLabelElement: null // Current label container
  114. },
  115. /**
  116. * Binding Click event
  117. *
  118. * @protected
  119. * @override
  120. */
  121. _onClick: function () {
  122. this._super();
  123. this._toggleLabel();
  124. },
  125. /**
  126. * Method responsible for replacing clicked element labels
  127. * @protected
  128. */
  129. _toggleLabel: function () {
  130. var cachedLabel, currentLabelSelector;
  131. if (this.options.newLabel) {
  132. cachedLabel = this.options.newLabel;
  133. currentLabelSelector = this.options.currentLabelElement ?
  134. $(this.options.currentLabelElement) : this.element;
  135. this.element.data('toggle-label', this.options.curLabel);
  136. currentLabelSelector.html(this.options.newLabel);
  137. this.options.curLabel = this.options.newLabel;
  138. this.options.newLabel = cachedLabel;
  139. }
  140. },
  141. /**
  142. * Method used to look for data attributes to override default options
  143. *
  144. * @protected
  145. * @override
  146. */
  147. _prepareOptions: function () {
  148. this.options.newLabel = this.element.data('toggle-label') ?
  149. this.element.data('toggle-label') : this.options.newLabel;
  150. this.options.currentLabelElement = this.element.data('current-label-el') ?
  151. this.element.data('current-label-el') : this.options.currentLabelElement;
  152. if (!this.options.currentLabelElement) {
  153. this.options.currentLabelElement = this.element;
  154. }
  155. this.options.curLabel = $(this.options.currentLabelElement).html();
  156. this._super();
  157. }
  158. });
  159. return $.mage.toggleAdvanced;
  160. });