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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/* global setLocation, Base64, updateElementAtCursor, varienGlobalEvents */
/* eslint-disable strict */
define([
'jquery',
'wysiwygAdapter',
'Magento_Ui/js/modal/alert',
'jquery/ui',
'mage/translate',
'mage/mage',
'mage/validation',
'mage/adminhtml/events',
'prototype',
'Magento_Ui/js/modal/modal'
], function (jQuery, wysiwyg, alert) {
var widgetTools = {
/**
* Sets the widget to be active and is the scope of the slide out if the value is set
*/
activeSelectedNode: null,
editMode: false,
cursorLocation: 0,
/**
* Set active selected node.
*
* @param {Object} activeSelectedNode
*/
setActiveSelectedNode: function (activeSelectedNode) {
this.activeSelectedNode = activeSelectedNode;
},
/**
* Get active selected node.
*
* @returns {null}
*/
getActiveSelectedNode: function () {
return this.activeSelectedNode;
},
/**
*
* @param {Boolean} editMode
*/
setEditMode: function (editMode) {
this.editMode = editMode;
},
/**
* @param {*} id
* @param {*} html
* @return {String}
*/
getDivHtml: function (id, html) {
if (!html) {
html = '';
}
return '<div id="' + id + '">' + html + '</div>';
},
/**
* @param {Object} transport
*/
onAjaxSuccess: function (transport) {
var response;
if (transport.responseText.isJSON()) {
response = transport.responseText.evalJSON();
if (response.error) {
throw response;
} else if (response.ajaxExpired && response.ajaxRedirect) {
setLocation(response.ajaxRedirect);
}
}
},
dialogOpened: false,
/**
* @return {Number}
*/
getMaxZIndex: function () {
var max = 0,
cn = document.body.childNodes,
i, el, zIndex;
for (i = 0; i < cn.length; i++) {
el = cn[i];
zIndex = el.nodeType == 1 ? parseInt(el.style.zIndex, 10) || 0 : 0; //eslint-disable-line eqeqeq
if (zIndex < 10000) {
max = Math.max(max, zIndex);
}
}
return max + 10;
},
/**
* @param {String} widgetUrl
*/
openDialog: function (widgetUrl) {
var oThis = this,
title = 'Insert Widget',
mode = 'new',
dialog;
if (this.editMode) {
title = 'Edit Widget';
mode = 'edit';
}
if (this.dialogOpened) {
return;
}
this.dialogWindow = jQuery('<div/>').modal({
title: jQuery.mage.__(title),
type: 'slide',
buttons: [],
/**
* Opened.
*/
opened: function () {
dialog = jQuery(this).addClass('loading magento-message');
widgetUrl += 'mode/' + mode;
new Ajax.Updater($(this), widgetUrl, {
evalScripts: true,
/**
* On complete.
*/
onComplete: function () {
dialog.removeClass('loading');
}
});
},
/**
* @param {jQuery.Event} e
* @param {Object} modal
*/
closed: function (e, modal) {
modal.modal.remove();
oThis.dialogOpened = false;
}
});
this.dialogOpened = true;
this.dialogWindow.modal('openModal');
}
},
WysiwygWidget = {};
WysiwygWidget.Widget = Class.create();
WysiwygWidget.Widget.prototype = {
/**
* @param {HTMLElement} formEl
* @param {HTMLElement} widgetEl
* @param {*} widgetOptionsEl
* @param {*} optionsSourceUrl
* @param {*} widgetTargetId
*/
initialize: function (formEl, widgetEl, widgetOptionsEl, optionsSourceUrl, widgetTargetId) {
$(formEl).insert({
bottom: widgetTools.getDivHtml(widgetOptionsEl)
});
this.formEl = formEl;
this.widgetEl = $(widgetEl);
this.widgetOptionsEl = $(widgetOptionsEl);
this.optionsUrl = optionsSourceUrl;
this.optionValues = new Hash({});
this.widgetTargetId = widgetTargetId;
if (typeof wysiwyg != 'undefined' && wysiwyg.activeEditor()) { //eslint-disable-line eqeqeq
this.bMark = wysiwyg.activeEditor().selection.getBookmark();
}
// disable -- Please Select -- option from being re-selected
this.widgetEl.querySelector('option').setAttribute('disabled', 'disabled');
Event.observe(this.widgetEl, 'change', this.loadOptions.bind(this));
this.initOptionValues();
},
/**
* @return {String}
*/
getOptionsContainerId: function () {
return this.widgetOptionsEl.id + '_' + this.widgetEl.value.gsub(/\//, '_');
},
/**
* @param {*} containerId
*/
switchOptionsContainer: function (containerId) {
$$('#' + this.widgetOptionsEl.id + ' div[id^=' + this.widgetOptionsEl.id + ']').each(function (e) {
this.disableOptionsContainer(e.id);
}.bind(this));
if (containerId != undefined) { //eslint-disable-line eqeqeq
this.enableOptionsContainer(containerId);
}
this._showWidgetDescription();
},
/**
* @param {*} containerId
*/
enableOptionsContainer: function (containerId) {
$$('#' + containerId + ' .widget-option').each(function (e) {
e.removeClassName('skip-submit');
if (e.hasClassName('obligatory')) {
e.removeClassName('obligatory');
e.addClassName('required-entry');
}
});
$(containerId).removeClassName('no-display');
},
/**
* @param {*} containerId
*/
disableOptionsContainer: function (containerId) {
if ($(containerId).hasClassName('no-display')) {
return;
}
$$('#' + containerId + ' .widget-option').each(function (e) {
// Avoid submitting fields of unactive container
if (!e.hasClassName('skip-submit')) {
e.addClassName('skip-submit');
}
// Form validation workaround for unactive container
if (e.hasClassName('required-entry')) {
e.removeClassName('required-entry');
e.addClassName('obligatory');
}
});
$(containerId).addClassName('no-display');
},
/**
* Assign widget options values when existing widget selected in WYSIWYG.
*
* @return {Boolean}
*/
initOptionValues: function () {
var e, widgetCode;
if (!this.wysiwygExists()) {
return false;
}
e = this.getWysiwygNode();
if (e.localName === 'span') {
e = e.firstElementChild;
}
if (e != undefined && e.id) { //eslint-disable-line eqeqeq
// attempt to Base64-decode id on selected node; exception is thrown if it is in fact not a widget node
try {
widgetCode = Base64.idDecode(e.id);
} catch (ex) {
return false;
}
if (widgetCode.indexOf('{{widget') !== -1) {
this.optionValues = new Hash({});
widgetCode.gsub(/([a-z0-9\_]+)\s*\=\s*[\"]{1}([^\"]+)[\"]{1}/i, function (match) {
if (match[1] == 'type') { //eslint-disable-line eqeqeq
this.widgetEl.value = match[2];
} else {
this.optionValues.set(match[1], match[2]);
}
}.bind(this));
this.loadOptions();
}
}
},
/**
* Load options.
*/
loadOptions: function () {
var optionsContainerId,
params,
msg,
msgTmpl,
$wrapper,
typeName = this.optionValues.get('type_name');
if (!this.widgetEl.value) {
if (typeName) {
msgTmpl = jQuery.mage.__('The widget %1 is no longer available. Select a different widget.');
msg = jQuery.mage.__(msgTmpl).replace('%1', typeName);
jQuery('body').notification('clear').notification('add', {
error: true,
message: msg,
/**
* @param {String} message
*/
insertMethod: function (message) {
$wrapper = jQuery('<div/>').html(message);
$wrapper.insertAfter('.modal-slide .page-main-actions');
}
});
}
this.switchOptionsContainer();
return;
}
optionsContainerId = this.getOptionsContainerId();
if ($(optionsContainerId) != undefined) { //eslint-disable-line eqeqeq
this.switchOptionsContainer(optionsContainerId);
return;
}
this._showWidgetDescription();
params = {
'widget_type': this.widgetEl.value,
values: this.optionValues
};
new Ajax.Request(this.optionsUrl, {
parameters: {
widget: Object.toJSON(params)
},
/**
* On success.
*/
onSuccess: function (transport) {
try {
widgetTools.onAjaxSuccess(transport);
this.switchOptionsContainer();
if ($(optionsContainerId) == undefined) { //eslint-disable-line eqeqeq
this.widgetOptionsEl.insert({
bottom: widgetTools.getDivHtml(optionsContainerId, transport.responseText)
});
} else {
this.switchOptionsContainer(optionsContainerId);
}
} catch (e) {
alert({
content: e.message
});
}
}.bind(this)
});
},
/**
* @private
*/
_showWidgetDescription: function () {
var noteCnt = this.widgetEl.next().down('small'),
descrCnt = $('widget-description-' + this.widgetEl.selectedIndex),
description;
if (noteCnt != undefined) { //eslint-disable-line eqeqeq
description = descrCnt != undefined ? descrCnt.innerHTML : ''; //eslint-disable-line eqeqeq
noteCnt.update(description);
}
},
/**
* Validate field.
*/
validateField: function () {
jQuery(this.widgetEl).valid();
jQuery('#insert_button').removeClass('disabled');
},
/**
* Closes the modal
*/
closeModal: function () {
widgetTools.dialogWindow.modal('closeModal');
},
/* eslint-disable max-depth*/
/**
* Insert widget.
*/
insertWidget: function () {
var validationResult,
$form = jQuery('#' + this.formEl),
formElements,
i,
params,
editor,
activeNode;
// remove cached validator instance, which caches elements to validate
jQuery.data($form[0], 'validator', null);
$form.validate({
/**
* Ignores elements with .skip-submit, .no-display ancestor elements
*/
ignore: function () {
return jQuery(this).closest('.skip-submit, .no-display').length;
},
errorClass: 'mage-error'
});
validationResult = $form.valid();
if (validationResult) {
formElements = [];
i = 0;
Form.getElements($(this.formEl)).each(function (e) {
if (!e.hasClassName('skip-submit')) {
formElements[i] = e;
i++;
}
});
// Add as_is flag to parameters if wysiwyg editor doesn't exist
params = Form.serializeElements(formElements);
if (!this.wysiwygExists()) {
params += '&as_is=1';
}
new Ajax.Request($(this.formEl).action, {
parameters: params,
onComplete: function (transport) {
try {
editor = wysiwyg.activeEditor();
widgetTools.onAjaxSuccess(transport);
widgetTools.dialogWindow.modal('closeModal');
if (editor) {
editor.focus();
activeNode = widgetTools.getActiveSelectedNode();
if (activeNode) {
editor.selection.select(activeNode);
editor.selection.setContent(transport.responseText);
} else if (this.bMark) {
wysiwyg.activeEditor().selection.moveToBookmark(this.bMark);
}
}
if (!activeNode) {
this.updateContent(transport.responseText);
}
} catch (e) {
alert({
content: e.message
});
}
}.bind(this)
});
}
},
/**
* @param {Object} content
*/
updateContent: function (content) {
var textarea;
if (this.wysiwygExists()) {
wysiwyg.insertContent(content, false);
} else {
textarea = document.getElementById(this.widgetTargetId);
updateElementAtCursor(textarea, content);
varienGlobalEvents.fireEvent('tinymceChange');
jQuery(textarea).change();
}
},
/**
* @return {Boolean}
*/
wysiwygExists: function () {
return typeof wysiwyg != 'undefined' && wysiwyg.get(this.widgetTargetId);
},
/**
* @return {null|wysiwyg.Editor|*}
*/
getWysiwyg: function () {
return wysiwyg.activeEditor();
},
/**
* @return {*|Element}
*/
getWysiwygNode: function () {
return widgetTools.getActiveSelectedNode() || wysiwyg.activeEditor().selection.getNode();
}
};
WysiwygWidget.chooser = Class.create();
WysiwygWidget.chooser.prototype = {
// HTML element A, on which click event fired when choose a selection
chooserId: null,
// Source URL for Ajax requests
chooserUrl: null,
// Chooser config
config: null,
// Chooser dialog window
dialogWindow: null,
// Chooser content for dialog window
dialogContent: null,
overlayShowEffectOptions: null,
overlayHideEffectOptions: null,
/**
* @param {*} chooserId
* @param {*} chooserUrl
* @param {*} config
*/
initialize: function (chooserId, chooserUrl, config) {
this.chooserId = chooserId;
this.chooserUrl = chooserUrl;
this.config = config;
},
/**
* @return {String}
*/
getResponseContainerId: function () {
return 'responseCnt' + this.chooserId;
},
/**
* @return {jQuery|*|HTMLElement}
*/
getChooserControl: function () {
return $(this.chooserId + 'control');
},
/**
* @return {jQuery|*|HTMLElement}
*/
getElement: function () {
return $(this.chooserId + 'value');
},
/**
* @return {jQuery|*|HTMLElement}
*/
getElementLabel: function () {
return $(this.chooserId + 'label');
},
/**
* Open.
*/
open: function () {
$(this.getResponseContainerId()).show();
},
/**
* Close.
*/
close: function () {
$(this.getResponseContainerId()).hide();
this.closeDialogWindow();
},
/**
* Choose.
*/
choose: function () {
// Open dialog window with previously loaded dialog content
var responseContainerId;
if (this.dialogContent) {
this.openDialogWindow(this.dialogContent);
return;
}
// Show or hide chooser content if it was already loaded
responseContainerId = this.getResponseContainerId();
// Otherwise load content from server
new Ajax.Request(this.chooserUrl, {
parameters: {
'element_value': this.getElementValue(),
'element_label': this.getElementLabelText()
},
/**
* On success.
*/
onSuccess: function (transport) {
try {
widgetTools.onAjaxSuccess(transport);
this.dialogContent = widgetTools.getDivHtml(responseContainerId, transport.responseText);
this.openDialogWindow(this.dialogContent);
} catch (e) {
alert({
content: e.message
});
}
}.bind(this)
});
},
/**
* Open dialog winodw.
*
* @param {*} content
*/
openDialogWindow: function (content) {
this.dialogWindow = jQuery('<div/>').modal({
title: this.config.buttons.open,
type: 'slide',
buttons: [],
/**
* Opened.
*/
opened: function () {
jQuery(this).addClass('magento-message');
},
/**
* @param {jQuery.Event} e
* @param {Object} modal
*/
closed: function (e, modal) {
modal.modal.remove();
this.dialogWindow = null;
}
});
this.dialogWindow.modal('openModal').append(content);
},
/**
* Close dialog window.
*/
closeDialogWindow: function () {
this.dialogWindow.modal('closeModal').remove();
},
/**
* @return {*|Number}
*/
getElementValue: function () {
return this.getElement().value;
},
/**
* @return {String}
*/
getElementLabelText: function () {
return this.getElementLabel().innerHTML;
},
/**
* @param {*} value
*/
setElementValue: function (value) {
this.getElement().value = value;
},
/**
* @param {*} value
*/
setElementLabel: function (value) {
this.getElementLabel().innerHTML = value;
}
};
window.WysiwygWidget = WysiwygWidget;
window.widgetTools = widgetTools;
});