forked from donhinkelman/moodle-block_sharing_cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
1213 lines (1049 loc) · 41.5 KB
/
script.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
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Sharing Cart
*
* @package block_sharing_cart
* @copyright 2017 (C) VERSION2, INC.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(['jquery', 'core/modal_factory', 'core/modal_events'], function ($, ModalFactory, ModalEvents) {
$(document).ready(function () {
/**
* Returns a localized string
*
* @param {String} identifier
* @return {String}
*/
function str(identifier) {
return M.str.block_sharing_cart[identifier] || M.str.moodle[identifier];
}
/**
* Get an action URL
*
* @param {String} name The action name
* @param {Object} [args] The action parameters
* @return {String}
*/
function get_action_url(name, args) {
var url = M.cfg.wwwroot + '/blocks/sharing_cart/' + name + '.php';
if (args) {
var q = [];
for (var k in args) {
q.push(k + '=' + encodeURIComponent(args[k]));
}
url += '?' + q.join('&');
}
return url;
}
/**
* Modal called when confirming an action.
*
* @param obj
*/
function confirm_modal(obj) {
// Checkbox for copying userdata confirmation.
if (obj.checkbox) {
obj.body +=
'<div class="modal-checbox-wrapper">' +
'<input type="checkbox" id="modal-checkbox" class="modal-checkbox" checked>' +
'<label for="modal-checkbox">' + str('modal_checkbox') + '</label>' +
'</div>';
}
ModalFactory.create({
type: ModalFactory.types.SAVE_CANCEL,
title: obj.title,
body: obj.body,
}).done(function (modal) {
modal.setSaveButtonText(obj.save_button);
// On save save check - if checkbox is checked.
modal.getRoot().on(ModalEvents.save, function (e) {
var response = {
'checkbox': $(e.target).find('.modal-checkbox').is(':checked'),
};
obj.next(response);
});
// Remove modal from html.
modal.getRoot().on(ModalEvents.hidden, function () {
$('body').removeClass('modal-open');
});
modal.show();
});
}
/**
* Get the section name from the section when
* it's changed with the in place editor
*
* @param $section
* @returns {*}
*/
function in_place_edit_section_name($section) {
var sectionName = '';
var $inPlaceEditable = $section.find('h3.sectionname .inplaceeditable');
if ($inPlaceEditable.length) {
sectionName = $inPlaceEditable.data('value');
}
return sectionName;
}
/**
* @param post_data
* @param title_str
* @param body_str
* @param isSection
*/
function on_backup_modal(post_data, title_str, body_str, isSection) {
(function (on_success) {
$.post(get_action_url('rest'), post_data,
function (response) {
on_success(response);
}, "text")
.fail(function (response) {
show_error(response);
});
})(function (response) {
var copyable = response === '1';
var checkbox = false;
if (copyable) {
checkbox = true;
}
confirm_modal({
'title': title_str,
'body': body_str,
'save_button': str('modal_confirm_backup'),
'checkbox': checkbox,
'next': function (data) {
if (isSection === true) {
backup_section(post_data.sectionid, post_data.sectionnumber, post_data.courseid, data.checkbox);
} else {
backup(post_data.cmid, data.checkbox);
}
}
});
});
}
/** @var {Object} The icon configurations */
var icon = {
// Actions
'backup': {
css: 'editing_backup',
iconClass: 'fa fa-frown-o',
},
'movedir': {
css: 'editing_right',
iconClass: 'fa fa-arrow-right',
},
'move': {
css: 'editing_move_',
iconClass: 'fa fa-arrows-v',
},
'edit': {
css: 'editing_update',
iconClass: 'fa fa-pencil',
},
'cancel': {
css: 'editing_cancel',
iconClass: 'fa fa-ban',
},
'delete': {
css: 'editing_update',
iconClass: 'fa fa-trash',
},
'restore': {
css: 'editing_restore',
iconClass: 'fa fa-clone',
},
// Directories
'dir-open': {
iconClass: 'fa fa-folder-open-o'
},
'dir-closed': {
iconClass: 'fa fa-folder-o'
},
};
/** @var {Node} The Sharing Cart block container node */
var $block = $('.block_sharing_cart');
/** @var {Object} The current course */
var course = new function () {
var body = $('body');
this.id = body.attr('class').match(/course-(\d+)/)[1];
this.is_frontpage = body.hasClass('pagelayout-frontpage');
}();
/**
* Shows an error message with given Ajax error
*
* @param {Object} response The Ajax response
*/
function show_error(response) {
try {
var ex = JSON.parse(response.responseText);
new M.core.exception({
name: str('pluginname') + ' - ' + str('error'),
message: ex.message
});
} catch (e) {
new M.core.exception({
name: str('pluginname') + ' - ' + str('error'),
message: response.responseText
});
}
}
/**
* Check special layout (theme boost)
*
* @return {Boolean}
*/
function verify_layout() {
var menuelement = $block.find('.menubar .dropdown .dropdown-menu');
return (menuelement.length);
}
/**
* Set Cookie
* @param name
* @param value
* @param expireTimeInMillisecond
*/
function setCookie(name, value, expireTimeInMillisecond) {
var d = new Date();
d.setTime(d.getTime() + expireTimeInMillisecond);
var expires = 'expires=' + d.toUTCString();
document.cookie = name + '=' + value + ';' + expires + '';
}
/**
* Get Cookie Value
* @param param
* @returns {*}
*/
function getCookieValue(param) {
var readCookie = document.cookie.match('(^|;)\\s*' + param + '\\s*=\\s*([^;]+)');
return readCookie ? readCookie.pop() : '';
}
/**
* Create a command icon
*
* @param {String} name The command name, predefined in icon
* @param {String} [pix] The icon pix name to override
*/
function create_command(name) {
var iconElement = $('<i/>')
.attr('alt', str(name))
.attr('class', icon[name].iconClass);
// If (verify_layout()) {
// iconElement.addClass('iconcustom');
// }
return $('<a href="javascript:void(0)"/>')
.addClass(icon[name].css)
.attr('title', str(name))
.append(iconElement);
}
/**
* Create a spinner
* @param $node
* @returns {*|jQuery}
*/
function add_spinner() {
var $spinner = ($('<div class="block_spinner"><i class="fa fa-circle-o-notch fa-spin fa-2x"></i></div>'));
$('section.block_sharing_cart').append($spinner);
return $spinner;
}
/**
*
* @param $node
* @returns {jQuery.fn.init}
*/
function add_node_spinner($node) {
var $node_spinner = $('<i class="fa fa-circle-o-notch fa-spin node_spinner"></i>');
$node.append($node_spinner);
return $node_spinner;
}
/**
*
* Reload the Sharing Cart item tree
*/
function reload_tree() {
$.post(get_action_url("rest"),
{
"action": "render_tree"
},
function (response) {
$block.find(".tree").replaceWith($(response));
$.init_item_tree();
}, "html")
.fail(function (response) {
show_error(response);
});
}
/**
* Backup an activity
*
* @param {int} cmid
* @param {Boolean} userdata
*/
function backup(cmid, userdata) {
var $commands = $('#module-' + cmid + ' .actions');
if (!$commands.length) {
$commands = $('[data-owner="#module-' + cmid + '"]');
}
var $spinner = add_spinner();
var $node_spinner = add_node_spinner($commands);
$.post(get_action_url("rest"),
{
"action": "backup",
"cmid": cmid,
"userdata": userdata,
"sesskey": M.cfg.sesskey,
"course": course.id
},
function () {
reload_tree();
})
.fail(function (response) {
show_error(response);
})
.always(function () {
$node_spinner.hide();
$spinner.hide();
});
}
/**
* Backup an activities in a section
*
* @param {int} sectionId
* @param {int} sectionNumber
* @param {int} courseId
* @param {Boolean} userdata
*/
function backup_section(sectionId, sectionNumber, courseId, userdata) {
var $commands = $('span.inplaceeditable[data-itemtype=sectionname][data-itemid=' + sectionId + ']');
var $section = $commands.closest("li.section.main");
var sectionName = $section.attr('aria-label');
if (sectionName === null) {
sectionName = String($('#region-main .section_action_menu[data-sectionid=\'' + sectionId + '\']')
.parent().parent().find('h3.sectionname').text());
}
var inPlaceEditSectionName = in_place_edit_section_name($section);
sectionName = (inPlaceEditSectionName !== '') ? inPlaceEditSectionName : sectionName;
var $spinner = add_spinner();
var $node_spinner = add_node_spinner($commands);
$.post(get_action_url("rest"),
{
"action": "backup_section",
"sectionid": sectionId,
"sectionnumber": sectionNumber,
"courseid": courseId,
"sectionname": sectionName,
"userdata": userdata,
"sesskey": M.cfg.sesskey,
"course": course.id
},
function () {
reload_tree();
})
.fail(function (response) {
show_error(response);
})
.always(function () {
$spinner.hide();
$node_spinner.hide();
});
}
// /////// CLASSES /////////
/**
* @class Directory states manager
*/
var directories = new function () {
var KEY = 'block_sharing_cart-dirs';
var opens = getCookieValue(KEY).split(',').map(function (v) {
return parseInt(v);
});
function save() {
var expires = new Date();
expires.setDate(expires.getDate() + 30);
setCookie(KEY, opens.join(','), expires);
}
function open($dir, visible) {
var iconIndex = visible ? 'dir-open' : 'dir-closed';
var iconElement = icon[iconIndex].iconClass;
$dir.find('> div i.icon').attr('class', 'icon ' + iconElement);
$dir.find('> ul.list')[visible ? 'show' : 'hide']();
}
function toggle(e) {
var $dir = $(e.target).closest('li.directory');
var i = $dir.attr('id').match(/(\d+)$/)[1];
var v = $dir.find('> ul.list').css('display') === 'none';
open($dir, v);
opens[i] = v ? 1 : 0;
save();
}
/**
* Initialize directory states
*/
this.init = function () {
var i = 0;
$block.find('li.directory').each(function (index, dir) {
var $dir = $(dir);
$dir.attr('id', 'block_sharing_cart-dir-' + i);
if (i >= opens.length) {
opens.push(0);
} else if (opens[i]) {
open($dir, true);
}
$dir.find('> div div.toggle-wrapper').css('cursor', 'pointer').on('click', function (e) {
toggle(e);
});
i++;
});
};
/**
* Reset directory states
*/
this.reset = function () {
opens = [];
this.init();
save();
};
}();
/**
* @class Targets for moving an item directory
*/
var move_targets = new function () {
var $cancel = null,
targets = [];
/**
* Hide move targets
*/
this.hide = function () {
if ($cancel !== null) {
var $commands = $cancel.closest('.commands');
$cancel.remove();
$cancel = null;
$commands.closest('li.activity').css('opacity', 1.0);
$commands.find('a').each(function () {
$(this).show();
});
$.each(targets, function (index, $target) {
$target.remove();
});
targets = [];
}
};
/**
* Show move targets for a given item
*
* @param {int} id The item ID
*/
this.show = function (item_id) {
this.hide();
function move(e) {
var m = $(e.target).closest('a').attr('class').match(/move-(\d+)-to-(\d+)/);
var item_id = m[1],
area_to = m[2];
var $spinner = add_spinner();
$.post(get_action_url("rest"),
{
"action": "move",
"item_id": item_id,
"area_to": area_to,
"sesskey": M.cfg.sesskey
},
function () {
reload_tree();
})
.fail(function (response) {
show_error(response);
})
.always(function () {
$spinner.hide();
});
}
var $current = $block.find('#block_sharing_cart-item-' + item_id);
var $next = $current.next();
var $list = $current.closest('ul');
var next_id = 0;
if ($next.length) {
next_id = $next.attr('id').match(/item-(\d+)$/)[1];
}
/**
*
* @param item_id
* @param area_to
* @returns {jQuery}
*/
function create_target(item_id, area_to) {
var $anchor = $('<a href="javascript:void(0)"/>')
.addClass('move-' + item_id + '-to-' + area_to)
.attr('title', str('movehere'))
.append(
$('<p>' + str('clicktomove') + '</p>')
.attr('alt', str('movehere'))
);
var $target = $('<li class="activity move-to"/>')
.append($anchor);
$anchor.on('click', function (e) {
move(e);
});
return $target;
}
$list.find('> li.activity').each(function (index, item) {
var $item = $(item);
var to = $item.attr('id').match(/item-(\d+)$/)[1];
if (to === item_id) {
$cancel = create_command('cancel', 't/left');
$cancel.on('click', function () {
move_targets.hide();
});
var $commands = $item.find('.commands');
$commands.find('a').each(function () {
$(this).hide();
});
$commands.append($cancel);
$item.css('opacity', 0.5);
} else if (to !== next_id) {
var $target = create_target(item_id, to);
$item.before($target);
targets.push($target);
}
}, this);
if ($next) {
var $target = create_target(item_id, 0);
$list.append($target);
targets.push($target);
}
};
}();
/**
* @class Targets for restoring an item
*/
var restore_targets = new function () {
this.is_directory = null;
var $clipboard = null,
targets = [];
/**
*
* @param id
* @param section
* @returns {jQuery}
*/
function create_target(id, section) {
var href = '';
var inSection = $('#copy-section-form').data('in-section');
if (restore_targets.is_directory) {
href = get_action_url('restore', {
'directory': true,
'path': id,
'course': course.id,
'section': section,
'in_section': inSection,
'sesskey': M.cfg.sesskey
});
} else {
href = get_action_url('restore', {
'directory': false,
'id': id,
'course': course.id,
'section': section,
'in_section': inSection,
'sesskey': M.cfg.sesskey
});
}
var $target = $('<a/>')
.attr('href', href)
.attr('title', str('copyhere'))
.append(
$('<img class="move_target"/>')
.attr('alt', str('copyhere'))
.attr('src', M.util.image_url('dropzone_arrow', 'block_sharing_cart'))
);
targets.push($target);
return $target;
}
/**
* Hide restore targets
*/
this.hide = function () {
if ($clipboard !== null) {
$clipboard.remove();
$clipboard = null;
$.each(targets, function (index, $target) {
$target.remove();
});
targets = [];
}
};
/**
*
*
* @param {int} id The item ID
*/
this.show = function (id) {
this.hide();
var $view = $("<span/>");
if (this.is_directory) {
$view.html(id).css('display', 'inline');
$view.prepend(
$("<i/>").addClass("icon")
.attr("alt", id)
// .attr("src", M.util.image_url(icon['dir-closed'].pix, null))
);
} else {
var $item = $block.find('#block_sharing_cart-item-' + id);
$view = $($item.find('div')[0].cloneNode(true)).css('display', 'inline');
$view.attr('class', $view.attr('class').replace(/mod-indent-\d+/, ''));
$view.find('.commands').remove();
}
var $cancel = create_command('cancel');
$cancel.on('click', this.hide);
$clipboard = $('<div class="clipboard"/>');
$clipboard.append(str('clipboard') + ": ").append($view).append($cancel);
if (course.is_frontpage) {
var $sitetopic = $('.sitetopic');
var $mainmenu = $('.block_site_main_menu');
if ($sitetopic) {
$sitetopic.find('*').before($clipboard);
} else if ($mainmenu) {
$mainmenu.find('.content').before($clipboard);
}
// Mainmenu = section #0, sitetopic = section #1
if ($mainmenu) {
$mainmenu.find('.footer').before(create_target(id, 0));
}
if ($sitetopic) {
$sitetopic.find('ul.section').append(create_target(id, 1));
}
} else {
var $container = $('.course-content');
$container.one('*').before($clipboard);
$container.find(M.course.format.get_section_wrapper(null)).each(function (index, sectionDOM) {
var $section = $(sectionDOM);
var section = $section.attr('id').match(/(\d+)$/)[1];
$section.find('ul.section').first().append(create_target(id, section));
}, this);
}
};
}();
// /////// INITIALIZATION /////////
/**
*
* @returns {string|*}
*/
$.get_plugin_name = function () {
var $blockheader = $block.find("h2");
if (!$blockheader.length) {
$blockheader = $block.find("h3");
if ($blockheader.length) {
return $blockheader.html();
}
} else {
return $blockheader.html();
}
return "";
};
/**
*
* @param e
* @param activityName
*/
$.on_backup = function (e, activityName) {
var cmid = (function ($backup) {
var $activity = $backup.closest('li.activity');
if ($activity.length) {
return $activity.attr('id').match(/(\d+)$/)[1];
}
var $commands = $backup.closest('.commands');
var dataowner = $commands.attr('data-owner');
if (dataowner.length) {
return dataowner.match(/(\d+)$/)[1];
}
return $commands.find('a.editing_delete').attr('href').match(/delete=(\d+)/)[1];
})($(e.target));
var data =
{
"action": "is_userdata_copyable",
"cmid": cmid
};
on_backup_modal(data, activityName, str('confirm_backup'), false);
};
/**
* On movedir command clicked
*
* @param {DOMEventFacade} e
*/
$.on_movedir = function (e) {
var $commands = $(e.target).closest('.commands');
var $current_dir = $commands.closest('li.directory');
var current_path = $current_dir.length ? $current_dir.attr('directory-path') : '/';
var item_id = $(e.target).closest('li.activity').attr('id').match(/(\d+)$/)[1];
var dirs = [];
$block.find('li.directory').each(function () {
dirs.push($(this).attr('directory-path'));
});
var $form = $('<form/>');
$form.attr('action', 'javascript:void(0)');
function submit() {
var folder_to = $form.find('[name="to"]').val();
var $spinner = add_spinner();
$.post(get_action_url('rest'),
{
"action": "movedir",
"item_id": item_id,
"folder_to": folder_to,
"sesskey": M.cfg.sesskey
},
function () {
reload_tree();
directories.reset();
})
.fail(function (response) {
show_error(response);
})
.always(function () {
$spinner.hide();
});
}
$form.submit(submit);
if (dirs.length === 0) {
var $input = $('<input class="form-control" type="text" name="to"/>').val(current_path);
setTimeout(function () {
$input.focus();
}, 1);
$form.append($input);
} else {
dirs.unshift('/');
var $select = $('<select class="custom-select" name="to"/>');
for (var i = 0; i < dirs.length; i++) {
$select.append($('<option/>').val(dirs[i]).append(dirs[i]));
}
$select.val(current_path);
$select.change(submit);
$form.append($select);
var $edit = create_command('edit');
$edit.on('click', function () {
var $input = $('<input type="text" name="to"/>').val(current_path);
$select.remove();
$edit.replaceWith($input);
$input.focus();
});
$form.append($edit);
}
var $cancel = create_command('cancel');
$cancel.on('click', function () {
$form.remove();
$commands.find('a').show();
});
$form.append($cancel);
$commands.find('a').each(function () {
$(this).hide();
});
$commands.append($form);
};
/**
* On move command clicked
*
* @param {DOMEventFacade} e
*/
$.on_move = function (e) {
var $item = $(e.target).closest('li.activity');
var id = $item.attr('id').match(/(\d+)$/)[1];
move_targets.show(id);
};
/**
* On delete command clicked
*
* @param {DOMEventFacade} e
*/
$.on_delete = function (e) {
var $item = $(e.target).closest('li');
var liText = $item[0].innerText;
var isDirectory = false;
var modalBody;
var item;
var description_text = '';
if ($item.hasClass("directory")) {
isDirectory = true;
item = str('folder_string');
description_text = str('delete_folder');
} else {
item = str('activity_string');
}
modalBody = '<p class="delete-item">' + item + ' ' + liText + description_text + '</p>';
confirm_modal({
'title': str('confirm_delete'),
'body': modalBody,
'save_button': str('modal_confirm_delete'),
'checkbox': false,
'next': function () {
var data = {};
if (isDirectory === true) {
data = {
"action": "delete_directory",
"path": $item.attr("directory-path"),
"sesskey": M.cfg.sesskey
};
} else if ($item.hasClass("activity")) {
data = {
"action": "delete",
"id": $item.attr('id').match(/(\d+)$/)[1],
"sesskey": M.cfg.sesskey
};
}
var $spinner = add_spinner();
$.post(get_action_url("rest"), data,
function () {
reload_tree();
})
.fail(function (response) {
show_error(response);
})
.always(function () {
$spinner.hide();
});
e.stopPropagation();
}
});
};
/**
* On restore command clicked
*
* @param {DOMEventFacade} e
*/
$.on_restore = function (e) {
var $item = $(e.target).closest('li');
var id = null;
if ($item.hasClass("directory")) {
id = $item.attr("directory-path");
restore_targets.is_directory = true;
} else if ($item.hasClass("activity")) {
id = $item.attr('id').match(/(\d+)$/)[1];
restore_targets.is_directory = false;
}
restore_targets.show(id);
};
/**
* On backup the whole section as a folder
*
* @param {int} sectionId
* @param {int} sectionNumber
* @param {int} courseId
* @param {string} sectionName
*/
$.on_section_backup = function (sectionId, sectionNumber, courseId, sectionName) {
var data =
{
"action": "is_userdata_copyable_section",
"sectionid": sectionId,
"sectionnumber": sectionNumber,
"courseid": courseId,
};
on_backup_modal(data, sectionName, str('confirm_backup_section'), true);
};
/**
* Initialize the delete bulk
*/
$.init_bulk_delete = function (isspeciallayout) {
var bulkdelete = $block.find('.editing_bulkdelete');
if (bulkdelete.length) {
if (isspeciallayout) {
bulkdelete.attr('role', 'menuitem').addClass('dropdown-item menu-action');
bulkdelete.append($("<span class='menu-action-text'/>").append(bulkdelete.attr('title')));
$block.find('.menubar .dropdown .dropdown-menu').append(bulkdelete);
} else {
$block.find('.header .commands').append(bulkdelete);
}
}
};
/**
* Initialize the help icon
*/
$.init_help_icon = function (isspeciallayout) {
var helpicon = $block.find('.header-commands > .help-icon');
if (isspeciallayout) {
$block.find('.header-commands').parent().css('display', 'block');
} else {
$block.find('.header .commands').append(helpicon);
}
};
/**
* Initialize the Sharing Cart block header
*/
$.init_block_header = function () {
var isspeciallayout = verify_layout();
$.init_bulk_delete(isspeciallayout);
$.init_help_icon(isspeciallayout);
};