translate-inline.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. (function (root, factory) {
  6. 'use strict';
  7. if (typeof define === 'function' && define.amd) {
  8. define([
  9. 'jquery',
  10. 'mage/template',
  11. 'jquery/ui',
  12. 'mage/translate'
  13. ], factory);
  14. } else {
  15. factory(root.jQuery, root.mageTemplate);
  16. }
  17. }(this, function ($, mageTemplate) {
  18. 'use strict';
  19. $.widget('mage.translateInline', $.ui.dialog, {
  20. options: {
  21. translateForm: {
  22. template: '#translate-form-template',
  23. data: {
  24. id: 'translate-inline-form',
  25. message: 'Please refresh the page to see your changes after submitting this form.'
  26. }
  27. },
  28. autoOpen: false,
  29. translateArea: null,
  30. modal: true,
  31. dialogClass: 'popup-window',
  32. width: '75%',
  33. title: $.mage.__('Translate'),
  34. height: 470,
  35. position: {
  36. my: 'left top',
  37. at: 'center top',
  38. of: 'body'
  39. },
  40. buttons: [{
  41. text: $.mage.__('Submit'),
  42. 'class': 'action-primary',
  43. /**
  44. * Click
  45. */
  46. click: function () {
  47. $(this).translateInline('submit');
  48. }
  49. },
  50. {
  51. text: $.mage.__('Close'),
  52. 'class': 'action-close',
  53. /**
  54. * Click.
  55. */
  56. click: function () {
  57. $(this).translateInline('close');
  58. }
  59. }],
  60. /**
  61. * Open.
  62. */
  63. open: function () {
  64. var topMargin;
  65. $(this).closest('.ui-dialog').addClass('ui-dialog-active');
  66. topMargin = jQuery(this).closest('.ui-dialog').children('.ui-dialog-titlebar').outerHeight() + 45;
  67. jQuery(this).closest('.ui-dialog').css('margin-top', topMargin);
  68. },
  69. /**
  70. * Close.
  71. */
  72. close: function () {
  73. $(this).closest('.ui-dialog').removeClass('ui-dialog-active');
  74. }
  75. },
  76. /**
  77. * Translate Inline creation
  78. * @protected
  79. */
  80. _create: function () {
  81. this.tmpl = mageTemplate(this.options.translateForm.template);
  82. (this.options.translateArea && $(this.options.translateArea).length ?
  83. $(this.options.translateArea) :
  84. this.element.closest('body'))
  85. .on('edit.editTrigger', $.proxy(this._onEdit, this));
  86. this._super();
  87. },
  88. /**
  89. * @param {*} templateData
  90. * @return {*|jQuery|HTMLElement}
  91. * @private
  92. */
  93. _prepareContent: function (templateData) {
  94. var data = $.extend({
  95. items: templateData,
  96. escape: $.mage.escapeHTML
  97. }, this.options.translateForm.data);
  98. this.data = data;
  99. return $(this.tmpl({
  100. data: data
  101. }));
  102. },
  103. /**
  104. * Render translation form and open dialog
  105. * @param {Object} e - object
  106. * @protected
  107. */
  108. _onEdit: function (e) {
  109. this.target = e.target;
  110. this.element.html(this._prepareContent($(e.target).data('translate')));
  111. this.open(e);
  112. },
  113. /**
  114. * Submit.
  115. */
  116. submit: function () {
  117. if (this.formIsSubmitted) {
  118. return;
  119. }
  120. this._formSubmit();
  121. },
  122. /**
  123. * Send ajax request on form submit
  124. * @protected
  125. */
  126. _formSubmit: function () {
  127. var parameters;
  128. this.formIsSubmitted = true;
  129. parameters = $.param({
  130. area: this.options.area
  131. }) + '&' + $('#' + this.options.translateForm.data.id).serialize();
  132. $.ajax({
  133. url: this.options.ajaxUrl,
  134. type: 'POST',
  135. data: parameters,
  136. loaderContext: this.element,
  137. showLoader: true
  138. }).complete($.proxy(this._formSubmitComplete, this));
  139. },
  140. /**
  141. * @param {Object} response
  142. * @private
  143. */
  144. _formSubmitComplete: function (response) {
  145. this.close();
  146. this.formIsSubmitted = false;
  147. this._updatePlaceholder(response.responseJSON[this.data.items[0].original]);
  148. },
  149. /**
  150. * @param {*} newValue
  151. * @private
  152. */
  153. _updatePlaceholder: function (newValue) {
  154. var target = jQuery(this.target);
  155. target.data('translate')[0].shown = newValue;
  156. target.data('translate')[0].translated = newValue;
  157. target.html(newValue);
  158. },
  159. /**
  160. * Destroy translateInline
  161. */
  162. destroy: function () {
  163. this.element.off('.editTrigger');
  164. this._super();
  165. }
  166. });
  167. // @TODO move the "escapeHTML" method into the file with global utility functions
  168. $.extend(true, $, {
  169. mage: {
  170. /**
  171. * @param {String} str
  172. * @return {Boolean}
  173. */
  174. escapeHTML: function (str) {
  175. return str ?
  176. jQuery('<div/>').text(str).html().replace(/"/g, '&quot;') :
  177. false;
  178. }
  179. }
  180. });
  181. return $.mage.translateInline;
  182. }));