forked from PrestaShop/PrestaShop
/
module.js
938 lines (832 loc) · 34 KB
/
module.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
$(document).ready(function() {
var controller = new AdminModuleController();
controller.init();
});
/**
* Module Admin Page Controller.
* @constructor
*/
var AdminModuleController = function() {
this.currentDisplay = '';
this.isCategoryGridDisplayed = false;
this.currentTagsList = [];
this.currentRefCategory = null;
this.currentRefStatus = null;
this.currentSorting = null;
this.baseAddonsUrl = 'https://addons.prestashop.com/';
this.pstaggerInput = null;
this.lastBulkAction = null;
this.isUploadStarted = false;
/**
* Loaded modules list.
* Containing the card and list display.
* @type {Array}
*/
this.modulesList = [];
this.addonsCardGrid = null;
this.addonsCardList = null;
// Selectors into vars to make it easier to change them while keeping same code logic
this.moduleItemGridSelector = '.module-item-grid';
this.moduleItemListSelector = '.module-item-list';
this.categorySelectorLabelSelector = '.module-category-selector-label';
this.categorySelector = '.module-category-selector';
this.categoryItemSelector = '.module-category-menu';
this.addonsLoginButtonSelector = '#addons_login_btn';
this.categoryResetBtnSelector = '.module-category-reset';
this.moduleInstallBtnSelector = 'input.module-install-btn';
this.moduleSortingDropdownSelector = '.module-sorting-author select';
this.categoryGridSelector = '#modules-categories-grid';
this.categoryGridItemSelector = '.module-category-item';
this.addonItemGridSelector = '.module-addons-item-grid';
this.addonItemListSelector = '.module-addons-item-list';
// Upgrade All selectors
this.upgradeAllSource = '.module_action_menu_upgrade_all';
this.upgradeAllTargets = '#modules-list-container-update .module_action_menu_upgrade:visible';
// Bulk action selectors
this.bulkActionDropDownSelector = '.module-bulk-actions select';
this.checkedBulkActionListSelector = '.module-checkbox-bulk-list input:checked';
this.checkedBulkActionGridSelector = '.module-checkbox-bulk-grid input:checked';
this.bulkActionCheckboxGridSelector = '.module-checkbox-bulk-grid';
this.bulkActionCheckboxListSelector = '.module-checkbox-bulk-list';
this.bulkActionCheckboxSelector = '#module-modal-bulk-checkbox';
this.bulkConfirmModalSelector = '#module-modal-bulk-confirm';
this.bulkConfirmModalActionNameSelector = '#module-modal-bulk-confirm-action-name';
this.bulkConfirmModalListSelector = '#module-modal-bulk-confirm-list';
this.bulkConfirmModalAckBtnSelector = '#module-modal-confirm-bulk-ack';
// Placeholders
this.placeholderGlobalSelector = '.module-placeholders-wrapper';
this.placeholderFailureGlobalSelector = '.module-placeholders-failure';
this.placeholderFailureMsgSelector = '.module-placeholders-failure-msg';
this.placeholderFailureRetryBtnSelector = '#module-placeholders-failure-retry';
// Module's statuses selectors
this.statusSelectorLabelSelector = '.module-status-selector-label';
this.statusItemSelector = '.module-status-menu';
this.statusResetBtnSelector = '.module-status-reset';
// Selectors for Module Import and Addons connect
this.addonsConnectModalBtnSelector = '#page-header-desc-configuration-addons_connect';
this.addonsLogoutModalBtnSelector = '#page-header-desc-configuration-addons_logout';
this.addonsImportModalBtnSelector = '#page-header-desc-configuration-add_module';
this.dropZoneModalSelector = '#module-modal-import';
this.dropZoneModalFooterSelector = '#module-modal-import .modal-footer';
this.dropZoneImportZoneSelector = '#importDropzone';
this.addonsConnectModalSelector = '#module-modal-addons-connect';
this.addonsLogoutModalSelector = '#module-modal-addons-logout';
this.addonsConnectForm = '#addons-connect-form';
this.moduleImportModalCloseBtn = '#module-modal-import-closing-cross';
this.moduleImportStartSelector = '.module-import-start';
this.moduleImportProcessingSelector = '.module-import-processing';
this.moduleImportSuccessSelector = '.module-import-success';
this.moduleImportSuccessConfigureBtnSelector = '.module-import-success-configure';
this.moduleImportFailureSelector = '.module-import-failure';
this.moduleImportFailureRetrySelector = '.module-import-failure-retry';
this.moduleImportFailureDetailsBtnSelector = '.module-import-failure-details-action';
this.moduleImportSelectFileManualSelector = '.module-import-start-select-manual';
this.moduleImportFailureMsgDetailsSelector = '.module-import-failure-details';
this.moduleImportConfirmSelector = '.module-import-confirm';
/**
* Initialize all listners and bind everything
* @method init
* @memberof AdminModule
*/
this.init = function () {
this.initBOEventRegistering();
this.loadVariables();
this.initSortingDisplaySwitch();
this.initSortingDropdown();
this.initSearchBlock();
this.initCategorySelect();
this.initCategoriesGrid();
this.initActionButtons();
this.initAddonsSearch();
this.initAddonsConnect();
this.initAddModuleAction();
this.initDropzone();
this.initPageChangeProtection();
this.initBulkActions();
this.initPlaceholderMechanism();
this.initFilterStatusDropdown();
this.fetchModulesList();
this.getNotificationsCount();
};
this.initFilterStatusDropdown = function() {
var self = this;
var body = $('body');
body.on('click', this.statusItemSelector, function () {
// Get data from li DOM input
self.currentRefStatus = parseInt($(this).attr('data-status-ref'));
var statusSelectedDisplayName = $(this).find('a:first').text();
// Change dropdown label to set it to the current status' displayname
$(self.statusSelectorLabelSelector).text(statusSelectedDisplayName);
$(self.statusResetBtnSelector).show();
// Do Search on categoryRef
self.updateModuleVisibility();
});
body.on('click', this.statusResetBtnSelector, function () {
var text = $(this).find('a').text();
$(self.statusSelectorLabelSelector).text(text);
$(this).hide();
self.currentRefStatus = null;
self.updateModuleVisibility();
});
};
this.initBOEventRegistering = function() {
BOEvent.on('Module Disabled', this.onModuleDisabled, this);
BOEvent.on('Module Uninstalled', this.updateTotalResults, this);
};
this.onModuleDisabled = function() {
var moduleItemSelector = this.getModuleItemSelector();
var self = this;
$('.modules-list').each(function() {
var totalForCurrentSelector = $(this).find(moduleItemSelector+':visible').length;
self.updateTotalResults(totalForCurrentSelector, $(this));
});
};
this.initPlaceholderMechanism = function() {
var self = this;
if ($(this.placeholderGlobalSelector).length) {
this.ajaxLoadPage();
}
// Retry loading mechanism
$('body').on('click', this.placeholderFailureRetryBtnSelector, function() {
$(self.placeholderFailureGlobalSelector).fadeOut();
$(self.placeholderGlobalSelector).fadeIn();
self.ajaxLoadPage();
});
};
this.ajaxLoadPage = function() {
var self = this;
$.ajax({
method: 'GET',
url: moduleURLs.catalogRefresh
}).done(function (response) {
if (response.status === true) {
if (typeof response.domElements === 'undefined') response.domElements = null;
if (typeof response.msg === 'undefined') response.msg = null;
var stylesheet = document.styleSheets[0];
var stylesheetRule = '{display: none}';
var moduleGlobalSelector = '.modules-list';
var moduleSortingSelector = '.module-sorting-menu';
var requiredSelectorCombination = moduleGlobalSelector + ', ' + moduleSortingSelector;
if (stylesheet.insertRule) {
stylesheet.insertRule(
requiredSelectorCombination +
stylesheetRule, stylesheet.cssRules.length
);
} else if (stylesheet.addRule) {
stylesheet.addRule(
requiredSelectorCombination,
stylesheetRule,
-1
);
}
$(self.placeholderGlobalSelector).fadeOut(800, function() {
$.each(response.domElements, function(index, element){
$(element.selector).append(element.content);
});
$(moduleGlobalSelector).fadeIn(800).css('display','flex');
$(moduleSortingSelector).fadeIn(800);
$('[data-toggle="popover"]').popover();
self.initCurrentDisplay();
self.fetchModulesList();
});
} else {
$(self.placeholderGlobalSelector).fadeOut(800, function() {
$(self.placeholderFailureMsgSelector).text(response.msg);
$(self.placeholderFailureGlobalSelector).fadeIn(800);
});
}
}).fail(function(response) {
$(self.placeholderGlobalSelector).fadeOut(800, function() {
$(self.placeholderFailureMsgSelector).text(response.statusText);
$(self.placeholderFailureGlobalSelector).fadeIn(800);
});
});
};
this.fetchModulesList = function() {
var self = this;
self.modulesList = [];
$(".modules-list").each(function() {
var container = $(this);
container.find(".module-item").each(function() {
var $this = $(this);
self.modulesList.push({
domObject: $this,
id: $this.attr('data-id'),
name: $this.attr('data-name').toLowerCase(),
scoring: parseFloat($this.attr('data-scoring')),
logo: $this.attr('data-logo'),
author: $this.attr('data-author').toLowerCase(),
version: $this.attr('data-version'),
description: $this.attr('data-description').toLowerCase(),
techName: $this.attr('data-tech-name').toLowerCase(),
childCategories: $this.attr('data-child-categories'),
categories: $this.attr('data-categories').toLowerCase(),
type: $this.attr('data-type'),
price: parseFloat($this.attr('data-price')),
active: parseInt($this.attr('data-active')),
access: $this.attr('data-last-access'),
display: $this.hasClass('module-item-list') ? 'list' : 'grid',
container: container
});
$this.remove();
});
});
self.addonsCardGrid = $(this.addonItemGridSelector);
self.addonsCardList = $(this.addonItemListSelector);
this.updateModuleVisibility();
$('body').trigger('moduleCatalogLoaded');
};
this.updateModuleVisibility = function() {
var self = this;
if (self.currentSorting) {
// Modules sorting
var order = 'asc';
var key = self.currentSorting;
if (key.split('-').length > 1) {
key = key.split('-')[0];
}
if (self.currentSorting.indexOf('-desc') != -1) {
order = 'desc';
}
function currentCompare(a, b) {
if (a[key] < b[key]) return -1;
if (a[key] > b[key]) return 1;
return 0;
}
self.modulesList.sort(currentCompare);
if (order == 'desc') {
self.modulesList.reverse();
}
}
$('.modules-list').html('');
// Modules visibility management
for (var i = 0; i < this.modulesList.length; i++) {
var currentModule = this.modulesList[i];
if (currentModule.display == this.currentDisplay) {
var isVisible = true;
if (this.currentRefCategory !== null) {
isVisible &= currentModule.categories === this.currentRefCategory;
}
if (self.currentRefStatus !== null) {
isVisible &= currentModule.active === this.currentRefStatus;
}
if (self.currentTagsList.length) {
var tagExists = false;
$.each(self.currentTagsList, function(index, value) {
value = value.toLowerCase();
tagExists |= (
currentModule.name.indexOf(value) != -1
|| currentModule.description.indexOf(value) != -1
|| currentModule.author.indexOf(value) != -1
|| currentModule.techName.indexOf(value) != -1
);
});
isVisible &= tagExists;
}
if (isVisible) {
currentModule.container.append(currentModule.domObject);
}
}
}
if (this.currentTagsList.length) {
if ('grid' === this.currentDisplay) {
$(".modules-list").append(this.addonsCardGrid);
} else {
$(".modules-list").append(this.addonsCardList);
}
}
this.updateTotalResults();
};
this.initPageChangeProtection = function() {
var self = this;
$(window).on('beforeunload', function() {
if (self.isUploadStarted === true) {
return "It seems some critical operation are running, are you sure you want to change page ? It might cause some unexepcted behaviors.";
}
});
};
this.initBulkActions = function() {
var self = this;
var body = $('body');
body.on('change', this.bulkActionDropDownSelector, function() {
if (0 === $(self.getBulkCheckboxesCheckedSelector()).length) {
$.growl.warning({message: translate_javascripts['Bulk Action - One module minimum']});
return;
}
self.lastBulkAction = $(this).find(':checked').attr('value');
var modulesListString = self.buildBulkActionModuleList();
var actionString = $(this).find(':checked').text().toLowerCase();
$(self.bulkConfirmModalListSelector).html(modulesListString);
$(self.bulkConfirmModalActionNameSelector).text(actionString);
if (self.lastBulkAction !== 'bulk-uninstall') {
$(self.bulkActionCheckboxSelector).hide();
}
$(self.bulkConfirmModalSelector).modal('show');
});
body.on('click', this.bulkConfirmModalAckBtnSelector, function(event) {
event.preventDefault();
event.stopPropagation();
$(self.bulkConfirmModalSelector).modal('hide');
self.doBulkAction(self.lastBulkAction);
});
};
this.buildBulkActionModuleList = function() {
var checkBoxesSelector = this.getBulkCheckboxesCheckedSelector();
var moduleItemSelector = this.getModuleItemSelector();
var alreadyDoneFlag = 0;
var htmlGenerated = '';
$(checkBoxesSelector).each(function() {
if (alreadyDoneFlag != 10) {
var currentElement = $(this).parents(moduleItemSelector);
htmlGenerated += '- ' + currentElement.attr('data-name') + '<br/>';
alreadyDoneFlag += 1;
} else {
// Break each
htmlGenerated += '- ...';
return false;
}
});
return htmlGenerated;
};
this.initAddonsConnect = function () {
var self = this;
// Make addons connect modal ready to be clicked
if ($(this.addonsConnectModalBtnSelector).attr('href') == '#') {
$(this.addonsConnectModalBtnSelector).attr('data-toggle', 'modal');
$(this.addonsConnectModalBtnSelector).attr('data-target', this.addonsConnectModalSelector);
}
if ($(this.addonsLogoutModalBtnSelector).attr('href') == '#') {
$(this.addonsLogoutModalBtnSelector).attr('data-toggle', 'modal');
$(this.addonsLogoutModalBtnSelector).attr('data-target', this.addonsLogoutModalSelector);
}
$('body').on('submit', this.addonsConnectForm, function (event) {
event.preventDefault();
event.stopPropagation();
$.ajax({
method: 'POST',
url: $(this).attr('action'),
dataType: 'json',
data: $(this).serialize(),
beforeSend: function() {
$(self.addonsLoginButtonSelector).show();
$("button.btn[type='submit']", self.addonsConnectForm).hide();
}
}).done(function (response) {
var responseCode = response.success;
var responseMsg = response.message;
if (responseCode === 1) {
location.reload();
} else {
$.growl.error({message: responseMsg});
$(self.addonsLoginButtonSelector).hide();
$("button.btn[type='submit']", self.addonsConnectForm).fadeIn();
}
});
});
};
this.initAddModuleAction = function () {
var addModuleButton = $(this.addonsImportModalBtnSelector);
addModuleButton.attr('data-toggle', 'modal');
addModuleButton.attr('data-target', this.dropZoneModalSelector);
};
this.initDropzone = function () {
var self = this;
var body = $('body');
var dropzone = $('.dropzone');
// Reset modal when click on Retry in case of failure
body.on('click', this.moduleImportFailureRetrySelector, function() {
$(self.moduleImportSuccessSelector + ', ' + self.moduleImportFailureSelector + ', ' + self.moduleImportProcessingSelector).fadeOut(function() {
// Added timeout for a better render of animation and avoid to have displayed at the same time
setTimeout(function() {
$(self.moduleImportStartSelector).fadeIn(function() {
$(self.moduleImportFailureMsgDetailsSelector).hide();
$(self.moduleImportSuccessConfigureBtnSelector).hide();
dropzone.removeAttr('style');
});
}, 550);
});
});
// Reinit modal on exit, but check if not already processing something
body.on('hidden.bs.modal', this.dropZoneModalSelector, function () {
$(self.moduleImportSuccessSelector + ', ' + self.moduleImportFailureSelector).hide();
$(self.moduleImportStartSelector).show();
dropzone.removeAttr('style');
$(self.moduleImportFailureMsgDetailsSelector).hide();
$(self.moduleImportSuccessConfigureBtnSelector).hide();
$(self.dropZoneModalFooterSelector).html('');
$(self.moduleImportConfirmSelector).hide();
});
// Change the way Dropzone.js lib handle file input trigger
body.on(
'click', '.dropzone:not('+this.moduleImportSelectFileManualSelector+', '+this.moduleImportSuccessConfigureBtnSelector+')',
function(event, manual_select) {
// if click comes from .module-import-start-select-manual, stop everything
if (typeof manual_select == "undefined") {
event.stopPropagation();
event.preventDefault();
}
}
);
body.on('click', this.moduleImportSelectFileManualSelector, function(event) {
event.stopPropagation();
event.preventDefault();
// Trigger click on hidden file input, and pass extra data to .dropzone click handler fro it to notice it comes from here
$('.dz-hidden-input').trigger('click', ["manual_select"]);
});
// Handle modal closure
body.on('click', this.moduleImportModalCloseBtn, function() {
if (self.isUploadStarted === true) {
// TODO: Display tooltip saying you can't escape at this stage
} else {
$(self.dropZoneModalSelector).modal('hide');
}
});
// Fix issue on click configure button
body.on('click', this.moduleImportSuccessConfigureBtnSelector, function(event) {
event.stopPropagation();
event.preventDefault();
window.location = $(this).attr('href');
});
// Open failure message details box
body.on('click', this.moduleImportFailureDetailsBtnSelector, function() {
$(self.moduleImportFailureMsgDetailsSelector).slideDown();
});
// @see: dropzone.js
var dropzoneOptions = {
url: moduleURLs.moduleImport,
acceptedFiles: '.zip, .tar',
// The name that will be used to transfer the file
paramName: 'file_uploaded',
maxFilesize: 50, // can't be greater than 50Mb because it's an addons limitation
uploadMultiple: false,
addRemoveLinks: true,
dictDefaultMessage: '',
hiddenInputContainer: self.dropZoneImportZoneSelector,
timeout:0, // add unlimited timeout. Otherwise dropzone timeout is 30 seconds and if a module is long to install, it is not possible to install the module.
addedfile: function() {
self.animateStartUpload();
},
processing: function () {
// Leave it empty since we don't require anything while processing upload
},
error: function (file, message) {
self.displayOnUploadError(message);
},
complete: function (file) {
if (file.status !== 'error') {
var responseObject = jQuery.parseJSON(file.xhr.response);
if (typeof responseObject.is_configurable === 'undefined') responseObject.is_configurable = null;
if (typeof responseObject.module_name === 'undefined') responseObject.module_name = null;
self.displayOnUploadDone(responseObject);
}
// State that we have finish the process to unlock some actions
self.isUploadStarted = false;
}
};
dropzone.dropzone($.extend(dropzoneOptions));
this.animateStartUpload = function() {
// State that we start module upload
self.isUploadStarted = true;
$(self.moduleImportStartSelector).hide(0);
dropzone.css('border', 'none');
$(self.moduleImportProcessingSelector).fadeIn();
};
this.animateEndUpload = function(callback) {
$(self.moduleImportProcessingSelector).finish().fadeOut(callback);
};
/**
* Method to call for upload modal, when the ajax call went well.
*
* @param object result containing the server response
*/
this.displayOnUploadDone = function(result) {
var self = this;
self.animateEndUpload(function() {
if (result.status === true) {
if (result.is_configurable === true) {
var configureLink = moduleURLs.configurationPage.replace('1', result.module_name);
$(self.moduleImportSuccessConfigureBtnSelector).attr('href', configureLink);
$(self.moduleImportSuccessConfigureBtnSelector).show();
}
$(self.moduleImportSuccessSelector).fadeIn();
} else if (typeof result.confirmation_subject !== 'undefined') {
self.displayPrestaTrustStep(result);
} else {
$(self.moduleImportFailureMsgDetailsSelector).html(result.msg);
$(self.moduleImportFailureSelector).fadeIn();
}
});
};
/**
* Method to call for upload modal, when the ajax call went wrong or when the action requested could not
* succeed for some reason.
*
* @param string message explaining the error.
*/
this.displayOnUploadError = function(message) {
self.animateEndUpload(function() {
$(self.moduleImportFailureMsgDetailsSelector).html(message);
$(self.moduleImportFailureSelector).fadeIn();
});
};
/**
* If PrestaTrust needs to be confirmed, we ask for the confirmation modal content and we display it in the
* currently displayed one. We also generate the ajax call to trigger once we confirm we want to install
* the module.
*
* @param Previous server response result
*/
this.displayPrestaTrustStep = function (result) {
var self = this;
var modal = module_card_controller.replacePrestaTrustPlaceholders(result);
var moduleName = result.module.attributes.name;
$(this.moduleImportConfirmSelector).html(modal.find('.modal-body').html()).fadeIn();
$(this.dropZoneModalFooterSelector).html(modal.find('.modal-footer').html()).fadeIn();
$(this.dropZoneModalFooterSelector).find(".pstrust-install").off('click').on('click', function() {
$(self.moduleImportConfirmSelector).hide();
$(self.dropZoneModalFooterSelector).html('');
self.animateStartUpload();
// Install ajax call
$.post(result.module.attributes.urls.install, { 'actionParams[confirmPrestaTrust]': "1"})
.done(function(data) {
self.displayOnUploadDone(data[moduleName]);
})
.fail(function(data) {
self.displayOnUploadError(data[moduleName]);
})
.always(function() {
self.isUploadStarted = false;
});
});
};
};
this.getBulkCheckboxesSelector = function () {
return this.currentDisplay == 'grid'
? this.bulkActionCheckboxGridSelector
: this.bulkActionCheckboxListSelector;
};
this.getBulkCheckboxesCheckedSelector = function () {
return this.currentDisplay == 'grid'
? this.checkedBulkActionGridSelector
: this.checkedBulkActionListSelector;
};
this.loadVariables = function () {
this.initCurrentDisplay();
};
this.getModuleItemSelector = function () {
return this.currentDisplay == 'grid'
? this.moduleItemGridSelector
: this.moduleItemListSelector;
};
/**
* Get the module notifications count and displays it as a badge on the notification tab
* @return void
*/
this.getNotificationsCount = function () {
var destinationTab = $("#subtab-AdminModulesNotifications");
if (destinationTab.length === 0) {
return;
}
var token = window.location.search;
var urlToCall = moduleURLs.notificationsCount;
$.getJSON(urlToCall, function(badge) {
// TODO: This HTML code comes from an already specific template.
// To be moved in a template, with generic classes for badges
destinationTab.append('<span class="notification-container">\
<span class="notification-counter">'+badge.count+'</span>\
</span>\
');
}).fail(function() {
console.error('Could not retrieve module notifications count.');
});
};
this.initAddonsSearch = function () {
var self = this;
$('body').on('click', this.addonItemGridSelector+', '+this.addonItemListSelector, function () {
var searchQuery = '';
if (self.currentTagsList.length) {
searchQuery = encodeURIComponent(self.currentTagsList.join(' '));
}
var hrefUrl = self.baseAddonsUrl+'search.php?search_query='+searchQuery;
window.open(hrefUrl, '_blank');
});
};
this.initCategoriesGrid = function () {
if (typeof refMenu === 'undefined') var refMenu = null;
var self = this;
$('body').on('click', this.categoryGridItemSelector, function (event) {
event.stopPropagation();
event.preventDefault();
var refCategory = $(this).attr('data-category-ref');
// In case we have some tags we need to reset it !
if (self.currentTagsList.length) {
self.pstaggerInput.resetTags(false);
self.currentTagsList = [];
}
var menuCategoryToTrigger = $(self.categoryItemSelector+'[data-category-ref="' + refCategory + '"]');
if (!menuCategoryToTrigger.length) {
console.warn('No category with ref ('+refMenu+') seems to exist!');
return false;
}
// Hide current category grid
if (self.isCategoryGridDisplayed === true) {
$(self.categoryGridSelector).fadeOut();
self.isCategoryGridDisplayed = false;
}
// Trigger click on right category
$(self.categoryItemSelector+'[data-category-ref="'+refCategory+'"]').click();
});
};
this.initCurrentDisplay = function() {
if (this.currentDisplay === '') {
this.currentDisplay = 'list';
} else {
this.currentDisplay = 'grid';
}
}
this.initSortingDropdown = function () {
var self = this;
self.currentSorting = $(this.moduleSortingDropdownSelector).find(':checked').attr('value');
$('body').on('change', this.moduleSortingDropdownSelector, function() {
self.currentSorting = $(this).find(':checked').attr('value');
self.updateModuleVisibility();
});
};
this.doBulkAction = function(requestedBulkAction) {
// This object is used to check if requested bulkAction is available and give proper
// url segment to be called for it
var forceDeletion = $('#force_bulk_deletion').prop('checked');
var bulkActionToUrl = {
'bulk-uninstall': 'uninstall',
'bulk-disable': 'disable',
'bulk-enable': 'enable',
'bulk-disable-mobile': 'disable_mobile',
'bulk-enable-mobile': 'enable_mobile',
'bulk-reset': 'reset'
};
// Note no grid selector used yet since we do not needed it at dev time
// Maybe useful to implement this kind of things later if intended to
// use this functionality elsewhere but "manage my module" section
if (typeof bulkActionToUrl[requestedBulkAction] === "undefined") {
$.growl.error({message: translate_javascripts['Bulk Action - Request not found'].replace('[1]', requestedBulkAction)});
return false;
}
// Loop over all checked bulk checkboxes
var bulkActionSelectedSelector = this.getBulkCheckboxesCheckedSelector();
if ($(bulkActionSelectedSelector).length > 0) {
var bulkModulesTechNames = [];
$(bulkActionSelectedSelector).each(function () {
var moduleTechName = $(this).attr('data-tech-name');
bulkModulesTechNames.push({
techName: moduleTechName,
actionMenuObj: $(this).parent().next()
});
});
$.each(bulkModulesTechNames, function (index, data) {
var actionMenuObj = data.actionMenuObj;
var moduleTechName = data.techName;
var urlActionSegment = bulkActionToUrl[requestedBulkAction];
if (typeof module_card_controller !== 'undefined') {
// We use jQuery to get the specific link for this action. If found, we send it.
var urlElement = $(module_card_controller.moduleActionMenuLinkSelector + urlActionSegment, actionMenuObj);
if (urlElement.length > 0) {
module_card_controller.requestToController(urlActionSegment, urlElement, forceDeletion);
} else {
$.growl.error({message: translate_javascripts["Bulk Action - Request not available for module"]
.replace('[1]', urlActionSegment)
.replace('[2]', moduleTechName)});
}
}
});
} else {
console.warn(translate_javascripts['Bulk Action - One module minimum']);
return false;
}
};
this.initActionButtons = function () {
$('body').on('click', this.moduleInstallBtnSelector, function(event) {
var $this = $(this);
var $next = $($this.next());
event.preventDefault();
$this.hide();
$next.show();
$.ajax({
url: $this.attr('data-url'),
dataType: 'json'
}).done(function () {
$next.fadeOut();
});
});
// "Upgrade All" button handler
var that = this;
$('body').on('click', this.upgradeAllSource, function(event) {
event.preventDefault();
$(that.upgradeAllTargets).click();
});
};
this.initCategorySelect = function () {
var self = this;
var body = $('body');
body.on('click', this.categoryItemSelector, function () {
// Get data from li DOM input
self.currentRefCategory = $(this).attr('data-category-ref').toLowerCase();
var categorySelectedDisplayName = $(this).attr('data-category-display-name');
// Change dropdown label to set it to the current category's displayname
$(self.categorySelectorLabelSelector).text(categorySelectedDisplayName);
$(self.categoryResetBtnSelector).show();
// Do Search on categoryRef
self.updateModuleVisibility();
});
body.on('click', this.categoryResetBtnSelector, function () {
var rawText = $(self.categorySelector).attr('aria-labelledby');
var upperFirstLetter = rawText.charAt(0).toUpperCase();
var removedFirstLetter = rawText.slice(1);
var originalText = upperFirstLetter + removedFirstLetter;
$(self.categorySelectorLabelSelector).text(originalText);
$(this).hide();
self.currentRefCategory = null;
self.updateModuleVisibility();
});
};
this.updateTotalResults = function() {
// If there are some shortlist: each shortlist count the modules on the next container.
var $shortLists = $('.module-short-list');
if ($shortLists.length > 0) {
$shortLists.each(function() {
var $this = $(this);
updateText(
$this.find('.module-search-result-wording'),
$this.next('.modules-list').find('.module-item').length
);
});
// If there is no shortlist: the wording directly update from the only module container.
} else {
var modulesCount = $('.modules-list').find('.module-item').length;
updateText(
$('.module-search-result-wording'),
modulesCount
);
$(this.addonItemGridSelector).toggle(modulesCount !== (this.modulesList.length/2));
$(this.addonItemListSelector).toggle(modulesCount !== (this.modulesList.length/2));
if (modulesCount === 0) {
$('.module-addons-search-link').attr(
'href',
this.baseAddonsUrl
+ 'search.php?search_query='
+ encodeURIComponent(this.currentTagsList.join(' '))
);
}
}
function updateText(element, value) {
var explodedText = element.text().split(' ');
explodedText[0] = value;
element.text(explodedText.join(' '));
}
};
this.initSearchBlock = function() {
var self = this;
this.pstaggerInput = $('#module-search-bar').pstagger({
onTagsChanged: function(tagList) {
self.currentTagsList = tagList;
self.updateModuleVisibility();
},
onResetTags: function() {
self.currentTagsList = [];
self.updateModuleVisibility();
},
inputPlaceholder: translate_javascripts['Search - placeholder'],
closingCross: true,
context: self,
});
$('body').on('click', '.module-addons-search-link', function(event) {
event.preventDefault();
event.stopPropagation();
var href = $(this).attr('href');
window.open(href, '_blank');
});
};
/**
* Initialize display switching between List or Grid
*/
this.initSortingDisplaySwitch = function() {
var self = this;
$('body').on('click', '.module-sort-switch', function() {
var switchTo = $(this).attr('data-switch');
var isAlreadyDisplayed = $(this).hasClass('active-display');
if (typeof switchTo !== 'undefined' && isAlreadyDisplayed === false) {
self.switchSortingDisplayTo(switchTo);
self.currentDisplay = switchTo;
}
});
};
this.switchSortingDisplayTo = function (switchTo) {
if (switchTo == 'grid' || switchTo == 'list') {
$('.module-sort-switch').removeClass('module-sort-active');
$('#module-sort-'+switchTo).addClass('module-sort-active');
this.currentDisplay = switchTo;
this.updateModuleVisibility();
} else {
console.error('Can\'t switch to undefined display property "' + switchTo + '"');
}
};
};