MDL-63303 message: fix bugs in message drawer part 4
[moodle.git] / message / amd / src / notification_processor_settings.js
blobbb3763b05c100cdcfa34033fa560fd2c48cb35f1
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.
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/>.
16 /**
17  * Load the settings for a message processor.
18  *
19  * @module     core_message/notification_processor_settings
20  * @class      notification_processor_settings
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
24  */
25 define(['jquery', 'core/ajax', 'core/notification', 'core/fragment', 'core/templates', 'core/str', 'tool_lp/dialogue'],
26         function($, Ajax, Notification, Fragment, Templates, Str, Dialogue) {
28     var SELECTORS = {
29         PROCESSOR: '[data-processor-name]',
30         PREFERENCE_ROW: '[data-region="preference-row"]',
31     };
33     /**
34      * Constructor for the notification processor settings.
35      *
36      * @param {object} element jQuery object root element of the processor
37      */
38     var NotificationProcessorSettings = function(element) {
39         this.root = $(element);
40         this.name = this.root.attr('data-name');
41         this.userId = this.root.attr('data-user-id');
42         this.contextId = this.root.attr('data-context-id');
43     };
45     /**
46      * Show the notification processor settings dialogue.
47      *
48      * @method show
49      */
50     NotificationProcessorSettings.prototype.show = function() {
51         Fragment.loadFragment('message', 'processor_settings', this.contextId, {
52             userid: this.userId,
53             type: this.name,
54         })
55         .done(function(html, js) {
56             Str.get_string('processorsettings', 'message').done(function(string) {
57                 var dialogue = new Dialogue(
58                     string,
59                     html,
60                     function() {
61                         Templates.runTemplateJS(js);
62                     },
63                     function() {
64                         // Removed dialogue from the DOM after close.
65                         dialogue.close();
66                     }
67                 );
69                 $(document).on('mpp:formsubmitted', function() {
70                     dialogue.close();
71                     this.updateConfiguredStatus();
72                 }.bind(this));
74                 $(document).on('mpp:formcancelled', function() {
75                     dialogue.close();
76                 });
77             }.bind(this));
78         }.bind(this));
79     };
81     /**
82      * Checks if the processor has been configured. If so then remove the unconfigured
83      * status from the interface.
84      *
85      * @method updateConfiguredStatus
86      * @return {Promise|boolean}
87      */
88     NotificationProcessorSettings.prototype.updateConfiguredStatus = function() {
89         var processorHeader = this.root.closest(SELECTORS.PROCESSOR);
91         if (!processorHeader.hasClass('unconfigured')) {
92             return false;
93         }
95         var processorName = processorHeader.attr('data-processor-name');
96         var request = {
97             methodname: 'core_message_get_message_processor',
98             args: {
99                 name: processorName,
100                 userid: this.userId,
101             },
102         };
104         return Ajax.call([request])[0]
105             .fail(Notification.exception)
106             .done(function(result) {
107                 // Check if the user has figured configuring the processor.
108                 if (result.userconfigured) {
109                     // If they have then we can enable the settings.
110                     var notifications = $(SELECTORS.PREFERENCE_ROW + ' [data-processor-name="' + processorName + '"]');
111                     processorHeader.removeClass('unconfigured');
112                     notifications.removeClass('disabled');
113                 }
114             });
115     };
117     return NotificationProcessorSettings;