diff --git a/paragraphs.collapsible.js b/paragraphs.collapsible.js index f846e4a..155ab9e 100644 --- a/paragraphs.collapsible.js +++ b/paragraphs.collapsible.js @@ -188,9 +188,11 @@ $.each(self.actions, function(action_key, action_data) { button_selectors.push('.' + action_data.ajax.button.classname); }); - if (button_selectors.length > 0) { - is_ajax = $item.children('.form-actions').find(button_selectors.join()).length > 0; + var $content = self.controller.findItemContentElement($item); + if ($content && $content.length > 0) { + is_ajax = $content.children('.form-actions').find(button_selectors.join()).length > 0; + } } item_element.paragraphsItemIsAjax = is_ajax; @@ -233,8 +235,14 @@ return crawl($item); } + // Determine content wrapper. + var $content = self.controller.findItemContentElement($item); + if (!$content || $content.length === 0) { + return crawl($item); + } + // Get form actions wrapper element. - var $form_actions = $item.children('.form-actions'); + var $form_actions = $content.children('.form-actions'); if (!$form_actions.length) { return crawl($item); } @@ -275,23 +283,31 @@ $(document).on('ajaxComplete.paragraphsCollapsible__' + button_element_name, function(ajax_event, ajax_xhr, ajax_settings) { if (('extraData' in ajax_settings) && ('_triggering_element_name' in ajax_settings.extraData) && ajax_settings.extraData._triggering_element_name === button_element_name) { + var $new_item; var new_button_element_name = button_element_name.replace(/\_[^_]+\_button$/, '_' + other_action.ajax.button.name + '_button'); - var $new_item = $('.paragraph-bundle-content-collapsible > .form-actions [name="' + new_button_element_name + '"]') - .closest('.paragraph-bundle-content-collapsible'); - // On complete actions. - if ($new_item.length > 0) { - // Expand nested elements. - if (requested_action.isOpen) { - // Expand collapsible fieldsets. - if (settings.expandFieldsets) { - var $fieldsets = self.controller.findItemCollapsedFieldsets($new_item); - $fieldsets.each(function() { - self.controller.expandCollapsibleFieldset(this); - }); - } + var $new_button = $('.paragraph-bundle-content-collapsible > .form-actions [name="' + new_button_element_name + '"]'); + if ($new_button.length === 0) { + $new_button = $('.paragraph-bundle-content-collapsible > .ajax-new-content > .form-actions [name="' + new_button_element_name + '"]'); + } - var new_item_element = $new_item.get(0); - new_item_element.paragraphsAjaxing = false; + if ($new_button.length > 0) { + $new_item = $new_button.closest('.paragraph-bundle-content-collapsible'); + + // On complete actions. + if ($new_item.length > 0) { + // Expand nested elements. + if (requested_action.isOpen) { + // Expand collapsible fieldsets. + if (settings.expandFieldsets) { + var $fieldsets = self.controller.findItemCollapsedFieldsets($new_item); + $fieldsets.each(function() { + self.controller.expandCollapsibleFieldset(this); + }); + } + + var new_item_element = $new_item.get(0); + new_item_element.paragraphsAjaxing = false; + } } } @@ -299,7 +315,9 @@ $(document).off('ajaxComplete.paragraphsCollapsible__' + button_element_name); // Crawl to the next item. - crawl($new_item); + if ($new_item) { + crawl($new_item); + } } }); @@ -361,12 +379,13 @@ // Initialize item processing. var item_element = this; var $item = $(item_element); + var $content = self.controller.findItemContentElement($item); var requested_action = initial_requested_action; // Block collapse for special cases. if (!requested_action.isOpen) { // Do not collapse deleted paragraphs. - if ($item.children('.form-actions').find('.paragraphs-deleteconfirm-button').length > 0) { + if ($content.children('.form-actions').find('.paragraphs-deleteconfirm-button').length > 0) { if ($item.hasClass(self.actions.collapse.classname)) { // Expand this special item. requested_action = self.actions.expand; @@ -409,9 +428,11 @@ .addClass(requested_action.classname); // Update hidden toggle text. - $item - .find('> .paragraph-bundle-title > .paragraph-bundle-title-collapsible-prefix') - .html(requested_action.onText); + if (!$content || $content.length === 0) { + $content + .find('> .paragraph-bundle-title > .paragraph-bundle-title-collapsible-prefix') + .html(requested_action.onText); + } // Expand nested elements. if (requested_action.isOpen) { @@ -486,6 +507,21 @@ }; /** + * Find the content wrapper of the item. + * + * @param object item + * The element of the field item row. + * + * @return object + * The jQuery for the content wrapper. + */ + controller.findItemContentElement = function(item) { + var $item = $(item); + var $new_content = $item.children('.ajax-new-content'); + return $new_content.length > 0 ? $new_content : $item; + }; + + /** * Find all collapsible elements within a field item. * * @param object item @@ -495,14 +531,10 @@ * The jQuery set of found elements. */ controller.findItemCollapsibleElements = function(item) { - var $item = $(item); var $elements; - var $ajax_content = $item.children('.ajax-new-content'); - if ($ajax_content.length > 0) { - $elements = $ajax_content.children(':not(.paragraph-bundle-content-collapsible-shown)'); - } - else { - $elements = $item.children(':not(.paragraph-bundle-content-collapsible-shown)'); + var $content = this.findItemContentElement(item); + if ($content && $content.length > 0) { + $elements = $content.children(':not(.paragraph-bundle-content-collapsible-shown)'); } return $elements;