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
 * Module to add categories.
18
 *
19
 * @module     tool_dataprivacy/add_category
20
 * @copyright  2018 David Monllao
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define([
24
    'jquery',
25
    'core/str',
26
    'core/ajax',
27
    'core/notification',
28
    'core/modal_save_cancel',
29
    'core/modal_events',
30
    'core/fragment',
31
    'core_form/changechecker',
32
], function(
33
    $,
34
    Str,
35
    Ajax,
36
    Notification,
37
    ModalSaveCancel,
38
    ModalEvents,
39
    Fragment,
40
    FormChangeChecker
41
) {
42
 
43
        var SELECTORS = {
44
            CATEGORY_LINK: '[data-add-element="category"]',
45
        };
46
 
47
        var AddCategory = function(contextId) {
48
            this.contextId = contextId;
49
 
50
            var stringKeys = [
51
                {
52
                    key: 'addcategory',
53
                    component: 'tool_dataprivacy'
54
                },
55
                {
56
                    key: 'save',
57
                    component: 'admin'
58
                }
59
            ];
60
            this.strings = Str.get_strings(stringKeys);
61
 
62
            this.registerEventListeners();
63
        };
64
 
65
        /**
66
         * @var {int} contextId
67
         * @private
68
         */
69
        AddCategory.prototype.contextId = 0;
70
 
71
        /**
72
         * @var {Promise}
73
         * @private
74
         */
75
        AddCategory.prototype.strings = 0;
76
 
77
        AddCategory.prototype.registerEventListeners = function() {
78
 
79
            var trigger = $(SELECTORS.CATEGORY_LINK);
80
            trigger.on('click', function() {
81
                this.strings.then(function(strings) {
82
                    return Promise.all([
83
                        ModalSaveCancel.create({
84
                            title: strings[0],
85
                            body: '',
86
                        }),
87
                        strings[1],
88
                    ]).then(function([modal, string]) {
89
                        this.setupFormModal(modal, string);
90
                        return modal;
91
                    }.bind(this));
92
                }.bind(this))
93
                .catch(Notification.exception);
94
            }.bind(this));
95
 
96
        };
97
 
98
        /**
99
         * @method getBody
100
         * @param {Object} formdata
101
         * @private
102
         * @return {Promise}
103
         */
104
        AddCategory.prototype.getBody = function(formdata) {
105
 
106
            var params = null;
107
            if (typeof formdata !== "undefined") {
108
                params = {jsonformdata: JSON.stringify(formdata)};
109
            }
110
            // Get the content of the modal.
111
            return Fragment.loadFragment('tool_dataprivacy', 'addcategory_form', this.contextId, params);
112
        };
113
 
114
        AddCategory.prototype.setupFormModal = function(modal, saveText) {
115
            modal.setLarge();
116
 
117
            modal.setSaveButtonText(saveText);
118
 
119
            // We want to reset the form every time it is opened.
120
            modal.getRoot().on(ModalEvents.hidden, this.destroy.bind(this));
121
 
122
            modal.setBody(this.getBody());
123
 
124
            // We catch the modal save event, and use it to submit the form inside the modal.
125
            // Triggering a form submission will give JS validation scripts a chance to check for errors.
126
            modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this));
127
            // We also catch the form submit event and use it to submit the form with ajax.
128
            modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this));
129
 
130
            this.modal = modal;
131
 
132
            modal.show();
133
        };
134
 
135
        /**
136
         * This triggers a form submission, so that any mform elements can do final tricks before the form submission is processed.
137
         *
138
         * @method submitForm
139
         * @param {Event} e Form submission event.
140
         * @private
141
         */
142
        AddCategory.prototype.submitForm = function(e) {
143
            e.preventDefault();
144
            this.modal.getRoot().find('form').submit();
145
        };
146
 
147
        AddCategory.prototype.submitFormAjax = function(e) {
148
            // We don't want to do a real form submission.
149
            e.preventDefault();
150
 
151
            // Convert all the form elements values to a serialised string.
152
            var formData = this.modal.getRoot().find('form').serialize();
153
 
154
            Ajax.call([{
155
                methodname: 'tool_dataprivacy_create_category_form',
156
                args: {jsonformdata: JSON.stringify(formData)},
157
                done: function(data) {
158
                    if (data.validationerrors) {
159
                        this.modal.setBody(this.getBody(formData));
160
                    } else {
161
                        this.close();
162
                    }
163
                }.bind(this),
164
                fail: Notification.exception
165
            }]);
166
        };
167
 
168
        AddCategory.prototype.close = function() {
169
            this.destroy();
170
            document.location.reload();
171
        };
172
 
173
        AddCategory.prototype.destroy = function() {
174
            FormChangeChecker.resetAllFormDirtyStates();
175
            this.modal.destroy();
176
        };
177
 
178
        AddCategory.prototype.removeListeners = function() {
179
            $(SELECTORS.CATEGORY_LINK).off('click');
180
        };
181
 
182
        return /** @alias module:tool_dataprivacy/add_category */ {
183
            getInstance: function(contextId) {
184
                return new AddCategory(contextId);
185
            }
186
        };
187
    }
188
);