Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * A dialogue type designed to display an appropriate error when a generic
3
 * javascript error was thrown and caught.
4
 *
5
 * @module moodle-core-notification
6
 * @submodule moodle-core-notification-exception
7
 */
8
 
9
var EXCEPTION_NAME = 'Moodle exception',
10
    EXCEPTION;
11
 
12
/**
13
 * Extends core Dialogue to show the exception dialogue.
14
 *
15
 * @param {Object} config Object literal specifying the dialogue configuration properties.
16
 * @constructor
17
 * @class M.core.exception
18
 * @extends M.core.dialogue
19
 */
20
EXCEPTION = function(c) {
21
    var config = Y.mix({}, c);
22
    config.width = config.width || (M.cfg.developerdebug) ? Math.floor(Y.one(document.body).get('winWidth') / 3) + 'px' : null;
23
    config.closeButton = true;
24
 
25
    // We need to allow some properties which are part of the exception
26
    // prototype, otherwise AttributeCore filters them during value normalisation.
27
    var allowlist = [
28
        'message',
29
        'name',
30
        'fileName',
31
        'lineNumber',
32
        'stack'
33
    ];
34
    Y.Array.each(allowlist, function(k) {
35
        config[k] = c[k];
36
    });
37
 
38
    EXCEPTION.superclass.constructor.apply(this, [config]);
39
};
40
Y.extend(EXCEPTION, M.core.notification.info, {
41
    _hideTimeout: null,
42
    _keypress: null,
43
    initializer: function(config) {
44
        var content,
45
            self = this,
46
            delay = this.get('hideTimeoutDelay'),
47
            labelsep = M.util.get_string('labelsep', 'langconfig');
48
        this.get(BASE).addClass('moodle-dialogue-exception');
49
        this.setStdModContent(Y.WidgetStdMod.HEADER,
50
                '<h5 id="moodle-dialogue-' + this.get('COUNT') + '-wrap-header-text">' + Y.Escape.html(config.name) + '</h5>',
51
                Y.WidgetStdMod.REPLACE);
52
        content = Y.Node.create('<div class="moodle-exception" data-rel="fatalerror"></div>')
53
                .append(Y.Node.create('<div class="moodle-exception-message">' + Y.Escape.html(this.get('message')) + '</div>'))
54
                .append(Y.Node.create('<div class="moodle-exception-param hidden param-filename"><label>' +
55
                        M.util.get_string('file', 'moodle') + labelsep + '</label> ' +
56
                        Y.Escape.html(this.get('fileName')) + '</div>'))
57
                .append(Y.Node.create('<div class="moodle-exception-param hidden param-linenumber"><label>' +
58
                        M.util.get_string('line', 'debug') + labelsep + '</label> ' +
59
                        Y.Escape.html(this.get('lineNumber')) + '</div>'))
60
                .append(Y.Node.create('<div class="moodle-exception-param hidden param-stacktrace"><label>' +
61
                        M.util.get_string('stacktrace', 'debug') + labelsep + '</label> <pre>' +
62
                        this.get('stack') + '</pre></div>'));
63
        if (M.cfg.developerdebug) {
64
            content.all('.moodle-exception-param').removeClass('hidden');
65
        }
66
        this.setStdModContent(Y.WidgetStdMod.BODY, content, Y.WidgetStdMod.REPLACE);
67
 
68
        if (delay) {
69
            this._hideTimeout = setTimeout(function() {
70
                self.hide();
71
            }, delay);
72
        }
73
        this.after('visibleChange', this.visibilityChanged, this);
74
        this._keypress = Y.on('key', this.hide, window, 'down:13,27', this);
75
        this.centerDialogue();
76
    },
77
    visibilityChanged: function(e) {
78
        if (e.attrName === 'visible' && e.prevVal && !e.newVal) {
79
            if (this._keypress) {
80
                this._keypress.detach();
81
            }
82
            var self = this;
83
            setTimeout(function() {
84
                self.destroy();
85
            }, 1000);
86
        }
87
    }
88
}, {
89
    NAME: EXCEPTION_NAME,
90
    CSS_PREFIX: DIALOGUE_PREFIX,
91
    ATTRS: {
92
        /**
93
         * The message of the alert.
94
         *
95
         * @attribute message
96
         * @type String
97
         * @default ''
98
         */
99
        message: {
100
            value: ''
101
        },
102
 
103
        /**
104
         * The name of the alert.
105
         *
106
         * @attribute title
107
         * @type String
108
         * @default ''
109
         */
110
        name: {
111
            value: ''
112
        },
113
 
114
        /**
115
         * The name of the file where the error was thrown.
116
         *
117
         * @attribute fileName
118
         * @type String
119
         * @default ''
120
         */
121
        fileName: {
122
            value: ''
123
        },
124
 
125
        /**
126
         * The line number where the error was thrown.
127
         *
128
         * @attribute lineNumber
129
         * @type String
130
         * @default ''
131
         */
132
        lineNumber: {
133
            value: ''
134
        },
135
 
136
        /**
137
         * The backtrace from the error
138
         *
139
         * @attribute lineNumber
140
         * @type String
141
         * @default ''
142
         */
143
        stack: {
144
            setter: function(str) {
145
                var lines = Y.Escape.html(str).split("\n"),
146
                    pattern = new RegExp('^(.+)@(' + M.cfg.wwwroot + ')?(.{0,75}).*:(\\d+)$'),
147
                    i;
148
                for (i in lines) {
149
                    lines[i] = lines[i].replace(pattern,
150
                            "<div class='stacktrace-line'>ln: $4</div>" +
151
                            "<div class='stacktrace-file'>$3</div>" +
152
                            "<div class='stacktrace-call'>$1</div>");
153
                }
154
                return lines.join("\n");
155
            },
156
            value: ''
157
        },
158
 
159
        /**
160
         * If set, the dialogue is hidden after the specified timeout period.
161
         *
162
         * @attribute hideTimeoutDelay
163
         * @type Number
164
         * @default null
165
         * @optional
166
         */
167
        hideTimeoutDelay: {
168
            validator: Y.Lang.isNumber,
169
            value: null
170
        }
171
    }
172
});
173
 
174
M.core.exception = EXCEPTION;