diff --git a/paragraphs.js b/paragraphs.js index c66487f..2bf5b94 100644 --- a/paragraphs.js +++ b/paragraphs.js @@ -93,41 +93,43 @@ }); // Trigger for all paragaphs. - $('span.paragraphs-collapsible a', context).once("paragraphs").click(function(event) { - // Prevent the default click event. - event.preventDefault(); - var $t = $(this); - var $triggers = [$t]; + $('span.paragraphs-collapsible a', context).once("paragraphs") + .click(function(event) { + // Prevent the default click event. + event.preventDefault(); + var $t = $(this); + var $triggers = [$t]; - var $items_table = $t.closest('table.field-multiple-table'); - if ($items_table.length) { - // Items table click. - $triggers.push($items_table.siblings('table.sticky-header').find('span.paragraphs-collapsible a')); - } - else { - // Sticky header click. - $triggers.push($t.closest('table.sticky-header').siblings('table.field-multiple-table').find('span.paragraphs-collapsible a')); - } + var $items_table = $t.closest('table.field-multiple-table'); + if ($items_table.length) { + // Items table click. + $triggers.push($items_table.siblings('table.sticky-header').find('span.paragraphs-collapsible a')); + } + else { + // Sticky header click. + $triggers.push($t.closest('table.sticky-header').siblings('table.field-multiple-table').find('span.paragraphs-collapsible a')); + } - if ($t.hasClass('collapse')) { - showhideParagraphs(this, 'collapse'); - $.each($triggers, function(index, $trigger) { - if ($trigger.length) { - $trigger.removeClass('collapse').addClass('expand'); - $trigger.text(Drupal.t('Collapse All')); - } - }); - } - else { - showhideParagraphs(this, 'expand'); - $.each($triggers, function(index, $trigger) { - if ($trigger.length) { - $trigger.removeClass('expand').addClass('collapse'); - $trigger.text(Drupal.t('Expand All')); - } - }); - } - }); + if ($t.hasClass('collapsed')) { + showhideParagraphs(this, 'expand'); + $.each($triggers, function(index, $trigger) { + if ($trigger.length) { + $trigger.removeClass('collapsed').addClass('expanded'); + $trigger.text(Drupal.t('Expand All')); + } + }); + } + else { + showhideParagraphs(this, 'collapse'); + $.each($triggers, function(index, $trigger) { + if ($trigger.length) { + $trigger.removeClass('expanded').addClass('collapsed'); + $trigger.text(Drupal.t('Collapse All')); + } + }); + } + }) + .parent('.paragraphs-collapsible-init-collapsed').find('a.expanded').click(); } }; diff --git a/paragraphs.module b/paragraphs.module index f6f9d89..d45033d 100644 --- a/paragraphs.module +++ b/paragraphs.module @@ -11,6 +11,8 @@ define('PARAGRAPHS_DEFAULT_TITLE_MULTIPLE', 'Paragraphs'); define('PARAGRAPHS_DEFAULT_EDIT_MODE', 'open'); define('PARAGRAPHS_DEFAULT_EDIT_MODE_OVERRIDE', 1); define('PARAGRAPHS_DEFAULT_ADD_MODE', 'select'); +define('PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE', TRUE); +define('PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE_COLLAPSED', TRUE); /** * Modules should return this value from hook_paragraphs_item_access() to allow access to a paragraphs item. @@ -431,6 +433,8 @@ function paragraphs_field_info() { 'title_multiple' => PARAGRAPHS_DEFAULT_TITLE_MULTIPLE, 'allowed_bundles' => array(), 'bundle_weights' => array(), + 'open_collapsible' => PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE, + 'open_collapsible_collapsed' => PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE_COLLAPSED, ), 'default_widget' => 'paragraphs_hidden', 'default_formatter' => 'paragraphs_view', @@ -593,6 +597,32 @@ function paragraphs_field_instance_settings_form($field, $instance) { '#required' => TRUE, ); + $element['open_collapsible'] = array( + '#type' => 'checkbox', + '#title' => t('Enable collapsible form items'), + '#description' => t('Provides client-side collapsible paragraphs via JavaScript.'), + '#default_value' => isset($settings['open_collapsible']) ? $settings['open_collapsible'] : PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE, + '#states' => array( + // Show the settings when the default edit mode is set to "Open". + 'visible' => array( + ':input[name="instance[settings][default_edit_mode]"]' => array('value' => 'open'), + ), + ), + ); + + $element['open_collapsible_collapsed'] = array( + '#type' => 'checkbox', + '#field_prefix' => '    ', + '#title' => t('Collapse all paragraphs initially'), + '#default_value' => isset($settings['open_collapsible_collapsed']) ? $settings['open_collapsible_collapsed'] : PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE_COLLAPSED, + '#states' => array( + // Show the settings when open_collapsible is enabled. + 'visible' => array( + ':input[name="instance[settings][open_collapsible]"]' => array('checked' => TRUE), + ), + ), + ); + if (!count($bundles)) { $element['allowed_bundles_explain'] = array( '#type' => 'markup', @@ -1154,14 +1184,32 @@ function theme_paragraphs_field_multiple_value_form($variables) { if (!isset($instance['settings']['title_multiple'])) { $instance['settings']['title_multiple'] = PARAGRAPHS_DEFAULT_TITLE_MULTIPLE; } + if (!isset($instance['settings']['open_collapsible'])) { + $instance['settings']['open_collapsible'] = PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE; + } + if (!isset($instance['settings']['open_collapsible_collapsed'])) { + $instance['settings']['open_collapsible_collapsed'] = PARAGRAPHS_DEFAULT_OPEN_COLLAPSIBLE_COLLAPSED; + } $add_mode = (isset($instance['settings']['add_mode']) ? $instance['settings']['add_mode'] : PARAGRAPHS_DEFAULT_ADD_MODE); $default_edit_mode = isset($instance['settings']['default_edit_mode']) ? $instance['settings']['default_edit_mode'] : PARAGRAPHS_DEFAULT_EDIT_MODE; $required = !empty($element['#required']) ? theme('form_required_marker', $variables) : ''; + + // Open collapsible processing. $expand_collapse = ''; - if ($default_edit_mode == 'open') { - $expand_collapse = '' . l('Collapse All', '#', array('external' => TRUE)) . ''; + if ($default_edit_mode == 'open' && !empty($instance['settings']['open_collapsible'])) { + $expand_collapse_classes = 'paragraphs-collapsible'; + if (!empty($instance['settings']['open_collapsible_collapsed'])) { + $expand_collapse_classes .= ' paragraphs-collapsible-init-collapsed'; + } + + $expand_collapse = ''; + $expand_collapse .= l(t('Collapse All'), '#', array( + 'external' => TRUE, + 'attributes' => array('class' => array('expanded')), + )); + $expand_collapse .= ''; } // Sort items according to '_weight' (needed when the form comes back after