widgets: use list of child instances instead of hashes; let 'widget-removed' traverse the widget structure

This commit is contained in:
jean-emmanuel 2018-10-14 11:01:42 +02:00
parent 9fe74e3983
commit 94da9103e6
11 changed files with 91 additions and 81 deletions

View file

@ -9,17 +9,17 @@ var updateWidget = function(widget, options = {}) {
// save state
var sidepanel = DOM.get('#sidepanel')[0],
scroll = sidepanel.scrollTop,
oldWidgets = widget.childrenHashes.concat(widget.hash),
oldWidgets = widget.getAllChildren().concat(widget),
wasSelected = editor.selectedWidgets.includes(widget),
wScroll = {}
stateManager.incrementQueue()
for (let h of oldWidgets) {
if (widgetManager.widgets[h]) {
let id = widgetManager.widgets[h].getProp('id'),
value = widgetManager.widgets[h].getValue(),
valueProp = widgetManager.widgets[h].getProp('value')
for (let widget of oldWidgets) {
if (widgetManager.widgets[widget.hash]) {
let id = widget.getProp('id'),
value = widget.getValue(),
valueProp = widget.getProp('value')
stateManager.pushValueState(id, value)
if (valueProp !== '' && valueProp !== undefined) stateManager.pushValueOldProp(id, valueProp)

View file

@ -132,13 +132,7 @@ var Editor = class Editor {
var curWidget = this.selectedWidgets[0],
toSelect
if (curWidget.parent !== widgetManager) {
toSelect = curWidget.parent.childrenHashes
.map(h=>widgetManager.widgets[h])
.filter(w=>w && w.parent === curWidget.parent && w !== curWidget)
toSelect.unshift(curWidget)
}
if (toSelect) {
this.select(toSelect)
this.select(curWidget.parent.children)
}
})
keyboardJS.bind(['mod + up','mod + down','mod + left' , 'mod + right'], (e)=>{
@ -151,9 +145,7 @@ var Editor = class Editor {
toSelect = curWidget.parent
}
else if(e.key == 'ArrowDown' ){
const toSelectList = curWidget.childrenHashes
.map(h=>widgetManager.widgets[h])
.filter(w=>w && w.parent==curWidget)
const toSelectList = curWidget.children
if(toSelectList && toSelectList.length){
toSelectList.sort((a,b)=>a.container.offsetLeft>b.container.offsetLeft)
@ -163,12 +155,10 @@ var Editor = class Editor {
}
else if((e.key == 'ArrowLeft') || (e.key == 'ArrowRight')){
if (curWidget.parent === widgetManager) return
const toSelectList = curWidget.parent.childrenHashes
.map(h=>widgetManager.widgets[h])
.filter(w=>w && w.parent==curWidget.parent)
const toSelectList = curWidget.parent.children
if(toSelectList && toSelectList.length){
toSelectList.sort((a,b)=>a.container.offsetLeft>b.container.offsetLeft)
const idx = toSelectList.findIndex(e=>e.hash===curWidget.hash)
const idx = toSelectList.indexOf(curWidget)
if(idx>=0){
const nextIdx = (idx + (e.key == 'ArrowLeft'?-1:1)+toSelectList.length) % toSelectList.length
toSelect = toSelectList[nextIdx]

View file

@ -157,9 +157,9 @@ var WidgetManager = class WidgetManager extends EventEmitter {
}
removeWidget(hash) {
removeWidget(widget) {
var widget = this.widgets[hash],
var hash = widget.hash,
address = this.createAddressRef(widget),
linkId = widget.getProp('linkId'),
id = widget.getProp('id'),
@ -174,7 +174,7 @@ var WidgetManager = class WidgetManager extends EventEmitter {
hash:hash
})
this.trigger('widget-removed.*', [{widget: widget}])
widget.trigger('widget-removed.*', [{widget: widget}])
}
if (id && this.idRoute[id].indexOf(hash) != -1) this.idRoute[id].splice(this.idRoute[id].indexOf(hash), 1)
@ -188,14 +188,14 @@ var WidgetManager = class WidgetManager extends EventEmitter {
}
removeWidgets(hashes) {
removeWidgets(widgets) {
for (let i in hashes) {
for (let i in widgets) {
if (
this.widgets[hashes[i]]
this.widgets[widgets[i].hash]
) {
this.removeWidget(hashes[i])
this.removeWidget(widgets[i])
}
}
@ -227,7 +227,7 @@ var WidgetManager = class WidgetManager extends EventEmitter {
reset() {
this.removeWidgets(Object.keys(this.widgets))
this.removeWidgets(Object.values(this.widgets))
this.removeEvent()

View file

@ -0,0 +1,39 @@
var Widget = require('./widget'),
widgetManager = require('../../managers/widgets')
class Container extends Widget {
constructor(options) {
super(options)
this.on(`widget-created.${this.hash}`, (e)=>{
if (e.widget.parent === this) {
this.children.push(e.widget)
}
})
this.on(`widget-removed.${this.hash}`, (e)=>{
if (e.widget.parent === this) {
this.children.splice(this.children.indexOf(e.widget), 1)
}
})
}
onRemove() {
widgetManager.off(`widget-removed.${this.hash}`)
super.onRemove()
}
}
module.exports = Container

View file

@ -138,7 +138,7 @@ class Widget extends EventEmitter {
this.parent = options.root ? widgetManager : options.parent
this.parentNode = options.parentNode
this.hash = nanoid('abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10)
this.childrenHashes = []
this.children = []
this.reCreateOptions = options.reCreateOptions
// strip parent ? no position
@ -217,14 +217,6 @@ class Widget extends EventEmitter {
this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0))
}
this.on('widget-created', (e)=>{
if (e.widget == this) return
this.childrenHashes.push(e.widget.hash)
})
if (options.container) {
this.container = DOM.create(`
@ -243,15 +235,27 @@ class Widget extends EventEmitter {
contains(widget) {
if (this.childrenHashes.includes(widget.hash)) {
return true
}
if (this.children.indexOf(widget) > 0) return true
var parent = widget.parent
while (parent && parent !== widgetManager) {
if (parent === this) return true
parent = parent.parent
}
return false
}
getAllChildren() {
var children = []
for (var i = 0; i < this.children.length; i++) {
children.push(this.children[i])
children = children.concat(this.children[i].getAllChildren())
}
return children
}
created() {
@ -535,7 +539,7 @@ class Widget extends EventEmitter {
}
}
if (reCreate && this.childrenHashes.indexOf(widget.hash) == -1 && !(widget === this && updatedProps.length === 1 && updatedProps[0] === 'value')) {
if (reCreate && !this.contains(widget) && widget !== this && !(widget === this && updatedProps.length === 1 && updatedProps[0] === 'value')) {
this.reCreateWidget(options)
return true

View file

@ -1,4 +1,4 @@
var Widget = require('../common/widget'),
var Container = require('../common/container'),
widgetManager = require('../../managers/widgets'),
resize = require('../../events/resize'),
parser = require('../../parser'),
@ -6,7 +6,7 @@ var Widget = require('../common/widget'),
var excludedCloneClasses = ['widget', 'absolute-position', 'ui-resizable', 'ui-draggable', 'not-editable']
class Clone extends Widget {
class Clone extends Container {
static defaults() {

View file

@ -1,8 +1,8 @@
var Widget = require('../common/widget'),
var Container = require('../common/container'),
widgetManager = require('../../managers/widgets'),
parser = require('../../parser')
class Panel extends Widget {
class Panel extends Container {
static defaults() {
@ -146,13 +146,8 @@ class Panel extends Widget {
switch (propName) {
case 'color':
for (var i = this.childrenHashes.length - 1; i >= 0; i--) {
let widget = widgetManager.widgets[this.childrenHashes[i]]
if (!widget) {
this.childrenHashes.splice(i, 1)
continue
}
widget.onPropChanged('color')
for (var w of this.children) {
w.onPropChanged('color')
}
return

View file

@ -1,8 +1,8 @@
var Widget = require('../common/widget'),
var Container = require('../common/container'),
{enableTraversingGestures} = require('../../events/drag')
module.exports = class _matrix_base extends Widget {
module.exports = class _matrix_base extends Container {
constructor(options) {

View file

@ -74,38 +74,22 @@ class Matrix extends _matrix_base {
case 'props':
var hashes = [...this.childrenHashes]
this.childrenHashes = []
var children = [...this.children]
for (let i = hashes.length - 1; i >= 0; i--) {
for (let i = children.length - 1; i >= 0; i--) {
let widget = widgetManager.widgets[hashes[i]],
let widget = children[i],
data = this.resolveProp('props', undefined, false, false, false, {'$':i})
if (!widget) {
hashes.splice(i, 1)
continue
}
Object.assign(widget.props, data)
widget.updateProps(Object.keys(data), this)
}
if (!this.childrenHashes.length) {
// only dynamic props updated
this.childrenHashes = hashes
} else {
// widgets recreated
for (let i = this.childrenHashes.length - 1; i >= 0; i--) {
let widget = widgetManager.widgets[this.childrenHashes[i]]
if (!widget) {
this.childrenHashes.splice(i, 1)
continue
}
widget.container.classList.add('not-editable')
}
for (let i = this.children - 1; i >= 0; i--) {
let widget = children[i]
widget.container.classList.add('not-editable')
}
return

View file

@ -100,10 +100,8 @@ module.exports = class Multifader extends _matrix_base {
switch (propName) {
case 'color':
for (var h of this.childrenHashes) {
if (widgetManager.widgets[h]) {
widgetManager.widgets[h].onPropChanged('color')
}
for (var w of this.children) {
w.onPropChanged('color')
}
return

View file

@ -45,7 +45,7 @@ module.exports = class _switchers_base extends Widget {
for (var i in widgets) {
if (widgets[i].childrenHashes.indexOf(widget.hash) != -1) return true
if (widgets[i].contains(widget) || widgets[i] === widget) return true
}