diff --git a/core/includes/common.inc b/core/includes/common.inc index 5ff6167..9267a9f 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -4114,6 +4114,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', 2)), ), 'type' => 'setting', 'scope' => 'header', diff --git a/core/misc/ajax.js b/core/misc/ajax.js index bd17811..1eb981e 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 a4f6335..5350fb8 100644 --- a/core/misc/autocomplete.js +++ b/core/misc/autocomplete.js @@ -310,7 +310,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 c7917b0..d06a0a3 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -111,6 +111,52 @@ 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'); + // If no console is available, return a dummy function. + if (!consoleAvailable) { + return function () {}; + } + // 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); + console[mapToConsole[type]].apply(console, args); + } + } + }; +}()); + +/** * Encode special characters in a plain-text string for display as HTML. * * @ingroup sanitization diff --git a/core/modules/simpletest/tests/common.test b/core/modules/simpletest/tests/common.test index 12df48c..79b685f 100644 --- a/core/modules/simpletest/tests/common.test +++ b/core/modules/simpletest/tests/common.test @@ -1240,8 +1240,8 @@ class CommonJavaScriptTestCase extends DrupalWebTestCase { */ function testAddSetting() { $javascript = drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting'); - $this->assertEqual(280342800, $javascript['settings']['data'][2]['dries'], t('JavaScript setting is set correctly.')); - $this->assertEqual('rocks', $javascript['settings']['data'][2]['drupal'], t('The other JavaScript setting is set correctly.')); + $this->assertEqual(280342800, $javascript['settings']['data'][3]['dries'], t('JavaScript setting is set correctly.')); + $this->assertEqual('rocks', $javascript['settings']['data'][3]['drupal'], t('The other JavaScript setting is set correctly.')); } /**