diff --git a/js/components/alert.js b/js/components/alert.js index c13810c8d..ecaa3bc0a 100644 --- a/js/components/alert.js +++ b/js/components/alert.js @@ -43,10 +43,10 @@ $(function() _create: function () { this._bindEvents(); - let bStoredSectionState = JSON.parse(localStorage.getItem(this.options.collapsibleStateStorageKey)); - let bIsSectionOpenedInitially = (bStoredSectionState == null) - ? this.options.bOpenedByDefault - : bStoredSectionState; + let bIsSectionOpenedInitially = GetUserPreferenceAsBoolean( + this.options.collapsibleStateStorageKey, + this.options.bOpenedByDefault + ); if (bIsSectionOpenedInitially) { this.element.addClass(this.css_classes.opened); } else { @@ -75,9 +75,10 @@ $(function() this.element.toggleClass(this.css_classes.opened); if (this.options.collapsibleStateStorageKey) { - localStorage.setItem( + SetUserPreference( this.options.collapsibleStateStorageKey, - this.element.hasClass(this.css_classes.opened) + this.element.hasClass(this.css_classes.opened), + true ); } } diff --git a/js/components/collapsible-section.js b/js/components/collapsible-section.js index 5f50fbb80..4650e618a 100644 --- a/js/components/collapsible-section.js +++ b/js/components/collapsible-section.js @@ -41,9 +41,10 @@ $(function () { _create: function () { this._bindEvents(); - let bStoredSectionState = JSON.parse(localStorage.getItem(this.options.collapsibleStateStorageKey)); - let bIsSectionOpenedInitially = (bStoredSectionState == null) ? this.options.bOpenedByDefault : bStoredSectionState; - + let bIsSectionOpenedInitially = GetUserPreferenceAsBoolean( + this.options.collapsibleStateStorageKey, + this.options.bOpenedByDefault + ); if (bIsSectionOpenedInitially) { this.element.addClass(this.css_classes.opened); } else { @@ -65,9 +66,10 @@ $(function () { this.element.toggleClass(this.css_classes.opened); if (this.options.collapsibleStateStorageKey) { - localStorage.setItem( + SetUserPreference( this.options.collapsibleStateStorageKey, - this.element.hasClass(this.css_classes.opened) + this.element.hasClass(this.css_classes.opened), + true ); } } diff --git a/js/utils.js b/js/utils.js index 628a35e51..d41067cfe 100644 --- a/js/utils.js +++ b/js/utils.js @@ -346,17 +346,41 @@ function SetUserPreference(sPreferenceCode, sPrefValue, bPersistent) { /** * Get user specific preferences - * depends on a global variable oUserPreferences created/filled by the iTopWebPage * that acts as a local -write through- cache + * @borrows global variable oUserPreferences created/filled by the iTopWebPage if login method was called */ function GetUserPreference(sPreferenceCode, sDefaultValue) { var value = sDefaultValue; - if ((typeof(oUserPreferences) !== 'undefined') && (typeof(oUserPreferences[sPreferenceCode]) !== 'undefined')) { + if ((typeof (oUserPreferences) !== 'undefined') && (typeof (oUserPreferences[sPreferenceCode]) !== 'undefined')) { value = oUserPreferences[sPreferenceCode]; } return value; } +/** + * @param {string} sPreferenceCode + * @param {boolean} bDefaultValue + * @returns {boolean} + * @since 3.0.0 + */ +function GetUserPreferenceAsBoolean(sPreferenceCode, bDefaultValue) { + let sVal = GetUserPreference(sPreferenceCode, bDefaultValue); + try { + sVal = sVal.toLowerCase(); + } catch (error) { + // nothing : this may be the boolean default value ! + } + + if (sVal === "true") { + return true; + } + if (sVal === "false") { + return false; + } + + return bDefaultValue; +} + /** * Check/uncheck a whole list of checkboxes */ diff --git a/sources/application/UI/Base/Component/Alert/Alert.php b/sources/application/UI/Base/Component/Alert/Alert.php index b4e7378c9..3017753f4 100644 --- a/sources/application/UI/Base/Component/Alert/Alert.php +++ b/sources/application/UI/Base/Component/Alert/Alert.php @@ -21,7 +21,6 @@ namespace Combodo\iTop\Application\UI\Base\Component\Alert; use Combodo\iTop\Application\UI\Base\UIBlock; -use utils; /** * Class Alert @@ -129,8 +128,7 @@ class Alert extends UIBlock public function EnableSaveCollapsibleState($sSectionStateStorageKey) { $this->bIsSaveCollapsibleStateEnabled = true; - $sSectionStateStorageKeyPrefix = utils::GetConfig()->GetItopInstanceid(); - $this->sSectionStateStorageKey = $sSectionStateStorageKeyPrefix.'/'.$sSectionStateStorageKey; + $this->sSectionStateStorageKey = $sSectionStateStorageKey; return $this; } diff --git a/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php b/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php index 072245f49..56be2a168 100644 --- a/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php +++ b/sources/application/UI/Base/Component/CollapsibleSection/CollapsibleSection.php @@ -22,7 +22,6 @@ 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 @@ -64,8 +63,7 @@ class CollapsibleSection extends UIContentBlock public function EnableSaveCollapsibleState($sSectionStateStorageKey) { $this->bIsSaveCollapsibleStateEnabled = true; - $sSectionStateStorageKeyPrefix = utils::GetConfig()->GetItopInstanceid(); - $this->sSectionStateStorageKey = $sSectionStateStorageKeyPrefix.'/'.$sSectionStateStorageKey; + $this->sSectionStateStorageKey = $sSectionStateStorageKey; return $this; } diff --git a/sources/application/WebPage/iTopWebPage.php b/sources/application/WebPage/iTopWebPage.php index 5bdef6667..7a8827bbd 100644 --- a/sources/application/WebPage/iTopWebPage.php +++ b/sources/application/WebPage/iTopWebPage.php @@ -461,28 +461,14 @@ JS // TODO 3.0.0: To preserve $this->add_ready_script(InlineImage::FixImagesWidth()); - /* - * Not used since the sorting of the tables is always performed server-side - AttributeDateTime::InitTableSorter($this, 'custom_date_time'); - AttributeDate::InitTableSorter($this, 'custom_date'); - */ + // user pref for client side + // see GetUserPreference() in utils.js + $sUserPrefs = appUserPreferences::GetAsJSON(); + $this->add_script("var oUserPreferences = $sUserPrefs;"); // TODO 3.0.0: What is this for? - $sUserPrefs = appUserPreferences::GetAsJSON(); $this->add_script( <<SetContentLayout($oPageContentLayout);