Commit ab459f87 authored by catch's avatar catch
Browse files

Issue #492720 by pivica, seutje, nod_, JamesAn, sun: Refactor system.js file...

Issue #492720 by pivica, seutje, nod_, JamesAn, sun: Refactor system.js file and use Drupal form states and form ajax.
parent 11cfa543
......@@ -1642,8 +1642,6 @@ function install_configure_form($form, &$form_state, &$install_state) {
// We add these strings as settings because JavaScript translation does not
// work during installation.
drupal_add_js(array('copyFieldValue' => array('edit-site-mail' => array('edit-account-mail'))), 'setting');
// Add JS to show / hide the 'Email administrator about site updates' elements
drupal_add_js('jQuery(function () { Drupal.hideEmailAdministratorCheckbox() });', 'inline');
// Cache a fully-built schema. This is necessary for any invocation of
// index.php because: (1) setting cache table entries requires schema
......@@ -1972,6 +1970,13 @@ function _install_configure_form($form, &$form_state, &$install_state) {
'#description' => st('The system will notify you when updates and important security releases are available for installed components. Anonymous information about your site is sent to <a href="@drupal">Drupal.org</a>.', array('@drupal' => 'http://drupal.org')),
'#weight' => 15,
);
$form['update_notifications']['update_status_module'][2] = array(
'#states' => array(
'visible' => array(
'input[name="update_status_module[1]"]' => array('checked' => TRUE),
),
),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
......
......@@ -1649,8 +1649,11 @@ function system_performance_settings($form, &$form_state) {
'#type' => 'checkbox',
'#title' => t('Compress cached pages.'),
'#default_value' => $config->get('response.gzip'),
'#prefix' => '<div id="page-compression-wrapper"' . $js_hide . '>',
'#suffix' => '</div>',
'#states' => array(
'visible' => array(
'input[name="cache"]' => array('checked' => TRUE),
),
),
);
$form['bandwidth_optimization']['preprocess_css'] = array(
'#type' => 'checkbox',
......@@ -2135,14 +2138,6 @@ function system_add_date_format_type_form_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/config/regional/date-time';
}
/**
* Return the date for a given format string via Ajax.
*/
function system_date_time_lookup() {
$result = format_date(REQUEST_TIME, 'custom', $_GET['format']);
return new JsonResponse($result);
}
/**
* Form builder; Configure the site's maintenance status.
*
......@@ -2770,18 +2765,6 @@ function system_date_time_formats() {
* Allow users to add additional date formats.
*/
function system_configure_date_formats_form($form, &$form_state, $dfid = 0) {
$js_settings = array(
'type' => 'setting',
'data' => array(
'dateTime' => array(
'date-format' => array(
'text' => t('Displayed as'),
'lookup' => url('admin/config/regional/date-time/formats/lookup'),
),
),
),
);
if ($dfid) {
$form['dfid'] = array(
'#type' => 'value',
......@@ -2799,9 +2782,10 @@ function system_configure_date_formats_form($form, &$form_state, $dfid = 0) {
'#description' => t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://php.net/manual/function.date.php')),
'#default_value' => ($dfid ? $format->format : ''),
'#field_suffix' => ' <small id="edit-date-format-suffix">' . $now . '</small>',
'#attached' => array(
'library' => array(array('system', 'drupal.system')),
'js' => array($js_settings),
'#ajax' => array(
'callback' => 'system_date_time_lookup',
'event' => 'keyup',
'progress' => array('type' => 'throbber', 'message' => NULL),
),
'#required' => TRUE,
);
......@@ -2949,6 +2933,21 @@ function system_date_format_localize_form($form, &$form_state, $langcode) {
return $form;
}
/**
* Ajax callback; Returns the date for a given format string.
*/
function system_date_time_lookup($form, &$form_state) {
$format = '';
if (!empty($form_state['values']['date_format'])) {
$format = t('Displayed as %date_format', array('%date_format' => format_date(REQUEST_TIME, 'custom', $form_state['values']['date_format'])));
}
// Return a command instead of a string, since the Ajax framework
// automatically prepends an additional empty DIV element for a string, which
// breaks the layout.
$commands[] = ajax_command_replace('#edit-date-format-suffix', '<small id="edit-date-format-suffix">' . $format . '</small>');
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Form submission handler for system_date_format_localize_form().
*/
......
(function ($) {
(function ($, Drupal, drupalSettings) {
"use strict";
/**
* Show/hide the 'Email site administrator when updates are available' checkbox
* on the install page.
*/
Drupal.hideEmailAdministratorCheckbox = function () {
// Make sure the secondary box is shown / hidden as necessary on page load.
if ($('#edit-update-status-module-1').is(':checked')) {
$('.form-item-update-status-module-2').show();
}
else {
$('.form-item-update-status-module-2').hide();
}
// Toggle the display as necessary when the checkbox is clicked.
$('#edit-update-status-module-1').change( function () {
$('.form-item-update-status-module-2').toggle();
});
};
// Cache IDs in an array for ease of use.
var ids = [];
/**
* When a field is filled out, apply its value to other fields that will likely
......@@ -27,68 +11,53 @@ Drupal.hideEmailAdministratorCheckbox = function () {
* administrator e-mail address with the same value as the site e-mail address.
*/
Drupal.behaviors.copyFieldValue = {
attach: function (context, settings) {
for (var sourceId in settings.copyFieldValue) {
$('#' + sourceId, context).once('copy-field-values').bind('blur', function () {
// Get the list of target fields.
var targetIds = settings.copyFieldValue[sourceId];
// Add the behavior to update target fields on blur of the primary field.
for (var delta in targetIds) {
var targetField = $('#' + targetIds[delta]);
if (targetField.val() === '') {
targetField.val(this.value);
}
}
});
}
}
};
/**
* Show/hide custom format sections on the regional settings page.
*/
Drupal.behaviors.dateTime = {
attach: function (context, settings) {
for (var fieldName in settings.dateTime) {
if (settings.dateTime.hasOwnProperty(fieldName)) {
(function (fieldSettings, fieldName) {
var source = '#edit-' + fieldName;
var suffix = source + '-suffix';
// Attach keyup handler to custom format inputs.
$(context).find('input' + source).once('date-time').keyup(function () {
var input = $(this);
var url = fieldSettings.lookup + (/\?q=/.test(fieldSettings.lookup) ? '&format=' : '?format=') + encodeURIComponent(input.val());
$.getJSON(url, function (data) {
$(suffix).empty().append(' ' + fieldSettings.text + ': <em>' + data + '</em>');
});
});
})(settings.dateTime[fieldName], fieldName);
attach: function (context) {
// List of fields IDs on which to bind the event listener.
// Create an array of IDs to use with jQuery.
for (var sourceId in drupalSettings.copyFieldValue) {
if (drupalSettings.copyFieldValue.hasOwnProperty(sourceId)) {
ids.push(sourceId);
}
}
if (ids.length) {
// Listen to value:copy events on all dependant fields.
// We have to use body and not document because of the way jQuery events
// bubble up the DOM tree.
$('body').once('copy-field-values').on('value:copy', this.valueTargetCopyHandler);
// Listen on all source elements.
$('#' + ids.join(', #')).once('copy-field-values').on('blur', this.valueSourceBlurHandler);
}
},
detach: function (context, settings, trigger) {
if (trigger === 'unload' && ids.length) {
$('body').removeOnce('copy-field-values').off('value:copy');
$('#' + ids.join(', #')).removeOnce('copy-field-values').off('blur');
}
},
/**
* Event handler that fill the target element with the specified value.
*
* @param e
* Event object.
* @param value
* Custom value from jQuery trigger.
*/
valueTargetCopyHandler: function (e, value) {
var $target = $(e.target);
if ($target.val() === '') {
$target.val(value);
}
},
/**
* Handler for a Blur event on a source field.
*
* This event handler will trigger a 'value:copy' event on all dependant fields.
*/
valueSourceBlurHandler: function (e) {
var value = $(e.target).val();
var targetIds = drupalSettings.copyFieldValue[e.target.id];
$('#' + targetIds.join(', #')).trigger('value:copy', value);
}
};
/**
* Show/hide settings for page caching depending on whether page caching is
* enabled or not.
*/
Drupal.behaviors.pageCache = {
attach: function (context, settings) {
var $context = $(context);
$context.find('#edit-cache-0').change(function () {
$('#page-compression-wrapper').hide();
$('#cache-error').hide();
});
$context.find('#edit-cache-1').change(function () {
$('#page-compression-wrapper').show();
$('#cache-error').hide();
});
$context.find('#edit-cache-2').change(function () {
$('#page-compression-wrapper').show();
$('#cache-error').show();
});
}
};
})(jQuery);
})(jQuery, Drupal, drupalSettings);
......@@ -1965,6 +1965,7 @@ function system_library_info() {
array('system', 'jquery'),
array('system', 'drupal'),
array('system', 'drupalSettings'),
array('system', 'jquery.once'),
),
);
$libraries['drupal.system.cron'] = 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