diff --git a/js/components/alert.js b/js/components/alert.js index 47cca8eb0..c13810c8d 100644 --- a/js/components/alert.js +++ b/js/components/alert.js @@ -26,8 +26,8 @@ $(function() // default options options: { - hasCollapsibleStateSavingEnabled: false, - collapsibleStateStorageKey: null, + bOpenedByDefault: true, + collapsibleStateStorageKey: null, // if non null will save collapsible state }, css_classes: { @@ -42,6 +42,16 @@ $(function() // the constructor _create: function () { this._bindEvents(); + + let bStoredSectionState = JSON.parse(localStorage.getItem(this.options.collapsibleStateStorageKey)); + let bIsSectionOpenedInitially = (bStoredSectionState == null) + ? this.options.bOpenedByDefault + : bStoredSectionState; + if (bIsSectionOpenedInitially) { + this.element.addClass(this.css_classes.opened); + } else { + this.element.removeClass(this.css_classes.opened); + } }, // events bound via _bind are removed automatically // revert other modifications here @@ -64,25 +74,12 @@ $(function() _onCollapseTogglerClick: function (oEvent) { this.element.toggleClass(this.css_classes.opened); - if (this.options.hasCollapsibleStateSavingEnabled) { + if (this.options.collapsibleStateStorageKey) { 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 383641122..5f50fbb80 100644 --- a/js/components/collapsible-section.js +++ b/js/components/collapsible-section.js @@ -25,8 +25,8 @@ $(function () { // default options options: { - hasCollapsibleStateSavingEnabled: false, - collapsibleStateStorageKey: null, + bOpenedByDefault: true, + collapsibleStateStorageKey: null, // if non null will save collapsible state }, css_classes: { @@ -40,6 +40,15 @@ $(function () { // the constructor _create: function () { this._bindEvents(); + + let bStoredSectionState = JSON.parse(localStorage.getItem(this.options.collapsibleStateStorageKey)); + let bIsSectionOpenedInitially = (bStoredSectionState == null) ? this.options.bOpenedByDefault : bStoredSectionState; + + if (bIsSectionOpenedInitially) { + this.element.addClass(this.css_classes.opened); + } else { + this.element.removeClass(this.css_classes.opened); + } }, // events bound via _bind are removed automatically // revert other modifications here @@ -55,25 +64,12 @@ $(function () { _onCollapseTogglerClick: function (oEvent) { this.element.toggleClass(this.css_classes.opened); - if (this.options.hasCollapsibleStateSavingEnabled) { + if (this.options.collapsibleStateStorageKey) { 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/templates/base/components/alert/layout.js.twig b/templates/base/components/alert/layout.js.twig index 642b68664..d37e9c573 100644 --- a/templates/base/components/alert/layout.js.twig +++ b/templates/base/components/alert/layout.js.twig @@ -1,7 +1,4 @@ -$('#{{ oUIBlock.GetId() }}').alert(); -{% if oUIBlock.IsSaveCollapsibleStateEnabled() %} -$('#{{ oUIBlock.GetId() }}').alert("instance").enableSaveCollapsibleState( - {{ oUIBlock.IsOpenedByDefault() }}, - '{{ oUIBlock.GetSessionCollapsibleStateStorageKey() }}' -); -{% endif %} \ No newline at end of file +$('#{{ oUIBlock.GetId() }}').alert({ + bOpenedByDefault: {{ oUIBlock.IsOpenedByDefault()|var_export }} + {% if oUIBlock.IsSaveCollapsibleStateEnabled() %}, collapsibleStateStorageKey: '{{ 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 1c3d6f952..ee297e0db 100644 --- a/templates/base/components/collapsible-section/layout.js.twig +++ b/templates/base/components/collapsible-section/layout.js.twig @@ -1,7 +1,4 @@ -$('#{{ oUIBlock.GetId() }}').collapsibleSection(); -{% if oUIBlock.IsSaveCollapsibleStateEnabled() %} -$('#{{ oUIBlock.GetId() }}').collapsibleSection("instance").enableSaveCollapsibleState( - {{ oUIBlock.IsOpenedByDefault()|var_export }}, - '{{ oUIBlock.GetSessionCollapsibleStateStorageKey() }}' -); -{% endif %} \ No newline at end of file +$('#{{ oUIBlock.GetId() }}').collapsibleSection({ + bOpenedByDefault: {{ oUIBlock.IsOpenedByDefault()|var_export }} + {% if oUIBlock.IsSaveCollapsibleStateEnabled() %}, collapsibleStateStorageKey: '{{ oUIBlock.GetSessionCollapsibleStateStorageKey() }}'{% endif %} +}); \ No newline at end of file