Commit 0762f600 authored by webchick's avatar webchick

#315798 by Rob Loach, mfer, Grugnog2, and sun: Add weighting to drupal_add_js().

parent 9020a9a2
......@@ -84,7 +84,7 @@ function _batch_progress_page_js() {
// error messages. Only safe strings should be passed in to batch_set().
$current_set = _batch_current_set();
drupal_set_title($current_set['title'], PASS_THROUGH);
drupal_add_js('misc/progress.js', array('type' => 'core', 'cache' => FALSE));
drupal_add_js('misc/progress.js', array('cache' => FALSE));
$url = url($batch['url'], array('query' => array('id' => $batch['id'])));
$js_setting = array(
......@@ -95,7 +95,7 @@ function _batch_progress_page_js() {
),
);
drupal_add_js($js_setting, 'setting');
drupal_add_js('misc/batch.js', array('type' => 'core', 'cache' => FALSE));
drupal_add_js('misc/batch.js', array('cache' => FALSE));
$output = '<div id="progress"></div>';
return $output;
......
This diff is collapsed.
......@@ -1893,7 +1893,7 @@ function form_process_ahah($element) {
// Adding the same javascript settings twice will cause a recursion error,
// we avoid the problem by checking if the javascript has already been added.
if (isset($element['#ahah']['path']) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) {
drupal_add_js('misc/jquery.form.js');
drupal_add_js('misc/jquery.form.js', array('weight' => JS_LIBRARY));
drupal_add_js('misc/ahah.js');
$ahah_binding = array(
......@@ -1919,7 +1919,7 @@ function form_process_ahah($element) {
// Add progress.js if we're doing a bar display.
if ($ahah_binding['progress']['type'] == 'bar') {
drupal_add_js('misc/progress.js');
drupal_add_js('misc/progress.js', array('cache' => FALSE));
}
drupal_add_js(array('ahah' => array($element['#id'] => $ahah_binding)), 'setting');
......
......@@ -158,7 +158,7 @@ function _init_theme($theme, $base_theme = array(), $registry_callback = '_theme
// Add scripts used by this theme.
foreach ($final_scripts as $script) {
drupal_add_js($script, 'theme');
drupal_add_js($script, array('weight' => JS_THEME));
}
$theme_engine = NULL;
......
......@@ -726,7 +726,7 @@ function install_tasks($profile, $task) {
// Add JavaScript validation.
_user_password_dynamic_validation();
drupal_add_js(drupal_get_path('module', 'system') . '/system.js', 'module');
drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
// We add these strings as settings because JavaScript translation does not
// work on install time.
drupal_add_js(array('copyFieldValue' => array('edit-site-mail' => array('edit-account-mail')), 'cleanURL' => array('success' => st('Your server has been successfully tested to support this feature.'), 'failure' => st('Your system configuration does not currently support this feature. The <a href="http://drupal.org/node/15365">handbook page on Clean URLs</a> has additional troubleshooting information.'), 'testing' => st('Testing clean URLs...'))), 'setting');
......
......@@ -154,7 +154,7 @@ function color_scheme_form(&$form_state, $theme) {
// Add Farbtastic color picker.
drupal_add_css('misc/farbtastic/farbtastic.css', array('preprocess' => FALSE));
drupal_add_js('misc/farbtastic/farbtastic.js');
drupal_add_js('misc/farbtastic/farbtastic.js', array('weight' => JS_LIBRARY));
// Add custom CSS and JS.
drupal_add_css($base . '/color.css', array('preprocess' => FALSE));
......
......@@ -522,20 +522,17 @@ function locale_update_js_files() {
$javascript = drupal_add_js();
$files = $new_files = FALSE;
foreach ($javascript as $scope) {
foreach ($scope as $type => $data) {
if ($type != 'setting' && $type != 'inline') {
foreach ($data as $filepath => $info) {
$files = TRUE;
if (!in_array($filepath, $parsed)) {
// Don't parse our own translations files.
if (substr($filepath, 0, strlen($dir)) != $dir) {
locale_inc_callback('_locale_parse_js_file', $filepath);
watchdog('locale', 'Parsed JavaScript file %file.', array('%file' => $filepath));
$parsed[] = $filepath;
$new_files = TRUE;
}
}
foreach ($javascript as $item) {
if ($item['type'] == 'file') {
$files = TRUE;
$filepath = $item['data'];
if (!in_array($filepath, $parsed)) {
// Don't parse our own translations files.
if (substr($filepath, 0, strlen($dir)) != $dir) {
locale_inc_callback('_locale_parse_js_file', $filepath);
watchdog('locale', 'Parsed JavaScript file %file.', array('%file' => $filepath));
$parsed[] = $filepath;
$new_files = TRUE;
}
}
}
......@@ -566,7 +563,7 @@ function locale_update_js_files() {
// Add the translation JavaScript file to the page.
if ($files && !empty($language->javascript)) {
drupal_add_js($dir . '/' . $language->language . '_' . $language->javascript . '.js', 'core');
drupal_add_js($dir . '/' . $language->language . '_' . $language->javascript . '.js');
}
}
......
......@@ -250,7 +250,7 @@ function node_filter_form() {
$form['filters']['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset'));
}
drupal_add_js('misc/form.js', 'core');
drupal_add_js('misc/form.js');
return $form;
}
......
......@@ -210,7 +210,10 @@ function simpletest_test_form() {
function theme_simpletest_test_table($table) {
drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', 'module');
// Since SimpleTest is a special use case for the table select, stick the
// SimpleTest JavaScript above the table select.
drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', array('weight' => JS_DEFAULT - 1));
// Create header for test selection table.
$header = array(
......
......@@ -306,6 +306,11 @@ class DrupalSetContentTestCase extends DrupalWebTestCase {
* Tests for the JavaScript system.
*/
class JavaScriptTestCase extends DrupalWebTestCase {
/**
* Store configured value for JavaScript preprocessing.
*/
var $preprocess_js = NULL;
/**
* Implementation of getInfo().
*/
......@@ -316,45 +321,58 @@ class JavaScriptTestCase extends DrupalWebTestCase {
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp();
// Enable locale in test environment.
parent::setUp('locale');
// Disable preprocessing
$this->preprocess_js = variable_get('preprocess_js', 0);
variable_set('preprocess_js', 0);
// Reset drupal_add_js() before each test.
drupal_add_js(NULL, NULL, TRUE);
}
/**
* Implementation of tearDown().
*/
function tearDown() {
// Restore configured value for JavaScript preprocessing.
variable_set('preprocess_js', $this->preprocess_js);
parent::tearDown();
}
/**
* Test default JavaScript is empty.
*/
function testDefault() {
$this->assertEqual(array(), drupal_add_js(), t('Default JavaScript is empty.'));
}
/**
* Test adding a JavaScript file.
*/
function testAddFile() {
drupal_add_js('misc/collapse.js');
$javascript = drupal_add_js();
$this->assertTrue(array_key_exists('misc/jquery.js', $javascript['header']['core']), t('jQuery is added when a file is added.'));
$this->assertTrue(array_key_exists('misc/drupal.js', $javascript['header']['core']), t('Drupal.js is added when file is added.'));
$this->assertTrue(array_key_exists('misc/collapse.js', $javascript['header']['module']), t('JavaScript files are correctly added.'));
$this->assertEqual(base_path(), $javascript['header']['setting'][0]['basePath'], t('Base path JavaScript setting is correctly set.'));
$javascript = drupal_add_js('misc/collapse.js');
$this->assertTrue(array_key_exists('misc/jquery.js', $javascript), t('jQuery is added when a file is added.'));
$this->assertTrue(array_key_exists('misc/drupal.js', $javascript), t('Drupal.js is added when file is added.'));
$this->assertTrue(array_key_exists('misc/collapse.js', $javascript), t('JavaScript files are correctly added.'));
$this->assertEqual(base_path(), $javascript['settings']['data'][0]['basePath'], t('Base path JavaScript setting is correctly set.'));
}
/**
* Test adding settings.
*/
function testAddSetting() {
drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting');
$javascript = drupal_add_js();
$this->assertEqual(280342800, $javascript['header']['setting'][1]['dries'], t('JavaScript setting is set correctly.'));
$this->assertEqual('rocks', $javascript['header']['setting'][1]['drupal'], t('The other JavaScript setting is set correctly.'));
$javascript = drupal_add_js(array('drupal' => 'rocks', 'dries' => 280342800), 'setting');
$this->assertEqual(280342800, $javascript['settings']['data'][1]['dries'], t('JavaScript setting is set correctly.'));
$this->assertEqual('rocks', $javascript['settings']['data'][1]['drupal'], t('The other JavaScript setting is set correctly.'));
}
/**
* Test drupal_get_js() for JavaScript settings.
*/
......@@ -365,27 +383,27 @@ class JavaScriptTestCase extends DrupalWebTestCase {
$this->assertTrue(strpos($javascript, 'testSetting') > 0, t('Rendered JavaScript header returns custom setting.'));
$this->assertTrue(strpos($javascript, 'misc/jquery.js') > 0, t('Rendered JavaScript header includes jQuery.'));
}
/**
* Test to see if resetting the JavaScript empties the cache.
*/
function testReset() {
drupal_add_js('misc/collapse.js');
drupal_add_js(NULL, NULL, TRUE);
drupal_add_js(NULL, NULL, TRUE);
$this->assertEqual(array(), drupal_add_js(), t('Resetting the JavaScript correctly empties the cache.'));
}
/**
* Test adding inline scripts.
*/
function testAddInline() {
$inline = '$(document).ready(function(){});';
drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
$javascript = drupal_add_js();
$this->assertTrue(array_key_exists('misc/jquery.js', $javascript['header']['core']), t('jQuery is added when inline scripts are added.'));
$this->assertEqual($inline, $javascript['footer']['inline'][0]['code'], t('Inline JavaScript is correctly added to the footer.'));
$javascript = drupal_add_js($inline, array('type' => 'inline', 'scope' => 'footer'));
$this->assertTrue(array_key_exists('misc/jquery.js', $javascript), t('jQuery is added when inline scripts are added.'));
$data = end($javascript);
$this->assertEqual($inline, $data['data'], t('Inline JavaScript is correctly added to the footer.'));
}
/**
* Test drupal_get_js() with a footer scope.
*/
......@@ -395,14 +413,30 @@ class JavaScriptTestCase extends DrupalWebTestCase {
$javascript = drupal_get_js('footer');
$this->assertTrue(strpos($javascript, $inline) > 0, t('Rendered JavaScript footer returns the inline code.'));
}
/**
* Test drupal_add_js() sets preproccess to false when cache is set to false.
*/
function testNoCache() {
drupal_add_js('misc/collapse.js', array('cache' => FALSE));
$javascript = drupal_add_js();
$this->assertTrue(!$javascript['header']['module']['misc/collapse.js']['preprocess'], t('Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.'));
$javascript = drupal_add_js('misc/collapse.js', array('cache' => FALSE));
$this->assertFalse($javascript['misc/collapse.js']['preprocess'], t('Setting cache to FALSE sets proprocess to FALSE when adding JavaScript.'));
}
/**
* Test adding a JavaScript file with a different weight.
*/
function testDifferentWeight() {
$javascript = drupal_add_js('misc/collapse.js', array('weight' => JS_THEME));
$this->assertEqual($javascript['misc/collapse.js']['weight'], JS_THEME, t('Adding a JavaScript file with a different weight caches the given weight.'));
}
/**
* Test rendering the JavaScript with a file's weight above jQuery's.
*/
function testRenderDifferentWeight() {
drupal_add_js('misc/collapse.js', array('weight' => JS_LIBRARY - 10));
$javascript = drupal_get_js();
$this->assertTrue(strpos($javascript, 'misc/collapse.js') < strpos($javascript, 'misc/jquery.js'), t('Rendering a JavaScript file above jQuery.'));
}
}
......
......@@ -1528,7 +1528,7 @@ function system_rss_feeds_settings() {
* @see system_date_time_settings_submit()
*/
function system_date_time_settings() {
drupal_add_js(drupal_get_path('module', 'system') . '/system.js', 'module');
drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
drupal_add_js(array('dateTime' => array('lookup' => url('admin/settings/date-time/lookup'))), 'setting');
// Date settings:
......@@ -1736,7 +1736,7 @@ function system_clean_url_settings() {
if (!variable_get('clean_url', 0)) {
if (strpos(request_uri(), '?q=') !== FALSE) {
drupal_add_js(drupal_get_path('module', 'system') . '/system.js', 'module');
drupal_add_js(drupal_get_path('module', 'system') . '/system.js');
$form['clean_url']['#description'] .= ' <span>' . t('Before enabling clean URLs, you must perform a test to determine if your server is properly configured. If you are able to see this page again after clicking the "Run the clean URL test" link, the test has succeeded and the radio buttons above will be available. If instead you are directed to a "Page not found" error, you will need to change the configuration of your server. The <a href="@handbook">handbook page on Clean URLs</a> has additional troubleshooting information.', array('@handbook' => 'http://drupal.org/node/15365')) . '</span>';
......
......@@ -83,7 +83,7 @@ function user_filter_form() {
);
}
drupal_add_js('misc/form.js', 'core');
drupal_add_js('misc/form.js');
return $form;
}
......
......@@ -2160,7 +2160,7 @@ function _user_password_dynamic_validation() {
global $user;
// Only need to do once per page.
if (!$complete) {
drupal_add_js(drupal_get_path('module', 'user') . '/user.js', 'module');
drupal_add_js(drupal_get_path('module', 'user') . '/user.js');
drupal_add_js(array(
'password' => array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment