@@ -30,12 +30,23 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
3030 }
3131
3232 ngOnInit ( ) : void {
33+ this . load ( ) ;
34+ }
35+
36+ private load ( ) : void {
3337 const localModel = localStorage . getItem ( 'indenter.model' ) ;
38+ const localOptionGroups = localStorage . getItem ( 'indenter.options' ) ;
39+ this . isLocalStorageOK = localModel != null && localOptionGroups != null ;
40+
41+ if ( ! this . isLocalStorageOK ) {
42+ this . getDefaults ( ) ;
43+ return ;
44+ }
45+
3446 if ( localModel ) {
3547 this . model = < IndenterViewModel > JSON . parse ( localModel ) ;
3648 }
3749
38- const localOptionGroups = localStorage . getItem ( 'indenter.options' ) ;
3950 if ( localOptionGroups ) {
4051 const optionGroups = < IndenterOptionGroups > JSON . parse ( localOptionGroups ) ;
4152 this . isExpanded = optionGroups . isExpanded ;
@@ -46,13 +57,13 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
4657 this . isVerticalOptionsExpanded = optionGroups . isVerticalOptionsExpanded ;
4758 this . isApiAboutBoxExpanded = optionGroups . isApiAboutBoxExpanded ;
4859 }
49-
50- this . isLocalStorageOK = localModel != null || localOptionGroups != null ;
51- if ( ! this . isLocalStorageOK ) {
52- this . getDefaults ( ) ;
53- }
5460 }
5561
62+ private clearStorage ( ) : void {
63+ localStorage . removeItem ( 'indenter.model' ) ;
64+ localStorage . removeItem ( 'indenter.options' ) ;
65+ }
66+
5667 public getDefaults ( ) : void {
5768 this . service . getIndenterDefaults ( ) . subscribe ( model => {
5869 this . model = model ;
@@ -68,7 +79,17 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
6879 private _isApiAboutBoxExpanded : boolean = false ;
6980
7081 public isIndenterBusy : boolean = false ;
71- public isLocalStorageOK : boolean = false ;
82+
83+ private _isLocalStorageOK : boolean = false ;
84+ public get isLocalStorageOK ( ) : boolean {
85+ return this . _isLocalStorageOK ;
86+ }
87+ public set isLocalStorageOK ( value : boolean ) {
88+ this . _isLocalStorageOK = value ;
89+ if ( ! value ) {
90+ this . clearStorage ( ) ;
91+ }
92+ }
7293
7394 public get model ( ) : IndenterViewModel {
7495 return this . _model ;
@@ -77,9 +98,7 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
7798 private set model ( value : IndenterViewModel ) {
7899 this . _model = value ;
79100 this . invalidateClipboard ( ) ;
80- if ( this . isLocalStorageOK ) {
81- localStorage . setItem ( 'indenter.model' , JSON . stringify ( this . model ) )
82- } ;
101+ this . saveModel ( ) ;
83102 }
84103
85104 public get asJson ( ) : string {
@@ -98,7 +117,6 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
98117 public get isIndentOptionsExpanded ( ) : boolean {
99118 return this . _isIndentOptionsExpanded ;
100119 }
101-
102120 public set isIndentOptionsExpanded ( value : boolean ) {
103121 this . _isIndentOptionsExpanded = value ;
104122 this . saveOptions ( ) ;
@@ -117,15 +135,13 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
117135 this . _isVerticalOptionsExpanded = value ;
118136 this . saveOptions ( ) ;
119137 }
120-
121138 public get isApiAboutBoxExpanded ( ) : boolean {
122139 return this . _isApiAboutBoxExpanded ;
123140 }
124141 public set isApiAboutBoxExpanded ( value : boolean ) {
125142 this . _isApiAboutBoxExpanded = value ;
126143 this . saveOptions ( ) ;
127144 }
128-
129145 public get isOutdentOptionsExpanded ( ) : boolean {
130146 return this . _isOutdentOptionsExpanded ;
131147 }
@@ -159,7 +175,9 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
159175 }
160176 }
161177 private saveOptions ( ) : void {
162- localStorage . setItem ( 'indenter.options' , JSON . stringify ( this . asOptionGroups ) ) ;
178+ if ( this . isLocalStorageOK ) {
179+ localStorage . setItem ( 'indenter.options' , JSON . stringify ( this . asOptionGroups ) ) ;
180+ }
163181 }
164182
165183 public indent ( ) : void {
0 commit comments