Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Load the settings for a message processor.
18
 *
19
 * @module     core_message/notification_processor_settings
20
 * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
 
25
import $ from 'jquery';
26
import * as Ajax from 'core/ajax';
27
import * as Str from 'core/str';
28
import * as Notification from 'core/notification';
29
import * as CustomEvents from 'core/custom_interaction_events';
30
import Modal from 'core/modal';
31
import * as Fragment from 'core/fragment';
32
 
33
const SELECTORS = {
34
    SAVE_BUTTON: '[data-action="save"]',
35
    CANCEL_BUTTON: '[data-action="cancel"]',
36
    PROCESSOR: '[data-processor-name]',
37
    PREFERENCE_ROW: '[data-region="preference-row"]',
38
};
39
 
40
export default class NotificationProcessorSettings extends Modal {
41
    static TYPE = 'core_message-notification_processor_settings';
42
    static TEMPLATE = 'core/modal_save_cancel';
43
 
44
    /**
45
     * Constructor for the Modal.
46
     *
47
     * @class
48
     * @param {object} root The root jQuery element for the modal.
49
     */
50
    constructor(root) {
51
        super(root);
52
        this.name = null;
53
        this.userId = null;
54
        this.contextId = null;
55
        this.element = null;
56
        this.saveButton = this.getFooter().find(SELECTORS.SAVE_BUTTON);
57
        this.cancelButton = this.getFooter().find(SELECTORS.CANCEL_BUTTON);
58
    }
59
 
60
    /**
61
     * Set the userid to the given value.
62
     *
63
     * @method setUserId
64
     * @param {int} id The notification userid
65
     */
66
    setUserId(id) {
67
        this.userId = id;
68
    }
69
 
70
    /**
71
     * Retrieve the current userid, if any.
72
     *
73
     * @method getUserId
74
     * @return {int|null} The notification userid
75
     */
76
    getUserId() {
77
        return this.userId;
78
    }
79
 
80
    /**
81
     * Set the object to the given value.
82
     *
83
     * @method setElement
84
     * @param {object} element The notification node element.
85
     */
86
    setElement(element) {
87
        this.element = element;
88
    }
89
 
90
    /**
91
     * Retrieve the current element, if any.
92
     *
93
     * @method getElement
94
     * @return {object|null} The notification node element.
95
     */
96
    getElement() {
97
        return this.element;
98
    }
99
 
100
    /**
101
     * Set the name to the given value.
102
     *
103
     * @method setName
104
     * @param {string} name The notification name.
105
     */
106
    setName(name) {
107
        this.name = name;
108
    }
109
 
110
    /**
111
     * Retrieve the current name, if any.
112
     *
113
     * @method getName
114
     * @return {string|null} The notification name.
115
     */
116
    getName() {
117
        return this.name;
118
    }
119
    /**
120
     * Set the context id to the given value.
121
     *
122
     * @method setContextId
123
     * @param {Number} id The notification context id
124
     */
125
    setContextId(id) {
126
        this.contextId = id;
127
    }
128
 
129
    /**
130
     * Retrieve the current context id, if any.
131
     *
132
     * @method getContextId
133
     * @return {Number|null} The notification context id
134
     */
135
    getContextId() {
136
        return this.contextId;
137
    }
138
 
139
    /**
140
     * Get the form element from the modal.
141
     *
142
     * @method getForm
143
     * @return {object}
144
     */
145
    getForm() {
146
        return this.getBody().find('form');
147
    }
148
 
149
    /**
150
     * Disable the buttons in the footer.
151
     *
152
     * @method disableButtons
153
     */
154
    disableButtons() {
155
        this.saveButton.prop('disabled', true);
156
        this.cancelButton.prop('disabled', true);
157
    }
158
 
159
    /**
160
     * Enable the buttons in the footer.
161
     *
162
     * @method enableButtons
163
     */
164
    enableButtons() {
165
        this.saveButton.prop('disabled', false);
166
        this.cancelButton.prop('disabled', false);
167
    }
168
 
169
    /**
170
     * Load the title for the modal to the appropriate value
171
     * depending on message outputs.
172
     *
173
     * @method loadTitleContent
174
     * @return {object} A promise resolved with the new title text.
175
     */
176
    loadTitleContent() {
177
        this.titlePromise = Str.get_string('processorsettings', 'message');
178
        this.setTitle(this.titlePromise);
179
 
180
        return this.titlePromise;
181
    }
182
 
183
    /**
184
     * Load the body for the modal to the appropriate value
185
     * depending on message outputs.
186
     *
187
     * @method loadBodyContent
188
     * @return {object} A promise resolved with the fragment html and js from
189
     */
190
    loadBodyContent() {
191
        this.disableButtons();
192
 
193
        const args = {
194
            userid: this.getUserId(),
195
            type: this.getName(),
196
        };
197
 
198
        this.bodyPromise = Fragment.loadFragment('message', 'processor_settings', this.getContextId(), args);
199
        this.setBody(this.bodyPromise);
200
 
201
        this.bodyPromise.then(() => {
202
            this.enableButtons();
203
            return;
204
        })
205
        .catch(Notification.exception);
206
 
207
        return this.bodyPromise;
208
    }
209
 
210
    /**
211
     * Load both the title and body content.
212
     *
213
     * @method loadAllContent
214
     * @return {object} promise
215
     */
216
    loadAllContent() {
217
        return $.when(this.loadTitleContent(), this.loadBodyContent());
218
    }
219
 
220
    /**
221
     * Load the modal content before showing it. This
222
     * is to allow us to re-use the same modal for creating and
223
     * editing different message outputs within the page.
224
     *
225
     * @method show
226
     */
227
    show() {
228
        this.loadAllContent();
229
        super.show(this);
230
    }
231
 
232
    /**
233
     * Clear the notification from the modal when it's closed so
234
     * that it is loaded fresh next time it's displayed.
235
     *
236
     * @method hide
237
     */
238
    hide() {
239
        super.hide(this);
240
        this.setContextId(null);
241
        this.setName(null);
242
        this.setUserId(null);
243
    }
244
 
245
    /**
246
     * Checks if the processor has been configured. If so then remove the unconfigured
247
     * status from the interface.
248
     *
249
     * @method updateConfiguredStatus
250
     * @return {Promise|boolean}
251
     */
252
    updateConfiguredStatus() {
253
        const processorHeader = $(this.getElement()).closest(SELECTORS.PROCESSOR);
254
 
255
        if (!processorHeader.hasClass('unconfigured')) {
256
            return false;
257
        }
258
 
259
        const processorName = processorHeader.attr('data-processor-name');
260
        const request = {
261
            methodname: 'core_message_get_message_processor',
262
            args: {
263
                name: processorName,
264
                userid: this.userId,
265
            },
266
        };
267
 
268
        return Ajax.call([request])[0]
269
            .then((result) => {
270
                // Check if the user has figured configuring the processor.
271
                if (result.userconfigured) {
272
                    // If they have then we can enable the settings.
273
                    const notifications = $(SELECTORS.PREFERENCE_ROW + ' [data-processor-name="' + processorName + '"]');
274
                    processorHeader.removeClass('unconfigured');
275
                    notifications.removeClass('disabled');
276
                }
277
                return result;
278
            });
279
    }
280
 
281
    /**
282
     * Set up all of the event handling for the modal.
283
     *
284
     * @method registerEventListeners
285
     */
286
    registerEventListeners() {
287
        // Apply parent event listeners.
288
        super.registerEventListeners(this);
289
 
290
        // When the user clicks the save button we trigger the form submission.
291
        this.getModal().on(CustomEvents.events.activate, SELECTORS.SAVE_BUTTON, (e, data) => {
292
            this.getForm().submit();
293
            data.originalEvent.preventDefault();
294
        });
295
 
296
        this.getModal().on('mpp:formsubmitted', (e) => {
297
            this.hide();
298
            this.updateConfiguredStatus();
299
            e.stopPropagation();
300
        });
301
 
302
        this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, (e, data) => {
303
            this.hide();
304
            data.originalEvent.preventDefault();
305
            e.stopPropagation();
306
        });
307
    }
308
}
309
 
310
NotificationProcessorSettings.registerModalType();