more refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-06-15 18:43:26 +03:00
parent f200d24f56
commit 538216cfb9

View File

@ -328,9 +328,7 @@ function(item, event, evt, ...args){
// Generate item event method... // Generate item event method...
// //
// makeItemEventMethod(event_name) // makeItemEventMethod(event_name)
// makeItemEventMethod(event_name, handler[, options]) // makeItemEventMethod(event_name, {handler, default_getter, filter, options, get_mode})
// makeItemEventMethod(event_name, handler, default_getter[, options])
// makeItemEventMethod(event_name, handler, default_getter, filter[, options])
// -> event_method // -> event_method
// //
// //
@ -372,16 +370,10 @@ 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(..))... // XXX destructuring: move the defaults to the arguments...
var makeItemEventMethod = var makeItemEventMethod =
module.makeItemEventMethod = module.makeItemEventMethod =
function(event, handler, action, default_item, filter, options){ function(event, {handler, action, default_item, filter, options, get_mode}={}){
// parse args...
var args = [...arguments].slice(3)
default_item = args[0] instanceof Function
&& args.shift()
filter = args[0] instanceof Function
&& args.shift()
var filterItems = function(items){ var filterItems = function(items){
items = items instanceof Array ? items = items instanceof Array ?
items items
@ -391,14 +383,14 @@ function(event, handler, action, default_item, filter, options){
return filter ? return filter ?
items.filter(filter) items.filter(filter)
: items } : items }
options = args.shift() //options = args.shift()
options = Object.assign( options = Object.assign(
// NOTE: we need to be able to pass item objects, so we can not // NOTE: we need to be able to pass item objects, so we can not
// use queries at the same time as there is not way to // use queries at the same time as there is not way to
// distinguish one from the other... // distinguish one from the other...
{ noQueryCheck: true }, { noQueryCheck: true },
options || {}) options || {})
var getter = options.getMode || 'search' var getter = get_mode || 'search'
// base event method... // base event method...
// NOTE: this is not returned directly as we need to query the items // NOTE: this is not returned directly as we need to query the items
// and pass those on to the handlers rather than the arguments // and pass those on to the handlers rather than the arguments
@ -443,47 +435,50 @@ function(event, handler, action, default_item, filter, options){
base) } base) }
// XXX should these .update() // Make event method edit item...
var makeItemOptionOnEventMethod = //
module.makeItemOptionOnEventMethod = // XXX should this .update()
function(event, option, {handler, default_item, filter, options, update=true}={}){ // XXX destructuring: move the defaults to the arguments...
return makeItemEventMethod(event, var makeItemOptionEventMethod =
function(evt, items){ module.makeItemOptionEventMethod =
function(event, action, {handler, default_item, filter, options, update=true}={}){
return makeItemEventMethod(event, {
handler: function(evt, items){
var that = this var that = this
var change = false var change = false
items.forEach(function(item){ items.forEach(function(item){
change = item[option] = true change = action(item)
handler handler
&& handler.call(that, item) }) && handler.call(that, item) })
// need to update for changes to show up... // need to update for changes to show up...
change change
&& update && update
&& this.update() }, && this.update() },
null, default_item:
default_item default_item
|| function(){ return this.focused }, || function(){ return this.focused },
filter, filter,
options) } options, }) }
// Make event method to toggle item attr on/off...
//
// XXX destructuring: move the defaults to the arguments...
var makeItemOptionOnEventMethod =
module.makeItemOptionOnEventMethod =
function(event, attr, {handler, default_item, filter, options, update=true}={}){
return makeItemOptionEventMethod(event,
function(item){
return item[attr] = true },
{ handler, default_item, filter, options, update }) }
var makeItemOptionOffEventMethod = var makeItemOptionOffEventMethod =
module.makeItemOptionOffEventMethod = module.makeItemOptionOffEventMethod =
function(event, option, {handler, default_item, filter, options, update=true}={}){ function(event, attr, {handler, default_item, filter, options, update=true}={}){
return makeItemEventMethod(event, return makeItemOptionEventMethod(event,
function(evt, items){ function(item){
var change = false change = !!item[attr]
items.forEach(function(item){ delete item[attr]
change = change || item[option] return change },
delete item[option] { handler, default_item, filter, options, update }) }
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...
@ -2639,9 +2634,10 @@ var BaseBrowserPrototype = {
// NOTE: this will ignore disabled items. // NOTE: this will ignore disabled items.
// NOTE: .focus('next') / .focus('prev') will not wrap around the // NOTE: .focus('next') / .focus('prev') will not wrap around the
// first last elements... // first last elements...
// NOTE: if focus does not change this will trigger any handlers...
// NOTE: this will reveal the focused item... // NOTE: this will reveal the focused item...
focus: makeItemEventMethod('focus', focus: makeItemEventMethod('focus', {
function(evt, items){ handler: function(evt, items){
// blur .focused... // blur .focused...
this.focused this.focused
&& this.blur(this.focused) && this.blur(this.focused)
@ -2650,18 +2646,16 @@ var BaseBrowserPrototype = {
item != null item != null
&& this.reveal(item) && this.reveal(item)
&& (item.focused = true) }, && (item.focused = true) },
null, default_item: function(){ return this.get(0) },
function(){ return this.get(0) }, options: {
{ get_mode: 'get',
getMode: 'get',
skipDisabled: true, skipDisabled: true,
}), } }),
blur: makeItemEventMethod('blur', blur: makeItemEventMethod('blur', {
function(evt, items){ handler: function(evt, items){
items.forEach(function(item){ items.forEach(function(item){
delete item.focused }) }, delete item.focused }) },
null, default_item: function(){ return this.focused } }),
function(){ return this.focused }),
// NOTE: .next() / .prev() will wrap around the first/last elements... // NOTE: .next() / .prev() will wrap around the first/last elements...
next: function(){ next: function(){
this.focus('next').focused || this.focus('first') this.focus('next').focused || this.focus('first')
@ -2676,19 +2670,17 @@ var BaseBrowserPrototype = {
false), false),
// selection... // selection...
// XXX these should skip disabled... option??? // XXX these should skip disabled... option???
select: makeItemEventMethod('select', select: makeItemEventMethod('select', {
function(evt, items){ handler: function(evt, items){
items.forEach(function(item){ items.forEach(function(item){
item.selected = true }) }, item.selected = true }) },
null,
// XXX is this a good default??? // XXX is this a good default???
function(){ return this.focused }), default_item: function(){ return this.focused } }),
deselect: makeItemEventMethod('deselect', deselect: makeItemEventMethod('deselect', {
function(evt, items){ handler: function(evt, items){
items.forEach(function(item){ items.forEach(function(item){
delete item.selected }) }, delete item.selected }) },
null, default_item: function(){ return this.focused } }),
function(){ return this.focused }),
toggleSelect: makeItemEventToggler( toggleSelect: makeItemEventToggler(
'selected', 'selected',
'select', 'deselect', 'select', 'deselect',
@ -2725,17 +2717,14 @@ var BaseBrowserPrototype = {
'focused'), 'focused'),
// primary/secondary/ternary? item actions... // primary/secondary/ternary? item actions...
open: makeItemEventMethod('open', open: makeItemEventMethod('open', {
function(evt, item){},
// XXX not yet sure if this is correct... // XXX not yet sure if this is correct...
function(evt, item){ action: function(evt, item){
item.length > 0 item.length > 0
&& this.toggleCollapse(item) }, && this.toggleCollapse(item) },
function(){ return this.focused }), default_item: function(){ return this.focused } }),
launch: makeItemEventMethod('launch', launch: makeItemEventMethod('launch', {
function(evt, item){}, default_item: function(){ return this.focused } }),
null,
function(){ return this.focused }),
// Update state (make then render)... // Update state (make then render)...
// //