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
 * Controls the content area of the notification area on the
18
 * notification page.
19
 *
20
 * @module     message_popup/notification_area_content_area
21
 * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
define(['jquery', 'core/templates', 'core/notification', 'core/custom_interaction_events',
25
        'message_popup/notification_repository', 'message_popup/notification_area_events'],
26
    function($, Templates, DebugNotification, CustomEvents, NotificationRepo, NotificationAreaEvents) {
27
 
28
    var SELECTORS = {
29
        CONTAINER: '[data-region="notification-area"]',
30
        CONTENT: '[data-region="content"]',
31
        HEADER: '[data-region="header"]',
32
        FOOTER: '[data-region="footer"]',
33
        TOGGLE_MODE: '[data-action="toggle-mode"]',
34
    };
35
 
36
    var TEMPLATES = {
37
        HEADER: 'message_popup/notification_area_content_area_header',
38
        CONTENT: 'message_popup/notification_area_content_area_content',
39
        FOOTER: 'message_popup/notification_area_content_area_footer',
40
    };
41
 
42
    /**
43
     * Constructor for the ContentArea
44
     *
45
     * @class
46
     * @param {object} root The root element for the content area
47
     * @param {int} userId The user id of the current user
48
     */
49
    var ContentArea = function(root, userId) {
50
        this.root = $(root);
51
        this.container = this.root.closest(SELECTORS.CONTAINER);
52
        this.userId = userId;
53
        this.header = this.root.find(SELECTORS.HEADER);
54
        this.content = this.root.find(SELECTORS.CONTENT);
55
        this.footer = this.root.find(SELECTORS.FOOTER);
56
 
57
        this.registerEventListeners();
58
    };
59
 
60
    /**
61
     * Get the root element.
62
     *
63
     * @method getRoot
64
     * @return {object} jQuery element
65
     */
66
    ContentArea.prototype.getRoot = function() {
67
        return this.root;
68
    };
69
 
70
    /**
71
     * Get the container element (which the content area is within).
72
     *
73
     * @method getContainer
74
     * @return {object} jQuery element
75
     */
76
    ContentArea.prototype.getContainer = function() {
77
        return this.container;
78
    };
79
 
80
    /**
81
     * Get the user id.
82
     *
83
     * @method getUserId
84
     * @return {int}
85
     */
86
    ContentArea.prototype.getUserId = function() {
87
        return this.userId;
88
    };
89
 
90
    /**
91
     * Get the content area header element.
92
     *
93
     * @method getHeader
94
     * @return {object} jQuery element
95
     */
96
    ContentArea.prototype.getHeader = function() {
97
        return this.header;
98
    };
99
 
100
    /**
101
     * Get the content area content element.
102
     *
103
     * @method getContent
104
     * @return {object} jQuery element
105
     */
106
    ContentArea.prototype.getContent = function() {
107
        return this.content;
108
    };
109
 
110
    /**
111
     * Get the content area footer element.
112
     *
113
     * @method getFooter
114
     * @return {object} jQuery element
115
     */
116
    ContentArea.prototype.getFooter = function() {
117
        return this.footer;
118
    };
119
 
120
    /**
121
     * Display the content area. Typically used with responsive
122
     * styling on smaller screens.
123
     *
124
     * @method show
125
     */
126
    ContentArea.prototype.show = function() {
127
        this.getContainer().addClass('show-content-area');
128
    };
129
 
130
    /**
131
     * Hide the content area. Typically used with responsive
132
     * styling on smaller screens.
133
     *
134
     * @method hide
135
     */
136
    ContentArea.prototype.hide = function() {
137
        this.getContainer().removeClass('show-content-area');
138
    };
139
 
140
    /**
141
     * Change the HTML in the content area header element.
142
     *
143
     * @method setHeaderHTML
144
     * @param {string} html The HTML to be set
145
     */
146
    ContentArea.prototype.setHeaderHTML = function(html) {
147
        this.getHeader().empty().html(html);
148
    };
149
 
150
    /**
151
     * Change the HTML in the content area content element.
152
     *
153
     * @method setContentHMTL
154
     * @param {string} html The HTML to be set.
155
     */
156
    ContentArea.prototype.setContentHTML = function(html) {
157
        this.getContent().empty().html(html);
158
    };
159
 
160
    /**
161
     * Change the HTML in the content area footer element.
162
     *
163
     * @method setFooterHTML
164
     * @param {string} html The HTML to be set.
165
     */
166
    ContentArea.prototype.setFooterHTML = function(html) {
167
        this.getFooter().empty().html(html);
168
    };
169
 
170
    /**
171
     * Render the given notification context in the content area.
172
     *
173
     * @method showNotification
174
     * @param {object} notification The notification context (from a webservice)
175
     * @return {object} jQuery promise
176
     */
177
    ContentArea.prototype.showNotification = function(notification) {
178
        var headerPromise = Templates.render(TEMPLATES.HEADER, notification).done(function(html) {
179
            this.setHeaderHTML(html);
180
        }.bind(this));
181
 
182
        var contentPromise = Templates.render(TEMPLATES.CONTENT, notification).done(function(html) {
183
            this.setContentHTML(html);
184
        }.bind(this));
185
 
186
        var footerPromise = Templates.render(TEMPLATES.FOOTER, notification).done(function(html) {
187
            this.setFooterHTML(html);
188
        }.bind(this));
189
 
190
        return $.when(headerPromise, contentPromise, footerPromise).done(function() {
191
            this.show();
192
            this.getContainer().trigger(NotificationAreaEvents.notificationShown, [notification]);
193
        }.bind(this));
194
    };
195
 
196
    /**
197
     * Create the event listeners for the content area.
198
     *
199
     * @method registerEventListeners
200
     */
201
    ContentArea.prototype.registerEventListeners = function() {
202
        CustomEvents.define(this.getRoot(), [
203
            CustomEvents.events.activate
204
        ]);
205
 
206
        this.getRoot().on(CustomEvents.events.activate, SELECTORS.VIEW_TOGGLE, function() {
207
            this.hide();
208
        }.bind(this));
209
 
210
        this.getContainer().on(NotificationAreaEvents.showNotification, function(e, notification) {
211
            this.showNotification(notification);
212
        }.bind(this));
213
    };
214
 
215
    return ContentArea;
216
});