From 5fc72f591eb2dc034b1b4794ab0e10c0e43f5f8e Mon Sep 17 00:00:00 2001 From: Tanya Fomina Date: Fri, 12 Aug 2022 12:24:28 +0300 Subject: [PATCH] Move enabling confirmation state to separate method --- src/components/utils/popover.ts | 63 ++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/src/components/utils/popover.ts b/src/components/utils/popover.ts index 2df6994f..c82e565c 100644 --- a/src/components/utils/popover.ts +++ b/src/components/utils/popover.ts @@ -503,32 +503,9 @@ export default class Popover extends EventsDispatcher { const clickedItem = this.items[itemIndex]; this.cleanUpConfirmationState(); + if (clickedItem.confirmation) { - /** Save root item requiring confirmation to restore original state on popover hide */ - if (this.itemRequiringConfirmation === null) { - this.itemRequiringConfirmation = clickedItem; - } - const newItemData = { - ...clickedItem, - ...clickedItem.confirmation, - } as PopoverItem; - - /** Prevent confirmation loop */ - if (newItemData.confirmation === clickedItem.confirmation) { - delete newItemData.confirmation; - } - - this.items[itemIndex] = newItemData; - - const confirmationStateItemEl = this.createItem(newItemData as PopoverItem); - - confirmationStateItemEl.classList.add(Popover.CSS.itemConfirmation); - itemEl.parentElement.replaceChild(confirmationStateItemEl, itemEl); - - this.reactivateFlipper( - this.flippableElements, - this.flippableElements.indexOf(confirmationStateItemEl) - ); + this.enableConfirmationStateForItem(clickedItem as PopoverItemWithConfirmation, itemEl, itemIndex); return; } @@ -540,6 +517,42 @@ export default class Popover extends EventsDispatcher { } } + /** + * Enables confirmation state for specified item. + * Replaces item element in popover so that is becomes highlighted in a special way + * + * @param item - item to enable confirmation state for + * @param itemEl - html element corresponding to the item + * @param itemIndex - index of the item in all items list + */ + private enableConfirmationStateForItem(item: PopoverItemWithConfirmation, itemEl: HTMLElement, itemIndex: number): void { + /** Save root item requiring confirmation to restore original state on popover hide */ + if (this.itemRequiringConfirmation === null) { + this.itemRequiringConfirmation = item; + } + const newItemData = { + ...item, + ...item.confirmation, + } as PopoverItem; + + /** Prevent confirmation loop */ + if (newItemData.confirmation === item.confirmation) { + delete newItemData.confirmation; + } + + this.items[itemIndex] = newItemData; + + const confirmationStateItemEl = this.createItem(newItemData as PopoverItem); + + confirmationStateItemEl.classList.add(Popover.CSS.itemConfirmation); + itemEl.parentElement.replaceChild(confirmationStateItemEl, itemEl); + + this.reactivateFlipper( + this.flippableElements, + this.flippableElements.indexOf(confirmationStateItemEl) + ); + } + /** * Brings popover item in confirmation state to its original state */