MDL-67795 contentbank: delete content UI
[moodle.git] / contentbank / amd / src / actions.js
CommitLineData
45192e9a
SA
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
17 * Module to manage content bank actions, such as delete or rename.
18 *
19 * @module core_contentbank/actions
20 * @package core_contentbank
21 * @copyright 2020 Sara Arjona <sara@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24define([
25 'jquery',
26 'core/ajax',
27 'core/notification',
28 'core/str',
29 'core/templates',
30 'core/url',
31 'core/modal_factory',
32 'core/modal_events'],
33function($, Ajax, Notification, Str, Templates, Url, ModalFactory, ModalEvents) {
34
35 /**
36 * List of action selectors.
37 *
38 * @type {{DELETE_CONTENT: string}}
39 */
40 var ACTIONS = {
41 DELETE_CONTENT: '[data-action="deletecontent"]',
42 };
43
44 /**
45 * Actions class.
46 */
47 var Actions = function() {
48 this.registerEvents();
49 };
50
51 /**
52 * Register event listeners.
53 */
54 Actions.prototype.registerEvents = function() {
55 $(ACTIONS.DELETE_CONTENT).click(function(e) {
56 e.preventDefault();
57
58 var contentname = $(this).data('contentname');
59 var contentid = $(this).data('contentid');
60 var contextid = $(this).data('contextid');
61
62 var strings = [
63 {
64 key: 'deletecontent',
65 component: 'core_contentbank'
66 },
67 {
68 key: 'deletecontentconfirm',
69 component: 'core_contentbank',
70 param: {
71 name: contentname,
72 }
73 },
74 {
75 key: 'delete',
76 component: 'core'
77 },
78 ];
79
80 var deleteButtonText = '';
81 Str.get_strings(strings).then(function(langStrings) {
82 var modalTitle = langStrings[0];
83 var modalContent = langStrings[1];
84 deleteButtonText = langStrings[2];
85
86 return ModalFactory.create({
87 title: modalTitle,
88 body: modalContent,
89 type: ModalFactory.types.SAVE_CANCEL,
90 large: true
91 });
92 }).done(function(modal) {
93 modal.setSaveButtonText(deleteButtonText);
94 modal.getRoot().on(ModalEvents.save, function() {
95 // The action is now confirmed, sending an action for it.
96 return deleteContent(contentid, contextid);
97 });
98
99 // Handle hidden event.
100 modal.getRoot().on(ModalEvents.hidden, function() {
101 // Destroy when hidden.
102 modal.destroy();
103 });
104
105 // Show the modal.
106 modal.show();
107
108 return;
109 }).catch(Notification.exception);
110 });
111 };
112
113 /**
114 * Delete content from the content bank.
115 *
116 * @param {int} contentid The content to delete.
117 * @param {int} contextid The contextid where the content belongs.
118 */
119 function deleteContent(contentid, contextid) {
120 var request = {
121 methodname: 'core_contentbank_delete_content',
122 args: {
123 contentids: {contentid}
124 }
125 };
126
127 var requestType = 'success';
128 Ajax.call([request])[0].then(function(data) {
129 if (data.result) {
130 return Str.get_string('contentdeleted', 'core_contentbank');
131 }
132 requestType = 'error';
133 return Str.get_string('contentnotdeleted', 'core_contentbank');
134
135 }).done(function(message) {
136 var params = {
137 contextid: contextid
138 };
139 if (requestType == 'success') {
140 params.statusmsg = message;
141 } else {
142 params.errormsg = message;
143 }
144 // Redirect to the main content bank page and display the message as a notification.
145 window.location.href = Url.relativeUrl('contentbank/index.php', params, false);
146 }).fail(Notification.exception);
147 }
148
149 return /** @alias module:core_contentbank/actions */ {
150 // Public variables and functions.
151
152 /**
153 * Initialise the contentbank actions.
154 *
155 * @method init
156 * @return {Actions}
157 */
158 'init': function() {
159 return new Actions();
160 }
161 };
162});