1 |
efrain |
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 |
* Adds support for confirmation via JS modal for some management actions at the Manage policies page.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_policy/managedocsactions
|
|
|
20 |
* @copyright 2018 David Mudrák <david@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define([
|
|
|
24 |
'jquery',
|
|
|
25 |
'core/log',
|
|
|
26 |
'core/config',
|
|
|
27 |
'core/str',
|
|
|
28 |
'core/modal_save_cancel',
|
|
|
29 |
'core/modal_events'
|
|
|
30 |
], function($, Log, Config, Str, ModalSaveCancel, ModalEvents) {
|
|
|
31 |
|
|
|
32 |
"use strict";
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* List of action selectors.
|
|
|
36 |
*
|
|
|
37 |
* @property {string} LINKS - Selector for all action links
|
|
|
38 |
* @property {string} MAKE_CURRENT
|
|
|
39 |
*/
|
|
|
40 |
var ACTION = {
|
|
|
41 |
LINKS: '[data-action]',
|
|
|
42 |
MAKE_CURRENT: '[data-action="makecurrent"]',
|
|
|
43 |
INACTIVATE: '[data-action="inactivate"]',
|
|
|
44 |
DELETE: '[data-action="delete"]'
|
|
|
45 |
};
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* @constructor
|
|
|
49 |
* @param {Element} base - Management area wrapping element
|
|
|
50 |
*/
|
|
|
51 |
function ManageDocsActions(base) {
|
|
|
52 |
this.base = base;
|
|
|
53 |
|
|
|
54 |
this.initEvents();
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Register event listeners.
|
|
|
59 |
*/
|
|
|
60 |
ManageDocsActions.prototype.initEvents = function() {
|
|
|
61 |
var self = this;
|
|
|
62 |
|
|
|
63 |
self.base.on('click', ACTION.LINKS, function(e) {
|
|
|
64 |
e.stopPropagation();
|
|
|
65 |
|
|
|
66 |
var link = $(e.currentTarget);
|
|
|
67 |
var promise;
|
|
|
68 |
var strings;
|
|
|
69 |
|
|
|
70 |
if (link.is(ACTION.MAKE_CURRENT)) {
|
|
|
71 |
promise = Str.get_strings([
|
|
|
72 |
{key: 'activating', component: 'tool_policy'},
|
|
|
73 |
{key: 'activateconfirm', component: 'tool_policy', param: {
|
|
|
74 |
name: link.closest('[data-policy-name]').attr('data-policy-name'),
|
|
|
75 |
revision: link.closest('[data-policy-revision]').attr('data-policy-revision')
|
|
|
76 |
}},
|
|
|
77 |
{key: 'activateconfirmyes', component: 'tool_policy'}
|
|
|
78 |
]);
|
|
|
79 |
|
|
|
80 |
} else if (link.is(ACTION.INACTIVATE)) {
|
|
|
81 |
promise = Str.get_strings([
|
|
|
82 |
{key: 'inactivating', component: 'tool_policy'},
|
|
|
83 |
{key: 'inactivatingconfirm', component: 'tool_policy', param: {
|
|
|
84 |
name: link.closest('[data-policy-name]').attr('data-policy-name'),
|
|
|
85 |
revision: link.closest('[data-policy-revision]').attr('data-policy-revision')
|
|
|
86 |
}},
|
|
|
87 |
{key: 'inactivatingconfirmyes', component: 'tool_policy'}
|
|
|
88 |
]);
|
|
|
89 |
|
|
|
90 |
} else if (link.is(ACTION.DELETE)) {
|
|
|
91 |
promise = Str.get_strings([
|
|
|
92 |
{key: 'deleting', component: 'tool_policy'},
|
|
|
93 |
{key: 'deleteconfirm', component: 'tool_policy', param: {
|
|
|
94 |
name: link.closest('[data-policy-name]').attr('data-policy-name'),
|
|
|
95 |
revision: link.closest('[data-policy-revision]').attr('data-policy-revision')
|
|
|
96 |
}},
|
|
|
97 |
{key: 'delete', component: 'core'}
|
|
|
98 |
]);
|
|
|
99 |
|
|
|
100 |
} else {
|
|
|
101 |
Log.error('unknown action type detected', 'tool_policy/managedocsactions');
|
|
|
102 |
return;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
e.preventDefault();
|
|
|
106 |
|
|
|
107 |
promise.then(function(strs) {
|
|
|
108 |
strings = strs;
|
|
|
109 |
return ModalSaveCancel.create({
|
|
|
110 |
title: strings[0],
|
|
|
111 |
body: strings[1],
|
|
|
112 |
});
|
|
|
113 |
|
|
|
114 |
}).then(function(modal) {
|
|
|
115 |
modal.setSaveButtonText(strings[2]);
|
|
|
116 |
modal.getRoot().on(ModalEvents.save, function() {
|
|
|
117 |
window.location.href = link.attr('href') + '&sesskey=' + Config.sesskey + '&confirm=1';
|
|
|
118 |
});
|
|
|
119 |
|
|
|
120 |
modal.getRoot().on(ModalEvents.hidden, function() {
|
|
|
121 |
modal.destroy();
|
|
|
122 |
});
|
|
|
123 |
|
|
|
124 |
modal.show();
|
|
|
125 |
return true;
|
|
|
126 |
|
|
|
127 |
}).catch(function(e) {
|
|
|
128 |
Log.error(e);
|
|
|
129 |
return false;
|
|
|
130 |
});
|
|
|
131 |
});
|
|
|
132 |
};
|
|
|
133 |
|
|
|
134 |
return {
|
|
|
135 |
/**
|
|
|
136 |
* Factory method returning instance of the ManageDocsActions
|
|
|
137 |
*
|
|
|
138 |
* @param {String} baseid - ID of the management area wrapping element
|
|
|
139 |
* @return {ManageDocsActions}
|
|
|
140 |
*/
|
|
|
141 |
init: function(baseid) {
|
|
|
142 |
var base = $(document.getElementById(baseid));
|
|
|
143 |
|
|
|
144 |
if (base.length) {
|
|
|
145 |
return new ManageDocsActions(base);
|
|
|
146 |
|
|
|
147 |
} else {
|
|
|
148 |
throw new Error("managedocsactions: Invalid base element identifier");
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
};
|
|
|
152 |
});
|