added local storage config + lifecycle events/actions...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2015-12-11 10:55:27 +03:00
parent cb27807a99
commit 8e4a520de9
3 changed files with 247 additions and 33 deletions

View File

@ -233,7 +233,6 @@ WalkPrototype.options = {
list: listDir, list: listDir,
//disableItemPattern: false,
fileCountPattern: '*', fileCountPattern: '*',
} }
WalkPrototype.options.__proto__ = browse.Browser.prototype.options WalkPrototype.options.__proto__ = browse.Browser.prototype.options

View File

@ -83,6 +83,7 @@ module.GLOBAL_KEYBOARD = {
}, },
F5: doc('Full reload viewer', F5: doc('Full reload viewer',
function(){ function(){
//a.stop()
/* /*
killAllWorkers() killAllWorkers()
.done(function(){ .done(function(){
@ -259,26 +260,31 @@ $(function(){
//a.experimental = true //a.experimental = true
// setup actions... // setup actions...
viewer.ImageGridFeatures.setup(a, [ viewer.ImageGridFeatures
'viewer-testing', .setup(a, [
'viewer-testing',
// XXX this is not for production...
'experiments',
])
a.logger = a.logger || {emit: function(e, v){ console.log(' ', e, v) }}
// XXX this is not for production...
'experiments',
])
// setup the viewer... // setup the viewer...
a.load({ a
//.toggleAutoStoreConfig()
.load({
viewer: $('.viewer'), viewer: $('.viewer'),
}) })
.setEmptyMsg('Loading...') .setEmptyMsg('Loading...')
.start()
// load last loaded path... // load some testing data if nothing else loaded...
if(a.url_history && Object.keys(a.url_history).length > 0){ if(!a.url_history || Object.keys(a.url_history).length == 0){
a.loadLastSavedBasePath()
// load some testing data...
} else {
// NOTE: we can (and do) load this in parts... // NOTE: we can (and do) load this in parts...
a a
.load({ .load({
@ -297,8 +303,6 @@ $(function(){
'Press \'O\' to load, \'F1\' for help or \'?\' for keyboard mappings.') 'Press \'O\' to load, \'F1\' for help or \'?\' for keyboard mappings.')
a.logger = a.logger || {emit: function(e, v){ console.log(' ', e, v) }}
// setup base keyboard for devel, in case something breaks... // setup base keyboard for devel, in case something breaks...
// This branch does not drop keys... // This branch does not drop keys...

View File

@ -149,14 +149,49 @@ module.ImageGridFeatures = Object.create(features.FeatureSet)
/*********************************************************************/ /*********************************************************************/
//
// XXX Tasks to accomplish here: // XXX should this be a generic library thing???
// - life-cycle actions/events // XXX should this also unbind events???
// - setup // ...in case we manually stop...
// - reset // XXX should his have state???
// - "features" and the mechanism to turn them on or off (action-sets) // ...if so, should this be a toggler???
// var LifeCycleActions = actions.Actions({
// // XXX avoid binding multiple times per object...
start: ['- System/',
function(){
var that = this
this.logger && this.logger.emit('start')
// setup exit...
// browser and nw.js
if(typeof('window') != 'undefined'){
$(window).unload(function(){ a.stop() })
// pure node.js
} else if(typeof(process) != 'undefined'){
process.on('exit', function(){ that.stop() })
}
}],
// XXX unbind events...
stop: ['- System/',
function(){
this.logger && this.logger.emit('stop')
}],
})
var LifeCycle =
module.LifeCycle = ImageGridFeatures.Feature({
title: '',
doc: '',
tag: 'lifecycle',
actions: LifeCycleActions,
})
//---------------------------------------------------------------------
// XXX split this into read and write actions... // XXX split this into read and write actions...
var BaseActions = var BaseActions =
@ -1586,6 +1621,127 @@ module.Journal = ImageGridFeatures.Feature({
//---------------------------------------------------------------------
var ConfigLocalStorageActions = actions.Actions({
config: {
'config-local-storage-key': 'config',
// NOTE: this is in seconds...
// NOTE: if this is null or 0 the timer will not start...
'auto-save-config-local-storage-interval': 5*60,
},
__config_loaded: null,
__auto_save_config_timer: null,
// Disable localStorage in child...
clone: [function(){
return function(res){
res.config['config-local-storage-key'] = null
}
}],
storeConfig: ['File/Store configuration',
function(key){
var key = key || this.config['config-local-storage-key']
if(key != null){
localStorage[key] = JSON.stringify(this.config)
}
}],
loadStoredConfig: ['File/Load stored configuration',
function(key){
key = key || this.config['config-local-storage-key']
if(key && localStorage[key]){
this.config = JSON.parse(localStorage[key])
}
}],
// XXX make this a real toggler...
toggleAutoStoreConfig: ['File/Store configuration',
function(state){
var that = this
// NOTE: this is the target state and not the current...
state = state
|| (this.__auto_save_config_timer == null ? 'on' : 'off')
if(state == '?'){
return this.__auto_save_config_timer == null ? 'off' : 'on'
}
interval = this.config['auto-save-config-local-storage-interval']
// no timer interval set...
if(!interval){
return this.toggleAutoStoreConfig('?')
}
if(this.__auto_save_config_timer != null){
clearTimeout(this.__auto_save_config_timer)
delete this.__auto_save_config_timer
}
if(state == 'on'
&& 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['auto-save-config-local-storage-interval']
if(!interval){
delete that.__auto_save_config_timer
return
}
interval *= 1000
that.__auto_save_config_timer = setTimeout(runner, interval)
}
runner()
}
return state
}],
})
var ConfigLocalStorage =
module.ConfigLocalStorage = ImageGridFeatures.Feature({
title: '',
doc: '',
tag: 'config-local-storage',
depends: [
'ui',
],
actions: ConfigLocalStorageActions,
handlers: [
['start',
function(){
this
.loadStoredConfig()
.toggleAutoStoreConfig('on')
}],
['stop',
function(){
this
.storeConfig()
.toggleAutoStoreConfig('off')
}],
],
})
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// NOTE: this is split out to an action so as to enable ui elements to // NOTE: this is split out to an action so as to enable ui elements to
@ -2084,9 +2240,15 @@ module.SingleImageView = ImageGridFeatures.Feature({
handlers:[ handlers:[
['fitImage.post', ['fitImage.post',
function(){ function(){
// singe image mode -- set image proportions... // singe image mode -- set image proportions...
if(this.toggleSingleImage('?') == 'on'){ if(this.toggleSingleImage('?') == 'on'){
updateImageProportions.call(this) updateImageProportions.call(this)
this.config['single-image-scale'] = this.screenwidth
} else {
this.config['ribbon-scale'] = this.screenwidth
} }
}], }],
// XXX this uses .screenwidth for scale, is this the right way to go? // XXX this uses .screenwidth for scale, is this the right way to go?
@ -2118,6 +2280,36 @@ module.SingleImageView = ImageGridFeatures.Feature({
}) })
var SingleImageViewLocalStorage =
module.SingleImageViewLocalStorage = ImageGridFeatures.Feature({
title: '',
doc: '',
tag: 'ui-single-image-view-local-storage',
depends: [
'ui-single-image-view',
'config-local-storage',
],
handlers:[
// set scale...
['load loadURLs',
function(){
// prevent this from doing anything while no viewer...
if(!this.ribbons || !this.ribbons.viewer || this.ribbons.viewer.length == 0){
return
}
if(this.toggleSingleImage('?') == 'on'){
this.screenwidth = this.config['single-image-scale'] || this.screenwidth
} else {
this.screenwidth = this.config['ribbon-scale'] || this.screenwidth
}
}],
],
})
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// These feature glue traverse and ribbon alignment... // These feature glue traverse and ribbon alignment...
@ -2856,13 +3048,21 @@ var makeActionLister = function(list, filter, pre_order){
// XXX get the correct parent... // XXX get the correct parent...
var o = overlay.Overlay(that.ribbons.viewer, var o = overlay.Overlay(that.ribbons.viewer,
list(null, actions, path) list(null, actions, {
path: path,
// load show disabled state from .config...
showDisabled: that.config['browse-actions-show-disabled'] || false,
})
.open(function(evt){ .open(function(evt){
if(!closingPrevented){ if(!closingPrevented){
o.close() o.close()
} }
closingPrevented = false closingPrevented = false
})) }))
// save show disabled state to .config...
.close(function(){
that.config['browse-actions-show-disabled'] = o.client.options.showDisabled
})
// XXX DEBUG // XXX DEBUG
//window.LIST = o.client //window.LIST = o.client
@ -2883,6 +3083,8 @@ var ActionTreeActions = actions.Actions({
'Edit/', 'Edit/',
'Navigate/', 'Navigate/',
], ],
'browse-actions-show-disabled': false,
}, },
// XXX move this to a generic modal overlay feature... // XXX move this to a generic modal overlay feature...
@ -4100,17 +4302,17 @@ var URLHistoryLocalStorageActions = actions.Actions({
localStorage[loaded] = this.base_path localStorage[loaded] = this.base_path
} }
}], }],
loadLastSavedBasePath: ['- History/', loadLastSavedBasePath: ['- History/',
function(){ function(){
var loaded = this.config['url-history-loaded-local-storage-key'] var loaded = this.config['url-history-loaded-local-storage-key']
if(loaded && localStorage[loaded]){ if(loaded && localStorage[loaded]){
this.openURLFromHistory(localStorage[loaded]) this.openURLFromHistory(localStorage[loaded])
} else { } else {
this.openURLFromHistory(0) this.openURLFromHistory(0)
} }
}] }]
}) })
var URLHistoryLocalStorage = var URLHistoryLocalStorage =
@ -4128,6 +4330,11 @@ module.URLHistoryLocalStorage = ImageGridFeatures.Feature({
// NOTE: loading is done by the .url_history prop... // NOTE: loading is done by the .url_history prop...
handlers: [ handlers: [
['start',
function(){ this.loadLastSavedBasePath() }],
['stop',
function(){ this.saveURLHistory() }],
// save base_path... // save base_path...
['load loadURLs', ['load loadURLs',
function(){ this.base_path && this.saveBasePath() }], function(){ this.base_path && this.saveBasePath() }],
@ -4731,6 +4938,7 @@ module.FileSystemWriterUI = ImageGridFeatures.Feature({
// //
ImageGridFeatures.Feature('viewer-testing', [ ImageGridFeatures.Feature('viewer-testing', [
'lifecycle',
'base', 'base',
'ui', 'ui',
@ -4751,7 +4959,10 @@ ImageGridFeatures.Feature('viewer-testing', [
'image-marks', 'image-marks',
'image-bookmarks', 'image-bookmarks',
// local storage...
'config-local-storage',
'url-history-local-storage', 'url-history-local-storage',
'ui-single-image-view-local-storage',
'fs-loader', 'fs-loader',
'ui-fs-loader', 'ui-fs-loader',