some refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2019-06-23 19:56:07 +03:00
parent 893741b98f
commit 1f6ac64bae

View File

@ -265,7 +265,17 @@ Items.EditablePinnedList = function(values){}
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Item... // Item...
var ItemPrototype = { var BaseItemClassPrototype = {
text: function(elem){
var txt = elem.value instanceof Array ?
elem.value.join(' ')
: elem.value == null || elem.value instanceof Object ?
elem.alt || elem.__id
: elem.value
return txt },
}
var BaseItemPrototype = {
parent: null, parent: null,
// children: null, // children: null,
@ -274,6 +284,8 @@ var ItemPrototype = {
// value: null, // value: null,
// alt: null, // alt: null,
// //
// dom: null,
//
// focused: null, // focused: null,
// disabled: null, // disabled: null,
// selected: null, // selected: null,
@ -284,28 +296,18 @@ var ItemPrototype = {
set id(value){ set id(value){
this.__id = value }, this.__id = value },
// XXX can value be a function or as a list contain a function???
// ...if so, to resolve it we'll need a context other than the item...
// XXX should we remove '$' here or in an extension???
get text(){ get text(){
var txt = this.value instanceof Array ? return this.constructor.text(this) },
this.value.join(' ')
: this.value == null || this.value instanceof Object ?
this.alt || this.__id
: this.value
return txt != null ?
(txt + '')
.replace(/\$(.)/g, '$1')
: txt },
__init__(...state){ __init__(...state){
Object.assign(this, ...state) }, Object.assign(this, ...state) },
} }
var BaseItem =
var Item = module.BaseItem =
module.Item = object.makeConstructor('BaseItem',
object.makeConstructor('Item', ItemPrototype) BaseItemClassPrototype,
BaseItemPrototype)
@ -1040,6 +1042,7 @@ var BaseBrowserPrototype = {
__list__: function(make, options){ __list__: function(make, options){
throw new Error('.__list__(..): Not implemented.') }, throw new Error('.__list__(..): Not implemented.') },
__item__: BaseItem,
// Make extension... // Make extension...
// //
@ -1126,7 +1129,7 @@ var BaseBrowserPrototype = {
// XXX not sure if this is the right way to go... // XXX not sure if this is the right way to go...
// ...for removal just remove the if statement and its // ...for removal just remove the if statement and its
// first branch... // first branch...
if(value instanceof Browser){ if(value instanceof BaseBrowser){
var item = value var item = value
item.parent = this item.parent = this
@ -1167,14 +1170,14 @@ var BaseBrowserPrototype = {
+`can't create multiple items with the same id.`) } +`can't create multiple items with the same id.`) }
// build the item... // build the item...
var item = new Item( var item = new this.__item__(
// XXX do we need this??? // XXX do we need this???
//options || {}, //options || {},
opts, opts,
{ parent: this }) { parent: this })
// XXX do we need both this and the above ref??? // XXX do we need both this and the above ref???
item.children instanceof Browser item.children instanceof BaseBrowser
&& (item.children.parent = this) && (item.children.parent = this)
} }
@ -3038,23 +3041,48 @@ module.KEYBOARD_CONFIG = {
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Item...
var HTMLItemClassPrototype = {
__proto__: BaseItem,
text: function(elem){
var txt = object.parent(HTMLItem.text, this).call(this, elem)
return txt != null ?
(txt + '')
.replace(/\$(.)/g, '$1')
: txt },
elem: function(elem){
elem = elem.dom || elem
return elem.classList.contains('list') ?
elem.querySelector('.item')
: elem },
}
var HTMLItemPrototype = {
__proto__: BaseItem.prototype,
get elem(){
return this.constructor.elem(this) },
}
var HTMLItem =
module.HTMLItem =
object.makeConstructor('HTMLItem',
HTMLItemClassPrototype,
HTMLItemPrototype)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Helpers... // Helpers...
// Get actual .item DOM element...
//
// XXX should this be a prop in the element???
var getElem = function(elem){
elem = elem.dom || elem
return elem.classList.contains('list') ?
elem.querySelector('.item')
: elem }
// Scrolling / offset... // Scrolling / offset...
// //
var scrollOffset = function(browser, direction, elem){ var scrollOffset = function(browser, direction, elem){
var elem = getElem(elem || browser.focused) var elem = (elem || browser.focused).elem
var lst = browser.dom.querySelector('.list') var lst = browser.dom.querySelector('.list')
return direction == 'top' ? return direction == 'top' ?
elem.offsetTop - lst.scrollTop elem.offsetTop - lst.scrollTop
@ -3087,7 +3115,7 @@ var focusItem = function(direction){
return function(){ return function(){
var name = direction == 'up' ? 'prev' : 'next' var name = direction == 'up' ? 'prev' : 'next'
object.parent(BrowserPrototype[name], name, this).call(this, ...arguments) object.parent(HTMLBrowserPrototype[name], name, this).call(this, ...arguments)
var threashold = this.options.focusOffsetWhileScrolling || 0 var threashold = this.options.focusOffsetWhileScrolling || 0
@ -3148,7 +3176,7 @@ var focusPage = function(direction){
var updateElemClass = function(action, cls, handler){ var updateElemClass = function(action, cls, handler){
return function(evt, elem, ...args){ return function(evt, elem, ...args){
elem elem
&& getElem(elem).classList[action](cls) && elem.elem.classList[action](cls)
return handler return handler
&& handler.call(this, evt, elem, ...args)} } && handler.call(this, evt, elem, ...args)} }
@ -3156,7 +3184,7 @@ var updateElemClass = function(action, cls, handler){
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
var BrowserClassPrototype = { var HTMLBrowserClassPrototype = {
__proto__: BaseBrowser, __proto__: BaseBrowser,
} }
@ -3165,8 +3193,9 @@ var BrowserClassPrototype = {
// XXX need a strategy to update the DOM -- i.e. add/remove nodes for // XXX need a strategy to update the DOM -- i.e. add/remove nodes for
// partial rendering instead of full DOM replacement... // partial rendering instead of full DOM replacement...
// XXX add a left button type/option -- expand/collapse and friends... // XXX add a left button type/option -- expand/collapse and friends...
var BrowserPrototype = { var HTMLBrowserPrototype = {
__proto__: BaseBrowser.prototype, __proto__: BaseBrowser.prototype,
__item__: HTMLItem,
options: { options: {
@ -3396,7 +3425,7 @@ var BrowserPrototype = {
: 0 : 0
return this.search(true, return this.search(true,
function(e, i, p, stop){ function(e, i, p, stop){
var edom = getElem(e) var edom = e.elem
// first below upper border... // first below upper border...
pos == 'top' pos == 'top'
&& Math.round(edom.offsetTop && Math.round(edom.offsetTop
@ -3421,10 +3450,10 @@ var BrowserPrototype = {
pattern = arguments[0] = pattern = arguments[0] =
// DOM element... // DOM element...
pattern instanceof HTMLElement ? pattern instanceof HTMLElement ?
function(e){ return e.dom === p || getElem(e) === p } function(e){ return e.elem === p || e.elem === p }
// jQuery object... // jQuery object...
: (typeof(jQuery) != 'undefined' && pattern instanceof jQuery) ? : (typeof(jQuery) != 'undefined' && pattern instanceof jQuery) ?
function(e){ return p.is(e.dom) || p.is(getElem(e)) } function(e){ return p.is(e.dom) || p.is(e.elem) }
// pagetop + offset... // pagetop + offset...
: pattern == 'pagetop' ? : pattern == 'pagetop' ?
getAtPagePosition('top', getAtPagePosition('top',
@ -3439,7 +3468,7 @@ var BrowserPrototype = {
: pattern : pattern
// call parent... // call parent...
return object.parent(BrowserPrototype.search, this).call(this, pattern, ...args) }, return object.parent(HTMLBrowserPrototype.search, this).call(this, pattern, ...args) },
// //
// Extended .get(..) to support: // Extended .get(..) to support:
// - 'pagetop'/'pagebottom' + offset... // - 'pagetop'/'pagebottom' + offset...
@ -3466,7 +3495,7 @@ var BrowserPrototype = {
stop(func ? stop(func ?
func.call(this, e, i, p) func.call(this, e, i, p)
: e) }, ...args) : e) }, ...args)
: object.parent(BrowserPrototype.get, this).call(this, pattern, func, ...args) }, : object.parent(HTMLBrowserPrototype.get, this).call(this, pattern, func, ...args) },
// Copy/Paste support... // Copy/Paste support...
@ -3553,7 +3582,7 @@ var BrowserPrototype = {
d.addEventListener('focus', d.addEventListener('focus',
function(){ function(){
that.focused that.focused
&& getElem(that.focused).focus() }) && that.focused.elem.focus() })
//*/ //*/
// XXX should this be done here or in .render(..)??? // XXX should this be done here or in .render(..)???
@ -3562,7 +3591,7 @@ var BrowserPrototype = {
// keep focus where it is... // keep focus where it is...
var focused = this.focused var focused = this.focused
focused focused
&& getElem(focused) && focused.elem
// XXX this will trigger the focus event... // XXX this will trigger the focus event...
// ...can we do this without triggering new events??? // ...can we do this without triggering new events???
.focus() .focus()
@ -3982,7 +4011,7 @@ var BrowserPrototype = {
/* XXX sort out .dom updates... /* XXX sort out .dom updates...
render: function(...args){ render: function(...args){
var res = object.parent(BrowserPrototype.render, this).call(this, ...args) var res = object.parent(HTMLBrowserPrototype.render, this).call(this, ...args)
// XXX set .dom... // XXX set .dom...
// ...need support for item lists... // ...need support for item lists...
@ -4050,7 +4079,7 @@ var BrowserPrototype = {
__focus__: function(evt, elem){ __focus__: function(evt, elem){
var that = this var that = this
elem elem
&& getElem(elem) && elem.elem
// update the focused CSS class... // update the focused CSS class...
// NOTE: we will not remove this class on blur as it keeps // NOTE: we will not remove this class on blur as it keeps
// the selected element indicated... // the selected element indicated...
@ -4067,7 +4096,7 @@ var BrowserPrototype = {
__blur__: function(evt, elem){ __blur__: function(evt, elem){
var that = this var that = this
elem elem
&& getElem(elem) && elem.elem
.run(function(){ .run(function(){
this.classList.remove('focused') this.classList.remove('focused')
// refocus the dialog... // refocus the dialog...
@ -4106,7 +4135,7 @@ var BrowserPrototype = {
scrollTo: function(pattern, position){ scrollTo: function(pattern, position){
var target = this.get(pattern) var target = this.get(pattern)
target target
&& getElem(target) && target.elem
.scrollIntoView({ .scrollIntoView({
behavior: (this.options || {}).scrollBehavior || 'auto', behavior: (this.options || {}).scrollBehavior || 'auto',
block: position || 'center', block: position || 'center',
@ -4157,11 +4186,18 @@ var BrowserPrototype = {
// XXX should this be a Widget too??? // XXX should this be a Widget too???
var Browser = var HTMLBrowser =
module.Browser = module.HTMLBrowser =
object.makeConstructor('Browser', object.makeConstructor('HTMLBrowser',
BrowserClassPrototype, HTMLBrowserClassPrototype,
BrowserPrototype) HTMLBrowserPrototype)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// shorthans...
module.Item = HTMLItem
module.Browser = HTMLBrowser