diff --git a/js/components/alert.js b/js/components/alert.js index 440a6dd4b..47cca8eb0 100644 --- a/js/components/alert.js +++ b/js/components/alert.js @@ -25,7 +25,10 @@ $(function() { // default options options: - {}, + { + hasCollapsibleStateSavingEnabled: false, + collapsibleStateStorageKey: null, + }, css_classes: { opened: 'ibo-is-opened', @@ -60,6 +63,26 @@ $(function() }, _onCollapseTogglerClick: function (oEvent) { this.element.toggleClass(this.css_classes.opened); + + if (this.options.hasCollapsibleStateSavingEnabled) { + localStorage.setItem( + this.options.collapsibleStateStorageKey, + this.element.hasClass(this.css_classes.opened) + ); + } + }, + enableSaveCollapsibleState: function (bOpenedByDefault, sSectionStateStorageKey) { + this.options.hasCollapsibleStateSavingEnabled = true; + this.options.collapsibleStateStorageKey = sSectionStateStorageKey; + + let bStoredSectionState = JSON.parse(localStorage.getItem(sSectionStateStorageKey)); + let bIsSectionOpenedInitially = (bStoredSectionState == null) ? bOpenedByDefault : bStoredSectionState; + + if (bIsSectionOpenedInitially) { + this.element.addClass(this.css_classes.opened); + } else { + this.element.removeClass(this.css_classes.opened); + } } }) }); diff --git a/js/components/collapsible-section.js b/js/components/collapsible-section.js index 47bd97130..383641122 100644 --- a/js/components/collapsible-section.js +++ b/js/components/collapsible-section.js @@ -24,7 +24,10 @@ $(function () { { // default options options: - {}, + { + hasCollapsibleStateSavingEnabled: false, + collapsibleStateStorageKey: null, + }, css_classes: { opened: 'ibo-is-opened', @@ -51,6 +54,26 @@ $(function () { }, _onCollapseTogglerClick: function (oEvent) { this.element.toggleClass(this.css_classes.opened); + + if (this.options.hasCollapsibleStateSavingEnabled) { + localStorage.setItem( + this.options.collapsibleStateStorageKey, + this.element.hasClass(this.css_classes.opened) + ); + } + }, + enableSaveCollapsibleState: function (bOpenedByDefault, sSectionStateStorageKey) { + this.options.hasCollapsibleStateSavingEnabled = true; + this.options.collapsibleStateStorageKey = sSectionStateStorageKey; + + let bStoredSectionState = JSON.parse(localStorage.getItem(sSectionStateStorageKey)); + let bIsSectionOpenedInitially = (bStoredSectionState == null) ? bOpenedByDefault : bStoredSectionState; + + if (bIsSectionOpenedInitially) { + this.element.addClass(this.css_classes.opened); + } else { + this.element.removeClass(this.css_classes.opened); + } } }) }); diff --git a/sources/application/UI/Base/Component/Alert/Alert.php b/sources/application/UI/Base/Component/Alert/Alert.php index bef1cb606..b4e7378c9 100644 --- a/sources/application/UI/Base/Component/Alert/Alert.php +++ b/sources/application/UI/Base/Component/Alert/Alert.php @@ -21,6 +21,7 @@ namespace Combodo\iTop\Application\UI\Base\Component\Alert; use Combodo\iTop\Application\UI\Base\UIBlock; +use utils; /** * Class Alert @@ -96,6 +97,10 @@ class Alert extends UIBlock protected $bIsCollapsible; /** @var bool Whether the alert is opened by default or not, only works when $bIsCollapsible set to true */ protected $bIsOpenedByDefault; + /** @var boolean if true will store collapsible state */ + protected $bIsSaveCollapsibleStateEnabled = false; + /** @var string localStorage key used to store collapsible state */ + protected $sSectionStateStorageKey; /** * Alert constructor. @@ -116,6 +121,20 @@ class Alert extends UIBlock parent::__construct($sId); } + /** + * @param $sSectionStateStorageKey + * + * @return self + */ + public function EnableSaveCollapsibleState($sSectionStateStorageKey) + { + $this->bIsSaveCollapsibleStateEnabled = true; + $sSectionStateStorageKeyPrefix = utils::GetConfig()->GetItopInstanceid(); + $this->sSectionStateStorageKey = $sSectionStateStorageKeyPrefix.'/'.$sSectionStateStorageKey; + + return $this; + } + /** * @return string */ @@ -247,4 +266,14 @@ class Alert extends UIBlock return $this; } + + public function IsSaveCollapsibleStateEnabled(): bool + { + return $this->bIsSaveCollapsibleStateEnabled; + } + + public function GetSessionCollapsibleStateStorageKey(): string + { + return $this->sSectionStateStorageKey; + } } \ No newline at end of file diff --git a/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php b/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php index d68884de0..072245f49 100644 --- a/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php +++ b/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php @@ -22,6 +22,7 @@ namespace Combodo\iTop\Application\UI\Base\Component\CollapsibleSection; use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock; use Combodo\iTop\Application\UI\Base\tUIContentAreas; +use utils; /** * @package Combodo\iTop\Application\UI\Base\Component\CollapsibleSection @@ -43,6 +44,10 @@ class CollapsibleSection extends UIContentBlock protected $bIsOpenedByDefault = false; /** @var string */ private $sTitle; + /** @var boolean if true will store collapsible state */ + protected $bIsSaveCollapsibleStateEnabled = false; + /** @var string localStorage key used to store collapsible state */ + protected $sSectionStateStorageKey; public function __construct(string $sTitle, array $aSubBlocks = [], ?string $sId = null) { @@ -51,6 +56,19 @@ class CollapsibleSection extends UIContentBlock $this->aSubBlocks = $aSubBlocks; } + /** + * @param $sSectionStateStorageKey + * + * @return self + */ + public function EnableSaveCollapsibleState($sSectionStateStorageKey) + { + $this->bIsSaveCollapsibleStateEnabled = true; + $sSectionStateStorageKeyPrefix = utils::GetConfig()->GetItopInstanceid(); + $this->sSectionStateStorageKey = $sSectionStateStorageKeyPrefix.'/'.$sSectionStateStorageKey; + + return $this; + } public function IsOpenedByDefault() { @@ -73,4 +91,14 @@ class CollapsibleSection extends UIContentBlock { return $this->sTitle; } + + public function IsSaveCollapsibleStateEnabled(): bool + { + return $this->bIsSaveCollapsibleStateEnabled; + } + + public function GetSessionCollapsibleStateStorageKey(): string + { + return $this->sSectionStateStorageKey; + } } \ No newline at end of file diff --git a/templates/base/components/alert/layout.js.twig b/templates/base/components/alert/layout.js.twig index 7591ae0c6..642b68664 100644 --- a/templates/base/components/alert/layout.js.twig +++ b/templates/base/components/alert/layout.js.twig @@ -1 +1,7 @@ -$('#{{ oUIBlock.GetId() }}').alert(); \ No newline at end of file +$('#{{ oUIBlock.GetId() }}').alert(); +{% if oUIBlock.IsSaveCollapsibleStateEnabled() %} +$('#{{ oUIBlock.GetId() }}').alert("instance").enableSaveCollapsibleState( + {{ oUIBlock.IsOpenedByDefault() }}, + '{{ oUIBlock.GetSessionCollapsibleStateStorageKey() }}' +); +{% endif %} \ No newline at end of file diff --git a/templates/base/components/collapsible-section/layout.js.twig b/templates/base/components/collapsible-section/layout.js.twig index e6eb589d6..1c3d6f952 100644 --- a/templates/base/components/collapsible-section/layout.js.twig +++ b/templates/base/components/collapsible-section/layout.js.twig @@ -1 +1,7 @@ -$('#{{ oUIBlock.GetId() }}').collapsibleSection(); \ No newline at end of file +$('#{{ oUIBlock.GetId() }}').collapsibleSection(); +{% if oUIBlock.IsSaveCollapsibleStateEnabled() %} +$('#{{ oUIBlock.GetId() }}').collapsibleSection("instance").enableSaveCollapsibleState( + {{ oUIBlock.IsOpenedByDefault()|var_export }}, + '{{ oUIBlock.GetSessionCollapsibleStateStorageKey() }}' +); +{% endif %} \ No newline at end of file diff --git a/test/VisualTest/Backoffice/RenderAllUiBlocks.php b/test/VisualTest/Backoffice/RenderAllUiBlocks.php index fbcfa0a1c..7a10db7d8 100644 --- a/test/VisualTest/Backoffice/RenderAllUiBlocks.php +++ b/test/VisualTest/Backoffice/RenderAllUiBlocks.php @@ -82,6 +82,9 @@ $oAlertNonClosable = AlertFactory::MakeNeutral('Alert not closable, not collapsa ->SetIsClosable(false) ->SetIsCollapsible(false); $oPageContentLayout->AddMainBlock($oAlertNonClosable); +$oAlertSaveCollapsibleState = AlertFactory::MakeNeutral('Alert with collapsible state saving', $sContent) + ->EnableSaveCollapsibleState('RenderAllUiBlocks-alert'); +$oPageContentLayout->AddMainBlock($oAlertSaveCollapsibleState); $oPageContentLayout->AddMainBlock(new Html('
')); @@ -167,7 +170,12 @@ $oPageContentLayout->AddMainBlock(new Html('
')); $oCollapsibleSectionTitle = new Html('

Collapsible Sections examples

'); $oPage->AddUiBlock($oCollapsibleSectionTitle); -$oCollapsibleSection = new CollapsibleSection('Section title', [new Html("This the section content !")]); +$sSectionContent = 'This is the section content !'; +$oCollapsibleSection = new CollapsibleSection('Section title', [new Html($sSectionContent)]); $oPage->AddUiBlock($oCollapsibleSection); +$oCollapsibleSectionSaveState = new CollapsibleSection('Section save state', [new Html($sSectionContent)]); +$oCollapsibleSectionSaveState->EnableSaveCollapsibleState('RenderAllUiBlocks-collapsible-section'); +$oPage->AddUiBlock($oCollapsibleSectionSaveState); + $oPage->output();