added event default action + some tweaking....

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-06-01 00:39:29 +03:00
parent 556533ae7a
commit 1b46c45918

View File

@ -252,7 +252,8 @@ function(event, func){
// //
// Make and event method... // Make and event method...
// makeEventMethod(event_name) // makeEventMethod(event_name)
// makeEventMethod(event_name, handler[, options]) // makeEventMethod(event_name, handler[, retrigger])
// makeEventMethod(event_name, handler, action[, retrigger])
// -> event_method // -> event_method
// //
// This will produce an event method that supports binding handlers to the // This will produce an event method that supports binding handlers to the
@ -270,8 +271,12 @@ function(event, func){
// //
var makeEventMethod = var makeEventMethod =
module.makeEventMethod = module.makeEventMethod =
function(event, handler, retrigger){ function(event, handler, action, retrigger){
retrigger = retrigger !== false var args = [...arguments].slice(2)
action = (args[0] !== true && args[0] !== false) ?
args.shift()
: null
retrigger = args.pop() !== false
return eventMethod(event, function(item){ return eventMethod(event, function(item){
// register handler... // register handler...
@ -281,12 +286,20 @@ function(event, handler, retrigger){
var evt = new BrowserEvent(event) var evt = new BrowserEvent(event)
// main handler...
handler handler
&& handler.call(this, evt, ...arguments) && handler.call(this, evt, ...arguments)
return retrigger ? // trigger the bound handlers...
this.trigger(evt, ...arguments) retrigger
: this && this.trigger(evt, ...arguments)
// default action...
action
&& !evt.defaultPrevented
&& action.call(this, evt, ...arguments)
return this
}) } }) }
@ -359,9 +372,9 @@ function(item, event, evt, ...args){
// up the tree. // up the tree.
var makeItemEventMethod = var makeItemEventMethod =
module.makeItemEventMethod = module.makeItemEventMethod =
function(event, handler, default_item, filter, options){ function(event, handler, action, default_item, filter, options){
// parse args... // parse args...
var args = [...arguments].slice(2) var args = [...arguments].slice(3)
default_item = args[0] instanceof Function default_item = args[0] instanceof Function
&& args.shift() && args.shift()
filter = args[0] instanceof Function filter = args[0] instanceof Function
@ -397,6 +410,7 @@ function(event, handler, default_item, filter, options){
// this will isolate each chain from the others in // this will isolate each chain from the others in
// state and handling propagation... // state and handling propagation...
callItemEventHandlers(item, event, null, ...args) }) }, callItemEventHandlers(item, event, null, ...args) }) },
...(action ? [action] : []),
false) false)
return Object.assign( return Object.assign(
// the actual method we return... // the actual method we return...
@ -1427,11 +1441,22 @@ var BaseBrowserPrototype = {
// -> false // -> false
// //
// //
// XXX add support for 'next'/'prev', ... keywords... (here or in .get(..)???) // NOTE: search is self-applicable, e.g.
// x.search(x.search(..), {noQueryCheck: true})
// should yield the same result as:
// x.search(..)
// this is very fast as we shortcut by simply checking of an
// item exists...
// NOTE: if .search(..) is passed a list of items (e.g. a result of
// another .search(..)) it will return the items that are in
// .index as-is regardless of what is set in options...
// given options in this case will be applied only to list items
// that are searched i.e. the non-items in the input list...
//
// XXX can .search(..) of a non-path array as a pattern be done in
// a single pass???
// XXX add support for fuzzy match search -- match substring by default // XXX add support for fuzzy match search -- match substring by default
// and exact title if using quotes... // and exact title if using quotes...
// XXX do we actually need to stop this as soon as we find something,
// i.e. options.firstOnly???
// XXX add diff support... // XXX add diff support...
// XXX should this check hidden items when doing an identity check??? // XXX should this check hidden items when doing an identity check???
__search_test_generators__: { __search_test_generators__: {
@ -1523,9 +1548,27 @@ var BaseBrowserPrototype = {
}, },
search: function(pattern, func, options){ search: function(pattern, func, options){
var that = this var that = this
var args = [...arguments]
// non-path array...
// NOTE: a non-path array is one where at least one element is
// an object...
// NOTE: this might get expensive as we call .search(..) per item...
if(pattern instanceof Array
&& !pattern
.reduce(function(r, e){
return r && typeof(e) != typeof({}) }, true)){
var index = new Set(Object.values(this.index))
return pattern
.map(function(pattern){
return index.has(pattern) ?
// item exists as-is...
pattern
: that.search(pattern, ...args.slice(1)) })
.flat()
.unique() }
// parse args... // parse args...
var args = [...arguments]
pattern = args.length == 0 ? pattern = args.length == 0 ?
true true
: args.shift() : args.shift()
@ -2317,6 +2360,7 @@ var BaseBrowserPrototype = {
item != null item != null
&& (item.focused = true) && (item.focused = true)
}, },
null,
function(){ return this.get(0) }, function(){ return this.get(0) },
{ {
getMode: 'get', getMode: 'get',
@ -2342,12 +2386,14 @@ var BaseBrowserPrototype = {
function(evt, items){ 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 }), function(){ return this.focused }),
deselect: makeItemEventMethod('deselect', deselect: makeItemEventMethod('deselect',
function(evt, items){ function(evt, items){
items.forEach(function(item){ items.forEach(function(item){
delete item.selected }) }, delete item.selected }) },
null,
function(){ return this.focused }), function(){ return this.focused }),
// XXX use a real toggler here??? (i.e. finish makeItemEventToggler2(..)) // XXX use a real toggler here??? (i.e. finish makeItemEventToggler2(..))
toggleSelect: makeItemEventToggler('selected', 'select', 'deselect', 'focused'), toggleSelect: makeItemEventToggler('selected', 'select', 'deselect', 'focused'),
@ -2359,6 +2405,7 @@ var BaseBrowserPrototype = {
item.forEach(function(e){ e.collapsed = true }) item.forEach(function(e){ e.collapsed = true })
this.update() this.update()
}, },
null,
function(){ return this.focused }, function(){ return this.focused },
function(elem){ return elem.value && elem.children }, function(elem){ return elem.value && elem.children },
{iterateCollapsed: true}), {iterateCollapsed: true}),
@ -2367,6 +2414,7 @@ var BaseBrowserPrototype = {
item.forEach(function(e){ delete e.collapsed }) item.forEach(function(e){ delete e.collapsed })
this.update() this.update()
}, },
null,
function(){ return this.focused }, function(){ return this.focused },
function(elem){ return elem.value && elem.children }, function(elem){ return elem.value && elem.children },
{iterateCollapsed: true}), {iterateCollapsed: true}),
@ -2378,15 +2426,17 @@ var BaseBrowserPrototype = {
{iterateCollapsed: true}), {iterateCollapsed: true}),
// primary/secondary item actions... // primary/secondary item actions...
// XXX revise default actions...
open: makeItemEventMethod('open', open: makeItemEventMethod('open',
// XXX if no open handlers defined trigger .launch(..)...
// ...the logic still needs refining...
// a different way of doing this is to trigger .launch(..)
// right away unless .preventDefault() was called...
function(evt, item){}, function(evt, item){},
// XXX not yet sure if this is correct...
function(evt, item){
item.length > 0
&& this.toggleCollapse(item) },
function(){ return this.focused }), function(){ return this.focused }),
launch: makeItemEventMethod('launch', launch: makeItemEventMethod('launch',
function(evt, item){}, function(evt, item){},
null,
function(){ return this.focused }), function(){ return this.focused }),
// Update state (make then render)... // Update state (make then render)...
@ -2405,7 +2455,8 @@ var BaseBrowserPrototype = {
// //
// XXX calling this on a nested browser should update the whole thing... // XXX calling this on a nested browser should update the whole thing...
// ...can we restore the context via .parent??? // ...can we restore the context via .parent???
update: makeEventMethod('update', function(evt, full, options){ update: makeEventMethod('update',
function(evt, full, options){
options = (full && full !== true && full !== false) ? options = (full && full !== true && full !== false) ?
full full
: options : options