action-btn.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/Plugin/action-btn", ["exports", "jquery", "Plugin"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports, require("jquery"), require("Plugin"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports, global.jQuery, global.Plugin);
  11. global.PluginActionBtn = mod.exports;
  12. }
  13. })(this, function (_exports, _jquery, _Plugin2) {
  14. "use strict";
  15. Object.defineProperty(_exports, "__esModule", {
  16. value: true
  17. });
  18. _exports.default = void 0;
  19. _jquery = babelHelpers.interopRequireDefault(_jquery);
  20. _Plugin2 = babelHelpers.interopRequireDefault(_Plugin2);
  21. var pluginName = 'actionBtn';
  22. var defaults = {
  23. trigger: 'click',
  24. // click, hover
  25. toggleSelector: '.site-action-toggle',
  26. listSelector: '.site-action-buttons',
  27. activeClass: 'active',
  28. onShow: function onShow() {},
  29. onHide: function onHide() {}
  30. };
  31. var actionBtn =
  32. /*#__PURE__*/
  33. function () {
  34. function actionBtn(element, options) {
  35. babelHelpers.classCallCheck(this, actionBtn);
  36. this.element = element;
  37. this.$element = (0, _jquery.default)(element);
  38. this.options = _jquery.default.extend({}, defaults, options, this.$element.data());
  39. this.init();
  40. }
  41. babelHelpers.createClass(actionBtn, [{
  42. key: "init",
  43. value: function init() {
  44. this.showed = false;
  45. this.$toggle = this.$element.find(this.options.toggleSelector);
  46. this.$list = this.$element.find(this.options.listSelector);
  47. var self = this;
  48. if (this.options.trigger === 'hover') {
  49. this.$element.on('mouseenter', this.options.toggleSelector, function () {
  50. if (!self.showed) {
  51. self.show();
  52. }
  53. });
  54. this.$element.on('mouseleave', this.options.toggleSelector, function () {
  55. if (self.showed) {
  56. self.hide();
  57. }
  58. });
  59. } else {
  60. this.$element.on('click', this.options.toggleSelector, function () {
  61. if (self.showed) {
  62. self.hide();
  63. } else {
  64. self.show();
  65. }
  66. });
  67. }
  68. }
  69. }, {
  70. key: "show",
  71. value: function show() {
  72. if (!this.showed) {
  73. this.$element.addClass(this.options.activeClass);
  74. this.showed = true;
  75. this.options.onShow.call(this);
  76. }
  77. }
  78. }, {
  79. key: "hide",
  80. value: function hide() {
  81. if (this.showed) {
  82. this.$element.removeClass(this.options.activeClass);
  83. this.showed = false;
  84. this.options.onHide.call(this);
  85. }
  86. }
  87. }], [{
  88. key: "_jQueryInterface",
  89. value: function _jQueryInterface(options) {
  90. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  91. args[_key - 1] = arguments[_key];
  92. }
  93. if (typeof options === 'string') {
  94. var method = options;
  95. if (/^\_/.test(method)) {
  96. return false;
  97. } else if (/^(get)$/.test(method)) {
  98. var api = this.first().data(pluginName);
  99. if (api && typeof api[method] === 'function') {
  100. return api[method].apply(api, args);
  101. }
  102. } else {
  103. return this.each(function () {
  104. var api = _jquery.default.data(this, pluginName);
  105. if (api && typeof api[method] === 'function') {
  106. api[method].apply(api, args);
  107. }
  108. });
  109. }
  110. } else {
  111. return this.each(function () {
  112. if (!_jquery.default.data(this, pluginName)) {
  113. _jquery.default.data(this, pluginName, new actionBtn(this, options));
  114. }
  115. });
  116. }
  117. }
  118. }]);
  119. return actionBtn;
  120. }();
  121. _jquery.default.fn[pluginName] = actionBtn._jQueryInterface;
  122. _jquery.default.fn[pluginName].constructor = actionBtn;
  123. _jquery.default.fn[pluginName].noConflict = function () {
  124. _jquery.default.fn[pluginName] = window.JQUERY_NO_CONFLICT;
  125. return actionBtn._jQueryInterface;
  126. };
  127. var ActionBtn =
  128. /*#__PURE__*/
  129. function (_Plugin) {
  130. babelHelpers.inherits(ActionBtn, _Plugin);
  131. function ActionBtn() {
  132. babelHelpers.classCallCheck(this, ActionBtn);
  133. return babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(ActionBtn).apply(this, arguments));
  134. }
  135. babelHelpers.createClass(ActionBtn, [{
  136. key: "getName",
  137. value: function getName() {
  138. return pluginName;
  139. }
  140. }], [{
  141. key: "getDefaults",
  142. value: function getDefaults() {
  143. return defaults;
  144. }
  145. }]);
  146. return ActionBtn;
  147. }(_Plugin2.default);
  148. _Plugin2.default.register(pluginName, ActionBtn);
  149. var _default = ActionBtn;
  150. _exports.default = _default;
  151. });