forked from django-cms/django-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cms.plugins.js
1657 lines (1441 loc) · 54.5 KB
/
cms.plugins.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
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright https://github.com/divio/django-cms
*/
var $ = require('jquery');
var Class = require('classjs');
var Helpers = require('./cms.base').API.Helpers;
var KEYS = require('./cms.base').KEYS;
var Modal = require('./cms.modal');
require('../polyfills/array.prototype.findindex');
var doc;
var clipboard;
var clipboardDraggable;
var clipboardPlugin;
/**
* Class for handling Plugins / Placeholders or Generics.
* Handles adding / moving / copying / pasting / menus etc
* in structureboard.
*
* @class Plugin
* @namespace CMS
* @uses CMS.API.Helpers
*/
var Plugin = new Class({
implement: [Helpers],
options: {
type: '', // bar, plugin or generic
placeholder_id: null,
plugin_type: '',
plugin_id: null,
plugin_language: '',
plugin_parent: null,
plugin_order: null,
plugin_restriction: [],
plugin_parent_restriction: [],
urls: {
add_plugin: '',
edit_plugin: '',
move_plugin: '',
copy_plugin: '',
delete_plugin: ''
}
},
initialize: function initialize(container, options) {
this.options = $.extend(true, {}, this.options, options);
this._setupUI(container);
// states
this.csrf = CMS.config.csrf;
this.click = 'click.cms.plugin';
this.pointerUp = 'pointerup.cms.plugin';
this.pointerDown = 'pointerdown.cms.plugin';
this.pointerOverAndOut = 'pointerover.cms.plugin pointerout.cms.plugin';
this.doubleClick = 'dblclick.cms.plugin';
this.keyUp = 'keyup.cms.plugin';
this.keyDown = 'keydown.cms.plugin';
this.mouseEvents = 'mousedown.cms.plugin mousemove.cms.plugin mouseup.cms.plugin';
this.touchStart = 'touchstart.cms.plugin';
this.touchEnd = 'touchend.cms.plugin';
// bind data element to the container
this.ui.container.data('settings', this.options);
if (Plugin.aliasPluginDuplicatesMap[this.options.plugin_id]) {
return;
}
Plugin._initializeDragItemsStates();
// determine type of plugin
switch (this.options.type) {
case 'placeholder': // handler for placeholder bars
this._setPlaceholder();
this._collapsables();
break;
case 'plugin': // handler for all plugins
Plugin.aliasPluginDuplicatesMap[this.options.plugin_id] = true;
this._setPlugin();
this._collapsables();
break;
default: // handler for static content
this._setGeneric();
}
},
/**
* Caches some jQuery references and sets up structure for
* further initialisation.
*
* @method _setupUI
* @private
* @param {String} container `cms-plugin-${id}`
*/
_setupUI: function setupUI(container) {
var wrapper = $('.' + container);
this.ui = {
container: wrapper,
publish: $('.cms-btn-publish'),
save: $('.cms-toolbar-item-switch-save-edit'),
window: $(window),
revert: $('.cms-toolbar-revert'),
dragbar: null,
draggable: null,
draggables: null,
submenu: null,
dropdown: null
};
},
/**
* Sets up behaviours and ui for placeholder.
*
* @method _setPlaceholder
* @private
*/
_setPlaceholder: function () {
var that = this;
this.ui.dragbar = $('.cms-dragbar-' + this.options.placeholder_id);
this.ui.draggables = this.ui.dragbar.closest('.cms-dragarea').find('> .cms-draggables');
this.ui.submenu = this.ui.dragbar.find('.cms-submenu-settings');
var title = this.ui.dragbar.find('.cms-dragbar-title');
var togglerLinks = this.ui.dragbar.find('.cms-dragbar-toggler a');
var expanded = 'cms-dragbar-title-expanded';
// register the subnav on the placeholder
this._setSettingsMenu(this.ui.submenu);
this._setAddPluginModal(this.ui.dragbar.find('.cms-submenu-add'));
// istanbul ignore next
CMS.settings.dragbars = CMS.settings.dragbars || []; // expanded dragbars array
// enable expanding/collapsing globally within the placeholder
togglerLinks.off(this.click).on(this.click, function (e) {
e.preventDefault();
if (title.hasClass(expanded)) {
that._collapseAll(title);
} else {
that._expandAll(title);
}
});
if ($.inArray(this.options.placeholder_id, CMS.settings.dragbars) !== -1) {
title.addClass(expanded);
}
this._checkIfPasteAllowed();
},
/**
* Sets up behaviours and ui for plugin.
*
* @method _setPlugin
* @private
*/
_setPlugin: function () {
var that = this;
// filling up ui object
this.ui.draggable = $('.cms-draggable-' + this.options.plugin_id);
this.ui.dragitem = this.ui.draggable.find('> .cms-dragitem');
this.ui.draggables = this.ui.draggable.find('> .cms-draggables');
this.ui.submenu = this.ui.dragitem.find('.cms-submenu');
this.ui.draggable.data('settings', this.options);
// adds double click to edit
this.ui.container.add(this.ui.dragitem).on(this.doubleClick, function (e) {
e.preventDefault();
e.stopPropagation();
that.editPlugin(
that.options.urls.edit_plugin,
that.options.plugin_name,
that._getPluginBreadcrumbs()
);
});
// adds edit tooltip
this.ui.container.on(this.pointerOverAndOut + ' ' + this.touchStart, function (e) {
// required for both, click and touch
// otherwise propagation won't work to the nested plugin
e.stopPropagation();
if (e.type === 'touchstart') {
CMS.API.Tooltip._forceTouchOnce();
}
var name = that.options.plugin_name;
var id = that.options.plugin_id;
var placeholderId = that._getId(that.ui.dragitem.closest('.cms-dragarea'));
var placeholder = $('.cms-placeholder-' + placeholderId);
if (placeholder.length && placeholder.data('settings')) {
name = placeholder.data('settings').name + ': ' + name;
}
CMS.API.Tooltip.displayToggle(e.type === 'pointerover' || e.type === 'touchstart', e, name, id);
});
// adds listener for all plugin updates
this.ui.container.add(this.ui.draggable).on('cms.plugins.update', function (e) {
e.stopPropagation();
that.movePlugin();
});
// adds listener for copy/paste updates
this.ui.container.add(this.ui.draggable).on('cms.plugin.update', function (e) {
e.stopPropagation();
var el = $(e.delegateTarget);
var dragitem = $('.cms-draggable-' + el.data('settings').plugin_id);
var placeholder_id = that._getId(
dragitem.parents('.cms-draggables').last().prevAll('.cms-dragbar').first()
);
// if placeholder_id is empty, cancel
if (!placeholder_id) {
return false;
}
var data = el.data('settings');
data.target = placeholder_id;
data.parent = that._getId(dragitem.parent().closest('.cms-draggable'));
data.move_a_copy = true;
that.movePlugin(data);
});
// attach event to the plugin menu
this._setSettingsMenu(this.ui.submenu);
// attach events for the "Add plugin" modal
this._setAddPluginModal(this.ui.dragitem.find('.cms-submenu-add'));
// clickability of "Paste" menu item
this._checkIfPasteAllowed();
},
/**
* Sets up behaviours and ui for generics.
* Generics do not show up in structure board.
*
* @method _setGeneric
* @private
*/
_setGeneric: function () {
var that = this;
// adds double click to edit
this.ui.container.on(this.doubleClick, function (e) {
e.preventDefault();
e.stopPropagation();
that.editPlugin(that.options.urls.edit_plugin, that.options.plugin_name, []);
});
// adds edit tooltip
this.ui.container.on(this.pointerOverAndOut + ' ' + this.touchStart, function (e) {
if (e.type !== 'touchstart') {
e.stopPropagation();
}
var name = that.options.plugin_name;
var id = that.options.plugin_id;
CMS.API.Tooltip.displayToggle(e.type === 'pointerover' || e.type === 'touchstart', e, name, id);
});
},
/**
* Checks if paste is allowed into current plugin/placeholder based
* on restrictions we have. Also determines which tooltip to show.
*
* WARNING: this relies on clipboard plugins always being instantiated
* first, so they have data('settings') by the time this method is called.
*
* @method _checkIfPasteAllowed
* @private
* @returns {Boolean}
*/
_checkIfPasteAllowed: function _checkIfPasteAllowed() {
var pasteButton = this.ui.dropdown.find('[data-rel=paste]');
var pasteItem = pasteButton.parent();
if (!clipboardPlugin.length) {
pasteItem.addClass('cms-submenu-item-disabled');
pasteItem.find('.cms-submenu-item-paste-tooltip-empty').css('display', 'block');
return false;
}
if (this.ui.draggable && this.ui.draggable.hasClass('cms-draggable-disabled')) {
pasteItem.addClass('cms-submenu-item-disabled');
pasteItem.find('.cms-submenu-item-paste-tooltip-disabled').css('display', 'block');
return false;
}
var bounds = this.options.plugin_restriction;
if (clipboardPlugin.data('settings')) {
var type = clipboardPlugin.data('settings').plugin_type;
var parent_bounds = $.grep(clipboardPlugin.data('settings').plugin_parent_restriction, function (r) {
// special case when PlaceholderPlugin has a parent restriction named "0"
return r !== '0';
});
var currentPluginType = this.options.plugin_type;
if ((bounds.length && $.inArray(type, bounds) === -1) ||
(parent_bounds.length && $.inArray(currentPluginType, parent_bounds) === -1)) {
pasteItem.addClass('cms-submenu-item-disabled');
pasteItem.find('.cms-submenu-item-paste-tooltip-restricted').css('display', 'block');
return false;
}
} else {
return false;
}
return true;
},
/**
* Calls api to create a plugin and then proceeds to edit it.
*
* @method addPlugin
* @param {String} type type of the plugin, e.g "Bootstrap3ColumnCMSPlugin"
* @param {String} name name of the plugin, e.g. "Column"
* @param {String} parent id of a parent plugin
*/
addPlugin: function (type, name, parent) {
var params = {
placeholder_id: this.options.placeholder_id,
plugin_type: type,
plugin_language: this.options.plugin_language
};
if (parent) {
params.plugin_parent = parent;
}
var url = this.options.urls.add_plugin + '?' + $.param(params);
var modal = new Modal({
onClose: this.options.onClose || false,
redirectOnClose: this.options.redirectOnClose || false
});
modal.open({
url: url,
title: name
});
modal.on('cms.modal.closed', function removePlaceholder() {
$('.cms-add-plugin-placeholder').remove();
});
},
/**
* Opens the modal for editing a plugin.
*
* @method editPlugin
* @param {String} url editing url
* @param {String} name Name of the plugin, e.g. "Column"
* @param {Object[]} breadcrumb array of objects representing a breadcrumb,
* each item is `{ title: 'string': url: 'string' }`
*/
editPlugin: function (url, name, breadcrumb) {
// trigger modal window
var modal = new Modal({
onClose: this.options.onClose || false,
redirectOnClose: this.options.redirectOnClose || false
});
modal.on('cms.modal.loaded', function removePlaceholder() {
$('.cms-add-plugin-placeholder').remove();
});
modal.on('cms.modal.closed', function removePlaceholder() {
$('.cms-add-plugin-placeholder').remove();
});
modal.open({
url: url,
title: name,
breadcrumbs: breadcrumb,
width: 850
});
},
/**
* Used for copying _and_ pasting a plugin. If either of params
* is present method assumes that it's "paste" and will make a call
* to api to insert current plugin to specified `options.target_plugin_id`
* or `options.target_placeholder_id`. Copying a plugin also first
* clears the clipboard.
*
* @method copyPlugin
* @param {Object} [opts=this.options]
* @param {String} source_language
* @returns {Boolean|void}
*/
// eslint-disable-next-line complexity
copyPlugin: function (opts, source_language) {
// cancel request if already in progress
if (CMS.API.locked) {
return false;
}
CMS.API.locked = true;
var move = !!(opts || source_language);
// set correct options
var options = opts || this.options;
var sourceLanguage = source_language;
if (sourceLanguage) {
options.target = options.placeholder_id;
options.plugin_id = '';
options.parent = '';
} else {
sourceLanguage = options.plugin_language;
}
var data = {
source_placeholder_id: options.placeholder_id,
source_plugin_id: options.plugin_id || '',
source_language: sourceLanguage,
target_plugin_id: options.parent || '',
target_placeholder_id: options.target || CMS.config.clipboard.id,
target_language: options.page_language || sourceLanguage,
csrfmiddlewaretoken: this.csrf
};
var request = {
type: 'POST',
url: options.urls.copy_plugin,
data: data,
success: function () {
CMS.API.Messages.open({
message: CMS.config.lang.success
});
// reload
Helpers.reloadBrowser();
},
error: function (jqXHR) {
CMS.API.locked = false;
var msg = CMS.config.lang.error;
// trigger error
CMS.API.Messages.open({
message: msg + jqXHR.responseText || jqXHR.status + ' ' + jqXHR.statusText,
error: true
});
}
};
if (move) {
$.ajax(request);
} else {
// ensure clipboard is cleaned
CMS.API.Clipboard.clear(function () {
$.ajax(request);
});
}
},
/**
* Essentially clears clipboard and moves plugin to a clipboard
* placholder through `movePlugin`.
*
* @method cutPlugin
* @returns {Boolean|void}
*/
cutPlugin: function () {
// if cut is once triggered, prevent additional actions
if (CMS.API.locked) {
return false;
}
CMS.API.locked = true;
var that = this;
var data = {
placeholder_id: CMS.config.clipboard.id,
plugin_id: this.options.plugin_id,
plugin_parent: '',
plugin_language: this.options.page_language,
plugin_order: [this.options.plugin_id],
csrfmiddlewaretoken: this.csrf
};
// ensure clipboard is cleaned
CMS.API.Clipboard.clear(function () {
// cancel request if already in progress
if (CMS.API.locked) {
return false;
}
CMS.API.locked = true;
// move plugin
$.ajax({
type: 'POST',
url: that.options.urls.move_plugin,
data: data,
success: function () {
CMS.API.Messages.open({
message: CMS.config.lang.success
});
// if response is reload
CMS.API.Helpers.reloadBrowser();
},
error: function (jqXHR) {
CMS.API.locked = false;
var msg = CMS.config.lang.error;
// trigger error
CMS.API.Messages.open({
message: msg + jqXHR.responseText || jqXHR.status + ' ' + jqXHR.statusText,
error: true
});
}
});
});
},
/**
* Method is called when you click on the paste button on the plugin.
* Uses existing solution of `copyPlugin(options)`
*
* @method pastePlugin
*/
pastePlugin: function () {
clipboardDraggable.appendTo(this.ui.draggables);
this.ui.draggables.trigger('cms.update');
clipboardDraggable.trigger('cms.plugin.update');
},
/**
* Moves plugin by querying the API and then updates some UI parts
* to reflect that the page has changed.
*
* @method movePlugin
* @param {Object} [opts=this.options]
* @param {String} [opts.placeholder_id]
* @param {String} [opts.plugin_id]
* @param {String} [opts.plugin_parent]
* @param {String} [opts.plugin_language]
* @param {Boolean} [opts.move_a_copy]
* @returns {Boolean|void}
*/
movePlugin: function (opts) {
// cancel request if already in progress
if (CMS.API.locked) {
return false;
}
CMS.API.locked = true;
var that = this;
// set correct options
var options = opts || this.options;
var plugin = $('.cms-plugin-' + options.plugin_id);
var dragitem = $('.cms-draggable-' + options.plugin_id);
// SETTING POSITION
this._setPosition(options.plugin_id, plugin, dragitem);
// SAVING POSITION
var placeholder_id = this._getId(
dragitem.parents('.cms-draggables').last().prevAll('.cms-dragbar').first()
);
var plugin_parent = this._getId(dragitem.parent().closest('.cms-draggable'));
var plugin_order = this._getIds(dragitem.siblings('.cms-draggable').andSelf());
if (options.move_a_copy) {
plugin_order = plugin_order.map(function (pluginId) {
var id = pluginId;
// TODO correct way would be to check if it's actually a
// pasted plugin and only then replace the id with copy token
// otherwise if we would copy from the same placeholder we would get
// two copy tokens instead of original and a copy.
// it's ok so far, as long as we copy only from clipboard
if (id === options.plugin_id) {
id = '__COPY__';
}
return id;
});
}
// cancel here if we have no placeholder id
if (placeholder_id === false) {
return false;
}
// gather the data for ajax request
var data = {
placeholder_id: placeholder_id,
plugin_id: options.plugin_id,
plugin_parent: plugin_parent || '',
// this is a hack: when moving to different languages use the global language
plugin_language: options.page_language,
plugin_order: plugin_order,
csrfmiddlewaretoken: this.csrf,
move_a_copy: options.move_a_copy
};
CMS.API.Toolbar.showLoader();
$.ajax({
type: 'POST',
url: options.urls.move_plugin,
data: data,
success: function (response) {
// if response is reload
if (response.reload) {
Helpers.reloadBrowser();
}
// set new url settings when moving #4803
if (response.urls) {
that._setSettings(options, {
urls: response.urls
});
}
// enable actions again
CMS.API.locked = false;
CMS.API.Toolbar.hideLoader();
// TODO: show only if (response.status)
that._showSuccess(dragitem);
Plugin._updateRegistry({
pluginId: options.plugin_id,
update: {
plugin_parent: plugin_parent || '',
placeholder_id: placeholder_id
}
});
that._setSettings(that.options, {
plugin_parent: plugin_parent || '',
placeholder_id: placeholder_id
});
},
error: function (jqXHR) {
CMS.API.locked = false;
var msg = CMS.config.lang.error;
// trigger error
CMS.API.Messages.open({
message: msg + jqXHR.responseText || jqXHR.status + ' ' + jqXHR.statusText,
error: true
});
CMS.API.Toolbar.hideLoader();
}
});
// show publish / save buttons
this.ui.publish
.addClass('cms-btn-publish-active')
.removeClass('cms-btn-disabled')
.parent().show();
this.ui.window.trigger('resize');
// enable revert to live
this.ui.revert.removeClass('cms-toolbar-item-navigation-disabled');
},
/**
* Changes the settings attributes on an initialised plugin.
*
* @method _setSettings
* @param {Object} oldSettings current settings
* @param {Object} newSettings new settings to be applied
* @private
*/
_setSettings: function _setSettings(oldSettings, newSettings) {
var settings = $.extend(true, {}, oldSettings, newSettings);
var plugin = $('.cms-plugin-' + settings.plugin_id);
var draggable = $('.cms-draggable-' + settings.plugin_id);
// set new setting on instance and plugin data
this.options = settings;
plugin.data('settings', settings);
draggable.data('settings', settings);
},
/**
* Opens a modal to delete a plugin.
*
* @method deletePlugin
* @param {String} url admin url for deleting a page
* @param {String} name plugin name, e.g. "Column"
* @param {Object[]} breadcrumb array of objects representing a breadcrumb,
* each item is `{ title: 'string': url: 'string' }`
*/
deletePlugin: function (url, name, breadcrumb) {
// trigger modal window
var modal = new Modal({
onClose: this.options.onClose || false,
redirectOnClose: this.options.redirectOnClose || false
});
modal.on('cms.modal.loaded', function removePlaceholder() {
$('.cms-add-plugin-placeholder').remove();
});
modal.open({
url: url,
title: name,
breadcrumbs: breadcrumb
});
},
/**
* Moves the plugin according to the place it should have in content mode.
*
* @method _setPosition
* @private
* @param {String} id
* @param {jQuery} plugin the `.cms-plugin` element
* @param {jQuery} dragitem the `.cms-draggable` of the plugin
*/
_setPosition: function (id, plugin, dragitem) {
// after we insert the plugin onto its new place, we need to figure out where to position it
var prevItem = dragitem.prev('.cms-draggable');
var nextItem = dragitem.next('.cms-draggable');
var parent = dragitem.parent().closest('.cms-draggable');
var child = $('.cms-plugin-' + this._getId(parent));
var placeholder = dragitem.closest('.cms-dragarea');
// determine if there are other plugins within the same level, this makes the move easier
if (prevItem.length) {
plugin.insertAfter($('.cms-plugin-' + this._getId(prevItem)));
} else if (nextItem.length) {
plugin.insertBefore($('.cms-plugin-' + this._getId(nextItem)));
} else if (parent.length) {
// if we can't find a plugin on the same level, we need to travel higher
// for this we need to find the deepest child
while (child.children().length) {
child = child.children();
}
child.append(plugin);
} else if (placeholder.length) {
// we also need to cover the case if we move the plugin to an empty placeholder
plugin.insertAfter($('.cms-placeholder-' + this._getId(placeholder)));
} else {
// if we did not found a match, reload
Helpers.reloadBrowser();
}
},
/**
* Called after plugin is added through ajax.
*
* @method editPluginPostAjax
* @param {Object} toolbar CMS.API.Toolbar instance (not used)
* @param {Object} response response from server
*/
editPluginPostAjax: function (toolbar, response) {
this.editPlugin(response.url, this.options.plugin_name, response.breadcrumb);
},
/**
* _setSettingsMenu sets up event handlers for settings menu.
*
* @method _setSettingsMenu
* @private
* @param {jQuery} nav
*/
_setSettingsMenu: function _setSettingsMenu(nav) {
var that = this;
this.ui.dropdown = nav.siblings('.cms-submenu-dropdown-settings');
var dropdown = this.ui.dropdown;
nav.on(this.pointerUp, function (e) {
e.preventDefault();
e.stopPropagation();
var trigger = $(this);
if (trigger.hasClass('cms-btn-active')) {
Plugin._hideSettingsMenu(trigger);
} else {
Plugin._hideSettingsMenu();
that._showSettingsMenu(trigger);
}
}).on(this.touchStart, function (e) {
// required on some touch devices so
// ui touch punch is not triggering mousemove
// which in turn results in pep triggering pointercancel
e.stopPropagation();
});
dropdown.on(this.mouseEvents, function (e) {
e.stopPropagation();
}).on(this.touchStart, function (e) {
// required for scrolling on mobile
e.stopPropagation();
});
that._setupActions(nav);
// prevent propagation
nav.on([this.pointerUp, this.pointerDown, this.click, this.doubleClick].join(' '), function (e) {
e.stopPropagation();
});
nav.siblings('.cms-quicksearch, .cms-submenu-dropdown-settings')
.on([this.pointerUp, this.click, this.doubleClick].join(' '), function (e) {
e.stopPropagation();
});
},
/**
* Simplistic implementation, only scrolls down, only works in structuremode
* and highly depends on the styles of the structureboard to work correctly
*
* @method _scrollToElement
* @private
* @param {jQuery} el element to scroll to
* @param {Object} [opts]
* @param {Number} [opts.duration=200] time to scroll
* @param {Number} [opts.offset=50] distance in px to the bottom of the screen
*/
_scrollToElement: function _scrollToElement(el, opts) {
var DEFAULT_DURATION = 200;
var DEFAULT_OFFSET = 50;
var duration = opts && opts.duration !== undefined ? opts.duration : DEFAULT_DURATION;
var offset = opts && opts.offset !== undefined ? opts.offset : DEFAULT_OFFSET;
var scrollable = el.offsetParent();
var win = $(window);
var scrollHeight = win.height();
var scrollTop = scrollable.scrollTop();
var elPosition = el.position().top;
var elHeight = el.height();
var isInViewport = elPosition + elHeight + offset <= scrollHeight;
if (!isInViewport) {
scrollable.animate({
scrollTop: elPosition + offset + elHeight + scrollTop - scrollHeight
}, duration);
}
},
/**
* Opens a modal with traversable plugins list, adds a placeholder to where
* the plugin will be added.
*
* @method _setAddPluginModal
* @private
* @param {jQuery} nav modal trigger element
* @returns {Boolean|void}
*/
_setAddPluginModal: function _setAddPluginModal(nav) {
if (nav.hasClass('cms-btn-disabled')) {
return false;
}
var that = this;
var modal;
var isTouching;
var plugins;
var initModal = Helpers.once(function initModal() {
var placeholder = $(
'<div class="cms-add-plugin-placeholder">' +
CMS.config.lang.addPluginPlaceholder +
'</div>'
);
var dragItem = nav.closest('.cms-dragitem');
var isPlaceholder = !dragItem.length;
var childrenList;
modal = new Modal({
minWidth: 400,
minHeight: 400
});
if (isPlaceholder) {
childrenList = nav.closest('.cms-dragarea').find('> .cms-draggables');
} else {
childrenList = nav.closest('.cms-draggable').find('> .cms-draggables');
}
modal.on('cms.modal.loaded', $.proxy(that._setupKeyboardTraversing, that));
modal.on('cms.modal.loaded', function addPlaceholder() {
if (childrenList.hasClass('cms-hidden') && !isPlaceholder) {
that._toggleCollapsable(dragItem);
}
$('.cms-add-plugin-placeholder').remove();
placeholder.appendTo(childrenList);
that._scrollToElement(placeholder);
});
modal.on('cms.modal.closed', function removePlaceholder() {
$('.cms-add-plugin-placeholder').remove();
});
modal.on('cms.modal.shown', function () {
var dropdown = $('.cms-modal-markup .cms-plugin-picker');
if (!isTouching) {
// only focus the field if using mouse
// otherwise keyboard pops up
dropdown.find('input').trigger('focus');
}
isTouching = false;
});
plugins = nav.siblings('.cms-plugin-picker');
that._setupQuickSearch(plugins);
});
nav.on(this.touchStart, function (e) {
isTouching = true;
// required on some touch devices so
// ui touch punch is not triggering mousemove
// which in turn results in pep triggering pointercancel
e.stopPropagation();
}).on(this.pointerUp, function (e) {
e.preventDefault();
e.stopPropagation();
Plugin._hideSettingsMenu();
initModal();
// since we don't know exact plugin parent (because dragndrop)
// we need to know the parent id by the time we open "add plugin" dialog
var pluginsCopy = plugins.clone(true, true).data(
'parentId', that._getId(nav.closest('.cms-draggable'))
).append(that._getPossibleChildClasses());
modal.open({
title: that.options.addPluginHelpTitle,
html: pluginsCopy,
width: 530,
height: 400
});
});
// prevent propagation
nav.on([this.pointerUp, this.pointerDown, this.click, this.doubleClick].join(' '), function (e) {
e.stopPropagation();
});
nav.siblings('.cms-quicksearch, .cms-submenu-dropdown')
.on([this.pointerUp, this.click, this.doubleClick].join(' '), function (e) {
e.stopPropagation();
});
},
/**
* Returns available plugin/placeholder child classes markup
* for "Add plugin" modal
*
* @method _getPossibleChildClasses
* @private
* @returns {jQuery} "add plugin" menu
*/
_getPossibleChildClasses: function _getPossibleChildClasses() {
var that = this;
var childRestrictions = this.options.plugin_restriction;
// have to check the placeholder every time, since plugin could've been
// moved as part of another plugin
var placeholderId = that._getId(that.ui.submenu.closest('.cms-dragarea'));
var resultElements = $($('#cms-plugin-child-classes-' + placeholderId).html());
if (childRestrictions && childRestrictions.length) {
resultElements = resultElements.filter(function () {
var item = $(this);
return item.hasClass('cms-submenu-item-title') ||
childRestrictions.indexOf(item.find('a').attr('href')) !== -1;
});
resultElements = resultElements.filter(function (index) {
var item = $(this);
return !item.hasClass('cms-submenu-item-title') ||
item.hasClass('cms-submenu-item-title') && (
!resultElements.eq(index + 1).hasClass('cms-submenu-item-title') &&
resultElements.eq(index + 1).length
);
});
}
resultElements.find('a').on(that.click, that._delegate.bind(that));
return resultElements;
},
/**
* Sets up event handlers for quicksearching in the plugin picker.
*
* @method _setupQuickSearch
* @private
* @param {jQuery} plugins plugins picker element
*/
_setupQuickSearch: function _setupQuickSearch(plugins) {
var that = this;
var FILTER_DEBOUNCE_TIMER = 100;
var FILTER_PICK_DEBOUNCE_TIMER = 110;
var handler = Helpers.debounce(function () {
var input = $(this);