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
 * Action selector.
18
 *
19
 * To handle 'save' events use: actionselector.on('save')
20
 * This will receive the information to display in popup.
21
 * The actions have the format [{'text': sometext, 'value' : somevalue}].
22
 *
23
 * @module     tool_lp/actionselector
24
 * @copyright  2016 Serge Gauthier - <serge.gauthier.2@umontreal.ca>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
define(['jquery',
29
        'core/notification',
30
        'core/ajax',
31
        'core/templates',
32
        'tool_lp/dialogue',
33
        'tool_lp/event_base'],
34
        function($, Notification, Ajax, Templates, Dialogue, EventBase) {
35
 
36
    /**
37
     * Action selector class.
38
     *
39
     * @class tool_lp/actionselector
40
     * @param {String} title The title of popup.
41
     * @param {String} message The message to display.
42
     * @param {object} actions The actions that can be selected.
43
     * @param {String} confirm Text for confirm button.
44
     * @param {String} cancel Text for cancel button.
45
     */
46
    var ActionSelector = function(title, message, actions, confirm, cancel) {
47
        var self = this;
48
 
49
        EventBase.prototype.constructor.apply(this, []);
50
        self._title = title;
51
        self._message = message;
52
        self._actions = actions;
53
        self._confirm = confirm;
54
        self._cancel = cancel;
55
        self._selectedValue = null;
56
        self._reset();
57
    };
58
 
59
    ActionSelector.prototype = Object.create(EventBase.prototype);
60
 
61
    /** @property {String} The value that was selected. */
62
    ActionSelector.prototype._selectedValue = null;
63
    /** @property {Dialogue} The reference to the dialogue. */
64
    ActionSelector.prototype._popup = null;
65
    /** @property {String} The title of popup. */
66
    ActionSelector.prototype._title = null;
67
    /** @property {String} The message in popup. */
68
    ActionSelector.prototype._message = null;
69
    /** @property {object} The information for radion buttons. */
70
    ActionSelector.prototype._actions = null;
71
    /** @property {String} The text for confirm button. */
72
    ActionSelector.prototype._confirm = null;
73
    /** @property {String} The text for cancel button. */
74
    ActionSelector.prototype._cancel = null;
75
 
76
    /**
77
     * Hook to executed after the view is rendered.
78
     *
79
     * @method _afterRender
80
     */
81
    ActionSelector.prototype._afterRender = function() {
82
        var self = this;
83
 
84
        // Confirm button is disabled until a choice is done.
85
        self._find('[data-action="action-selector-confirm"]').attr('disabled', 'disabled');
86
 
87
        // Add listener for radio buttons change.
88
        self._find('[data-region="action-selector-radio-buttons"]').change(function() {
89
            self._selectedValue = $("input[type='radio']:checked").val();
90
            self._find('[data-action="action-selector-confirm"]').removeAttr('disabled');
91
            self._refresh.bind(self);
92
        });
93
 
94
        // Add listener for cancel.
95
        self._find('[data-action="action-selector-cancel"]').click(function(e) {
96
            e.preventDefault();
97
            self.close();
98
        });
99
 
100
        // Add listener for confirm.
101
        self._find('[data-action="action-selector-confirm"]').click(function(e) {
102
            e.preventDefault();
103
            if (!self._selectedValue.length) {
104
                return;
105
            }
106
            self._trigger('save', {action: self._selectedValue});
107
            self.close();
108
        });
109
    };
110
 
111
    /**
112
     * Close the dialogue.
113
     *
114
     * @method close
115
     */
116
    ActionSelector.prototype.close = function() {
117
        var self = this;
118
        self._popup.close();
119
        self._reset();
120
    };
121
 
122
    /**
123
     * Opens the action selector.
124
     *
125
     * @method display
126
     * @return {Promise}
127
     */
128
    ActionSelector.prototype.display = function() {
129
        var self = this;
130
        return self._render().then(function(html) {
131
            self._popup = new Dialogue(
132
                self._title,
133
                html,
134
                self._afterRender.bind(self)
135
            );
136
            return;
137
        }).fail(Notification.exception);
138
    };
139
 
140
    /**
141
     * Find a node in the dialogue.
142
     *
143
     * @param {String} selector
144
     * @return {JQuery} The node
145
     * @method _find
146
     */
147
    ActionSelector.prototype._find = function(selector) {
148
        return $(this._popup.getContent()).find(selector);
149
    };
150
 
151
    /**
152
     * Refresh the view.
153
     *
154
     * @method _refresh
155
     * @return {Promise}
156
     */
157
    ActionSelector.prototype._refresh = function() {
158
        var self = this;
159
        return self._render().then(function(html) {
160
            self._find('[data-region="action-selector"]').replaceWith(html);
161
            self._afterRender();
162
            return;
163
        });
164
    };
165
 
166
    /**
167
     * Render the dialogue.
168
     *
169
     * @method _render
170
     * @return {Promise}
171
     */
172
    ActionSelector.prototype._render = function() {
173
        var self = this;
174
        var choices = [];
175
        for (var i in self._actions) {
176
            choices.push(self._actions[i]);
177
        }
178
        var content = {'message': self._message, 'choices': choices,
179
            'confirm': self._confirm, 'cancel': self._cancel};
180
 
181
        return Templates.render('tool_lp/action_selector', content);
182
    };
183
 
184
    /**
185
     * Reset the dialogue properties.
186
     *
187
     * This does not reset everything, just enough to reset the UI.
188
     *
189
     * @method _reset
190
     */
191
    ActionSelector.prototype._reset = function() {
192
        this._popup = null;
193
        this._selectedValue = '';
194
    };
195
 
196
    return ActionSelector;
197
 
198
});