translate-inline-vde.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. 'mage/translate-inline',
  16. 'mage/translate'
  17. ], factory);
  18. } else {
  19. factory(root.jQuery, root.mageTemplate);
  20. }
  21. }(this, function ($, mageTemplate) {
  22. 'use strict';
  23. /**
  24. * Widget for a dialog to edit translations.
  25. */
  26. $.widget('mage.translateInlineDialogVde', $.mage.translateInline, {
  27. options: {
  28. translateForm: {
  29. template: '#translate-inline-dialog-form-template',
  30. data: {
  31. selector: '#translate-inline-dialog-form'
  32. }
  33. },
  34. dialogClass: 'translate-dialog',
  35. draggable: false,
  36. modal: false,
  37. resizable: false,
  38. height: 'auto',
  39. minHeight: 0,
  40. buttons: [{
  41. text: $.mage.__('Cancel'),
  42. 'class': 'translate-dialog-cancel',
  43. /**
  44. * Click.
  45. */
  46. click: function () {
  47. $(this).translateInlineDialogVde('close');
  48. }
  49. },
  50. {
  51. text: $.mage.__('Save'),
  52. 'class': 'translate-dialog-save',
  53. /**
  54. * Click.
  55. */
  56. click: function () {
  57. $(this).translateInlineDialogVde('submit');
  58. }
  59. }],
  60. area: 'vde',
  61. ajaxUrl: null,
  62. textTranslations: null,
  63. imageTranslations: null,
  64. scriptTranslations: null,
  65. translateMode: null,
  66. translateModes: ['text', 'script', 'alt']
  67. },
  68. /**
  69. * Identifies if the form is already being submitted.
  70. *
  71. * @type {Boolean}
  72. */
  73. isSubmitting: false,
  74. /**
  75. * Identifies if inline text is being editied. Only one element can be edited at a time.
  76. *
  77. * @type {Boolean}
  78. */
  79. isBeingEdited: false,
  80. /**
  81. * Creates the translation dialog widget. Fulfills jQuery WidgetFactory _create hook.
  82. */
  83. _create: function () {
  84. this._super();
  85. // Unbind previously bound events that may be present from previous loads of vde container.
  86. if (parent && parent.jQuery) {
  87. parent.jQuery('[data-frame="editor"]')
  88. .off('modeChange')
  89. .on('modeChange', $.proxy(this._checkTranslateEditing, this));
  90. }
  91. },
  92. /**
  93. * @param {jQuery.Event} e
  94. * @param {Object} widget
  95. * @param {Function} callback
  96. */
  97. openWithWidget: function (e, widget, callback) {
  98. if (widget && callback) {
  99. this.callback = callback;
  100. this.element.html(this._prepareContent($(widget.element).data('translate')));
  101. this.triggerElement = widget.element;
  102. $(window).on('resize.translateInlineDialogVde', $.proxy(this._positionNearTarget, this));
  103. this._positionNearTarget();
  104. }
  105. this.open(arguments);
  106. },
  107. /**
  108. * @private
  109. */
  110. _positionNearTarget: function () {
  111. this.option('position', {
  112. of: this.triggerElement,
  113. my: 'left top',
  114. at: 'left-3 top-9'
  115. });
  116. this.option('width', $(this.triggerElement).width());
  117. },
  118. /**
  119. * Close.
  120. */
  121. close: function () {
  122. this._super();
  123. this._onCancel();
  124. this.isBeingEdited = false;
  125. $(window).off('resize.translateInlineVdeDialog');
  126. },
  127. /**
  128. * Shows translate mode applicable css styles.
  129. */
  130. toggleStyle: function (mode) {
  131. // TODO: need to remove eventually
  132. this._toggleOutline(mode);
  133. this.options.textTranslations.translateInlineVde('toggleIcon', mode);
  134. this.options.imageTranslations.translateInlineImageVde('toggleIcon', mode);
  135. this.options.scriptTranslations.translateInlineScriptVde('toggleIcon', mode);
  136. },
  137. /**
  138. * Determine if user has modified inline translation text, but has not saved it.
  139. */
  140. _checkTranslateEditing: function (event, data) {
  141. var url, dataDisable;
  142. if (this.isBeingEdited) {
  143. alert(data['alert_message']); //eslint-disable-line no-alert
  144. data['is_being_edited'] = true;
  145. } else {
  146. // Disable inline translation.
  147. url = parent.jQuery('[data-frame="editor"]').attr('src');
  148. dataDisable = {
  149. frameUrl: url.substring(0, url.indexOf('translation_mode')),
  150. mode: this.options.translateMode
  151. };
  152. parent.jQuery('[vde-translate]').trigger('disableInlineTranslation', dataDisable);
  153. // Inline translation text is not being edited. Continue on.
  154. parent.jQuery('[data-frame="editor"]').trigger(data['next_action'], data);
  155. }
  156. },
  157. /**
  158. * @return {*}
  159. * @private
  160. */
  161. _prepareContent: function () {
  162. var content = this._superApply(arguments);
  163. this._on(content.find('textarea[data-translate-input-index]'), {
  164. /**
  165. * @param {jQuery.Event} e
  166. */
  167. keydown: function (e) {
  168. var keyCode = $.ui.keyCode;
  169. switch (e.keyCode) {
  170. case keyCode.ESCAPE:
  171. e.preventDefault();
  172. this.close();
  173. break;
  174. case keyCode.ENTER:
  175. e.preventDefault();
  176. this._formSubmit();
  177. break;
  178. default:
  179. // keep track of the fact that translate text has been changed
  180. this.isBeingEdited = true;
  181. }
  182. }
  183. });
  184. this._on(content.find(this.options.translateForm.data.selector), {
  185. /**
  186. * @param {jQuery.Event} e
  187. * @return {Boolean}
  188. */
  189. submit: function (e) {
  190. e.preventDefault();
  191. this._formSubmit();
  192. return true;
  193. }
  194. });
  195. return content;
  196. },
  197. /**
  198. * Submits the form.
  199. */
  200. _formSubmit: function () {
  201. this._superApply(arguments);
  202. this.isBeingEdited = false;
  203. },
  204. /**
  205. * Callback for when the AJAX call in _formSubmit is completed.
  206. */
  207. _formSubmitComplete: function () {
  208. // TODO: need to replace with merged version
  209. var self = this;
  210. this.element.find('[data-translate-input-index]').each($.proxy(function (count, elem) {
  211. var index = $(elem).data('translate-input-index'),
  212. value = $(elem).val() || '';
  213. self.callback(index, value);
  214. self = null;
  215. }, this));
  216. $(window).off('resize.translateInlineVdeDialog');
  217. this._onSubmitComplete();
  218. this._superApply(arguments);
  219. this.isSubmitting = false;
  220. },
  221. /**
  222. * @param {*} mode
  223. * @private
  224. */
  225. _toggleOutline: function (mode) {
  226. var that = this;
  227. // TODO: need to remove eventually
  228. if (mode == null) {
  229. mode = this.options.translateMode;
  230. } else {
  231. this.options.translateMode = mode;
  232. }
  233. this.element.closest('[data-container="body"]').addClass('trnslate-inline-' + mode + '-area');
  234. $.each(this.options.translateModes, function () {
  235. if (this != mode) { //eslint-disable-line eqeqeq
  236. that.element.closest('[data-container="body"]').removeClass('trnslate-inline-' + this + '-area');
  237. }
  238. });
  239. },
  240. /**
  241. * @private
  242. */
  243. _onCancel: function () {
  244. // TODO: need to remove eventually
  245. this._toggleOutline();
  246. this.options.textTranslations.translateInlineVde('show');
  247. this.options.imageTranslations.translateInlineImageVde('show');
  248. this.options.scriptTranslations.translateInlineScriptVde('show');
  249. },
  250. /**
  251. * @private
  252. */
  253. _onSubmitComplete: function () {
  254. // TODO: need to remove eventually
  255. this._toggleOutline();
  256. this.options.textTranslations.translateInlineVde('show');
  257. this.options.imageTranslations.translateInlineImageVde('show');
  258. this.options.scriptTranslations.translateInlineScriptVde('show');
  259. }
  260. });
  261. /**
  262. * Widget for an icon to be displayed indicating that text can be translated.
  263. */
  264. $.widget('mage.translateInlineVde', {
  265. options: {
  266. iconTemplateSelector: '[data-template="translate-inline-icon"]',
  267. img: null,
  268. imgHover: null,
  269. offsetLeft: -16,
  270. dataAttrName: 'translate',
  271. translateMode: null,
  272. /**
  273. * On click.
  274. */
  275. onClick: function () {}
  276. },
  277. /**
  278. * Elements to wrap instead of just inserting a child element. This is
  279. * to work around some different behavior in Firefox vs. WebKit.
  280. *
  281. * @type {Array}
  282. */
  283. elementsToWrap: ['button'],
  284. /**
  285. * Determines if the template is already appended to the element.
  286. *
  287. * @type {Boolean}
  288. */
  289. isTemplateAttached: false,
  290. iconTemplate: null,
  291. iconWrapperTemplate: null,
  292. elementWrapperTemplate: null,
  293. /**
  294. * Determines if the element is suppose to be wrapped or just attached.
  295. *
  296. * @type {Boolean}, null is unset, false/true is set
  297. */
  298. isElementWrapped: null,
  299. /**
  300. * Creates the icon widget to indicate text that can be translated.
  301. * Fulfills jQuery's WidgetFactory _create hook.
  302. */
  303. _create: function () {
  304. this.element.addClass('translate-edit-icon-container');
  305. this._initTemplates();
  306. this.show();
  307. },
  308. /**
  309. * Shows the widget.
  310. */
  311. show: function () {
  312. this._attachIcon();
  313. this.iconTemplate.removeClass('hidden');
  314. if (this.element.data('translateMode') != this.options.translateMode) { //eslint-disable-line eqeqeq
  315. this.iconTemplate.addClass('hidden');
  316. }
  317. this.element.on('dblclick', $.proxy(this._invokeAction, this));
  318. this._disableElementClicks();
  319. },
  320. /**
  321. * Show edit icon for given translate mode.
  322. */
  323. toggleIcon: function (mode) {
  324. if (mode == this.element.data('translateMode')) { //eslint-disable-line eqeqeq
  325. this.iconTemplate.removeClass('hidden');
  326. } else {
  327. this.iconTemplate.addClass('hidden');
  328. }
  329. this.options.translateMode = mode;
  330. },
  331. /**
  332. * Determines if the element should have an icon element wrapped around it or
  333. * if an icon element should be added as a child element.
  334. */
  335. _shouldWrap: function () {
  336. var c;
  337. if (this.isElementWrapped !== null) {
  338. return this.isElementWrapped;
  339. }
  340. this.isElementWrapped = false;
  341. for (c = 0; c < this.elementsToWrap.length; c++) {
  342. if (this.element.is(this.elementsToWrap[c])) {
  343. this.isElementWrapped = true;
  344. break;
  345. }
  346. }
  347. return this.isElementWrapped;
  348. },
  349. /**
  350. * Attaches an icon to the widget's element.
  351. */
  352. _attachIcon: function () {
  353. if (this._shouldWrap()) {
  354. if (!this.isTemplateAttached) {
  355. this.iconWrapperTemplate = this.iconTemplate.wrap('<div/>').parent();
  356. this.iconWrapperTemplate.addClass('translate-edit-icon-wrapper-text');
  357. this.elementWrapperTemplate = this.element.wrap('<div/>').parent();
  358. this.elementWrapperTemplate.addClass('translate-edit-icon-container');
  359. this.iconTemplate.appendTo(this.iconWrapperTemplate);
  360. this.iconWrapperTemplate.appendTo(this.elementWrapperTemplate);
  361. }
  362. } else {
  363. this.iconTemplate.appendTo(this.element);
  364. this.element.removeClass('invisible');
  365. }
  366. this.isTemplateAttached = true;
  367. },
  368. /**
  369. * Disables the element click from actually performing a click.
  370. */
  371. _disableElementClicks: function () {
  372. this.element.find('a').off('click');
  373. if (this.element.is('A')) {
  374. this.element.on('click', function () {
  375. return false;
  376. });
  377. }
  378. },
  379. /**
  380. * Hides the widget.
  381. */
  382. hide: function () {
  383. this.element.off('dblclick');
  384. this.iconTemplate.addClass('hidden');
  385. },
  386. /**
  387. * Replaces the translated text inside the widget with the new value.
  388. */
  389. replaceText: function (index, value) {
  390. var translateData = this.element.data(this.options.dataAttrName),
  391. innerHtmlStr = this.element.html();
  392. if (value === null || value === '') {
  393. value = '&nbsp;';
  394. }
  395. innerHtmlStr = innerHtmlStr.replace(translateData[index].shown, value);
  396. this.element.html(innerHtmlStr);
  397. translateData[index].shown = value;
  398. translateData[index].translated = value;
  399. this.element.data(this.options.dataAttrName, translateData);
  400. },
  401. /**
  402. * Initializes all the templates for the widget.
  403. */
  404. _initTemplates: function () {
  405. this._initIconTemplate();
  406. this.iconTemplate.addClass('translate-edit-icon-text');
  407. },
  408. /**
  409. * Changes depending on hover action.
  410. */
  411. _hoverIcon: function () {
  412. if (this.options.imgHover) {
  413. this.iconTemplate.prop('src', this.options.imgHover);
  414. }
  415. },
  416. /**
  417. * Changes depending on hover action.
  418. */
  419. _unhoverIcon: function () {
  420. if (this.options.imgHover) {
  421. this.iconTemplate.prop('src', this.options.img);
  422. }
  423. },
  424. /**
  425. * Initializes the icon template for the widget. Sets the widget up to
  426. * respond to events.
  427. */
  428. _initIconTemplate: function () {
  429. this.iconTemplate = $(mageTemplate(this.options.iconTemplateSelector, {
  430. data: this.options
  431. }));
  432. this.iconTemplate.on('click', $.proxy(this._invokeAction, this))
  433. .on('mouseover', $.proxy(this._hoverIcon, this))
  434. .on('mouseout', $.proxy(this._unhoverIcon, this));
  435. },
  436. /**
  437. * Invokes the action (e.g. activate the inline dialog)
  438. */
  439. _invokeAction: function (event) {
  440. this._detachIcon();
  441. this.options.onClick(event, this);
  442. },
  443. /**
  444. * Destroys the widget. Fulfills jQuery's WidgetFactory _destroy hook.
  445. */
  446. _destroy: function () {
  447. this.iconTemplate.remove();
  448. this._detachIcon();
  449. },
  450. /**
  451. * Detaches an icon from the widget's element.
  452. */
  453. _detachIcon: function () {
  454. this._unhoverIcon();
  455. $(this.iconTemplate).detach();
  456. if (this._shouldWrap()) {
  457. this.iconWrapperTemplate.remove();
  458. this.element.unwrap();
  459. this.elementWrapperTemplate.remove();
  460. } else {
  461. this.element.addClass('invisible');
  462. }
  463. this.isTemplateAttached = false;
  464. }
  465. });
  466. $.widget('mage.translateInlineImageVde', $.mage.translateInlineVde, {
  467. /**
  468. * @private
  469. */
  470. _attachIcon: function () {
  471. if (!this.isTemplateAttached) {
  472. this.iconWrapperTemplate = this.iconTemplate.wrap('<div/>').parent();
  473. this.iconWrapperTemplate.addClass('translate-edit-icon-wrapper-image');
  474. this.elementWrapperTemplate = this.element.wrap('<div/>').parent();
  475. this.elementWrapperTemplate.addClass('translate-edit-icon-container');
  476. this.iconTemplate.appendTo(this.iconWrapperTemplate);
  477. this.iconWrapperTemplate.appendTo(this.elementWrapperTemplate);
  478. this.isTemplateAttached = true;
  479. }
  480. },
  481. /**
  482. * @private
  483. */
  484. _initTemplates: function () {
  485. this._initIconTemplate();
  486. this.iconTemplate.addClass('translate-edit-icon-image');
  487. },
  488. /**
  489. * @private
  490. */
  491. _detachIcon: function () {
  492. $(this.iconTemplate).detach();
  493. this.iconWrapperTemplate.remove();
  494. this.element.unwrap();
  495. this.elementWrapperTemplate.remove();
  496. this.isTemplateAttached = false;
  497. }
  498. });
  499. $.widget('mage.translateInlineScriptVde', $.mage.translateInlineVde, {});
  500. /*
  501. * @TODO move the "escapeHTML" method into the file with global utility functions
  502. */
  503. $.extend(true, $, {
  504. mage: {
  505. /**
  506. * @param {String} str
  507. * @return {String}
  508. */
  509. escapeHTML: function (str) {
  510. return str ? str.replace(/"/g, '&quot;') : '';
  511. }
  512. }
  513. });
  514. }));