N°3617 collapsible components state saving : use JQuery widget options instead of a method

This is the way JQuery widget are intended to work !
Thanks @Molkobain for the review !
This commit is contained in:
Pierre Goiffon
2021-01-12 17:17:39 +01:00
parent 028a3ccec8
commit 09da54ee56
4 changed files with 33 additions and 46 deletions

View File

@@ -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);
}
}
})
});