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