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