You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

216 line
5.4 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @deprecated since version 2.2.0
  7. */
  8. define([
  9. 'jquery',
  10. 'mage/template',
  11. 'jquery-ui-modules/widget'
  12. ], function ($, mageTemplate) {
  13. 'use strict';
  14. var editTriggerPrototype;
  15. $.widget('mage.editTrigger', {
  16. options: {
  17. img: '',
  18. alt: '[TR]',
  19. template: '#translate-inline-icon',
  20. zIndex: 2000,
  21. editSelector: '[data-translate]',
  22. delay: 2000,
  23. offsetTop: -3,
  24. singleElement: true
  25. },
  26. /**
  27. * editTriger creation
  28. * @protected
  29. */
  30. _create: function () {
  31. this.tmpl = mageTemplate(this.options.template);
  32. this._initTrigger();
  33. this._bind();
  34. },
  35. /**
  36. * @return {Object}
  37. * @private
  38. */
  39. _getCss: function () {
  40. return {
  41. position: 'absolute',
  42. cursor: 'pointer',
  43. display: 'none',
  44. 'z-index': this.options.zIndex
  45. };
  46. },
  47. /**
  48. * @param {*} appendTo
  49. * @return {*|jQuery}
  50. * @private
  51. */
  52. _createTrigger: function (appendTo) {
  53. var tmpl = this.tmpl({
  54. data: this.options
  55. });
  56. return $(tmpl)
  57. .css(this._getCss())
  58. .data('role', 'edit-trigger-element')
  59. .appendTo(appendTo);
  60. },
  61. /**
  62. * @private
  63. */
  64. _initTrigger: function () {
  65. this.trigger = this._createTrigger($('body'));
  66. },
  67. /**
  68. * Bind on mousemove event
  69. * @protected
  70. */
  71. _bind: function () {
  72. this.trigger.on('click.' + this.widgetName, $.proxy(this._onClick, this));
  73. this.element.on('mousemove.' + this.widgetName, $.proxy(this._onMouseMove, this));
  74. },
  75. /**
  76. * Show editTriger
  77. */
  78. show: function () {
  79. if (this.trigger.is(':hidden')) {
  80. this.trigger.show();
  81. }
  82. },
  83. /**
  84. * Hide editTriger
  85. */
  86. hide: function () {
  87. this.currentTarget = null;
  88. if (this.trigger && this.trigger.is(':visible')) {
  89. this.trigger.hide();
  90. }
  91. },
  92. /**
  93. * Set editTriger position
  94. * @protected
  95. */
  96. _setPosition: function (el) {
  97. var offset = el.offset();
  98. this.trigger.css({
  99. top: offset.top + el.outerHeight() + this.options.offsetTop,
  100. left: offset.left
  101. });
  102. },
  103. /**
  104. * Show/hide trigger on mouse move.
  105. *
  106. * @param {jQuery.Event} e
  107. * @protected
  108. */
  109. _onMouseMove: function (e) {
  110. var target = $(e.target),
  111. inner = target.find(this.options.editSelector);
  112. if ($(e.target).is('button') && inner.length) {
  113. target = inner;
  114. } else if (!target.is(this.trigger) && !target.is(this.options.editSelector)) {
  115. target = target.parents(this.options.editSelector).first();
  116. }
  117. if (target.length) {
  118. if (!target.is(this.trigger)) {
  119. this._setPosition(target);
  120. this.currentTarget = target;
  121. }
  122. this.show();
  123. } else {
  124. this.hide();
  125. }
  126. },
  127. /**
  128. * Trigger event "edit" on element for translate.
  129. *
  130. * @param {jQuery.Event} e
  131. * @protected
  132. */
  133. _onClick: function (e) {
  134. e.preventDefault();
  135. e.stopImmediatePropagation();
  136. $(this.currentTarget).trigger('edit.' + this.widgetName);
  137. this.hide(true);
  138. },
  139. /**
  140. * Destroy editTriger
  141. */
  142. destroy: function () {
  143. this.trigger.remove();
  144. this.element.off('.' + this.widgetName);
  145. return $.Widget.prototype.destroy.call(this);
  146. }
  147. });
  148. /**
  149. * Extention for widget editTrigger - hide trigger with delay
  150. */
  151. editTriggerPrototype = $.mage.editTrigger.prototype;
  152. $.widget('mage.editTrigger', $.extend({}, editTriggerPrototype, {
  153. /**
  154. * Added clear timeout on trigger show
  155. */
  156. show: function () {
  157. editTriggerPrototype.show.apply(this, arguments);
  158. if (this.options.delay) {
  159. this._clearTimer();
  160. }
  161. },
  162. /**
  163. * Added setTimeout on trigger hide
  164. */
  165. hide: function (immediate) {
  166. if (!immediate && this.options.delay) {
  167. if (!this.timer) {
  168. this.timer = setTimeout($.proxy(function () {
  169. editTriggerPrototype.hide.apply(this, arguments);
  170. this._clearTimer();
  171. }, this), this.options.delay);
  172. }
  173. } else {
  174. editTriggerPrototype.hide.apply(this, arguments);
  175. }
  176. },
  177. /**
  178. * Clear timer
  179. * @protected
  180. */
  181. _clearTimer: function () {
  182. if (this.timer) {
  183. clearTimeout(this.timer);
  184. this.timer = null;
  185. }
  186. }
  187. }));
  188. return $.mage.editTrigger;
  189. });