Skip to content

Commit 28d1af5

Browse files
author
Mateusz Krzeszowiak
committed
Initialize inline translations module only when they are enabled
1 parent 87b7cba commit 28d1af5

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

app/code/Magento/Translation/view/frontend/requirejs-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var config = {
1313
}
1414
},
1515
deps: [
16-
'mage/translate-inline',
1716
'mageTranslationDictionary'
1817
]
1918
};

lib/web/mage/translate-inline.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
define([
77
'jquery',
88
'mage/template',
9+
'mage/utils/misc',
10+
'mage/translate',
911
'jquery-ui-modules/dialog',
10-
'mage/translate'
11-
], function ($, mageTemplate) {
12+
], function ($, mageTemplate, miscUtils) {
1213
'use strict';
1314

1415
$.widget('mage.translateInline', $.ui.dialog, {
@@ -59,11 +60,12 @@ define([
5960
* Open.
6061
*/
6162
open: function () {
62-
var topMargin;
63+
var $uiDialog = $(this).closest('.ui-dialog'),
64+
topMargin = $uiDialog.children('.ui-dialog-titlebar').outerHeight() + 45;
6365

64-
$(this).closest('.ui-dialog').addClass('ui-dialog-active');
65-
topMargin = jQuery(this).closest('.ui-dialog').children('.ui-dialog-titlebar').outerHeight() + 45;
66-
jQuery(this).closest('.ui-dialog').css('margin-top', topMargin);
66+
$uiDialog
67+
.addClass('ui-dialog-active')
68+
.css('margin-top', topMargin)
6769
},
6870

6971
/**
@@ -79,11 +81,15 @@ define([
7981
* @protected
8082
*/
8183
_create: function () {
84+
var $translateArea = $(this.options.translateArea);
85+
86+
if (!$translateArea.length) {
87+
$translateArea = $('body');
88+
}
89+
$translateArea.on('edit.editTrigger', $.proxy(this._onEdit, this));
90+
8291
this.tmpl = mageTemplate(this.options.translateForm.template);
83-
(this.options.translateArea && $(this.options.translateArea).length ?
84-
$(this.options.translateArea) :
85-
this.element.closest('body'))
86-
.on('edit.editTrigger', $.proxy(this._onEdit, this));
92+
8793
this._super();
8894
},
8995

@@ -95,7 +101,7 @@ define([
95101
_prepareContent: function (templateData) {
96102
var data = $.extend({
97103
items: templateData,
98-
escape: $.mage.escapeHTML
104+
escape: miscUtils.escape,
99105
}, this.options.translateForm.data);
100106

101107
this.data = data;
@@ -131,16 +137,14 @@ define([
131137
* @protected
132138
*/
133139
_formSubmit: function () {
134-
var parameters;
140+
var parameters = $.param({
141+
area: this.options.area
142+
}) + '&' + $('#' + this.options.translateForm.data.id).serialize();
135143

136144
this.formIsSubmitted = true;
137-
parameters = $.param({
138-
area: this.options.area
139-
}) + '&' + $('#' + this.options.translateForm.data.id).serialize();
140145

141-
$.ajax({
146+
$.post({
142147
url: this.options.ajaxUrl,
143-
type: 'POST',
144148
data: parameters,
145149
loaderContext: this.element,
146150
showLoader: true
@@ -162,11 +166,13 @@ define([
162166
* @private
163167
*/
164168
_updatePlaceholder: function (newValue) {
165-
var target = jQuery(this.target);
169+
var $target = $(this.target),
170+
translateObject = $target.data('translate')[0];
171+
172+
translateObject.shown = newValue;
173+
translateObject.translated = newValue;
166174

167-
target.data('translate')[0].shown = newValue;
168-
target.data('translate')[0].translated = newValue;
169-
target.html(newValue);
175+
$target.html(newValue);
170176
},
171177

172178
/**
@@ -177,20 +183,6 @@ define([
177183
this._super();
178184
}
179185
});
180-
// @TODO move the "escapeHTML" method into the file with global utility functions
181-
$.extend(true, $, {
182-
mage: {
183-
/**
184-
* @param {String} str
185-
* @return {Boolean}
186-
*/
187-
escapeHTML: function (str) {
188-
return str ?
189-
jQuery('<div/>').text(str).html().replace(/"/g, '&quot;') :
190-
false;
191-
}
192-
}
193-
});
194186

195187
return $.mage.translateInline;
196188
});

lib/web/mage/utils/misc.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
define([
77
'underscore',
88
'jquery',
9-
'FormData'
10-
], function (_, $) {
9+
'mage/utils/objects'
10+
], function (_, $, utils) {
1111
'use strict';
1212

1313
var defaultAttributes,
@@ -120,7 +120,7 @@ define([
120120
*/
121121
submit: function (options, attrs) {
122122
var form = document.createElement('form'),
123-
data = this.serialize(options.data),
123+
data = utils.serialize(options.data),
124124
attributes = _.extend({}, defaultAttributes, attrs || {});
125125

126126
if (!attributes.action) {
@@ -205,11 +205,11 @@ define([
205205

206206
if (type === 'default') {
207207
formData = new FormData();
208-
_.each(this.serialize(data), function (val, name) {
208+
_.each(utils.serialize(data), function (val, name) {
209209
formData.append(name, val);
210210
});
211211
} else if (type === 'simple') {
212-
formData = this.serialize(data);
212+
formData = utils.serialize(data);
213213
}
214214

215215
return formData;
@@ -242,6 +242,16 @@ define([
242242
return data;
243243
},
244244

245+
/**
246+
* Replaces special characters with their corresponding HTML entities.
247+
*
248+
* @param {String} string Text to escape.
249+
* @returns {String} Escaped text.
250+
*/
251+
escape: function (string) {
252+
return string ? $('<p/>').text(str).html().replace(/"/g, '&quot;') : string;
253+
},
254+
245255
/**
246256
* Replaces symbol codes with their unescaped counterparts.
247257
*

0 commit comments

Comments
 (0)