11/* global selectionDate */
22/* global LC */
3- ( function ( ) {
3+ ( function ( w , d ) {
44
55 var DEFAULT_CALENDAR_FILE_NAME = 'my-life-calendar' ,
66 DEFAULT_IMAGE_TYPE = 'image/jpeg' ,
147147 ] ;
148148
149149
150- datePicker = selectionDate ( LC . update , langs [ 0 ] . months ) ;
151150
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 ) ;
154155
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 ( ) ;
155169
156170 theme . onchange = function ( e ) {
157171 var themeId = e . target . value || 0 ;
158172 var theme = themes [ themeId ] ;
159173
160174 LC . changeTheme ( theme ) ;
175+ saveKey ( 'theme' , themeId ) ;
161176 } ;
162177
163178 lang . onchange = function ( e ) {
164179 var langId = e . target . value || 0 ;
165180 var lang = langs [ langId ] ;
166181
167- datePicker = selectionDate ( LC . update , lang . months ) ;
182+ datePicker = selectionDate ( updateDate , lang . months ) ;
183+ restoreDate ( ) ;
168184
169185 updateLang ( lang . settings ) ;
170186 LC . changeLang ( lang . calendar ) ;
187+ saveKey ( 'lang' , langId ) ;
171188 } ;
172189
173190 saveImage . onclick = function ( e ) {
198215 return block . querySelector ( 'canvas' ) . toDataURL ( DEFAULT_IMAGE_TYPE ) ;
199216 }
200217
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+
201230 function updateLang ( lang ) {
202231 for ( var key in lang )
203232 {
211240 }
212241 }
213242 }
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