-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathactivity-panel.js
More file actions
1577 lines (1458 loc) · 57.8 KB
/
activity-panel.js
File metadata and controls
1577 lines (1458 loc) · 57.8 KB
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 (C) 2013-2024 Combodo SAS
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
;
$(function()
{
$.widget( 'itop.activity_panel',
{
// default options
options:
{
datetime_format: null,
datetimes_reformat_limit: 7, // In days
transaction_id: null, // Null until the user gets the lock on the object
lock_enabled: false, // Should only be true when object mode is set to "view" and the "concurrent_lock_enabled" config. param. enabled
lock_status: null,
lock_token: null,
lock_watcher_period: 30, // Period (in seconds) between lock status update, uses the "activity_panel.lock_watcher_period" config. param.
lock_endpoint: null,
show_multiple_entries_submit_confirmation: true,
save_state_endpoint: null,
last_loaded_entries_ids: {},
load_more_entries_endpoint: null,
},
css_classes:
{
is_expanded: 'ibo-is-expanded',
is_reduced: 'ibo-is-reduced',
is_opened: 'ibo-is-opened',
is_closed: 'ibo-is-closed',
is_active: 'ibo-is-active',
is_visible: 'ibo-is-visible',
is_hidden: 'ibo-is-hidden',
is_draft: 'ibo-is-draft',
is_current_user: 'ibo-is-current-user',
},
js_selectors:
{
panel_togglers: '[data-role="ibo-activity-panel--togglers"]',
panel_size_expand: '[data-role="ibo-activity-panel--expand-icon"]',
panel_size_reduce: '[data-role="ibo-activity-panel--reduce-icon"]',
panel_size_close: '[data-role="ibo-activity-panel--close-icon"]',
panel_size_open: '[data-role="ibo-activity-panel--closed-cover"]',
tab_toggler: '[data-role="ibo-activity-panel--tab-toggler"]',
tab_title: '[data-role="ibo-activity-panel--tab-title"]',
tabs_toolbars: '[data-role="ibo-activity-panel--tabs-toolbars"]',
tab_toolbar: '[data-role="ibo-activity-panel--tab-toolbar"]',
tab_toolbar_action: '[data-role="ibo-activity-panel--tab-toolbar-action"]',
lock_hint: '[data-role="ibo-caselog-entry-form--lock-indicator"]',
lock_message: '[data-role="ibo-caselog-entry-form--lock-message"]',
caselog_tab_open_all: '[data-role="ibo-activity-panel--caselog-open-all"]',
caselog_tab_close_all: '[data-role="ibo-activity-panel--caselog-close-all"]',
activity_filter: '[data-role="ibo-activity-panel--filter"]',
activity_filter_options: '[data-role="ibo-activity-panel--filter-options"]',
activity_filter_options_toggler: '[data-role="ibo-activity-panel--filter-options-toggler"]',
activity_filter_option_input: '[data-role="ibo-activity-panel--filter-option-input"]',
authors_count: '[data-role="ibo-activity-panel--tab-toolbar-info-authors-count"]',
messages_count: '[data-role="ibo-activity-panel--tab-toolbar-info-messages-count"]',
compose_button: '[data-role="ibo-activity-panel--add-caselog-entry-button"]',
compose_menu: '#ibo-activity-panel--compose-menu',
compose_menu_item: '#ibo-activity-panel--compose-menu [data-role="ibo-popover-menu--item"]',
caselog_entry_form: '[data-role="ibo-caselog-entry-form"]',
caselog_entry_forms_confirmation_dialog: '[data-role="ibo-activity-panel--entry-forms-confirmation-dialog"]',
caselog_entry_forms_confirmation_preference_input: '[data-role="ibo-activity-panel--entry-forms-confirmation-preference-input"]',
body: '[data-role="ibo-activity-panel--body"]',
entry_group: '[data-role="ibo-activity-panel--entry-group"]',
entry: '[data-role="ibo-activity-entry"]',
entry_medallion: '[data-role="ibo-activity-entry--medallion"]',
entry_main_information: '[data-role="ibo-activity-entry--main-information"]',
entry_author_name: '[data-role="ibo-activity-entry--author-name"]',
entry_datetime: '[data-role="ibo-activity-entry--datetime"]',
edits_entry_long_description: '[data-role="ibo-edits-entry--long-description"]',
edits_entry_long_description_toggler: '[data-role="ibo-edits-entry--long-description-toggler"]',
notification_entry_long_description: '[data-role="ibo-notification-entry--long-description"]',
notification_entry_long_description_toggler: '[data-role="ibo-notification-entry--long-description-toggler"]',
load_more_entries_container: '[data-role="ibo-activity-panel--load-more-entries-container"]',
load_more_entries: '[data-role="ibo-activity-panel--load-more-entries"]',
load_more_entries_icon: '[data-role="ibo-activity-panel--load-more-entries-icon"]',
load_all_entries: '[data-role="ibo-activity-panel--load-all-entries"]',
load_all_entries_icon: '[data-role="ibo-activity-panel--load-all-entries-icon"]',
},
enums: {
tab_types: {
caselog: 'caselog',
activity: 'activity',
},
entry_types: {
caselog: 'caselog',
transition: 'transition',
edits: 'edits',
},
lock_status: {
// Default, we can't be sure an object is unlocked as we only check from time to time
unknown: 'unknown',
// Current user wants the lock, we are trying to get it
request_pending: 'request_pending',
// Current user does not need the lock anymore
release_pending: 'release_pending',
// Current user has the lock
locked_by_myself: 'locked_by_myself',
// Object is locked by another user
locked_by_someone_else: 'locked_by_someone_else',
},
},
release_lock_promise_resolve: null, // N°4494 - Resolve callback of the Promise used for the action following the log entry send, which must be done only once the lock is released
// the constructor
_create: function () {
this.element.addClass('ibo-activity-panel');
// Should be initialized globally, but as we don't actually do it
moment.locale(GetUserLanguage());
this._bindEvents();
// Lock
if (null === this.options.lock_status) {
this.options.lock_status = this.enums.lock_status.unknown;
}
if (true === this.options.lock_enabled) {
this._InitializeLockWatcher();
}
this._InitializeCurrentTab();
this._ApplyEntriesFilters();
this._UpdateMessagesCounters();
this._UpdateFiltersCheckboxesFromOptions();
this._ReformatDateTimes();
this._PrepareEntriesSubmitConfirmationDialog();
this.element.trigger('ready.activity_panel.itop');
},
// events bound via _bind are removed automatically
// revert other modifications here
_destroy: function () {
this.element.removeClass('ibo-activity-panel');
},
_bindEvents: function () {
const me = this;
const oBodyElem = $('body');
// Tabs title
// - Click on the panel reduce/expand togglers
this.element.find(this.js_selectors.panel_size_expand+', '+this.js_selectors.panel_size_reduce).on('click', function (oEvent) {
me._onPanelSizeIconClick(oEvent);
});
// - Click on the panel close/open togglers
this.element.find(this.js_selectors.panel_size_close+', '+this.js_selectors.panel_size_open).on('click', function (oEvent) {
me._onPanelDisplayIconClick(oEvent);
});
// - Click on a tab title
this.element.find(this.js_selectors.tab_title).on('click', function (oEvent) {
me._onTabTitleClick(oEvent, $(this));
});
// Tabs toolbar
// - Change on a filter
this.element.find(this.js_selectors.activity_filter).on('change', function () {
me._onFilterChange($(this));
});
// - Click on a filter options toggler
this.element.find(this.js_selectors.activity_filter_options_toggler).on('click', function (oEvent) {
me._onFilterOptionsTogglerClick(oEvent, $(this));
})
// - Change on a filter option
this.element.find(this.js_selectors.activity_filter_option_input).on('change', function () {
me._onFilterOptionChange($(this));
});
// - Click on open all case log messages
this.element.find(this.js_selectors.caselog_tab_open_all).on('click', function () {
me._onOpenAllEntriesClick();
});
// - Click on close all case log messages
this.element.find(this.js_selectors.caselog_tab_close_all).on('click', function () {
me._onCloseAllEntriesClick();
});
// Entry form
// - Click on the compose button
this.element.find(this.js_selectors.compose_button).on('click', function (oEvent) {
me._onComposeButtonClick(oEvent);
});
// - Click on the compose menu items
this.element.find(this.js_selectors.compose_menu_item).on('click', function (oEvent) {
me._onComposeMenuItemClick(oEvent, $(this));
});
// - Draft value ongoing
this.element.on('draft.caselog_entry_form.itop', function (oEvent, oData) {
me._onDraftEntryForm(oData.attribute_code);
});
// - Empty value
this.element.on('emptied.caselog_entry_form.itop', function (oEvent, oData) {
me._onEmptyEntryForm(oData.attribute_code);
});
// - Entry form cancelled
this.element.on('cancelled_form.caselog_entry_form.itop', function () {
me._onCancelledEntryForm();
});
// - Entry form submission request
this.element.on('requested_submission.caselog_entry_form.itop', function (oEvent, oData) {
me._onRequestSubmission(oEvent, oData);
});
// Entries
// - Click on a closed case log message
this.element.on('click', this.js_selectors.entry+'.'+this.css_classes.is_closed+' '+this.js_selectors.entry_main_information, function (oEvent) {
me._onClosedEntryClick($(this).closest(me.js_selectors.entry));
});
// - Click on an edits entry's long description toggler
this.element.on('click', this.js_selectors.edits_entry_long_description_toggler, function (oEvent) {
me._onEntryLongDescriptionTogglerClick(oEvent, $(this).closest(me.js_selectors.entry));
});
// - Click on an notification entry's long description toggler
this.element.on('click', this.js_selectors.notification_entry_long_description_toggler, function (oEvent) {
me._onEntryLongDescriptionTogglerClick(oEvent, $(this).closest(me.js_selectors.entry));
});
// - Click on load more entries button
this.element.find(this.js_selectors.load_more_entries).on('click', function (oEvent) {
me._onLoadMoreEntriesButtonClick(oEvent);
});
// - Click on load all entries button
this.element.find(this.js_selectors.load_all_entries).on('click', function (oEvent) {
me._onLoadAllEntriesButtonClick(oEvent);
});
// Processing / cleanup when the leaving page
$(window).on('unload', function() {
if (true === me._HasDraftEntries()) {
return me._onUnload();
}
});
// Mostly for outside clicks that should close elements
oBodyElem.on('click', function (oEvent) {
me._onBodyClick(oEvent);
});
// Mostly for hotkeys
oBodyElem.on('keyup', function (oEvent) {
me._onBodyKeyUp(oEvent);
});
},
// Events callbacks
_onPanelSizeIconClick: function (oEvent) {
// Avoid anchor glitch
oEvent.preventDefault();
// Toggle menu
this.element.toggleClass(this.css_classes.is_expanded);
this._SaveStatePreferences();
},
_onPanelDisplayIconClick: function (oEvent) {
// Avoid anchor glitch
oEvent.preventDefault();
// Toggle menu
this.element.toggleClass(this.css_classes.is_closed);
this._SaveStatePreferences();
},
_onTabTitleClick: function (oEvent, oTabTitleElem) {
// Avoid anchor glitch
oEvent.preventDefault();
let oState = {};
const sId = this.element.attr('id');
const oTabTogglerElem = oTabTitleElem.closest(this.js_selectors.tab_toggler);
const sTabType = oTabTogglerElem.attr('data-tab-type');
// Show tab toggler
this.element.find(this.js_selectors.tab_toggler).removeClass(this.css_classes.is_active);
oTabTogglerElem.addClass(this.css_classes.is_active);
// Show toolbar and entries
this.element.find(this.js_selectors.tab_toolbar).removeClass(this.css_classes.is_active);
if(sTabType === 'caselog')
{
const sCaselogAttCode = oTabTogglerElem.attr('data-caselog-attribute-code');
this._ShowCaseLogTab(sCaselogAttCode);
oState[sId] = "caselog-"+sCaselogAttCode;
}
else
{
this.element.find(this.js_selectors.tab_toolbar + '[data-tab-type="activity"]').addClass(this.css_classes.is_active);
this._ShowActivityTab();
oState[sId] = "activity";
}
// Add current activity tab to url hash
$.bbq.pushState(oState);
},
/**
* @param oInputElem {Object} jQuery object representing the filter's input
* @private
*/
_onFilterChange: function(oInputElem)
{
// Propagate on filter options
if ('caselogs' === oInputElem.attr('name')) {
oInputElem.closest(this.js_selectors.tab_toolbar_action).find(this.js_selectors.activity_filter_option_input).prop('checked', oInputElem.prop('checked'));
}
this._ApplyEntriesFilters();
},
/**
* @param oEvent {Object} jQuery event
* @param oElem {Object} jQuery object representing the filter's options toggler
* @private
*/
_onFilterOptionsTogglerClick: function(oEvent, oElem)
{
oEvent.preventDefault();
this._ToggleFilterOptions(oElem.closest(this.js_selectors.tab_toolbar_action).find(this.js_selectors.activity_filter));
},
/**
* @param oInputElem {Object} jQuery object representing the filter option's input
* @private
*/
_onFilterOptionChange: function(oInputElem)
{
const oFilterOptionsElem = oInputElem.closest(this.js_selectors.activity_filter_options);
const oFilterInputElem = oInputElem.closest(this.js_selectors.tab_toolbar_action).find(this.js_selectors.activity_filter);
this._UpdateFiltersCheckboxesFromOptions();
this._ApplyEntriesFilters();
},
_onOpenAllEntriesClick: function()
{
this._OpenAllEntries();
},
_onCloseAllEntriesClick: function()
{
this._CloseAllEntries();
},
/**
* @param oEvent {Object}
* @return {void}
* @private
*/
_onComposeButtonClick: function (oEvent) {
oEvent.preventDefault();
const oActiveTabData = this._GetActiveTabData();
// If on a caselog tab, open its form if it has one
if ((this.enums.tab_types.caselog === oActiveTabData.type) && this._HasCaseLogEntryFormForTab(oActiveTabData.att_code)) {
// Note: Stop propagation to avoid the menu to be opened automatically by the popover handler
oEvent.stopImmediatePropagation();
this._ShowCaseLogTab(oActiveTabData.att_code);
this._ShowCaseLogsEntryForms();
this._SetFocusInCaseLogEntryForm(oActiveTabData.att_code);
}
// Else (activity tab) if only 1 clog tab, open it directly
else if (this._GetCaseLogEntryFormCount() === 1) {
// Note: Stop propagation to avoid the menu to be opened automatically by the popover handler
oEvent.stopImmediatePropagation();
// Simulate click on the only menu item
this.element.find(this.js_selectors.compose_menu_item+':first').trigger('click');
}
// Else, the compose menu will open automatically
},
/**
* @param oEvent {Object}
* @param oItemElem {Object} jQuery object representing the clicked item
* @return {void}
* @private
*/
_onComposeMenuItemClick: function (oEvent, oItemElem) {
oEvent.preventDefault();
// Change tab
this.element.find(this.js_selectors.tab_toggler+'[data-tab-type="'+this.enums.tab_types.caselog+'"][data-caselog-attribute-code="'+oItemElem.attr('data-caselog-attribute-code')+'"]')
.find(this.js_selectors.tab_title)
.trigger('click');
// Then open editor
this.element.find(this.js_selectors.compose_button).trigger('click');
},
/**
* @param oEvent {Object}
* @return {void}
* @private
*/
_onLoadMoreEntriesButtonClick: function (oEvent) {
oEvent.preventDefault();
this._LoadMoreEntries();
},
/**
* @param oEvent {Object}
* @return {void}
* @private
*/
_onLoadAllEntriesButtonClick: function (oEvent) {
oEvent.preventDefault();
this._LoadMoreEntries(false);
},
/**
* Indicate that there is a draft entry and will request lock on the object
*
* @param sCaseLogAttCode {string} Attribute code of the case log entry form being draft
* @private
*/
_onDraftEntryForm: function (sCaseLogAttCode) {
// Put draft indicator
this.element.find(this.js_selectors.tab_toggler+'[data-tab-type="'+this.enums.tab_types.caselog+'"][data-caselog-attribute-code="'+sCaseLogAttCode+'"]').addClass(this.css_classes.is_draft);
// Register leave handler blockers
this._RegisterLeaveHandlerBlockers();
if (this.options.lock_enabled === true) {
// Request lock
this._RequestLock();
} else {
// Only enable buttons
this.element.find(this.js_selectors.caselog_entry_form + '[data-attribute-code="' + sCaseLogAttCode + '"]').trigger('enable_submission.caselog_entry_form.itop');
}
},
/**
* Remove indication of a draft entry and will cancel the lock (acquired or pending) if no draft entry left
*
* @param sCaseLogAttCode {string} Attribute code of the case log entry form being emptied
* @private
*/
_onEmptyEntryForm: function (sCaseLogAttCode) {
// Remove draft indicator
this.element.find(this.js_selectors.tab_toggler+'[data-tab-type="'+this.enums.tab_types.caselog+'"][data-caselog-attribute-code="'+sCaseLogAttCode+'"]').removeClass(this.css_classes.is_draft);
// Unregister leave handler blockers (only in view mode, otherwise it would remove blocker on main form fields as well)
if (this._GetHostObjectMode() === 'view') {
this._UnregisterLeaveHandlerBlockers();
}
if (this.options.lock_enabled === true) {
// Cancel lock if all forms empty
if (false === this._HasDraftEntries()) {
this._CancelLock();
}
} else {
// Only disable buttons
this.element.find(this.js_selectors.caselog_entry_form + '[data-attribute-code="' + sCaseLogAttCode + '"]').trigger('disable_submission.caselog_entry_form.itop');
}
},
_onCancelledEntryForm: function () {
this._EmptyCaseLogsEntryForms();
this._HideCaseLogsEntryForms();
},
/**
* Called on submission request from a case log entry form, will display a confirmation dialog if multiple case logs have
* been edited and the user hasn't dismiss the dialog.
* @private
*/
_onRequestSubmission: async function (oEvent, oData) {
// Check lock state
if ((this.options.lock_enabled === true) && (this.enums.lock_status.locked_by_myself !== this.options.lock_status)) {
CombodoJSConsole.Debug('ActivityPanel: Could not submit entries, current user does not have the lock on the object');
return;
}
let sStimulusCode = (undefined !== oData.stimulus_code) ? oData.stimulus_code : null
// If several entry forms filled, show a confirmation message
if ((true === this.options.show_multiple_entries_submit_confirmation) && (Object.keys(await this._GetEntriesFromAllForms()).length > 1)) {
this._ShowEntriesSubmitConfirmation(sStimulusCode);
}
// Else push data directly to the server
else {
this._SendEntriesToServer(sStimulusCode);
}
},
_onClosedEntryClick: function (oEntryElem) {
this._OpenEntry(oEntryElem);
},
_onEntryLongDescriptionTogglerClick: function (oEvent, oEntryElem) {
// Avoid anchor glitch
oEvent.preventDefault();
oEntryElem.toggleClass(this.css_classes.is_closed);
},
/**
* Callback for mouse clicks that should interact with the activity panel (eg. Clic outside a dropdown should close it, ...)
*
* @param oEvent {Object} The jQuery event
* @private
*/
_onBodyClick: function(oEvent)
{
// Hide all filters' options only if click wasn't on one of them
if(($(oEvent.target).closest(this.js_selectors.activity_filter_options_toggler).length === 0)
&& $(oEvent.target).closest(this.js_selectors.activity_filter_options).length === 0) {
this._HideAllFiltersOptions();
}
},
/**
* Callback for key hits that should interact with the activity panel (eg. "Esc" to close all dropdowns, ...)
*
* @param oEvent {Object} The jQuery event
* @private
*/
_onBodyKeyUp: function (oEvent) {
// On "Esc" key
if (oEvent.key === 'Escape') {
// Hide all filters's options
this._HideAllFiltersOptions();
}
},
/**
* Called when the user leave the page, will remove the current lock if any draft entries
* @private
*/
_onUnload: function() {
return OnUnload(this.options.transaction_id, this.element.attr('data-object-class'), this.element.attr('data-object-id'), this.options.lock_token);
},
// Methods
// - Helpers on host object
_GetHostObjectClass: function () {
return this.element.attr('data-object-class');
},
_GetHostObjectID: function () {
return this.element.attr('data-object-id');
},
_GetHostObjectMode: function () {
return this.element.attr('data-object-mode');
},
/**
* Save to the user pref. the expanded and closed states the host object class / mode
*
* @return {void}
* @private
*/
_SaveStatePreferences: function () {
$.post(
this.options.save_state_endpoint,
{
'operation': 'activity_panel.save_state',
'object_class': this._GetHostObjectClass(),
'object_mode': this._GetHostObjectMode(),
'is_expanded': this.element.hasClass(this.css_classes.is_expanded),
'is_closed': this.element.hasClass(this.css_classes.is_closed),
}
);
},
// - Helpers on dates
/**
* Reformat date times to be relative (only if they are not too far in the past)
* @private
*/
_ReformatDateTimes: function () {
const me = this;
this.element.find(this.js_selectors.entry_datetime).each(function () {
const oEntryDateTime = moment($(this).attr('data-formatted-datetime'), me.options.datetime_format);
const oNowDateTime = moment();
// Reformat date time only if it is not too far in the past (eg. "2 years ago" is not easy to interpret)
const fDays = moment.duration(oNowDateTime.diff(oEntryDateTime)).asDays();
if (fDays < me.options.datetimes_reformat_limit) {
$(this).text(moment($(this).attr('data-formatted-datetime'), me.options.datetime_format).fromNow());
}
});
},
// - Helpers on tabs
/**
* @returns {Object} Data on the active tab:
*
* - Its type
* - Optionally, its attribute code
* - Optionally, its rank
* @private
*/
_GetActiveTabData: function()
{
const oTabTogglerElem = this.element.find(this.js_selectors.tab_toggler + '.' + this.css_classes.is_active);
// Consistency check
if(oTabTogglerElem.length === 0) {
throw 'No active tab, this should not be possible.';
}
const sTabType = oTabTogglerElem.attr('data-tab-type');
let oTabData = {
type: sTabType,
};
// Additional data for caselog tab
if (this.enums.tab_types.caselog === sTabType) {
oTabData.att_code = oTabTogglerElem.attr('data-caselog-attribute-code');
oTabData.rank = oTabTogglerElem.attr('data-caselog-rank');
}
return oTabData;
},
/**
* Set a tab active if it's specified in the url
* @returns {void}
* @private
*/
_InitializeCurrentTab : function(){
const sTabId = $.bbq.getState(this.element.attr('id'), true);
if(sTabId !== undefined){
let oTabTogglerElem = null;
if(sTabId.startsWith("caselog-")){
oTabTogglerElem = this._GetTabTogglerFromCaseLogAttCode(sTabId.replace("caselog-", "")).find(this.js_selectors.tab_title).trigger('click')
}
else if(sTabId === "activity"){
oTabTogglerElem = this.element.find(this.js_selectors.tab_toggler + '[data-tab-type="activity"]').find(this.js_selectors.tab_title).trigger('click')
}
// Scroll to the tab toggler if found
if(oTabTogglerElem !== null){
oTabTogglerElem[0].scrollIntoView();
}
}
},
/**
* @returns {Object} Active tab toolbar jQuery element
* @private
*/
_GetActiveTabToolbarElement: function() {
const oActiveTabData = this._GetActiveTabData();
let sSelector = this.js_selectors.tab_toolbar+'[data-tab-type="'+oActiveTabData.type+'"]';
if (this.enums.tab_types.caselog === oActiveTabData.type) {
sSelector += '[data-caselog-attribute-code="'+oActiveTabData.att_code+'"]';
}
return this.element.find(sSelector);
},
/**
* Show the case log tab of sCaseLogAttCode and applies its filters
* Note: It doesn't open the entry form
*
* @param sCaseLogAttCode {string}
* @return {void}
* @private
*/
_ShowCaseLogTab: function (sCaseLogAttCode) {
this.element.find(this.js_selectors.tab_toolbar+'[data-tab-type="caselog"][data-caselog-attribute-code="'+sCaseLogAttCode+'"]').addClass(this.css_classes.is_active);
// Show only entries from this case log
this._ShowAllEntries();
this._ApplyEntriesFilters();
},
_ShowActivityTab: function () {
// Show all entries but regarding the current filters
this._ShowAllEntries();
this._ApplyEntriesFilters();
},
GetCaseLogRank: function(sCaseLog)
{
let iIdx = 0;
let oCaselogTab = this.element.find(this.js_selectors.tab_toggler +
'[data-tab-type="caselog"]' +
'[data-caselog-attribute-code="'+ sCaseLog +'"]'
);
if(oCaselogTab.length > 0 && oCaselogTab.attr('data-caselog-rank'))
{
iIdx = parseInt(oCaselogTab.attr('data-caselog-rank'));
}
return iIdx;
},
// - Helpers on toolbars
/**
* Update the main filters checkboxes depending on the state of their filter's options.
* The main goal is to have an "indeterminated" state.
*
* @return {void}
* @private
*/
_UpdateFiltersCheckboxesFromOptions: function()
{
const me = this;
this.element.find(this.js_selectors.activity_filter_options).each(function(){
const oFilterOptionsElem = $(this);
const iTotalOptionsCount = oFilterOptionsElem.find(me.js_selectors.activity_filter_option_input).length;
const iCheckedOptionsCount = oFilterOptionsElem.find(me.js_selectors.activity_filter_option_input + ':checked').length;
let bChecked = false;
let bIndeterminate = false;
if (iCheckedOptionsCount === iTotalOptionsCount) {
bChecked = true;
}
else if ((0 < iCheckedOptionsCount) && (iCheckedOptionsCount < iTotalOptionsCount)) {
bIndeterminate = true;
}
oFilterOptionsElem.closest(me.js_selectors.tab_toolbar_action).find(me.js_selectors.activity_filter).prop({
indeterminate: bIndeterminate,
checked: bChecked
});
});
},
/**
* Show the oFilterElem's options
*
* @param oFilterElem {Object}
* @private
*/
_ShowFilterOptions: function(oFilterElem)
{
oFilterElem.parent().find(this.js_selectors.activity_filter_options_toggler).removeClass(this.css_classes.is_closed);
},
/**
* Hide the oFilterElem's options
*
* @param oFilterElem {Object}
* @private
*/
_HideFilterOptions: function(oFilterElem)
{
oFilterElem.parent().find(this.js_selectors.activity_filter_options_toggler).addClass(this.css_classes.is_closed);
},
/**
* Toggle the visibility of the oFilterElem's options
*
* @param oFilterElem {Object}
* @private
*/
_ToggleFilterOptions: function(oFilterElem)
{
oFilterElem.parent().find(this.js_selectors.activity_filter_options_toggler).toggleClass(this.css_classes.is_closed);
},
/**
* Hide all the filters' options from all toolbars
*
* @private
*/
_HideAllFiltersOptions: function () {
const me = this;
this.element.find(this.js_selectors.activity_filter_options_toggler).each(function () {
me._HideFilterOptions($(this));
});
},
// - Helpers on case logs entry forms
/**
* @returns {integer} The number of caselog entry forms
* @private
* @since 3.1.0
*/
_GetCaseLogEntryFormCount: function () {
return this.element.find(this.js_selectors.caselog_entry_form).length;
},
/**
* @param sCaseLogAttCode {string}
* @returns {boolean} Return true if there is a case log for entry for the sCaseLogAttCode tab
* @private
*/
_HasCaseLogEntryFormForTab: function (sCaseLogAttCode) {
return (this.element.find(this.js_selectors.tab_toolbar+'[data-tab-type="'+this.enums.tab_types.caselog+'"][data-caselog-attribute-code="'+sCaseLogAttCode+'"]').find(this.js_selectors.caselog_entry_form).length > 0);
},
_SetFocusInCaseLogEntryForm: function (sCaseLogAttCode) {
this.element.find(this.js_selectors.caselog_entry_form+'[data-attribute-code="'+sCaseLogAttCode+'"]').trigger('set_focus.caselog_entry_form.itop');
},
/**
* Show all case logs entry forms.
* Event is triggered on the corresponding elements.
*
* @return {void}
* @private
*/
_ShowCaseLogsEntryForms: function () {
this.element.find(this.js_selectors.caselog_entry_form).trigger('show_form.caselog_entry_form.itop');
this.element.find(this.js_selectors.compose_button).addClass(this.css_classes.is_hidden);
},
/**
* Hide all case logs entry forms.
* Event is triggered on the corresponding elements.
*
* @return {void}
* @private
*/
_HideCaseLogsEntryForms: function () {
this.element.find(this.js_selectors.caselog_entry_form).trigger('hide_form.caselog_entry_form.itop');
this.element.find(this.js_selectors.compose_button).removeClass(this.css_classes.is_hidden);
},
/**
* Empty all case logs entry forms
* Event is triggered on the corresponding elements.
*
* @return {void}
* @private
*/
_EmptyCaseLogsEntryForms: function () {
this.element.find(this.js_selectors.caselog_entry_form).trigger('clear_entry.caselog_entry_form.itop');
},
_FreezeCaseLogsEntryForms: function () {
this.element.find(this.js_selectors.caselog_entry_form).trigger('enter_pending_submission_state.caselog_entry_form.itop');
},
_UnfreezeCaseLogsEntryForms: function () {
this.element.find(this.js_selectors.caselog_entry_form).trigger('leave_pending_submission_state.caselog_entry_form.itop');
},
/**
* @returns {Object} The case logs having a new entry and their values, format is {<ATT_CODE_1>: <HTML_VALUE_1>, <ATT_CODE_2>: <HTML_VALUE_2>}
* @private
*/
_GetEntriesFromAllForms: async function () {
const me = this;
let oEntries = {};
// this.element.find(this.js_selectors.caselog_entry_form).each(async function () {
// const oEntryFormElem = $(this);
// const sEntryFormValue = await oEntryFormElem.triggerHandler('get_entry.caselog_entry_form.itop');
// console.log('huhu');
//
// if ('' !== sEntryFormValue) {
// const sCaseLogAttCode = oEntryFormElem.attr('data-attribute-code');
// oEntries[sCaseLogAttCode] = {
// value: sEntryFormValue,
// rank: me.element.find(me.js_selectors.tab_toggler+'[data-tab-type="caselog"][data-caselog-attribute-code="'+sCaseLogAttCode+'"]').attr('data-caselog-rank'),
// };
// }
// });
const aFormElements = this.element.find(this.js_selectors.caselog_entry_form);
// Create an array of promises for each form element
const aEntryPromises = aFormElements.map(async (index, element) => {
const oEntryFormElem = $(element);
const sEntryFormValue = await oEntryFormElem.triggerHandler('get_entry.caselog_entry_form.itop');
if ('' !== sEntryFormValue) {
const sCaseLogAttCode = oEntryFormElem.attr('data-attribute-code');
oEntries[sCaseLogAttCode] = {
value: sEntryFormValue,
rank: this.element.find(this.js_selectors.tab_toggler + '[data-tab-type="caselog"][data-caselog-attribute-code="' + sCaseLogAttCode + '"]').attr('data-caselog-rank'),
};
}
}).get(); // convert jQuery object to array
// Wait for all promises to resolve
await Promise.all(aEntryPromises);
return oEntries;
},
/**
* @returns {Object} The case logs having a new entry and their values, format is {<ATT_CODE_1>: <HTML_VALUE_1>, <ATT_CODE_2>: <HTML_VALUE_2>}
* @private
*/
_GetExtraInputsFromAllForms: function () {
const me = this;
let oExtraInputs = {};
this.element.find(this.js_selectors.caselog_entry_form).each(function () {
const oEntryFormElem = $(this);
oExtraInputs = $.extend(oExtraInputs, oEntryFormElem.triggerHandler('get_extra_inputs.caselog_entry_form.itop'));
});
return oExtraInputs;
},
/**
* @return {boolean} True if at least 1 of the entry form is draft (has some text in it)
* @private
*/
_HasDraftEntries: function () {
return Object.keys(this._GetEntriesFromAllForms()).length > 0;
},
/**
* Prepare the dialog for confirmation before submission when several case log entries have been edited.
* @private
*/
_PrepareEntriesSubmitConfirmationDialog: function () {
const me = this;
this.element.find(this.js_selectors.caselog_entry_forms_confirmation_dialog).dialog({
autoOpen: false,
minWidth: 400,
modal: true,
position: {my: "center center", at: "center center", of: this.js_selectors.tabs_toolbars},
close: function () { me._HideEntriesSubmitConfirmation(); },
buttons: [
{
text: Dict.S('UI:Button:Cancel'),
class: 'ibo-is-alternative',
click: function () {
me._HideEntriesSubmitConfirmation();
}
},
{
text: Dict.S('UI:Button:Send'),
class: 'ibo-is-primary',
click: function () {
const bDoNotShowAgain = $(this).find(me.js_selectors.caselog_entry_forms_confirmation_preference_input).prop('checked');
if (bDoNotShowAgain) {
me._SaveSubmitConfirmationPref();
}
// Needs to be retrieved before hiding the dialog as it will wipe out the value in the process
const sStimulusCode = $(this).attr('data-stimulus-code');
me._HideEntriesSubmitConfirmation();
me._SendEntriesToServer(sStimulusCode);
}
},
],
});
},
/**
* Show the confirmation dialog when multiple case log entries have been editied
* @param sStimulusCode {string|null} Code of the stimulus to apply if confirmation is given
* @private
*/
_ShowEntriesSubmitConfirmation: function(sStimulusCode = null)
{
$(this.js_selectors.caselog_entry_forms_confirmation_dialog)
.dialog('open')
.attr('data-stimulus-code', sStimulusCode);
},
/**
* Hide the confirmation dialog for multiple edited case log entries
* @private
*/
_HideEntriesSubmitConfirmation: function()
{
$(this.js_selectors.caselog_entry_forms_confirmation_dialog)
.dialog('close')
.attr('data-stimulus-code', '');
},
/**
* Save that the user don't want the confirmation dialog to be shown in the future
* @private
*/
_SaveSubmitConfirmationPref: function()
{
// Note: We have to send the value as a string because of the API limitation
SetUserPreference('activity_panel.show_multiple_entries_submit_confirmation', 'false', true);
},
/**
* Send the edited case logs entries to the server
* @param sStimulusCode {string|null} Stimulus code to apply after the entries are saved
* @return {void}
* @private
*/
_SendEntriesToServer: async function (sStimulusCode = null) {
const me = this;
const oEntries = await this._GetEntriesFromAllForms();
const oExtraInputs = this._GetExtraInputsFromAllForms();
// Proceed only if entries to send
if (Object.keys(oEntries).length === 0) {
return false;
}
// Prepare parameters
let oParams = $.extend(oExtraInputs, {
operation: 'activity_panel.add_caselog_entries',
object_class: this._GetHostObjectClass(),
object_id: this._GetHostObjectID(),
transaction_id: this.options.transaction_id,
entries: oEntries,
});
// Freeze case logs
this._FreezeCaseLogsEntryForms();
// Send request to server
$.post(
GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
oParams,
'json'
)
.fail(function (oXHR, sStatus, sErrorThrown) {
CombodoModal.OpenErrorModal(sErrorThrown);
})
.done(function (oData) {
if (false === oData.data.success) {
CombodoModal.OpenErrorModal(oData.data.error_message);
return false;
}
// Update the feed and tab toggler message counter
for (let sCaseLogAttCode in oData.data.entries) {
me._AddEntry(oData.data.entries[sCaseLogAttCode], 'start');
me._IncreaseTabTogglerMessagesCounter(sCaseLogAttCode);
}
me._ApplyEntriesFilters();
// Try to fix inline images width
CombodoInlineImage.FixImagesWidth();