1
1
/* global selectionDate */
2
2
/* global LC */
3
- ( function ( ) {
3
+ ( function ( w , d ) {
4
4
5
5
var DEFAULT_CALENDAR_FILE_NAME = 'my-life-calendar' ,
6
6
DEFAULT_IMAGE_TYPE = 'image/jpeg' ,
147
147
] ;
148
148
149
149
150
- datePicker = selectionDate ( LC . update , langs [ 0 ] . months ) ;
151
150
152
- updateLang ( langs [ 0 ] . settings ) ;
153
- LC . init ( block , langs [ 0 ] . calendar ) ;
151
+ // init language
152
+ var initLangIndex = getKey ( 'lang' , 0 ) ;
153
+ var defaultLang = langs [ initLangIndex ] ;
154
+ datePicker = selectionDate ( updateDate , defaultLang . months ) ;
154
155
156
+ LC . init ( block , defaultLang . calendar ) ;
157
+ updateLang ( defaultLang . settings ) ;
158
+ lang . value = initLangIndex ;
159
+
160
+
161
+ // init theme
162
+ var defaultThemeIndex = getKey ( 'theme' , 0 ) ;
163
+ var defaultTheme = themes [ defaultThemeIndex ] ;
164
+ theme . value = defaultThemeIndex ;
165
+ LC . changeTheme ( defaultTheme ) ;
166
+
167
+ // init date
168
+ restoreDate ( ) ;
155
169
156
170
theme . onchange = function ( e ) {
157
171
var themeId = e . target . value || 0 ;
158
172
var theme = themes [ themeId ] ;
159
173
160
174
LC . changeTheme ( theme ) ;
175
+ saveKey ( 'theme' , themeId ) ;
161
176
} ;
162
177
163
178
lang . onchange = function ( e ) {
164
179
var langId = e . target . value || 0 ;
165
180
var lang = langs [ langId ] ;
166
181
167
- datePicker = selectionDate ( LC . update , lang . months ) ;
182
+ datePicker = selectionDate ( updateDate , lang . months ) ;
183
+ restoreDate ( ) ;
168
184
169
185
updateLang ( lang . settings ) ;
170
186
LC . changeLang ( lang . calendar ) ;
187
+ saveKey ( 'lang' , langId ) ;
171
188
} ;
172
189
173
190
saveImage . onclick = function ( e ) {
198
215
return block . querySelector ( 'canvas' ) . toDataURL ( DEFAULT_IMAGE_TYPE ) ;
199
216
}
200
217
218
+ function updateDate ( date ) {
219
+ LC . update ( date ) ;
220
+ saveKey ( 'date' , date . getTime ( ) ) ;
221
+ }
222
+
223
+ function restoreDate ( ) {
224
+ var defaultDate = getKey ( 'date' , null ) ;
225
+ if ( ! ! defaultDate ) {
226
+ datePicker . setDate ( new Date ( parseInt ( defaultDate ) ) ) ;
227
+ }
228
+ }
229
+
201
230
function updateLang ( lang ) {
202
231
for ( var key in lang )
203
232
{
211
240
}
212
241
}
213
242
}
214
- } ( ) ) ;
243
+
244
+ function saveKey ( key , value ) {
245
+ w . localStorage . setItem ( key , value ) ;
246
+ }
247
+
248
+ function getKey ( key , defaultValue ) {
249
+ var value = w . localStorage . getItem ( key ) ;
250
+ return ! ! value ? value : defaultValue ;
251
+ }
252
+ } ( window , document ) ) ;
0 commit comments