/
translationEditor.js
325 lines (268 loc) · 10.8 KB
/
translationEditor.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
pimcore.registerNS("pimcore.settings.translation.editor");
pimcore.settings.translation.editor = Class.create({
initialize: function (context, field, translationType, editorType) {
Ext.WindowManager.each(function(window, idx, length) {
window.destroy();
});
this.field = field;
this.context = context;
let value = field.getValue();
let bbar = [];
if (editorType === 'wysiwyg') {
this.editableDivId = "translationeditor_" + uniqid();
var html = '<div class="pimcore_editable_wysiwyg" id="' + this.editableDivId + '" contenteditable="true"></div>';
var pConf = {
html: html,
border: true,
style: "margin-bottom: 10px",
height: '100%',
autoScroll: true
};
this.component = new Ext.Panel(pConf);
this.component.on("beforedestroy", function () {
if (this.ckeditor) {
this.ckeditor.destroy();
this.ckeditor = null;
}
}
);
this.component.on("afterlayout", this.initCkEditor.bind(this));
} else {
this.component = new Ext.form.TextArea({
width: '100%',
height: '100%',
value: value,
});
if(translationType === 'custom') {
bbar.push({
xtype: "displayfield",
value: t('symfony_translation_link')
});
}
}
bbar.push({
text: t("save"),
iconCls: 'pimcore_icon_save',
handler: function () {
let newValue = '';
if (editorType == "wysiwyg") {
try {
if (this.ckeditor) {
newValue = this.ckeditor.getData();
}
}
catch (e) {
}
} else {
newValue = this.component.getValue();
}
this.field.setValue(newValue);
this.context.setValueStatus(this.field, newValue);
this.editWin.close();
}.bind(this)
});
bbar.push({
text: t("cancel"),
iconCls: 'pimcore_icon_cancel',
handler: function () {
this.editWin.close();
}.bind(this)
});
this.editWin = new Ext.Window({
modal: false,
items: [this.component],
bodyStyle: "background: #fff; padding: 10px",
width: 700,
height: 400,
layout: 'fit',
closeAction: 'method-destroy',
autoScroll: true,
preventRefocus: true, // nasty hack because this is an internal property
// for html grid cell values with hrefs this prevents that the cell
// gets refocused which would then trigger another editor window
// upon close of this instance
bbar: bbar
});
this.editWin.show();
this.editWin.updateLayout();
},
destroy: function () {
if (this.editWin) {
this.editWin.destroy();
}
},
initCkEditor: function () {
if (this.ckeditor) {
return;
}
// add drop zone, use the parent panel here (container), otherwise this can cause problems when specifying a fixed height on the wysiwyg
var dd = new Ext.dd.DropZone(Ext.get(this.editableDivId).parent(), {
ddGroup: "element",
getTargetFromEvent: function(e) {
return this.getEl();
},
onNodeOver : function(target, dd, e, data) {
if (data.records.length == 1) {
var record = data.records[0];
data = record.data;
if (this.dndAllowed(data)) {
return Ext.dd.DropZone.prototype.dropAllowed;
}
}
return Ext.dd.DropZone.prototype.dropNotAllowed;
}.bind(this),
onNodeDrop : this.onNodeDrop.bind(this)
});
var eConfig = {};
eConfig.toolbarGroups = [
{name: 'basicstyles', groups: ['undo', 'find', 'basicstyles', 'list']},
'/',
{name: 'paragraph', groups: ['align', 'indent']},
{name: 'blocks'},
{name: 'links'},
{name: 'insert'},
'/',
{name: 'styles'},
{name: 'tools', groups: ['colors', 'tools', 'cleanup', 'mode', 'others']}
];
//prevent override important settings!
eConfig.resize_enabled = false;
eConfig.enterMode = CKEDITOR.ENTER_BR;
eConfig.entities = false;
eConfig.entities_greek = false;
eConfig.entities_latin = false;
eConfig.extraAllowedContent = "*[pimcore_type,pimcore_id]";
eConfig.baseFloatZIndex = 40000; // prevent that the editor gets displayed behind the grid cell editor window
if (eConfig.hasOwnProperty('removePlugins')) {
eConfig.removePlugins += ",tableresize";
}
else {
eConfig.removePlugins = "tableresize";
}
try {
this.ckeditor = CKEDITOR.inline(this.editableDivId, eConfig);
this.ckeditor.setData(this.field.getValue());
// disable URL field in image dialog
this.ckeditor.on("dialogShow", function (e) {
var urlField = e.data.getElement().findOne("input");
if (urlField && urlField.getValue()) {
if (urlField.getValue().indexOf("/image-thumbnails/") > 1) {
urlField.getParent().getParent().getParent().hide();
}
} else if (urlField) {
urlField.getParent().getParent().getParent().show();
}
});
} catch (e) {
console.log(e);
}
},
onNodeDrop: function (target, dd, e, data) {
if (!pimcore.helpers.dragAndDropValidateSingleItem(data)) {
return;
}
if (!this.ckeditor) {
return;
}
this.ckeditor.focus();
var node = data.records[0];
if (!this.ckeditor ||!this.dndAllowed(node.data)) {
return;
}
var wrappedText = node.data.text;
var textIsSelected = false;
try {
var selection = this.ckeditor.getSelection();
var bookmarks = selection.createBookmarks();
var range = selection.getRanges()[ 0 ];
var fragment = range.clone().cloneContents();
selection.selectBookmarks(bookmarks);
var retval = "";
var childList = fragment.getChildren();
var childCount = childList.count();
for (var i = 0; i < childCount; i++) {
var child = childList.getItem(i);
retval += ( child.getOuterHtml ?
child.getOuterHtml() : child.getText() );
}
if (retval.length > 0) {
wrappedText = retval;
textIsSelected = true;
}
}
catch (e2) {
}
// remove existing links out of the wrapped text
wrappedText = wrappedText.replace(/<\/?([a-z][a-z0-9]*)\b[^>]*>/gi, function ($0, $1) {
if($1.toLowerCase() == "a") {
return "";
}
return $0;
});
var id = node.data.id;
var uri = node.data.path;
var browserPossibleExtensions = ["jpg","jpeg","gif","png"];
if (node.data.elementType == "asset") {
if (node.data.type == "image" && textIsSelected == false) {
// images bigger than 600px or formats which cannot be displayed by the browser directly will be
// converted by the pimcore thumbnailing service so that they can be displayed in the editor
var defaultWidth = 600;
var additionalAttributes = "";
uri = Routing.generate('pimcore_admin_asset_getimagethumbnail') + "?id=" + id + "&width=" + defaultWidth + "&aspectratio=true";
if(typeof node.data.imageWidth != "undefined") {
if(node.data.imageWidth < defaultWidth
&& in_arrayi(pimcore.helpers.getFileExtension(node.data.text),
browserPossibleExtensions)) {
uri = node.data.path;
additionalAttributes += ' pimcore_disable_thumbnail="true"';
}
if(node.data.imageWidth < defaultWidth) {
defaultWidth = node.data.imageWidth;
}
}
this.ckeditor.insertHtml('<img src="' + uri + '" pimcore_type="asset" pimcore_id="' + id
+ '" style="width:' + defaultWidth + 'px;"' + additionalAttributes + ' />');
return true;
}
else {
this.ckeditor.insertHtml('<a href="' + uri + '" pimcore_type="asset" pimcore_id="'
+ id + '">' + wrappedText + '</a>');
return true;
}
}
if (node.data.elementType == "document" && (node.data.type=="page"
|| node.data.type=="hardlink" || node.data.type=="link")){
this.ckeditor.insertHtml('<a href="' + uri + '" pimcore_type="document" pimcore_id="'
+ id + '">' + wrappedText + '</a>');
return true;
}
if (node.data.elementType == "object"){
this.ckeditor.insertHtml('<a href="' + uri + '" pimcore_type="object" pimcore_id="'
+ id + '">' + wrappedText + '</a>');
return true;
}
},
dndAllowed: function(data) {
if (data.elementType == "document" && (data.type=="page"
|| data.type=="hardlink" || data.type=="link")){
return true;
} else if (data.elementType=="asset" && data.type != "folder"){
return true;
} else if (data.elementType=="object" && data.type != "folder"){
return true;
}
return false;
}
});