diff --git a/core/includes/common.inc b/core/includes/common.inc index 46caf3e..a57b952 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -4046,6 +4046,7 @@ function drupal_add_js($data = NULL, $options = NULL) { 'data' => array( array('basePath' => base_path()), array('pathPrefix' => empty($prefix) ? '' : $prefix), + array('errorLevel' => (integer) variable_get('error_level', ERROR_REPORTING_DISPLAY_ALL)), ), 'type' => 'setting', 'scope' => 'header', diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 652b8e2..b012414 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -259,7 +259,7 @@ Drupal.ajax.prototype.eventResponse = function (element, event) { // Unset the ajax.ajaxing flag here because it won't be unset during // the complete response. ajax.ajaxing = false; - alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message); + Drupal.log("An error occurred while attempting to process " + ajax.options.url + ": " + e.message, 'error'); } // For radio/checkbox, allow the default event. On IE, this means letting @@ -448,7 +448,7 @@ Drupal.ajax.prototype.getEffect = function (response) { * Handler for the form redirection error. */ Drupal.ajax.prototype.error = function (response, uri) { - alert(Drupal.ajaxError(response, uri)); + Drupal.log(Drupal.ajaxError(response, uri), 'error'); // Remove the progress element. if (this.progress.element) { $(this.progress.element).remove(); diff --git a/core/misc/autocomplete.js b/core/misc/autocomplete.js index 2f4e5f6..a310f18 100644 --- a/core/misc/autocomplete.js +++ b/core/misc/autocomplete.js @@ -309,7 +309,7 @@ Drupal.ACDB.prototype.search = function (searchString) { } }, error: function (xmlhttp) { - alert(Drupal.ajaxError(xmlhttp, db.uri)); + Drupal.log(Drupal.ajaxError(xmlhttp, db.uri), 'error'); } }); }, this.delay); diff --git a/core/misc/drupal.js b/core/misc/drupal.js index 83b0884..03ec875 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -1,4 +1,3 @@ - var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} }; // Allow other JavaScript libraries to use $. @@ -110,6 +109,54 @@ Drupal.detachBehaviors = function (context, settings, trigger) { }; /** + * TODO description + * + * Drupal.log(message, 'status'); console.log(message); + * Drupal.log(message, 'warning'); console.warn(message); + * Drupal.log(message, 'error'); console.error(message); + * + * Drupal.log(message, 'myType'); console.log('myType', message); + */ +Drupal.log = (function () { + // Initialisation is done in a closure in case Drupal.log is used in a + // demanding loop, inside an onresize function for example. + + // Status from drupal_set_message(). + var status = ['status', 'warning', 'error']; + var settings = Drupal.settings; + + // Map Drupal error level to console object methods. + var mapToConsole = {status: 'log', warning: 'warn', error: 'error'}; + var consoleAvailable = (typeof window['console'] === 'object'); + + // Actual logging function + return function (message, type) { + var customType = null; + var args = []; + // Checking here allow dynamic error level switching. + if (settings.errorLevel !== 0) { + type = type || 'status'; + // index in status table <=> error reporting level + var errorLevel = $.inArray(type, status); + if (errorLevel === -1) { + errorLevel = 0; + args.push(type + ':'); + type = 'status'; + } + if (settings.errorLevel >= errorLevel) { + args.push(message); + if (consoleAvailable) { + console[mapToConsole[type]].apply(this, args); + } + else { + alert(args.join("\n")); + } + } + } + }; +}()); + +/** * Encode special characters in a plain-text string for display as HTML. * * @ingroup sanitization