-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Kanban.js
2724 lines (2518 loc) · 119 KB
/
Kanban.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
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2022 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
import SearchInput from "../SearchTokenizer/SearchInput.js";
/* global escapeMarkupText */
/* global sortable */
/* global glpi_toast_error */
/**
* Kanban rights structure
* @since 10.0.0
*/
class GLPIKanbanRights {
constructor(rights) {
/**
* If true, then a button will be added to each column to allow new items to be added.
* When an item is added, a request is made via AJAX to create the item in the DB.
* Permissions are re-checked server-side during this request.
* Users will still be limited by the {@link create_card_limited_columns} right both client-side and server-side.
* @since 9.5.0
* @since 10.0.0 Moved to new rights class
* @type {boolean}
*/
this.create_item = rights['create_item'] || false;
/**
* If true, then a button will be added to each card to allow deleting them and the underlying item directly from the kanban.
* When a card is deleted, a request is made via AJAX to delete the item in the DB.
* Permissions are re-checked server-side during this request.
* @since 10.0.0
* @type {boolean}
*/
this.delete_item = rights['delete_item'] || false;
/**
* If true, then a button will be added to the add column form that lets the user create a new column.
* For Projects as an example, it would create a new project state.
* Permissions are re-checked server-side during this request.
* @since 9.5.0
* @since 10.0.0 Moved to new rights class
* @type {boolean}
*/
this.create_column = rights['create_column'] || false;
/**
* Global permission for being able to modify the Kanban state/view.
* This includes the order of cards in the columns.
* @since 9.5.0
* @since 10.0.0 Moved to new rights class
* @type {boolean}
*/
this.modify_view = rights['modify_view'] || false;
/**
* Limits the columns that the user can add cards to.
* By default, it is empty which allows cards to be added to all columns.
* If you don't want the user to add cards to any column, {@link rights.create_item} should be false.
* @since 9.5.0
* @since 10.0.0 Moved to new rights class
* @type {Array}
*/
this.create_card_limited_columns = rights['create_card_limited_columns'] || [];
/**
* Global right for ordering cards.
* @since 9.5.0
* @since 10.0.0 Moved to new rights class
* @type {boolean}
*/
this.order_card = rights['order_card'] || false;
}
/** @see this.create_item */
canCreateItem() {
return this.create_item;
}
/** @see this.delete_item */
canDeleteItem() {
return this.delete_item;
}
/** @see this.create_column */
canCreateColumn() {
return this.create_column;
}
/** @see this.modify_view */
canModifyView() {
return this.modify_view;
}
/** @see this.order_card */
canOrderCard() {
return this.order_card;
}
/** @see this.create_card_limited_columns */
getAllowedColumnsForNewCards() {
return this.create_card_limited_columns;
}
}
(function(){
window.GLPIKanban = function() {
/**
* Self-reference for property access in functions.
*/
const self = this;
/**
* Selector for the parent Kanban element. This is specified in PHP and passed in the GLPIKanban constructor.
* @since 9.5.0
* @type {string}
*/
this.element = "";
/**
* The original column state when the Kanban was built or refreshed.
* It should not be considered up to date beyond the initial build/refresh.
* @since 9.5.0
* @type {Array}
*/
this.columns = {};
/**
* The AJAX directory.
* @since 9.5.0
* @type {string}
*/
this.ajax_root = CFG_GLPI.root_doc + "/ajax/";
/**
* The maximum number of badges able to be shown before an overflow badge is added.
* @since 9.5.0
* @type {number}
*/
this.max_team_images = 3;
/**
* The size in pixels for the team badges.
* @since 9.5.0
* @type {number}
*/
this.team_image_size = 24;
/**
* The parent item for this Kanban. In the future, this may be null for personal/unrelated Kanbans. For now, it is expected to be defined.
* @since 9.5.0
* @type {Object|{itemtype: string, items_id: number}}
*/
this.item = null;
/**
* Object of itemtypes that can be used as items in the Kanban. They should be in the format:
* itemtype => [
* 'name' => Localized itemtype name
* 'fields' => [
* field_name => [
* 'placeholder' => placeholder text (optional) = blank,
* 'type' => input type (optional) default = text,
* 'value' => value (optional) default = blank
* ]
* ]
* ]
* @since 9.5.0
* @type {Object}
*/
this.supported_itemtypes = {};
/**
* User rights object
* @type {GLPIKanbanRights}
*/
this.rights = new GLPIKanbanRights({});
/** @deprecated 10.0.0 Use rights.canCreateItem() instead */
this.allow_add_item = false;
/** @deprecated 10.0.0 Use rights.canDeleteItem() instead */
this.allow_delete_item = false;
/** @deprecated 10.0.0 Use rights.canCreateColumn() instead */
this.allow_create_column = false;
/** @deprecated 10.0.0 Use rights.canModifyView() instead */
this.allow_modify_view = false;
/** @deprecated 10.0.0 Use rights.getAllowedColumnsForNewCards() instead */
this.limit_addcard_columns = [];
/** @deprecated 10.0.0 Use rights.canOrderCard() instead */
this.allow_order_card = false;
/**
* Specifies if the user's current palette is a dark theme (darker for example).
* This will help determine the colors of the generated badges.
* @since 9.5.0
* @type {boolean}
*/
this.dark_theme = false;
/**
* Name of the DB field used to specify columns and any extra fields needed to create the column (Ex: color).
* For example, Projects organize items by the state of the sub-Projects and sub-Tasks.
* Therefore, the column_field id is 'projectstates_id' with any additional fields needed being specified in extra_fields.
* @since 9.5.0
* @type {{id: string, extra_fields: Object}}
*/
this.column_field = {id: '', extra_fields: {}};
/**
* Specifies if the Kanban's toolbar (switcher, filters, etc.) should be shown.
* This is true by default, but may be set to false if used on a fullscreen display for example.
* @since 9.5.0
* @type {boolean}
*/
this.show_toolbar = true;
/**
* Filters being applied to the Kanban view.
* For now, only a simple/regex text filter is supported.
* This can be extended in the future to support more specific filters specified per itemtype.
* The name of internal filters like the text filter begin with an underscore.
* @since 9.5.0
* @type {{_text: string}}
*/
this.filters = {
_text: ''
};
this.filter_tokenizer = null;
this.supported_filters = [];
/**
* The ID of the add column form.
* @since 9.5.0
* @type {string}
*/
this.add_column_form = '';
/**
* The ID of the create column form.
* @since 9.5.0
* @type {string}
*/
this.create_column_form = '';
/**
* Cache for images to reduce network requests and keep the same generated image between cards.
* @since 9.5.0
* @type {{Group: {}, User: {}, Supplier: {}, Contact: {}}}
*/
this.team_badge_cache = {
User: {},
Group: {},
Supplier: {},
Contact: {}
};
/**
* If greater than zero, this specifies the amount of time in minutes between background refreshes,
* During a background refresh, items are added/moved/removed based on the data in the DB.
* It does not affect items in the process of being created.
* When sorting an item or column, the background refresh is paused to avoid a disruption or incorrect data.
* @since 9.5.0
* @type {number} Time in minutes between background refreshes.
*/
this.background_refresh_interval = 0;
/**
* Internal refresh function
* @since 9.5.0
* @type {function}
* @private
*/
let _backgroundRefresh = null;
/**
* Reference for the background refresh timer
* @type {null}
* @private
*/
var _backgroundRefreshTimer = null;
/**
* The user's state object.
* This contains an up-to-date list of columns that should be shown, the order they are in, and if they are folded.
* @since 9.5.0
* @type {{
* is_dirty: boolean,
* state: {}|{order_index: {column: number, folded: boolean, cards: {Array}}}
* }}
* The is_dirty flag indicates if the state was changed and needs to be saved.
*/
this.user_state = {is_dirty: false, state: {}};
/**
* The last time the Kanban was refreshed. This is used by the server to determine if the state needs to be sent to the client again.
* The state will only be sent if there was a change since this time.
* @type {?string}
*/
this.last_refresh = null;
/**
* Global sorting active state.
* @since 9.5.0
* @type {boolean}
*/
this.is_sorting_active = false;
this.sort_data = undefined;
this.mutation_observer = null;
this.display_initials = true;
/**
* Parse arguments and assign them to the object's properties
* @since 9.5.0
* @param {Object} args Object arguments
*/
const initParams = function(args) {
const overridableParams = [
'element', 'max_team_images', 'team_image_size', 'item',
'supported_itemtypes', 'allow_add_item', 'allow_add_column', 'dark_theme', 'background_refresh_interval',
'column_field', 'allow_modify_view', 'limit_addcard_columns', 'allow_order_card', 'allow_create_column',
'allow_delete_item', 'supported_filters', 'display_initials'
];
// Use CSS variable check for dark theme detection by default
self.dark_theme = $('html').css('--is-dark').trim() === 'true';
if (args.length === 1) {
for (let i = 0; i < overridableParams.length; i++) {
const param = overridableParams[i];
if (args[0][param] !== undefined) {
self[param] = args[0][param];
}
}
}
// Set rights
if (args[0]['rights'] !== undefined) {
self.rights = new GLPIKanbanRights(args[0]['rights']);
} else {
// 9.5.0 style compatibility
self.rights = new GLPIKanbanRights({
create_item: self.allow_add_item,
delete_item: self.allow_delete_item,
create_column: self.allow_create_column,
modify_view: self.allow_modify_view,
create_card_limited_columns: self.limit_addcard_columns,
order_card: self.allow_order_card
});
}
if (self.filters._text === undefined) {
self.filters._text = '';
}
/**
* @type {SearchInput}
*/
self.filter_input = null;
};
const initMutationObserver = function() {
self.mutation_observer = new MutationObserver((records) => {
records.forEach(r => {
if (r.addedNodes.length > 0) {
if (self.is_sorting_active) {
const sortable_placeholders = [...r.addedNodes].filter(n => n.classList.contains('sortable-placeholder'));
if (sortable_placeholders.length > 0) {
const placeholder = $(sortable_placeholders[0]);
const current_column = placeholder.closest('.kanban-column').attr('id');
// Compute current position based on list of sortable elements without current card.
// Indeed, current card is still in DOM (but invisible), making placeholder index in DOM
// not always corresponding to its position inside list of visible elements.
const sortable_elements = $('#' + current_column + ' ul.kanban-body > li:not([id="' + self.sort_data.card_id + '"])');
const current_position = sortable_elements.index(placeholder.get(0));
const card = $('#' + self.sort_data.card_id);
card.data('current-pos', current_position);
if (!self.rights.canOrderCard()) {
if (current_column === self.sort_data.source_column) {
if (current_position !== self.sort_data.source_position) {
placeholder.addClass('invalid-position');
} else {
placeholder.removeClass('invalid-position');
}
} else {
if (!$(placeholder).is(':last-child')) {
placeholder.addClass('invalid-position');
} else {
placeholder.removeClass('invalid-position');
}
}
}
}
}
}
});
});
self.mutation_observer.observe($(self.element).get(0), {
subtree: true,
childList: true
});
};
/**
* Build DOM elements and defer registering event listeners for when the document is ready.
* @since 9.5.0
**/
const build = function() {
$(self.element).trigger('kanban:pre_build');
initMutationObserver();
if (self.show_toolbar) {
buildToolbar();
}
const kanban_container = $("<div class='kanban-container'><div class='kanban-columns'></div></div>").appendTo($(self.element));
// Dropdown for single additions
let add_itemtype_dropdown = "<ul id='kanban-add-dropdown' class='kanban-dropdown dropdown-menu' style='display: none'>";
Object.keys(self.supported_itemtypes).forEach(function(itemtype) {
add_itemtype_dropdown += "<li id='kanban-add-" + itemtype + "' class='dropdown-item'><span>" + self.supported_itemtypes[itemtype]['name'] + '</span></li>';
});
add_itemtype_dropdown += '</ul>';
kanban_container.append(add_itemtype_dropdown);
// Dropdown for overflow (Column)
let column_overflow_dropdown = "<ul id='kanban-overflow-dropdown' class='kanban-dropdown dropdown-menu' style='display: none'>";
let add_itemtype_bulk_dropdown = "<ul id='kanban-bulk-add-dropdown' class='dropdown-menu' style='display: none'>";
Object.keys(self.supported_itemtypes).forEach(function(itemtype) {
add_itemtype_bulk_dropdown += "<li id='kanban-bulk-add-" + itemtype + "' class='dropdown-item'><span>" + self.supported_itemtypes[itemtype]['name'] + '</span></li>';
});
add_itemtype_bulk_dropdown += '</ul>';
const add_itemtype_bulk_link = '<a href="#">' + '<i class="fa-fw fas fa-list"></i>' + __('Bulk add') + '</a>';
column_overflow_dropdown += '<li class="dropdown-trigger dropdown-item">' + add_itemtype_bulk_link + add_itemtype_bulk_dropdown + '</li>';
if (self.rights.canModifyView()) {
column_overflow_dropdown += "<li class='kanban-remove dropdown-item' data-forbid-protected='true'><span>" + '<i class="fa-fw ti ti-trash"></i>' + __('Delete') + "</span></li>";
}
column_overflow_dropdown += '</ul>';
kanban_container.append(column_overflow_dropdown);
// Dropdown for overflow (Card)
let card_overflow_dropdown = "<ul id='kanban-item-overflow-dropdown' class='kanban-dropdown dropdown-menu' style='display: none'>";
card_overflow_dropdown += `
<li class='kanban-item-goto dropdown-item'>
<a href="#"><i class="fa-fw fas fa-share"></i>${__('Go to')}</a>
</li>`;
if (self.rights.canDeleteItem()) {
card_overflow_dropdown += `
<li class='kanban-item-remove dropdown-item'>
<span>
<i class="fa-fw ti ti-trash"></i>${__('Delete')}
</span>
</li>`;
}
card_overflow_dropdown += '</ul>';
kanban_container.append(card_overflow_dropdown);
$('#kanban-overflow-dropdown li.dropdown-trigger > a').on("click", function(e) {
$(this).parent().toggleClass('active');
$(this).parent().find('ul').toggle();
e.stopPropagation();
e.preventDefault();
});
$('#kanban-item-overflow-dropdown li.dropdown-trigger > a').on("click", function(e) {
$(this).parent().toggleClass('active');
$(this).parent().find('ul').toggle();
e.stopPropagation();
e.preventDefault();
});
const on_refresh = function() {
if (Object.keys(self.user_state.state).length === 0) {
// Save new state since none was stored for the user
saveState(true, true);
}
};
self.refresh(on_refresh, null, null, true);
if (self.rights.canModifyView()) {
buildAddColumnForm();
if (self.rights.canCreateColumn()) {
buildCreateColumnForm();
}
}
$(self.element).trigger('kanban:post_build');
};
const buildToolbar = function() {
$(self.element).trigger('kanban:pre_build_toolbar');
let toolbar = $("<div class='kanban-toolbar card flex-column flex-md-row'></div>").appendTo(self.element);
$("<select name='kanban-board-switcher'></select>").appendTo(toolbar);
let filter_input = $(`<input name='filter' class='form-control ms-1' type='text' placeholder="${__('Search or filter results')}" autocomplete="off"/>`).appendTo(toolbar);
if (self.rights.canModifyView()) {
let add_column = "<buttom rome='button' class='kanban-add-column btn btn-outline-secondary ms-1'>" + __('Add column') + "</button>";
toolbar.append(add_column);
}
self.filter_input = new SearchInput(filter_input, {
allowed_tags: self.supported_filters,
on_result_change: (e, result) => {
self.filters = {
_text: ''
};
self.filters._text = result.getFullPhrase();
result.getTaggedTerms().forEach(t => self.filters[t.tag] = {
term: t.term || '',
exclusion: t.exclusion || false,
prefix: t.prefix
});
self.filter();
},
tokenizer_options: {
custom_prefixes: {
'#': { // Regex prefix
label: __('Regex'),
token_color: '#00800080'
}
}
}
});
self.refreshSearchTokenizer();
self.filter();
$(self.element).trigger('kanban:post_build_toolbar');
};
const getColumnElementFromID = function(column_id) {
return '#column-' + self.column_field.id + '-' + column_id;
};
const getColumnIDFromElement = function(column_el) {
let element_id = [column_el];
if (typeof column_el !== 'string') {
element_id = $(column_el).prop('id').split('-');
} else {
element_id = column_el.split('-');
}
return element_id[element_id.length - 1];
};
const preserveNewItemForms = function() {
self.temp_forms = {};
let columns = $(self.element + " .kanban-column");
$.each(columns, function(i, column) {
let forms = $(column).find('.kanban-add-form');
if (forms.length > 0) {
self.temp_forms[column.id] = [];
$.each(forms, function(i2, form) {
// Copy event handlers for element and child elements
// Otherwise, the Add button will act like a normal submit button (not wanted)
self.temp_forms[column.id].push($(form).clone(true, true));
});
}
});
};
const restoreNewItemForms = function() {
if (self.temp_forms !== undefined && Object.keys(self.temp_forms).length > 0) {
$.each(self.temp_forms, function(column_id, forms) {
let column = $('#' + column_id);
if (column.length > 0) {
let column_body = column.find('.kanban-body').first();
$.each(forms, function(i, form) {
$(form).appendTo(column_body);
});
}
});
self.temp_forms = {};
}
};
const preserveScrolls = function() {
self.temp_kanban_scroll = {
left: $(self.element + ' .kanban-container').scrollLeft(),
top: $(self.element + ' .kanban-container').scrollTop()
};
self.temp_column_scrolls = {};
let columns = $(self.element + " .kanban-column");
$.each(columns, function(i, column) {
let column_body = $(column).find('.kanban-body');
if (column_body.scrollTop() !== 0) {
self.temp_column_scrolls[column.id] = column_body.scrollTop();
}
});
};
const restoreScrolls = function() {
if (self.temp_kanban_scroll !== null) {
$(self.element + ' .kanban-container').scrollLeft(self.temp_kanban_scroll.left);
$(self.element + ' .kanban-container').scrollTop(self.temp_kanban_scroll.top);
}
if (self.temp_column_scrolls !== null) {
$.each(self.temp_column_scrolls, function(column_id, scroll) {
$('#' + column_id + ' .kanban-body').scrollTop(scroll);
});
}
self.temp_kanban_scroll = {};
self.temp_column_scrolls = {};
};
/**
* Clear all columns from the Kanban.
* Should be used in conjunction with {@link fillColumns()} to refresh the Kanban.
* @since 9.5.0
*/
const clearColumns = function() {
preserveScrolls();
preserveNewItemForms();
$(self.element + " .kanban-column").remove();
};
/**
* Add all columns to the kanban. This does not clear the existing columns first.
* If you are refreshing the Kanban, you should call {@link clearColumns()} first.
* @since 9.5.0
* @param {Object} columns_container JQuery Object of columns container. Not required.
* If not specified, a new object will be created to reference this Kanban's columns container.
*/
const fillColumns = function(columns_container) {
if (columns_container === undefined) {
columns_container = $(self.element + " .kanban-container .kanban-columns").first();
}
let already_processed = [];
$.each(self.user_state.state, function(position, column) {
if (column['visible'] !== false && column !== 'false') {
if (self.columns[column['column']] !== undefined) {
appendColumn(column['column'], self.columns[column['column']], columns_container);
}
}
already_processed.push(column['column']);
});
$.each(self.columns, function(column_id, column) {
if (!already_processed.includes(column_id)) {
if (column['id'] === undefined) {
appendColumn(column_id, column, columns_container);
}
}
});
restoreNewItemForms();
restoreScrolls();
};
/**
* Add all event listeners. At this point, all elements should have been added to the DOM.
* @since 9.5.0
*/
const registerEventListeners = function() {
const add_dropdown = $('#kanban-add-dropdown');
const column_overflow_dropdown = $('#kanban-overflow-dropdown');
const card_overflow_dropdown = $('#kanban-item-overflow-dropdown');
refreshSortables();
if (Object.keys(self.supported_itemtypes).length > 0) {
$(self.element + ' .kanban-container').on('click', '.kanban-add', function(e) {
const button = $(e.target);
//Keep menu open if clicking on another add button
const force_stay_visible = $(add_dropdown.data('trigger-button')).prop('id') !== button.prop('id');
add_dropdown.css({
position: 'fixed',
left: button.offset().left,
top: button.offset().top + button.outerHeight(true),
display: (add_dropdown.css('display') === 'none' || force_stay_visible) ? 'inline' : 'none'
});
add_dropdown.data('trigger-button', button);
});
}
$(window).on('click', function(e) {
if (!$(e.target).hasClass('kanban-add')) {
add_dropdown.css({
display: 'none'
});
}
if (self.rights.canModifyView()) {
if (!$.contains($(self.add_column_form)[0], e.target)) {
$(self.add_column_form).css({
display: 'none'
});
}
if (self.rights.canCreateColumn()) {
if (!$.contains($(self.create_column_form)[0], e.target) && !$.contains($(self.add_column_form)[0], e.target)) {
$(self.create_column_form).css({
display: 'none'
});
}
}
}
});
if (Object.keys(self.supported_itemtypes).length > 0) {
$(self.element + ' .kanban-container').on('click', '.kanban-column-overflow-actions', function(e) {
const button = $(e.target);
//Keep menu open if clicking on another add button
const force_stay_visible = $(column_overflow_dropdown.data('trigger-button')).prop('id') !== button.prop('id');
column_overflow_dropdown.css({
position: 'fixed',
left: button.offset().left,
top: button.offset().top + button.outerHeight(true),
display: (column_overflow_dropdown.css('display') === 'none' || force_stay_visible) ? 'inline' : 'none'
});
// Hide sub-menus by default when opening the overflow menu
column_overflow_dropdown.find('ul').css({
display: 'none'
});
column_overflow_dropdown.find('li').removeClass('active');
// If this is a protected column, hide any items with data-forbid-protected='true'. Otherwise show them.
const column = $(e.target.closest('.kanban-column'));
if (column.hasClass('kanban-protected')) {
column_overflow_dropdown.find('li[data-forbid-protected="true"]').hide();
} else {
column_overflow_dropdown.find('li[data-forbid-protected="true"]').show();
}
column_overflow_dropdown.data('trigger-button', button);
});
}
$(self.element + ' .kanban-container').on('click', '.kanban-item-overflow-actions', function(e) {
const button = $(e.target);
//Keep menu open if clicking on another add button
const force_stay_visible = $(card_overflow_dropdown.data('trigger-button')).prop('id') !== button.prop('id');
card_overflow_dropdown.css({
position: 'fixed',
left: button.offset().left,
top: button.offset().top + button.outerHeight(true),
display: (card_overflow_dropdown.css('display') === 'none' || force_stay_visible) ? 'inline' : 'none'
});
// Hide sub-menus by default when opening the overflow menu
card_overflow_dropdown.find('ul').css({
display: 'none'
});
card_overflow_dropdown.find('li').removeClass('active');
card_overflow_dropdown.data('trigger-button', button);
const card = $(button.closest('.kanban-item'));
const form_link = card.data('form_link');
$(card_overflow_dropdown.find('.kanban-item-goto a')).attr('href', form_link);
let delete_action = $(card_overflow_dropdown.find('.kanban-item-remove'));
if (card.hasClass('deleted')) {
delete_action.html('<span><i class="ti ti-trash"></i>'+__('Purge')+'</span>');
} else {
delete_action.html('<span><i class="ti ti-trash"></i>'+__('Delete')+'</span>');
}
});
$(window).on('click', function(e) {
if (!$(e.target).hasClass('kanban-column-overflow-actions')) {
column_overflow_dropdown.css({
display: 'none'
});
}
if (!$(e.target).hasClass('kanban-item-overflow-actions')) {
card_overflow_dropdown.css({
display: 'none'
});
}
if (self.rights.canModifyView()) {
if (!$.contains($(self.add_column_form)[0], e.target)) {
$(self.add_column_form).css({
display: 'none'
});
}
if (self.rights.canCreateColumn()) {
if (!$.contains($(self.create_column_form)[0], e.target) && !$.contains($(self.add_column_form)[0], e.target)) {
$(self.create_column_form).css({
display: 'none'
});
}
}
}
});
$(self.element + ' .kanban-container').on('click', '.kanban-remove', function(e) {
// Get root dropdown, then the button that triggered it, and finally the column that the button is in
const column = $(e.target.closest('.kanban-dropdown')).data('trigger-button').closest('.kanban-column');
// Hide that column
hideColumn(getColumnIDFromElement(column));
});
$(self.element).on('click', '.item-details-panel .kanban-item-edit-team', (e) => {
self.showTeamModal($(e.target).closest('.item-details-panel').data('card'));
});
$(self.element + ' .kanban-container').on('click', '.kanban-item-remove', function(e) {
// Get root dropdown, then the button that triggered it, and finally the card that the button is in
const card = $(e.target.closest('.kanban-dropdown')).data('trigger-button').closest('.kanban-item').prop('id');
// Try to delete that card item
deleteCard(card, undefined, undefined);
});
$(self.element + ' .kanban-container').on('click', '.kanban-collapse-column', function(e) {
self.toggleCollapseColumn(e.target.closest('.kanban-column'));
});
$(self.element).on('click', '.kanban-add-column', function() {
refreshAddColumnForm();
});
$(self.add_column_form).on('input', "input[name='column-name-filter']", function() {
const filter_input = $(this);
$(self.add_column_form + ' li').hide();
$(self.add_column_form + ' li').filter(function() {
return $(this).text().toLowerCase().includes(filter_input.val().toLowerCase());
}).show();
});
$(self.add_column_form).on('change', "input[type='checkbox']", function() {
const column_id = $(this).parent().data('list-id');
if (column_id !== undefined) {
if ($(this).is(':checked')) {
showColumn(column_id);
} else {
hideColumn(column_id);
}
}
});
$(self.add_column_form).on('submit', 'form', function(e) {
e.preventDefault();
});
$(self.add_column_form).on('click', '.kanban-create-column', function() {
const toolbar = $(self.element + ' .kanban-toolbar');
$(self.add_column_form).css({
display: 'none'
});
$(self.create_column_form).css({
display: 'block',
position: 'fixed',
left: toolbar.offset().left + toolbar.outerWidth(true) - $(self.create_column_form).outerWidth(true),
top: toolbar.offset().top + toolbar.outerHeight(true)
});
});
$(self.create_column_form).on('submit', 'form', function(e) {
e.preventDefault();
const toolbar = $(self.element + ' .kanban-toolbar');
$(self.create_column_form).css({
display: 'none'
});
const name = $(self.create_column_form + " input[name='name']").val();
$(self.create_column_form + " input[name='name']").val("");
const color = $(self.create_column_form + " input[name='color']").val();
createColumn(name, {color: color}, function() {
// Refresh add column list
refreshAddColumnForm();
$(self.add_column_form).css({
display: 'block',
position: 'fixed',
left: toolbar.offset().left + toolbar.outerWidth(true) - $(self.add_column_form).outerWidth(true),
top: toolbar.offset().top + toolbar.outerHeight(true)
});
});
});
$('#kanban-add-dropdown li').on('click', function(e) {
e.preventDefault();
const selection = $(this).closest('li');
// The add dropdown is a single-level dropdown, so the parent is the ul element
const dropdown = selection.parent();
// Get the button that triggered the dropdown and then get the column that it is a part of
// This is because the dropdown exists outside all columns and is not recreated each time it is opened
const column = $($(dropdown.data('trigger-button')).closest('.kanban-column'));
// kanban-add-ITEMTYPE (We want the ITEMTYPE token at position 2)
const itemtype = selection.prop('id').split('-')[2];
self.clearAddItemForms(column);
self.showAddItemForm(column, itemtype);
delayRefresh();
});
$('#kanban-bulk-add-dropdown li').on('click', function(e) {
e.preventDefault();
const selection = $(this).closest('li');
// Traverse all the way up to the top-level overflow dropdown
const dropdown = selection.closest('.kanban-dropdown');
// Get the button that triggered the dropdown and then get the column that it is a part of
// This is because the dropdown exists outside all columns and is not recreated each time it is opened
const column = $($(dropdown.data('trigger-button')).closest('.kanban-column'));
// kanban-bulk-add-ITEMTYPE (We want the ITEMTYPE token at position 3)
const itemtype = selection.prop('id').split('-')[3];
// Force-close the full dropdown
dropdown.css({'display': 'none'});
self.clearAddItemForms(column);
self.showBulkAddItemForm(column, itemtype);
delayRefresh();
});
const switcher = $("select[name='kanban-board-switcher']").first();
$(self.element + ' .kanban-toolbar').on('select2:select', switcher, function(e) {
const items_id = e.params.data.id;
$.ajax({
type: "GET",
url: (self.ajax_root + "kanban.php"),
data: {
action: "get_url",
itemtype: self.item.itemtype,
items_id: items_id
},
success: function(url) {
window.location = url;
}
});
});
$(self.element).on('input', '.kanban-add-form input, .kanban-add-form textarea', function() {
delayRefresh();
});
if (!self.rights.canOrderCard()) {
$(self.element).on(
'mouseenter',
'.kanban-column',
function () {
if (self.is_sorting_active) {
return; // Do not change readonly states if user is sorting elements
}
// If user cannot order cards, make items temporarily readonly except for current column.
$(this).find('.kanban-body > li').removeClass('temporarily-readonly');
$(this).siblings().find('.kanban-body > li').addClass('temporarily-readonly');
}
);
$(self.element).on(
'mouseleave',
'.kanban-column',
function () {
if (self.is_sorting_active) {
return; // Do not change readonly states if user is sorting elements
}
$(self.element).find('.kanban-body > li').removeClass('temporarily-readonly');
}
);
}
$(self.element + ' .kanban-container').on('submit', '.kanban-add-form:not(.kanban-bulk-add-form)', function(e) {
e.preventDefault();
const form = $(e.target);
const data = {
inputs: form.serialize(),
itemtype: form.prop('id').split('_')[2],
action: 'add_item'
};
$.ajax({
method: 'POST',
url: (self.ajax_root + "kanban.php"),
data: data
}).done(function() {
self.refresh();
});
});
$(self.element + ' .kanban-container').on('click', '.kanban-item .kanban-item-title', function(e) {
e.preventDefault();
const card = $(e.target).closest('.kanban-item');
self.showCardPanel(card);
});
};
const showModal = (content, data) => {
const modal = $('#kanban-modal');
modal.removeData();
modal.data(data);
modal.find('.modal-body').html(content);
modal.modal('show');
};
const hideModal = () => {
$('#kanban-modal').modal('hide');
};
/**
* (Re-)Create the list of columns that can be shown/hidden.
* This involves fetching the list of valid columns from the server.
* @since 9.5.0
*/
const refreshAddColumnForm = function() {