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 |
* Manages the processor form on the message preferences page.
|
|
|
18 |
*
|
|
|
19 |
* @module core_message/preferences_processor_form
|
|
|
20 |
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery', 'core/ajax', 'core/notification'],
|
|
|
24 |
function($, Ajax, Notification) {
|
|
|
25 |
/**
|
|
|
26 |
* Constructor for the ProcessorForm.
|
|
|
27 |
*
|
|
|
28 |
* @class
|
|
|
29 |
* @param {object} element jQuery object root element of the preference
|
|
|
30 |
*/
|
|
|
31 |
var ProcessorForm = function(element) {
|
|
|
32 |
this.root = $(element);
|
|
|
33 |
this.userId = this.root.attr('data-user-id');
|
|
|
34 |
this.name = this.root.attr('data-processor-name');
|
|
|
35 |
|
|
|
36 |
this.root.find('form').on('submit', function(e) {
|
|
|
37 |
e.preventDefault();
|
|
|
38 |
this.save().done(function() {
|
|
|
39 |
$(element).trigger('mpp:formsubmitted');
|
|
|
40 |
});
|
|
|
41 |
}.bind(this));
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Flag the processor as loading.
|
|
|
46 |
*
|
|
|
47 |
* @method startLoading
|
|
|
48 |
*/
|
|
|
49 |
ProcessorForm.prototype.startLoading = function() {
|
|
|
50 |
this.root.addClass('loading');
|
|
|
51 |
};
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Remove the loading flag for this processor.
|
|
|
55 |
*
|
|
|
56 |
* @method stopLoading
|
|
|
57 |
*/
|
|
|
58 |
ProcessorForm.prototype.stopLoading = function() {
|
|
|
59 |
this.root.removeClass('loading');
|
|
|
60 |
};
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Check if this processor is loading.
|
|
|
64 |
*
|
|
|
65 |
* @method isLoading
|
|
|
66 |
* @return {bool}
|
|
|
67 |
*/
|
|
|
68 |
ProcessorForm.prototype.isLoading = function() {
|
|
|
69 |
return this.root.hasClass('loading');
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Persist the processor configuration.
|
|
|
74 |
*
|
|
|
75 |
* @method save
|
|
|
76 |
* @return {object} jQuery promise
|
|
|
77 |
*/
|
|
|
78 |
ProcessorForm.prototype.save = function() {
|
|
|
79 |
if (this.isLoading()) {
|
|
|
80 |
return $.Deferred();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
this.startLoading();
|
|
|
84 |
|
|
|
85 |
var data = this.root.find('form').serializeArray();
|
|
|
86 |
var request = {
|
|
|
87 |
methodname: 'core_message_message_processor_config_form',
|
|
|
88 |
args: {
|
|
|
89 |
userid: this.userId,
|
|
|
90 |
name: this.name,
|
|
|
91 |
formvalues: data,
|
|
|
92 |
}
|
|
|
93 |
};
|
|
|
94 |
|
|
|
95 |
return Ajax.call([request])[0]
|
|
|
96 |
.fail(Notification.exception)
|
|
|
97 |
.always(function() {
|
|
|
98 |
this.stopLoading();
|
|
|
99 |
}.bind(this));
|
|
|
100 |
};
|
|
|
101 |
|
|
|
102 |
return ProcessorForm;
|
|
|
103 |
});
|