Issue #1760548 by nod_: Remove dependency on jQuery drupal.js? for JS settings.

8.0.x
catch 2012-09-24 14:12:09 +01:00
parent e2743fdb89
commit bd07cedb74
17 changed files with 46 additions and 49 deletions

View File

@ -76,6 +76,11 @@ const CSS_DEFAULT = 0;
*/ */
const CSS_THEME = 100; const CSS_THEME = 100;
/**
* The default group for JavaScript settings added to the page.
*/
const JS_SETTING = -200;
/** /**
* The default group for JavaScript and jQuery libraries added to the page. * The default group for JavaScript and jQuery libraries added to the page.
*/ */
@ -91,11 +96,6 @@ const JS_DEFAULT = 0;
*/ */
const JS_THEME = 100; const JS_THEME = 100;
/**
* The default group for JavaScript settings added to the page.
*/
const JS_SETTING = 200;
/** /**
* Error code indicating that the request exceeded the specified timeout. * Error code indicating that the request exceeded the specified timeout.
* *
@ -4038,7 +4038,7 @@ function drupal_pre_render_scripts($elements) {
switch ($item['type']) { switch ($item['type']) {
case 'setting': case 'setting':
$element['#value_prefix'] = $embed_prefix; $element['#value_prefix'] = $embed_prefix;
$element['#value'] = 'jQuery.extend(Drupal.settings, ' . drupal_json_encode(drupal_array_merge_deep_array($item['data'])) . ");"; $element['#value'] = 'var drupalSettings = ' . drupal_json_encode(drupal_array_merge_deep_array($item['data'])) . ";";
$element['#value_suffix'] = $embed_suffix; $element['#value_suffix'] = $embed_suffix;
break; break;

View File

@ -1,14 +1,17 @@
var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} }; var Drupal = Drupal || { 'behaviors': {}, 'locale': {} };
// Allow other JavaScript libraries to use $. // Allow other JavaScript libraries to use $.
jQuery.noConflict(); jQuery.noConflict();
// JavaScript should be made compatible with libraries other than jQuery by // JavaScript should be made compatible with libraries other than jQuery by
// wrapping it in an anonymous closure. // wrapping it in an anonymous closure.
(function ($, Drupal, window, document, undefined) { (function ($, Drupal, drupalSettings) {
"use strict"; "use strict";
// Populate Drupal.settings with the drupalSettings variable.
Drupal.settings = drupalSettings;
/** /**
* Custom error type thrown after attach/detach if one or more behaviors failed. * Custom error type thrown after attach/detach if one or more behaviors failed.
* *
@ -71,7 +74,7 @@ DrupalBehaviorError.prototype = new Error();
*/ */
Drupal.attachBehaviors = function (context, settings) { Drupal.attachBehaviors = function (context, settings) {
context = context || document; context = context || document;
settings = settings || Drupal.settings; settings = settings || drupalSettings;
var i, errors = [], behaviors = Drupal.behaviors; var i, errors = [], behaviors = Drupal.behaviors;
// Execute all of them. // Execute all of them.
for (i in behaviors) { for (i in behaviors) {
@ -133,7 +136,7 @@ Drupal.attachBehaviors = function (context, settings) {
*/ */
Drupal.detachBehaviors = function (context, settings, trigger) { Drupal.detachBehaviors = function (context, settings, trigger) {
context = context || document; context = context || document;
settings = settings || Drupal.settings; settings = settings || drupalSettings;
trigger = trigger || 'unload'; trigger = trigger || 'unload';
var i, errors = [], behaviors = Drupal.behaviors; var i, errors = [], behaviors = Drupal.behaviors;
// Execute all of them. // Execute all of them.
@ -159,7 +162,7 @@ Drupal.detachBehaviors = function (context, settings, trigger) {
* @todo Temporary solution for the mobile initiative. * @todo Temporary solution for the mobile initiative.
*/ */
Drupal.checkWidthBreakpoint = function (width) { Drupal.checkWidthBreakpoint = function (width) {
width = width || Drupal.settings.widthBreakpoint || 640; width = width || drupalSettings.widthBreakpoint || 640;
return (document.documentElement.clientWidth > width); return (document.documentElement.clientWidth > width);
}; };
@ -259,7 +262,7 @@ Drupal.t = function (str, args, options) {
* Returns the URL to a Drupal page. * Returns the URL to a Drupal page.
*/ */
Drupal.url = function (path) { Drupal.url = function (path) {
return Drupal.settings.basePath + Drupal.settings.scriptPath + path; return drupalSettings.basePath + drupalSettings.scriptPath + path;
}; };
/** /**
@ -434,7 +437,7 @@ $('html').addClass('js');
//Attach all behaviors. //Attach all behaviors.
$(function () { $(function () {
Drupal.attachBehaviors(document, Drupal.settings); Drupal.attachBehaviors(document, drupalSettings);
}); });
/** /**
@ -455,4 +458,4 @@ $.extend(Drupal.theme, {
} }
}); });
})(jQuery, Drupal, this, this.document); })(jQuery, Drupal, window.drupalSettings);

View File

@ -771,7 +771,7 @@ function color_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
), ),
); );

View File

@ -397,7 +397,7 @@ function field_ui_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
), ),
); );

View File

@ -1710,7 +1710,7 @@ function file_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
), ),
); );

View File

@ -466,7 +466,7 @@ function locale_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
), ),
); );

View File

@ -4054,7 +4054,7 @@ function node_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'drupal.form'), array('system', 'drupal.form'),
), ),
); );

View File

@ -213,7 +213,7 @@ function overlay_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.ui.core'), array('system', 'jquery.ui.core'),
array('system', 'jquery.bbq'), array('system', 'jquery.bbq'),
), ),
@ -232,7 +232,7 @@ function overlay_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
), ),
); );

View File

@ -1988,7 +1988,7 @@ abstract class WebTestBase extends TestBase {
$this->plainTextContent = FALSE; $this->plainTextContent = FALSE;
$this->elements = FALSE; $this->elements = FALSE;
$this->drupalSettings = array(); $this->drupalSettings = array();
if (preg_match('/jQuery\.extend\(Drupal\.settings, (.*?)\);/', $content, $matches)) { if (preg_match('/var drupalSettings = (.*?);/', $content, $matches)) {
$this->drupalSettings = drupal_json_decode($matches[1]); $this->drupalSettings = drupal_json_decode($matches[1]);
} }
} }

View File

@ -551,7 +551,7 @@ function simpletest_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
array('system', 'drupal.tableselect'), array('system', 'drupal.tableselect'),
), ),

View File

@ -474,7 +474,7 @@ function statistics_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
), ),
); );

View File

@ -76,7 +76,7 @@ class JavaScriptTest extends WebTestBase {
*/ */
function testAddSetting() { function testAddSetting() {
// Add a file in order to test default settings. // Add a file in order to test default settings.
drupal_add_library('system', 'drupal.settings'); drupal_add_library('system', 'drupalSettings');
$javascript = drupal_add_js(); $javascript = drupal_add_js();
$last_settings = reset($javascript['settings']['data']); $last_settings = reset($javascript['settings']['data']);
$this->assertTrue($last_settings['currentPath'], 'The current path JavaScript setting is set correctly.'); $this->assertTrue($last_settings['currentPath'], 'The current path JavaScript setting is set correctly.');
@ -136,15 +136,13 @@ class JavaScriptTest extends WebTestBase {
* Test drupal_get_js() for JavaScript settings. * Test drupal_get_js() for JavaScript settings.
*/ */
function testHeaderSetting() { function testHeaderSetting() {
drupal_add_library('system', 'drupal.settings'); drupal_add_library('system', 'drupalSettings');
$javascript = drupal_get_js('header'); $javascript = drupal_get_js('header');
$this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.'); $this->assertTrue(strpos($javascript, 'basePath') > 0, 'Rendered JavaScript header returns basePath setting.');
$this->assertTrue(strpos($javascript, 'scriptPath') > 0, 'Rendered JavaScript header returns scriptPath setting.'); $this->assertTrue(strpos($javascript, 'scriptPath') > 0, 'Rendered JavaScript header returns scriptPath setting.');
$this->assertTrue(strpos($javascript, 'pathPrefix') > 0, 'Rendered JavaScript header returns pathPrefix setting.'); $this->assertTrue(strpos($javascript, 'pathPrefix') > 0, 'Rendered JavaScript header returns pathPrefix setting.');
$this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.'); $this->assertTrue(strpos($javascript, 'currentPath') > 0, 'Rendered JavaScript header returns currentPath setting.');
$this->assertTrue(strpos($javascript, 'core/misc/drupal.js') > 0, 'Rendered JavaScript header includes Drupal.js.');
$this->assertTrue(strpos($javascript, 'core/misc/jquery.js') > 0, 'Rendered JavaScript header includes jQuery.');
// Only the second of these two entries should appear in Drupal.settings. // Only the second of these two entries should appear in Drupal.settings.
drupal_add_js(array('commonTest' => 'commonTestShouldNotAppear'), 'setting'); drupal_add_js(array('commonTest' => 'commonTestShouldNotAppear'), 'setting');
@ -173,11 +171,11 @@ class JavaScriptTest extends WebTestBase {
$this->assertTrue($associative_array_override, t('drupal_add_js() correctly overrides settings within an associative array.')); $this->assertTrue($associative_array_override, t('drupal_add_js() correctly overrides settings within an associative array.'));
// Check in a rendered page. // Check in a rendered page.
$this->drupalGet('common-test/query-string'); $this->drupalGet('common-test/query-string');
$this->assertPattern('@<script>.+Drupal\.settings.+"currentPath":"common-test\\\/query-string"@s', 'currentPath is in the JS settings'); $this->assertPattern('@<script>.+drupalSettings.+"currentPath":"common-test\\\/query-string"@s', 'currentPath is in the JS settings');
$path = array('source' => 'common-test/query-string', 'alias' => 'common-test/currentpath-check'); $path = array('source' => 'common-test/query-string', 'alias' => 'common-test/currentpath-check');
path_save($path); path_save($path);
$this->drupalGet('common-test/currentpath-check'); $this->drupalGet('common-test/currentpath-check');
$this->assertPattern('@<script>.+Drupal\.settings.+"currentPath":"common-test\\\/query-string"@s', 'currentPath is in the JS settings for an aliased path'); $this->assertPattern('@<script>.+drupalSettings.+"currentPath":"common-test\\\/query-string"@s', 'currentPath is in the JS settings for an aliased path');
} }
/** /**

View File

@ -1187,16 +1187,12 @@ function system_library_info() {
); );
// Drupal settings. // Drupal settings.
$libraries['drupal.settings'] = array( $libraries['drupalSettings'] = array(
'title' => 'Drupal Settings', 'title' => 'Drupal Settings',
'version' => VERSION, 'version' => VERSION,
'js' => array( 'js' => array(
array('type' => 'setting', 'data' => array()), array('type' => 'setting', 'data' => array()),
), ),
'dependencies' => array(
array('system', 'jquery'),
array('system', 'drupal'),
),
); );
// Drupal's Ajax framework. // Drupal's Ajax framework.
@ -1210,7 +1206,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'drupal.progress'), array('system', 'drupal.progress'),
), ),
); );
@ -1225,7 +1221,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'drupal.progress'), array('system', 'drupal.progress'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
), ),
@ -1241,7 +1237,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
), ),
); );
@ -1270,7 +1266,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
), ),
); );
@ -1285,7 +1281,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
array('system', 'jquery.cookie'), array('system', 'jquery.cookie'),
), ),
@ -1384,7 +1380,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
// Vertical tabs relies on drupalGetSummary in form.js // Vertical tabs relies on drupalGetSummary in form.js
array('system', 'drupal.form'), array('system', 'drupal.form'),
), ),
@ -1891,7 +1887,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
), ),
); );
@ -1916,7 +1912,7 @@ function system_library_info() {
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
), ),
); );
@ -1929,7 +1925,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
), ),
); );
$libraries['drupal.system.cron'] = array( $libraries['drupal.system.cron'] = array(
@ -1941,7 +1937,7 @@ function system_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
), ),
); );

View File

@ -272,7 +272,7 @@ function common_test_library_info() {
* Adds a JavaScript file and a CSS file with a query string appended. * Adds a JavaScript file and a CSS file with a query string appended.
*/ */
function common_test_js_and_css_querystring() { function common_test_js_and_css_querystring() {
drupal_add_library('system', 'drupal.settings'); drupal_add_library('system', 'drupalSettings');
drupal_add_js(drupal_get_path('module', 'node') . '/node.js'); drupal_add_js(drupal_get_path('module', 'node') . '/node.js');
drupal_add_css(drupal_get_path('module', 'node') . '/node.admin.css'); drupal_add_css(drupal_get_path('module', 'node') . '/node.admin.css');
// A relative URI may have a query string. // A relative URI may have a query string.

View File

@ -1798,7 +1798,7 @@ function taxonomy_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'drupal.tabledrag'), array('system', 'drupal.tabledrag'),
), ),
); );

View File

@ -374,7 +374,7 @@ function toolbar_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
array('system', 'jquery.cookie'), array('system', 'jquery.cookie'),
), ),

View File

@ -3147,7 +3147,7 @@ function user_library_info() {
'dependencies' => array( 'dependencies' => array(
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupal.settings'), array('system', 'drupalSettings'),
), ),
); );