edit-trigger.js 5.6 KB

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