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