@@ -30,12 +30,23 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
30
30
}
31
31
32
32
ngOnInit ( ) : void {
33
+ this . load ( ) ;
34
+ }
35
+
36
+ private load ( ) : void {
33
37
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
+
34
46
if ( localModel ) {
35
47
this . model = < IndenterViewModel > JSON . parse ( localModel ) ;
36
48
}
37
49
38
- const localOptionGroups = localStorage . getItem ( 'indenter.options' ) ;
39
50
if ( localOptionGroups ) {
40
51
const optionGroups = < IndenterOptionGroups > JSON . parse ( localOptionGroups ) ;
41
52
this . isExpanded = optionGroups . isExpanded ;
@@ -46,13 +57,13 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
46
57
this . isVerticalOptionsExpanded = optionGroups . isVerticalOptionsExpanded ;
47
58
this . isApiAboutBoxExpanded = optionGroups . isApiAboutBoxExpanded ;
48
59
}
49
-
50
- this . isLocalStorageOK = localModel != null || localOptionGroups != null ;
51
- if ( ! this . isLocalStorageOK ) {
52
- this . getDefaults ( ) ;
53
- }
54
60
}
55
61
62
+ private clearStorage ( ) : void {
63
+ localStorage . removeItem ( 'indenter.model' ) ;
64
+ localStorage . removeItem ( 'indenter.options' ) ;
65
+ }
66
+
56
67
public getDefaults ( ) : void {
57
68
this . service . getIndenterDefaults ( ) . subscribe ( model => {
58
69
this . model = model ;
@@ -68,7 +79,17 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
68
79
private _isApiAboutBoxExpanded : boolean = false ;
69
80
70
81
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
+ }
72
93
73
94
public get model ( ) : IndenterViewModel {
74
95
return this . _model ;
@@ -77,9 +98,7 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
77
98
private set model ( value : IndenterViewModel ) {
78
99
this . _model = value ;
79
100
this . invalidateClipboard ( ) ;
80
- if ( this . isLocalStorageOK ) {
81
- localStorage . setItem ( 'indenter.model' , JSON . stringify ( this . model ) )
82
- } ;
101
+ this . saveModel ( ) ;
83
102
}
84
103
85
104
public get asJson ( ) : string {
@@ -98,7 +117,6 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
98
117
public get isIndentOptionsExpanded ( ) : boolean {
99
118
return this . _isIndentOptionsExpanded ;
100
119
}
101
-
102
120
public set isIndentOptionsExpanded ( value : boolean ) {
103
121
this . _isIndentOptionsExpanded = value ;
104
122
this . saveOptions ( ) ;
@@ -117,15 +135,13 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
117
135
this . _isVerticalOptionsExpanded = value ;
118
136
this . saveOptions ( ) ;
119
137
}
120
-
121
138
public get isApiAboutBoxExpanded ( ) : boolean {
122
139
return this . _isApiAboutBoxExpanded ;
123
140
}
124
141
public set isApiAboutBoxExpanded ( value : boolean ) {
125
142
this . _isApiAboutBoxExpanded = value ;
126
143
this . saveOptions ( ) ;
127
144
}
128
-
129
145
public get isOutdentOptionsExpanded ( ) : boolean {
130
146
return this . _isOutdentOptionsExpanded ;
131
147
}
@@ -159,7 +175,9 @@ export class IndenterComponent implements OnInit, IndenterOptionGroups {
159
175
}
160
176
}
161
177
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
+ }
163
181
}
164
182
165
183
public indent ( ) : void {
0 commit comments