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