Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

195 строки
5.3 KiB

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