mirror of
https://github.com/flynx/ImageGrid.git
synced 2025-10-29 10:20:08 +00:00
working on store API...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
df2b3108c6
commit
b3be3ee4ec
@ -46,7 +46,7 @@ var ConfigStoreActions = actions.Actions({
|
||||
// XXX
|
||||
storeConfig: ['File/Store configuration',
|
||||
function(key){
|
||||
// XXX
|
||||
// XXX this.saveData('*:config')
|
||||
}],
|
||||
// XXX
|
||||
loadConfig: ['File/Load stored configuration',
|
||||
@ -152,228 +152,5 @@ module.ConfigStore = core.ImageGridFeatures.Feature({
|
||||
|
||||
|
||||
|
||||
/*********************************************************************/
|
||||
/*/ XXX LEGACY...
|
||||
//
|
||||
// XXX might be a good idea to add an external payload mechanism for
|
||||
// other data to be saved to avoid re-implementing the same logic
|
||||
// ...like is done in features/history.js
|
||||
|
||||
var ConfigActions = actions.Actions({
|
||||
config: {
|
||||
'config-store-key': 'config',
|
||||
|
||||
// NOTE: this is in seconds...
|
||||
// NOTE: if this is null or 0 the timer will not start...
|
||||
'config-auto-save-interval': 3*60,
|
||||
},
|
||||
|
||||
// XXX should we store this in something like .default_config and
|
||||
// clone it???
|
||||
// ...do not think so, as the __base_config should always be set
|
||||
// to the values set in code... (check this!)
|
||||
__base_config: null,
|
||||
__config_loaded: null,
|
||||
__auto_save_config_timer: null,
|
||||
|
||||
|
||||
// Disable localStorage in child, preventing two viewers from messing
|
||||
// things up in one store...
|
||||
clone: [function(){
|
||||
return function(res){
|
||||
res.config['config-store-key'] = null
|
||||
}
|
||||
}],
|
||||
|
||||
// XXX make this a protocol to support multiple sources...
|
||||
// ...load only one, by priority/order
|
||||
// might be good to make this similar to collections loading...
|
||||
storeConfig: ['File/Store configuration',
|
||||
function(key){
|
||||
// XXX
|
||||
}],
|
||||
loadConfig: ['File/Load stored configuration',
|
||||
function(key){
|
||||
// XXX
|
||||
}],
|
||||
// XXX need to load the reset config, and not just set it...
|
||||
resetConfig: ['File/Reset settings',
|
||||
function(){
|
||||
this.config = this.__base_config || this.config
|
||||
}],
|
||||
|
||||
toggleAutoStoreConfig: ['File/Store configuration',
|
||||
toggler.Toggler(null,
|
||||
function(_, state){
|
||||
if(state == null){
|
||||
return this.__auto_save_config_timer || 'none'
|
||||
|
||||
} else {
|
||||
var that = this
|
||||
var interval = this.config['config-auto-save-interval']
|
||||
|
||||
// no timer interval set...
|
||||
if(!interval){
|
||||
return false
|
||||
}
|
||||
|
||||
// this cleans up before 'on' and fully handles 'off' action...
|
||||
if(this.__auto_save_config_timer != null){
|
||||
clearTimeout(this.__auto_save_config_timer)
|
||||
delete this.__auto_save_config_timer
|
||||
}
|
||||
|
||||
if(state == 'running'
|
||||
&& interval
|
||||
&& this.__auto_save_config_timer == null){
|
||||
|
||||
var runner = function(){
|
||||
clearTimeout(that.__auto_save_config_timer)
|
||||
|
||||
//that.logger && that.logger.emit('config', 'saving to local storage...')
|
||||
that.storeConfig()
|
||||
|
||||
var interval = that.config['config-auto-save-interval']
|
||||
if(!interval){
|
||||
delete that.__auto_save_config_timer
|
||||
return
|
||||
}
|
||||
interval *= 1000
|
||||
|
||||
that.__auto_save_config_timer = setTimeout(runner, interval)
|
||||
}
|
||||
|
||||
runner()
|
||||
}
|
||||
}
|
||||
},
|
||||
'running')],
|
||||
})
|
||||
|
||||
var Config =
|
||||
module.Config = core.ImageGridFeatures.Feature({
|
||||
title: '',
|
||||
doc: '',
|
||||
|
||||
tag: 'config',
|
||||
depends: [
|
||||
'store',
|
||||
],
|
||||
priority: 80,
|
||||
suggested: [
|
||||
'localstorage-config',
|
||||
'fs-config',
|
||||
],
|
||||
|
||||
actions: ConfigActions,
|
||||
|
||||
handlers: [
|
||||
// NOTE: considering that allot depends on this it must be
|
||||
// first to run...
|
||||
['start.pre',
|
||||
function(){
|
||||
console.log('--- PRE LOAD CONFIG')
|
||||
this.logger && this.logger
|
||||
.push('Startup')
|
||||
.emit('loaded', 'config')
|
||||
this
|
||||
.loadConfig()
|
||||
.toggleAutoStoreConfig('on')
|
||||
}],
|
||||
['stop.pre',
|
||||
function(){
|
||||
this.logger && this.logger
|
||||
.push('Shutdown')
|
||||
.emit('stored', 'config')
|
||||
this
|
||||
.storeConfig()
|
||||
.toggleAutoStoreConfig('off')
|
||||
}],
|
||||
],
|
||||
})
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
var ConfigLocalStorageActions = actions.Actions({
|
||||
config: {
|
||||
// XXX not sure what should be the default...
|
||||
'config-local-storage-save-diff': true,
|
||||
},
|
||||
|
||||
storeConfig: ['File/Store configuration',
|
||||
function(key){
|
||||
var key = key || this.config['config-store-key']
|
||||
|
||||
if(key != null){
|
||||
// build a diff...
|
||||
if(this.config['config-local-storage-save-diff']){
|
||||
var base = this.__base_config || {}
|
||||
var cur = this.config
|
||||
var config = {}
|
||||
Object.keys(cur)
|
||||
.forEach(function(e){
|
||||
if(cur.hasOwnProperty(e)
|
||||
&& base[e] != cur[e]
|
||||
// NOTE: this may go wrong for objects
|
||||
// if key order is different...
|
||||
// ...this is no big deal as false
|
||||
// positives are not lost data...
|
||||
|| JSON.stringify(base[e]) != JSON.stringify(cur[e])){
|
||||
config[e] = cur[e]
|
||||
}
|
||||
})
|
||||
|
||||
// full save...
|
||||
} else {
|
||||
var config = this.config
|
||||
}
|
||||
|
||||
// store...
|
||||
localStorage[key] = JSON.stringify(config)
|
||||
}
|
||||
}],
|
||||
loadConfig: ['File/Load stored configuration',
|
||||
function(key){
|
||||
key = key || this.config['config-store-key']
|
||||
|
||||
if(key && localStorage[key]){
|
||||
// get the original (default) config and keep it for
|
||||
// reference...
|
||||
// NOTE: this is here so as to avoid creating 'endless'
|
||||
// config inheritance chains...
|
||||
base = this.__base_config = this.__base_config || this.config
|
||||
|
||||
var loaded = JSON.parse(localStorage[key])
|
||||
loaded.__proto__ = base
|
||||
|
||||
this.config = loaded
|
||||
}
|
||||
}],
|
||||
})
|
||||
|
||||
var ConfigLocalStorage =
|
||||
module.ConfigLocalStorage = core.ImageGridFeatures.Feature({
|
||||
title: '',
|
||||
doc: '',
|
||||
|
||||
// XXX rename???
|
||||
tag: 'localstorage-config',
|
||||
depends: [
|
||||
'config',
|
||||
'ui',
|
||||
],
|
||||
priority: 80,
|
||||
|
||||
isApplicable: function(){
|
||||
return typeof(localStorage) != 'undefined'
|
||||
&& localStorage != null },
|
||||
|
||||
actions: ConfigLocalStorageActions,
|
||||
})
|
||||
//*/
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
* vim:set ts=4 sw=4 : */ return module })
|
||||
|
||||
@ -81,6 +81,18 @@ var StoreActions = actions.Actions({
|
||||
})],
|
||||
|
||||
// base API...
|
||||
// XXX we need to be able to save/load specific part of the data...
|
||||
// ...i.e. query by store and/or key...
|
||||
// the syntax could be:
|
||||
// <store>:<path>
|
||||
//
|
||||
// Example:
|
||||
// 'localstorage:config' - save config to localStorage
|
||||
// 'localstorage:*' - save all to localstorage
|
||||
// '*:config' - save config to all supported stores
|
||||
// '*:*' - save everything
|
||||
//
|
||||
// ...this must be supported by .prepareStoreToSave(..)
|
||||
prepareStoreToSave: ['- Store/',
|
||||
core.doc`
|
||||
|
||||
@ -104,14 +116,14 @@ var StoreActions = actions.Actions({
|
||||
},
|
||||
}
|
||||
`,
|
||||
function(mode, date){
|
||||
function(mode, data){
|
||||
var store = {}
|
||||
// populate the store...
|
||||
Object.keys(this.store_handlers)
|
||||
.forEach(function(key){ store[key] = {} })
|
||||
return {
|
||||
mode: mode || 'full',
|
||||
date: date || Date.timeStamp(),
|
||||
data: data || Date.timeStamp(),
|
||||
|
||||
store: store,
|
||||
}
|
||||
@ -125,10 +137,26 @@ var StoreActions = actions.Actions({
|
||||
// XXX async???
|
||||
// XXX we need to be able to save/load specific part of the data...
|
||||
// ...i.e. query by store and/or key...
|
||||
// the syntax could be:
|
||||
// <store>:<path>
|
||||
//
|
||||
// Example:
|
||||
// 'localstorage:config' - save config to localStorage
|
||||
// 'localstorage:*' - save all to localstorage
|
||||
// '*:config' - save config to all supported stores
|
||||
// '*:*' - save everything
|
||||
//
|
||||
// ...this must be supported by .prepareStoreToSave(..)
|
||||
// XXX API
|
||||
// .storeData(mode) - store all with mode...
|
||||
// .storeData(mode, data) - store data with mode...
|
||||
// .storeData(selector) - store only matching
|
||||
// .storeData(selector, data) - store data to selector...
|
||||
// XXX do we need mode here???
|
||||
saveData: ['- Store/',
|
||||
function(mode, date){
|
||||
function(mode, data){
|
||||
var handlers = this.store_handlers
|
||||
var data = this.prepareStoreToSave(mode, date)
|
||||
var data = this.prepareStoreToSave(mode, data)
|
||||
|
||||
Object.keys(data.store).forEach(function(store){
|
||||
var handler = handlers[store]
|
||||
@ -209,7 +237,7 @@ module.Store = core.ImageGridFeatures.Feature({
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
function makeStorageHandler(storage){
|
||||
var func = function(data){
|
||||
var func = function(data, key){
|
||||
storage = typeof(storage) == typeof('str') ? window[storage] : storage
|
||||
|
||||
var root_pattern = /^(\.\.)?[\\\/]/
|
||||
@ -232,6 +260,10 @@ function makeStorageHandler(storage){
|
||||
|
||||
// set...
|
||||
} else if(data){
|
||||
if(key){
|
||||
data = { key: data }
|
||||
}
|
||||
|
||||
var root_data = {}
|
||||
var root_paths = []
|
||||
|
||||
@ -280,10 +312,14 @@ function makeStorageHandler(storage){
|
||||
.${storage}DataHandler()
|
||||
-> data
|
||||
|
||||
Save data to ${storage}...
|
||||
Save data set to ${storage}...
|
||||
.${storage}DataHandler(data)
|
||||
-> this
|
||||
|
||||
Save data to key in ${storage}...
|
||||
.${storage}DataHandler(data, key)
|
||||
-> this
|
||||
|
||||
Delete all data from ${storage}...
|
||||
.${storage}DataHandler(null)
|
||||
-> this
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user