| 
									
										
										
										
											2018-01-17 06:28:09 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * Features: | 
					
						
							|  |  |  | *	- config | 
					
						
							|  |  |  | *		general config API | 
					
						
							|  |  |  | *	- localstorage-config | 
					
						
							|  |  |  | *		maintain configuration state in localStorage | 
					
						
							|  |  |  | *	- fs-config | 
					
						
							|  |  |  | *		maintain configuration state in file system | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * XXX this module need refactoring... | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							|  |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var toggler = require('lib/toggler') | 
					
						
							|  |  |  | var actions = require('lib/actions') | 
					
						
							|  |  |  | var features = require('lib/features') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var core = require('features/core') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 09:30:36 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 02:18:19 +03:00
										 |  |  | // XXX might be a good idea to add a .configLoaded(..) and .configChanged(..) 
 | 
					
						
							|  |  |  | // 		events thought it's not clear how are we going to track changes...
 | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | var ConfigStoreActions = actions.Actions({ | 
					
						
							|  |  |  | 	config: { | 
					
						
							|  |  |  | 		// XXX should this include path???
 | 
					
						
							|  |  |  | 		// 		...there should be modes:
 | 
					
						
							|  |  |  | 		// 			- 'read-only'	-- don't save...
 | 
					
						
							|  |  |  | 		// 			- 'portable'	-- use APP dir
 | 
					
						
							|  |  |  | 		// 			- 'normal'		-- use $HOME
 | 
					
						
							|  |  |  | 		'config-fs-filename': '.ImageGrid.json', | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 16:49:44 +03:00
										 |  |  | 		'config-auto-save-interval': 1000*5, | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	__base_config: null, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 16:49:44 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	// XXX
 | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 	storeConfig: ['File/Store configuration', | 
					
						
							|  |  |  | 		function(key){ | 
					
						
							| 
									
										
										
										
											2018-02-17 04:41:07 +03:00
										 |  |  | 			// XXX this.saveData('*:config')
 | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 		}], | 
					
						
							| 
									
										
										
										
											2018-02-14 16:49:44 +03:00
										 |  |  | 	// XXX
 | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 	loadConfig: ['File/Load stored configuration', | 
					
						
							|  |  |  | 		function(key){ | 
					
						
							|  |  |  | 			// XXX
 | 
					
						
							|  |  |  | 		}], | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 	// XXX should this also reload???
 | 
					
						
							|  |  |  | 	resetConfig: ['- Config/', | 
					
						
							|  |  |  | 		function(){ | 
					
						
							|  |  |  | 			var base = this.__base_config = this.__base_config || this.config | 
					
						
							|  |  |  | 			this.config = Object.create(base) | 
					
						
							|  |  |  | 		}], | 
					
						
							| 
									
										
										
										
											2018-02-14 16:49:44 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// XXX use timer events... (???)
 | 
					
						
							|  |  |  | 	// XXX this needs a working .storeConfig(..)
 | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 	toggleAutoStoreConfig: ['File/Store configuration', | 
					
						
							|  |  |  | 		toggler.Toggler(null,  | 
					
						
							|  |  |  | 			function(_, state){  | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 				var timer = 'config-auto-save-timer' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 				if(state == null){ | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 					return this.isPersistentInterval(timer) || 'none' | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					var that = this | 
					
						
							|  |  |  | 					var interval = this.config['config-auto-save-interval'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// no timer interval set...
 | 
					
						
							|  |  |  | 					if(!interval){ | 
					
						
							|  |  |  | 						return false | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 					// start/restart...
 | 
					
						
							|  |  |  | 					if(state == 'running' && interval){ | 
					
						
							|  |  |  | 						this.setPersistentInterval(timer, 'storeConfig', interval*1000) | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-13 01:14:35 +03:00
										 |  |  | 					// stop...
 | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						this.clearPersistentInterval(timer) | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			'running')], | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ConfigStore =  | 
					
						
							|  |  |  | module.ConfigStore = core.ImageGridFeatures.Feature({ | 
					
						
							|  |  |  | 	title: '', | 
					
						
							|  |  |  | 	doc: '', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tag: 'store-config', | 
					
						
							|  |  |  | 	priority: 80, | 
					
						
							|  |  |  | 	depends: [ | 
					
						
							| 
									
										
										
										
											2018-02-12 01:35:35 +03:00
										 |  |  | 		'timers', | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 		'store-localstorage', | 
					
						
							|  |  |  | 	], | 
					
						
							|  |  |  | 	suggested: [ | 
					
						
							|  |  |  | 		'store-fs-json-sync', | 
					
						
							|  |  |  | 	], | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	actions: ConfigStoreActions, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	handlers: [ | 
					
						
							| 
									
										
										
										
											2018-02-18 03:24:07 +03:00
										 |  |  | 		// XXX need to update rather than rewrite things...
 | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 		['prepareStoreToSave',  | 
					
						
							|  |  |  | 			function(res){ | 
					
						
							| 
									
										
										
										
											2018-02-18 15:02:45 +03:00
										 |  |  | 				var ls_path = '${INSTANCE}/config' | 
					
						
							|  |  |  | 				//var ls_path = 'config'
 | 
					
						
							| 
									
										
										
										
											2018-02-18 03:24:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 				// localStorage...
 | 
					
						
							|  |  |  | 				// NOTE: we do not need to clone anything here as this 
 | 
					
						
							|  |  |  | 				// 		will be done by the localStorage handler...
 | 
					
						
							| 
									
										
										
										
											2018-02-18 03:24:07 +03:00
										 |  |  | 				if(res.data.storage){ | 
					
						
							|  |  |  | 					res.data.storage[ls_path] = this.config | 
					
						
							| 
									
										
										
										
											2018-02-18 00:57:28 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 00:57:28 +03:00
										 |  |  | 				if(res.data.fsJSONSync){ | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 					// XXX should this include path???
 | 
					
						
							| 
									
										
										
										
											2018-02-18 00:57:28 +03:00
										 |  |  | 					res.data.fsJSONSync[this.config['config-fs-filename'] || '.ImageGrid.json'] = this.config | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}], | 
					
						
							| 
									
										
										
										
											2018-02-18 00:57:28 +03:00
										 |  |  | 		['prepareIndexForLoad', | 
					
						
							|  |  |  | 			function(){ | 
					
						
							|  |  |  | 			}], | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 		// NOTE: this is sync for sync stores...
 | 
					
						
							|  |  |  | 		['storeDataLoaded', | 
					
						
							| 
									
										
										
										
											2018-02-18 00:57:28 +03:00
										 |  |  | 			function(data){ | 
					
						
							|  |  |  | 				var base = this.__base_config = this.__base_config || this.config | 
					
						
							| 
									
										
										
										
											2018-02-18 15:02:45 +03:00
										 |  |  | 				var ls_path = '${INSTANCE}/config' | 
					
						
							| 
									
										
										
										
											2018-02-18 00:57:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// XXX sort out load priority/logic...
 | 
					
						
							|  |  |  | 				// 		- one or the other or both?
 | 
					
						
							|  |  |  | 				// 		- what order?
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 03:24:07 +03:00
										 |  |  | 				if((data.storage || {})[ls_path]){ | 
					
						
							|  |  |  | 					var config = data.storage[ls_path] || {} | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 					config.__proto__ = base | 
					
						
							| 
									
										
										
										
											2018-02-14 16:49:44 +03:00
										 |  |  | 					this.config = config | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 00:57:28 +03:00
										 |  |  | 				if((data.fsJSONSync || {}).config){ | 
					
						
							|  |  |  | 					var config = data.fsJSONSync.config || {} | 
					
						
							|  |  |  | 					config.__proto__ = base | 
					
						
							|  |  |  | 					this.config = config | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-14 16:49:44 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// auto-start auto-save...
 | 
					
						
							|  |  |  | 				this.config['config-auto-save-interval'] > 0  | 
					
						
							|  |  |  | 					&& this.toggleAutoStoreConfig('?') == 'off' | 
					
						
							|  |  |  | 					&& this.toggleAutoStoreConfig() | 
					
						
							| 
									
										
										
										
											2018-02-04 06:23:31 +03:00
										 |  |  | 			}], | 
					
						
							|  |  |  | 	], | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 06:28:09 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |