From 74b538c768a1d8e15660308ecce29af49ba11734 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 5 Dec 2015 04:38:15 +0300 Subject: [PATCH] changed action semantics to acomodate data threading through action call chains... Signed-off-by: Alex A. Naanou --- ui (gen4)/lib/actions.js | 45 ++++++++++++++++++++++++++++++++-------- ui (gen4)/viewer.js | 13 +++++++++--- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/ui (gen4)/lib/actions.js b/ui (gen4)/lib/actions.js index 9fe77b64..62b17ab8 100755 --- a/ui (gen4)/lib/actions.js +++ b/ui (gen4)/lib/actions.js @@ -51,6 +51,10 @@ define(function(require){ var module = {} // handlers in its inheritance chain will be called before the // respective actions they are bound to and all the post handlers // are called directly after. +// - pre handlers are passed the same arguments the original actions +// got when it was called. +// - post action handlers will get the root action result as first +// argument succeeded by the action arguments. // // // @@ -117,23 +121,23 @@ if(typeof(args2array) != 'function'){ // Action function format: // // // pre event code... -// function(){ +// function(..){ // ... // pre code // } // // // pre/post event code... -// function(){ +// function(..){ // ... // pre code -// return function(){ +// return function(, ..){ // ... // post code // } // } // // // same as above but using a deferred instead of a callback... -// function(){ +// function(..){ // ... // pre code // return $.Deferred() -// .done(function(){ +// .done(function(, ..){ // ... // post code // }) // } @@ -146,6 +150,9 @@ if(typeof(args2array) != 'function'){ // event is fired // post: if the action returns a callback function or a deferred // object it will be executed after the event is fired +// NOTE: the signature if the post stage is the same as the +// action's with the added return value as first argument +// (the rest og the arguments are shifted by 1). // // - actions automatically call the shadowed action, the pre stage is // executed down-up while the post stage is run in reverse order, @@ -161,6 +168,10 @@ if(typeof(args2array) != 'function'){ // - an action will return the deepest (root) action's return, if that // return is undefined, then the action set is returned instead. // +// - action arguments are "threaded" through the action chain down and +// root action return value and arguments are threaded back up the +// action chain. +// // NOTE: if the root handler is instance of Toggler (jli) and the action // is called with '?' as argument, then the toggler will be called // with the argument and return the result bypassing the handlers. @@ -235,17 +246,27 @@ function Action(name, doc, ldoc, func){ // call handlers -- post phase... // NOTE: post handlers need to get called last run pre first run post... - var results = handlers.reverse().map(function(h, i){ + var results = [] + handlers.reverse().forEach(function(h, i){ + var res = h // function... if(h instanceof Function){ - return h.apply(that, args) + //res = h.apply(that, args) + res = h.apply(that, + [results[0] !== undefined ? + reults[0] + : that].concat(args)) // deferred... } else if(h != null && h.resolve instanceof Function){ - return h.resolve() + //res = h.resolve() + res = h.resolve.apply(h, + [results[0] !== undefined ? + results[0] + : that].concat(args)) } - return h + results.push(res) }) // XXX might be a good idea to add an option to return the full @@ -402,6 +423,12 @@ module.MetaActions = { // 'post' - the handler is fired after the action is finished. // this is the default. // + // Handler Arguments: + // 'pre' - the handler will get the same arguments as the main + // action when called. + // 'post' - the handler will get the action return value followed + // by action arguments. + // // The optional tag marks the handler to enable group removal via // .off(..) // diff --git a/ui (gen4)/viewer.js b/ui (gen4)/viewer.js index 5747f6cb..c1bc4f9b 100755 --- a/ui (gen4)/viewer.js +++ b/ui (gen4)/viewer.js @@ -276,10 +276,15 @@ actions.Actions({ json: ['File/Dump state as JSON object', 'This will collect JSON data from every available attribute ' +'supporting the .dumpJSON() method.', - function(){ + function(mode){ var res = {} for(var k in this){ - if(this[k] != null && this[k].dumpJSON != null){ + // dump the base crop state... + if(k == 'data' && this.crop_stack && this.crop_stack.length > 0){ + res[k] = this.crop_stack[0].dumpJSON() + + // dump current state... + } else if(this[k] != null && this[k].dumpJSON != null){ res[k] = this[k].dumpJSON() } } @@ -668,6 +673,8 @@ actions.Actions({ // crop... // + // XXX should we keep this isolated or should we connect stuff like + // tags, ... crop: ['Crop/Crop image list', function(list, flatten){ list = list || this.data.order @@ -1856,7 +1863,7 @@ module.PartialRibbons = ImageGridFeatures.Feature({ this.updateRibbon(target) }], ['focusImage.post', - function(target){ + function(res, target){ this.preCacheJumpTargets(target) }], ['fitImage.pre',