refactored out some redundency in event code... (not done yet)

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-06-15 18:11:22 +03:00
parent 4e5a6409bb
commit f200d24f56

View File

@ -372,6 +372,7 @@ function(item, event, evt, ...args){
// NOTE: item events do not directly trigger the original caller's handlers // NOTE: item events do not directly trigger the original caller's handlers
// those will get celled recursively when the events are propagated // those will get celled recursively when the events are propagated
// up the tree. // up the tree.
// XXX use destructuring (a-la makeItemOptionOnEventMethod(..) / makeItemOptionOffEventMethod(..))...
var makeItemEventMethod = var makeItemEventMethod =
module.makeItemEventMethod = module.makeItemEventMethod =
function(event, handler, action, default_item, filter, options){ function(event, handler, action, default_item, filter, options){
@ -442,6 +443,49 @@ function(event, handler, action, default_item, filter, options){
base) } base) }
// XXX should these .update()
var makeItemOptionOnEventMethod =
module.makeItemOptionOnEventMethod =
function(event, option, {handler, default_item, filter, options, update=true}={}){
return makeItemEventMethod(event,
function(evt, items){
var that = this
var change = false
items.forEach(function(item){
change = item[option] = true
handler
&& handler.call(that, item) })
// need to update for changes to show up...
change
&& update
&& this.update() },
null,
default_item
|| function(){ return this.focused },
filter,
options) }
var makeItemOptionOffEventMethod =
module.makeItemOptionOffEventMethod =
function(event, option, {handler, default_item, filter, options, update=true}={}){
return makeItemEventMethod(event,
function(evt, items){
var change = false
items.forEach(function(item){
change = change || item[option]
delete item[option]
handler
&& handler.call(that, item) })
// need to update for changes to show up...
change
&& update
&& this.update() },
null,
default_item
|| function(){ return this.focused },
filter,
options) }
// Generate item event/state toggler... // Generate item event/state toggler...
// //
// XXX should this make a toggler.Toggler??? // XXX should this make a toggler.Toggler???
@ -744,6 +788,20 @@ var BaseBrowserPrototype = {
.select(value) }, .select(value) },
// XXX should this be cached???
// XXX should this set .options???
// XXX need to normalizePath(..)
// ...array .value is not compliant with POLS
get path(){
return this.__items != null ?
this.get('focused',
function(e, i, p){ return p.join('/') })
// XXX do we use .options.path???
: (this.options || {}).path },
set path(value){
this.load(value) },
// Length... // Length...
// //
// visible only... // visible only...
@ -2616,8 +2674,8 @@ var BaseBrowserPrototype = {
'focus', 'blur', 'focus', 'blur',
function(){ return this.focused || 0 }, function(){ return this.focused || 0 },
false), false),
// selection...
// XXX these should skip disabled... // XXX these should skip disabled... option???
select: makeItemEventMethod('select', select: makeItemEventMethod('select',
function(evt, items){ function(evt, items){
items.forEach(function(item){ items.forEach(function(item){
@ -2631,67 +2689,40 @@ var BaseBrowserPrototype = {
delete item.selected }) }, delete item.selected }) },
null, null,
function(){ return this.focused }), function(){ return this.focused }),
// XXX use a real toggler here??? (i.e. finish makeItemEventToggler2(..)) toggleSelect: makeItemEventToggler(
toggleSelect: makeItemEventToggler('selected', 'select', 'deselect', 'focused'), 'selected',
'select', 'deselect',
// NOTE: .expand(..) / .collapse(..) ignore item.collapsed state... 'focused'),
collapse: makeItemEventMethod('collapse', // topology...
function(evt, item){ collapse: makeItemOptionOnEventMethod('expand', 'collapsed', {
item.forEach(function(e){ e.collapsed = true }) filter: function(elem){ return elem.value && elem.children },
this.update() options: {iterateCollapsed: true}, }),
}, expand: makeItemOptionOffEventMethod('expand', 'collapsed', {
null, filter: function(elem){ return elem.value && elem.children },
function(){ return this.focused }, options: {iterateCollapsed: true}, }),
function(elem){ return elem.value && elem.children },
{iterateCollapsed: true}),
expand: makeItemEventMethod('expand',
function(evt, item){
item.forEach(function(e){ delete e.collapsed })
this.update()
},
null,
function(){ return this.focused },
function(elem){ return elem.value && elem.children },
{iterateCollapsed: true}),
toggleCollapse: makeItemEventToggler( toggleCollapse: makeItemEventToggler(
'collapsed', 'collapsed',
'collapse', 'expand', 'collapse', 'expand',
'focused', 'focused',
function(elem){ return elem.value && elem.children }, function(elem){ return elem.value && elem.children },
{iterateCollapsed: true}), {iterateCollapsed: true}),
// item state events...
// XXX not sure about these... disable: makeItemOptionOnEventMethod('disable', 'disabled',
disable: makeItemEventMethod('disable', { handler: function(item){ this.blur(item) }, }),
function(evt, items){ enable: makeItemOptionOffEventMethod('enable', 'disabled',
var that = this { options: {skipDisabled: false}, }),
var change = false
items.forEach(function(item){
change = item.disabled = true
item.focused
&& that.blur(item)
})
// need to update for changes to show up...
change
&& this.update() },
null,
// XXX is this a good default???
function(){ return this.focused }),
enable: makeItemEventMethod('enable',
function(evt, items){
var change = false
items.forEach(function(item){
change = change || item.disabled
delete item.disabled })
// need to update for changes to show up...
change
&& this.update() },
null,
{ skipDisabled: false }),
toggleDisabled: makeItemEventToggler( toggleDisabled: makeItemEventToggler(
'disabled', 'disabled',
'disable', 'enable', 'disable', 'enable',
'focused', 'focused',
{ skipDisabled: false }), { skipDisabled: false }),
// visibility...
hide: makeItemOptionOnEventMethod('hide', 'hidden'),
show: makeItemOptionOffEventMethod('show', 'hidden'),
toggleHidden: makeItemEventToggler(
'hidden',
'hide', 'show',
'focused'),
// primary/secondary/ternary? item actions... // primary/secondary/ternary? item actions...
open: makeItemEventMethod('open', open: makeItemEventMethod('open',
@ -2737,6 +2768,11 @@ var BaseBrowserPrototype = {
// NOTE: if given a path that does not exist this will try and load // NOTE: if given a path that does not exist this will try and load
// the longest existing sub-path... // the longest existing sub-path...
// XXX should level drawing be a feature of the browser or the
// client (as-is in browser.js)???
// XXX would also need to pass the path to .make(..) and friends for
// compatibility...
// ...or set .options.path (and keep it up to date in the API)...
load: makeEventMethod('load', load: makeEventMethod('load',
function(evt, target){}, function(evt, target){},
function(evt, target){ function(evt, target){
@ -2833,7 +2869,7 @@ module.KEYBOARD_CONFIG = {
Enter: 'open', Enter: 'open',
Space: 'toggleSelect', Space: 'toggleSelect!',
ctrl_A: 'select!: "*"', ctrl_A: 'select!: "*"',
ctrl_D: 'deselect!: "*"', ctrl_D: 'deselect!: "*"',
ctrl_I: 'toggleSelect!: "*"', ctrl_I: 'toggleSelect!: "*"',
@ -3327,6 +3363,7 @@ var BrowserPrototype = {
.concat(item['class'] || item.cls || []) .concat(item['class'] || item.cls || [])
// special classes... // special classes...
.concat([ .concat([
'focused',
'selected', 'selected',
'disabled', 'disabled',
'hidden', 'hidden',