Translated using Weblate (Slovenian)
[phpmyadmin.git] / js / error_report.js
blobb2d87d67e5bd970e09d62cfe99ebd9ef8bd8029e
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * general function, usually for data manipulation pages
4  *
5  */
7 var ErrorReport = {
8     /**
9      * @var object stores the last exception info
10      */
11     _last_exception: null,
12     /**
13      * handles thrown error exceptions based on user preferences
14      *
15      * @return void
16      */
17     error_handler: function (exception) {
18         if (exception.name === null || typeof(exception.name) == "undefined") {
19             exception.name = ErrorReport._extractExceptionName(exception);
20         }
21         ErrorReport._last_exception = exception;
22         $.get("error_report.php", {
23             ajax_request: true,
24             server: PMA_commonParams.get('server'),
25             token: PMA_commonParams.get('token'),
26             get_settings: true,
27             exception_type: 'js'
28         }, function (data) {
29             if (data.success !== true) {
30                 PMA_ajaxShowMessage(data.error, false);
31                 return;
32             }
33             if (data.report_setting == "ask") {
34                 ErrorReport._showErrorNotification();
35             } else if (data.report_setting == "always") {
36                 report_data = ErrorReport._get_report_data(exception);
37                 post_data = $.extend(report_data, {
38                     send_error_report: true,
39                     automatic: true
40                 });
41                 $.post("error_report.php", post_data, function (data) {
42                     if (data.success === false) {
43                         //in the case of an error, show the error message returned.
44                         PMA_ajaxShowMessage(data.error, false);
45                     } else {
46                         PMA_ajaxShowMessage(data.message, false);
47                     }
48                 });
49             }
50         });
51     },
52     /**
53      * Shows the modal dialog previewing the report
54      *
55      * @param exception object error report info
56      *
57      * @return void
58      */
59     _showReportDialog: function (exception) {
60         var report_data = ErrorReport._get_report_data(exception);
62         /*Remove the hidden dialogs if there are*/
63         if ($('#error_report_dialog').length !== 0) {
64             $('#error_report_dialog').remove();
65         }
66         var $div = $('<div id="error_report_dialog"></div>');
68         var button_options = {};
70         button_options[PMA_messages.strSendErrorReport] = function () {
71             var $dialog = $(this);
72             var post_data = $.extend(report_data, {
73                 send_error_report: true,
74                 description: $("#report_description").val(),
75                 always_send: $("#always_send_checkbox")[0].checked
76             });
77             $.post("error_report.php", post_data, function (data) {
78                 $dialog.dialog('close');
79                 if (data.success === false) {
80                     //in the case of an error, show the error message returned.
81                     PMA_ajaxShowMessage(data.error, false);
82                 } else {
83                     PMA_ajaxShowMessage(data.message, 3000);
84                 }
85             });
86         };
88         button_options[PMA_messages.strCancel] = function () {
89             $(this).dialog('close');
90         };
92         $.post("error_report.php", report_data, function (data) {
93             if (data.success === false) {
94                 //in the case of an error, show the error message returned.
95                 PMA_ajaxShowMessage(data.error, false);
96             } else {
97                 // Show dialog if the request was successful
98                 $div
99                 .append(data.message)
100                 .dialog({
101                     title: PMA_messages.strSubmitErrorReport,
102                     width: 650,
103                     modal: true,
104                     buttons: button_options,
105                     close: function () {
106                         $(this).remove();
107                     }
108                 });
109             }
110         }); // end $.get()
111     },
112     /**
113      * Shows the small notification that asks for user permission
114      *
115      * @return void
116      */
117     _showErrorNotification: function () {
118         ErrorReport._removeErrorNotification();
120         var $div = $(
121             '<div style="position:fixed;bottom:0;left:0;right:0;margin:0;' +
122             'z-index:1000" class="error" id="error_notification"></div>'
123         ).append(
124             PMA_getImage("s_error.png") + PMA_messages.strErrorOccurred
125         );
127         var $buttons = $('<div style="float:right"></div>');
129         var button_html  = '<button id="show_error_report">';
130         button_html += PMA_messages.strShowReportDetails;
131         button_html += '</button>';
133         button_html += '<a id="change_error_settings">';
134         button_html += PMA_getImage('s_cog.png', PMA_messages.strChangeReportSettings);
135         button_html += '</a>';
137         button_html += '<a href="#" id="ignore_error">';
138         button_html += PMA_getImage('b_close.png', PMA_messages.strIgnore);
139         button_html += '</a>';
141         $buttons.html(button_html);
143         $div.append($buttons);
144         $div.appendTo(document.body);
145         $("#change_error_settings").on("click", ErrorReport._redirect_to_settings);
146         $("#show_error_report").on("click", ErrorReport._createReportDialog);
147         $("#ignore_error").on("click", ErrorReport._removeErrorNotification);
148     },
149     /**
150      * Removes the notification if it was displayed before
151      *
152      * @return void
153      */
154     _removeErrorNotification: function (e) {
155         if (e) {
156             // don't remove the hash fragment by navigating to #
157             e.preventDefault();
158         }
159         $("#error_notification").fadeOut(function () {
160             $(this).remove();
161         });
162     },
163     /**
164      * Extracts Exception name from message if it exists
165      *
166      * @return String
167      */
168     _extractExceptionName: function (exception) {
169         if (exception.message === null || typeof(exception.message) == "undefined"){
170             return "";
171         } else {
172             var reg = /([a-zA-Z]+):/;
173             var regex_result = null;
174             regex_result = reg.exec(exception.message);
175             if(regex_result && regex_result.length == 2)
176                 return regex_result[1];
177             else
178                 return "";
179         }
180     },
181     /**
182      * Shows the modal dialog previewing the report
183      *
184      * @return void
185      */
186     _createReportDialog: function () {
187         ErrorReport._removeErrorNotification();
188         ErrorReport._showReportDialog(ErrorReport._last_exception);
189     },
190     /**
191      * Returns the needed info about stored microhistory
192      *
193      * @return object
194      */
195     _get_microhistory: function () {
196         var cached_pages = AJAX.cache.pages.slice(-7);
197         var remove = ["common_query", "table", "db", "token", "pma_absolute_uri"];
198         return {
199             pages: cached_pages.map(function (page) {
200                 var simplepage = {
201                     hash: page.hash
202                 };
204                 if (page.params) {
205                     simplepage.params = $.extend({}, page.params);
206                     $.each(simplepage.params, function (param) {
207                         if ($.inArray(param, remove) != -1) {
208                             delete simplepage.params[param];
209                         }
210                     });
211                 }
213                 return simplepage;
214             }),
215             current_index: AJAX.cache.current -
216                 (AJAX.cache.pages.length - cached_pages.length)
217         };
218     },
219     /**
220      * Redirects to the settings page containing error report
221      * preferences
222      *
223      * @return void
224      */
225     _redirect_to_settings: function () {
226         window.location.href = "prefs_forms.php?token=" + PMA_commonParams.get('token');
227     },
228     /**
229      * Returns the report data to send to the server
230      *
231      * @param exception object exception info
232      *
233      * @return object
234      */
235     _get_report_data: function (exception) {
236         var report_data = {
237             "ajax_request": true,
238             "token": PMA_commonParams.get('token'),
239             "exception": exception,
240             "current_url": window.location.href,
241             "microhistory": ErrorReport._get_microhistory(),
242             "exception_type": 'js'
243         };
244         if (typeof AJAX.cache.pages[AJAX.cache.current - 1] !== 'undefined') {
245             report_data.scripts = AJAX.cache.pages[AJAX.cache.current - 1].scripts.map(
246                 function (script) {
247                     return script.name;
248                 }
249             );
250         }
251         return report_data;
252     },
253     /**
254      * Wraps all global functions that start with PMA_
255      *
256      * @return void
257      */
258     wrap_global_functions: function () {
259         for (var key in window) {
260             if (key.indexOf("PMA_") === 0) {
261                 var global = window[key];
262                 if (typeof(global) === "function") {
263                     window[key] = ErrorReport.wrap_function(global);
264                 }
265             }
266         }
267     },
268     /**
269      * Wraps given function in error reporting code and returns wrapped function
270      *
271      * @param func function to be wrapped
272      *
273      * @return function
274      */
275     wrap_function: function (func) {
276         if (!func.wrapped) {
277             var new_func = function () {
278                 try {
279                     return func.apply(this, arguments);
280                 } catch (x) {
281                     TraceKit.report(x);
282                 }
283             };
284             new_func.wrapped = true;
285             //Set guid of wrapped function same as original function, so it can be removed
286             //See bug#4146 (problem with jquery draggable and sortable)
287             new_func.guid = func.guid = func.guid || new_func.guid || jQuery.guid++;
288             return new_func;
289         } else {
290             return func;
291         }
292     },
293     /**
294      * Automatically wraps the callback in AJAX.registerOnload
295      *
296      * @return void
297      */
298     _wrap_ajax_onload_callback: function () {
299         var oldOnload = AJAX.registerOnload;
300         AJAX.registerOnload = function (file, func) {
301             func = ErrorReport.wrap_function(func);
302             oldOnload.call(this, file, func);
303         };
304     },
305     /**
306      * Automatically wraps the callback in $.fn.on
307      *
308      * @return void
309      */
310     _wrap_$_on_callback: function () {
311         var oldOn = $.fn.on;
312         $.fn.on = function () {
313             for (var i = 1; i <= 3; i++) {
314                 if (typeof(arguments[i]) === "function") {
315                     arguments[i] = ErrorReport.wrap_function(arguments[i]);
316                     break;
317                 }
318             }
319             return oldOn.apply(this, arguments);
320         };
321     },
322     /**
323      * Wraps all global functions that start with PMA_
324      * also automatically wraps the callback in AJAX.registerOnload
325      *
326      * @return void
327      */
328     set_up_error_reporting: function () {
329         ErrorReport.wrap_global_functions();
330         ErrorReport._wrap_ajax_onload_callback();
331         ErrorReport._wrap_$_on_callback();
332     }
336 TraceKit.report.subscribe(ErrorReport.error_handler);
337 ErrorReport.set_up_error_reporting();
338 $(function () {
339     ErrorReport.wrap_global_functions();