Commit 43a27081 authored by catch's avatar catch
Browse files

Issue #1589176 by nod_: Fixed Use data-* to store #states api informations.

parent c9d22bc4
...@@ -2724,10 +2724,7 @@ function drupal_process_attached($elements, $dependency_check = FALSE) { ...@@ -2724,10 +2724,7 @@ function drupal_process_attached($elements, $dependency_check = FALSE) {
*/ */
function drupal_process_states(&$elements) { function drupal_process_states(&$elements) {
$elements['#attached']['library'][] = array('system', 'drupal.states'); $elements['#attached']['library'][] = array('system', 'drupal.states');
$elements['#attached']['js'][] = array( $elements['#attributes']['data-drupal-states'] = JSON::encode($elements['#states']);
'type' => 'setting',
'data' => array('states' => array('#' . $elements['#id'] => $elements['#states'])),
);
} }
/** /**
...@@ -3856,6 +3853,11 @@ function drupal_render(&$elements) { ...@@ -3856,6 +3853,11 @@ function drupal_render(&$elements) {
return ''; return '';
} }
// Add any JavaScript state information associated with the element.
if (!empty($elements['#states'])) {
drupal_process_states($elements);
}
// Get the children of the element, sorted by weight. // Get the children of the element, sorted by weight.
$children = element_children($elements, TRUE); $children = element_children($elements, TRUE);
...@@ -3901,11 +3903,6 @@ function drupal_render(&$elements) { ...@@ -3901,11 +3903,6 @@ function drupal_render(&$elements) {
$elements['#children'] = $elements['#markup'] . $elements['#children']; $elements['#children'] = $elements['#markup'] . $elements['#children'];
} }
// Add any JavaScript state information associated with the element.
if (!empty($elements['#states'])) {
drupal_process_states($elements);
}
// Add additional libraries, CSS, JavaScript an other custom // Add additional libraries, CSS, JavaScript an other custom
// attached data associated with this element. // attached data associated with this element.
if (!empty($elements['#attached'])) { if (!empty($elements['#attached'])) {
......
...@@ -18,17 +18,17 @@ var states = Drupal.states = { ...@@ -18,17 +18,17 @@ var states = Drupal.states = {
*/ */
Drupal.behaviors.states = { Drupal.behaviors.states = {
attach: function (context, settings) { attach: function (context, settings) {
var $context = $(context); var $states = $(context).find('[data-drupal-states]');
for (var selector in settings.states) { var config, state;
if (settings.states.hasOwnProperty(selector)) { for (var i = 0, il = $states.length; i < il; i += 1) {
for (var state in settings.states[selector]) { config = JSON.parse($states[i].getAttribute('data-drupal-states'));
if (settings.states[selector].hasOwnProperty(state)) { for (state in config) {
new states.Dependent({ if (config.hasOwnProperty(state)) {
element: $context.find(selector), new states.Dependent({
state: states.State.sanitize(state), element: $($states[i]),
constraints: settings.states[selector][state] state: states.State.sanitize(state),
}); constraints: config[state]
} });
} }
} }
} }
......
Supports Markdown
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