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
 * A notify function for the Atto editor.
18
 *
19
 * @module     moodle-editor_atto-notify
20
 * @submodule  notify
21
 * @package    editor_atto
22
 * @copyright  2014 Damyon Wiese
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
var LOGNAME_NOTIFY = 'moodle-editor_atto-editor-notify',
27
    NOTIFY_INFO = 'info',
28
    NOTIFY_WARNING = 'warning';
29
 
30
function EditorNotify() {}
31
 
32
EditorNotify.ATTRS = {
33
};
34
 
35
EditorNotify.prototype = {
36
 
37
    /**
38
     * A single Y.Node for this editor. There is only ever one - it is replaced if a new message comes in.
39
     *
40
     * @property messageOverlay
41
     * @type {Node}
42
     */
43
    messageOverlay: null,
44
 
45
    /**
46
     * A single timer object that can be used to cancel the hiding behaviour.
47
     *
48
     * @property hideTimer
49
     * @type {timer}
50
     */
51
    hideTimer: null,
52
 
53
    /**
54
     * Initialize the notifications.
55
     *
56
     * @method setupNotifications
57
     * @chainable
58
     */
59
    setupNotifications: function() {
60
        var preload1 = new Image(),
61
            preload2 = new Image();
62
 
63
        preload1.src = M.util.image_url('i/warning', 'moodle');
64
        preload2.src = M.util.image_url('i/info', 'moodle');
65
 
66
        return this;
67
    },
68
 
69
    /**
70
     * Show a notification in a floaty overlay somewhere in the atto editor text area.
71
     *
72
     * @method showMessage
73
     * @param {String} message The translated message (use get_string)
74
     * @param {String} type Must be either "info" or "warning"
75
     * @param {Number} timeout Time in milliseconds to show this message for.
76
     * @chainable
77
     */
78
    showMessage: function(message, type, timeout) {
79
        var messageTypeIcon = '',
80
            intTimeout,
81
            bodyContent;
82
 
83
        if (this.messageOverlay === null) {
84
            this.messageOverlay = Y.Node.create('<div class="editor_atto_notification"></div>');
85
 
86
            this.messageOverlay.hide(true);
87
            this.textarea.get('parentNode').append(this.messageOverlay);
88
 
89
            this.messageOverlay.on('click', function() {
90
                this.messageOverlay.hide(true);
91
            }, this);
92
        }
93
 
94
        if (this.hideTimer !== null) {
95
            this.hideTimer.cancel();
96
        }
97
 
98
        if (type === NOTIFY_WARNING) {
99
            messageTypeIcon = '<img src="' +
100
                              M.util.image_url('i/warning', 'moodle') +
101
                              '" alt="' + M.util.get_string('warning', 'moodle') + '"/>';
102
        } else if (type === NOTIFY_INFO) {
103
            messageTypeIcon = '<img src="' +
104
                              M.util.image_url('i/info', 'moodle') +
105
                              '" alt="' + M.util.get_string('info', 'moodle') + '"/>';
106
        } else {
107
            Y.log('Invalid message type specified: ' + type + '. Must be either "info" or "warning".', 'debug', LOGNAME_NOTIFY);
108
        }
109
 
110
        // Parse the timeout value.
111
        intTimeout = parseInt(timeout, 10);
112
        if (intTimeout <= 0) {
113
            intTimeout = 60000;
114
        }
115
 
116
        // Convert class to atto_info (for example).
117
        type = 'atto_' + type;
118
 
119
        bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
120
                                        messageTypeIcon + ' ' +
121
                                        Y.Escape.html(message) +
122
                                        '</div>');
123
        this.messageOverlay.empty();
124
        this.messageOverlay.append(bodyContent);
125
        this.messageOverlay.show(true);
126
 
127
        this.hideTimer = Y.later(intTimeout, this, function() {
128
            Y.log('Hide Atto notification.', 'debug', LOGNAME_NOTIFY);
129
            this.hideTimer = null;
130
            if (this.messageOverlay.inDoc()) {
131
                this.messageOverlay.hide(true);
132
            }
133
        });
134
 
135
        return this;
136
    }
137
 
138
};
139
 
140
Y.Base.mix(Y.M.editor_atto.Editor, [EditorNotify]);