Merge pull request #392 from jean-emmanuel/clone-refactor-2
Clone refactor 2
This commit is contained in:
commit
a62faaa27b
2 changed files with 127 additions and 80 deletions
|
|
@ -550,7 +550,7 @@ class Widget extends EventEmitter {
|
|||
}
|
||||
if (reCreate && !this.contains(widget) && widget !== this && !(widget === this && updatedProps.length === 1 && updatedProps[0] === 'value')) {
|
||||
|
||||
this.reCreateWidget(options)
|
||||
this.reCreateWidget({reCreateOptions: options})
|
||||
return true
|
||||
|
||||
} else if (changedProps.length) {
|
||||
|
|
@ -714,9 +714,9 @@ class Widget extends EventEmitter {
|
|||
|
||||
}
|
||||
|
||||
reCreateWidget(options){
|
||||
reCreateWidget(options={}){
|
||||
|
||||
updateWidget(this, {reCreateOptions:options})
|
||||
updateWidget(this, options)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ var Container = require('../common/container'),
|
|||
widgetManager = require('../../managers/widgets'),
|
||||
resize = require('../../events/resize'),
|
||||
parser = require('../../parser'),
|
||||
{deepCopy} = require('../../utils')
|
||||
{deepCopy, deepEqual} = require('../../utils'),
|
||||
diff = require('jsondiffpatch')
|
||||
|
||||
var excludedCloneClasses = ['widget', 'absolute-position', 'ui-resizable', 'ui-draggable', 'not-editable']
|
||||
|
||||
|
|
@ -37,21 +38,30 @@ class Clone extends Container {
|
|||
|
||||
this.container.classList.add('empty')
|
||||
|
||||
this.getCloneTarget()
|
||||
if (this.cloneTarget) this.createClone()
|
||||
this.bindTarget(this.getCloneTarget())
|
||||
if (this.cloneTarget) this.createClone(true)
|
||||
|
||||
// global listenner to catch cloneTarget's creation if no target is locked
|
||||
widgetManager.on('widget-created', (e)=>{
|
||||
|
||||
if (this.cloneTarget === null) {
|
||||
var {id, widget} = e
|
||||
|
||||
var {id, widget} = e
|
||||
if (id === this.getProp('widgetId') && this.isValidCloneTarget(widget)) {
|
||||
|
||||
if (id === this.getProp('widgetId') && this.isValidCloneTarget(widget)) {
|
||||
var clone = this.children[0]
|
||||
|
||||
this.cloneTarget = widget
|
||||
this.unbindTarget()
|
||||
|
||||
if (!clone) {
|
||||
|
||||
this.updateContainer(false)
|
||||
this.bindTarget(widget)
|
||||
this.createClone()
|
||||
resize.check(this.container)
|
||||
|
||||
} else {
|
||||
|
||||
this.bindTarget(widget)
|
||||
this.updateClone()
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -69,10 +79,9 @@ class Clone extends Container {
|
|||
const widgets = widgetManager.getWidgetById(this.getProp('widgetId'))
|
||||
.filter(el=>this.isValidCloneTarget(el))
|
||||
|
||||
if (widgets.length === 0) return
|
||||
if (widgets.length === 0) return null
|
||||
if (widgets.length === 1) {
|
||||
this.cloneTarget = widgets[0]
|
||||
return
|
||||
return widgets[0]
|
||||
}
|
||||
|
||||
// when duplicating clones pointing to an object with a static id,
|
||||
|
|
@ -84,18 +93,20 @@ class Clone extends Container {
|
|||
return parent && parent.getProp('type') === 'clone'
|
||||
}
|
||||
|
||||
this.cloneTarget = widgets[0]
|
||||
var target = widgets[0]
|
||||
for (const w of widgets) {
|
||||
if (!isCreatedByAClone(w)) {
|
||||
this.cloneTarget = w
|
||||
target = w
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// we may want to remove this warning
|
||||
if (isCreatedByAClone(this.cloneTarget)) {
|
||||
console.warn('no deduped clone found : ',this.getProp('widgetId'))
|
||||
}
|
||||
// if (isCreatedByAClone(this.cloneTarget)) {
|
||||
// console.warn('no deduped clone found : ',this.getProp('widgetId'))
|
||||
// }
|
||||
|
||||
return target
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -105,69 +116,46 @@ class Clone extends Container {
|
|||
|
||||
}
|
||||
|
||||
cleanClone() {
|
||||
createClone(init) {
|
||||
|
||||
// don't clean if clone is already empty
|
||||
if (!this.children.length) return
|
||||
|
||||
//console.log('clear clone',this.getProp('id'))
|
||||
|
||||
// unregister cloned content
|
||||
widgetManager.removeWidgets(this.getAllChildren())
|
||||
|
||||
// clean inner html
|
||||
this.widget.innerHTML = ''
|
||||
this.container.classList.remove(...this.cloneClass)
|
||||
this.container.classList.add('clone-container', 'empty')
|
||||
this.cloneClass = []
|
||||
|
||||
// clear event listeners on target
|
||||
if (this.cloneTarget) {
|
||||
this.cloneTarget.removeEventContext(this)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
createClone() {
|
||||
|
||||
if (this.cloneLock) {
|
||||
//console.warn('clone locked',this.id)
|
||||
return
|
||||
}
|
||||
//console.log('create clone',this.getProp('id'))
|
||||
if (this.cloneLock) return
|
||||
|
||||
this.cloneLock = true
|
||||
|
||||
this.cleanClone()
|
||||
var data = {...deepCopy(this.cloneTarget.props), ...this.getProp('props')},
|
||||
clone = parser.parse({
|
||||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
|
||||
this.cloneClass = [...this.cloneTarget.container.classList].filter(i=>excludedCloneClasses.indexOf(i) === -1)
|
||||
this.container.classList.remove('empty')
|
||||
this.container.classList.add(...this.cloneClass)
|
||||
let clonedProps = deepCopy(this.cloneTarget.props)
|
||||
var clone = parser.parse({
|
||||
data: {...clonedProps, ...this.getProp('props')},
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
if (clone.getProp('id') === this.cloneTarget.getProp('id')) {
|
||||
widgetManager.trigger('change', [{
|
||||
widget: this.cloneTarget,
|
||||
id: this.cloneTarget.getProp('id'),
|
||||
linkId: this.cloneTarget.getProp('linkId'),
|
||||
options: {}
|
||||
}])
|
||||
}
|
||||
this.updateContainer(!init)
|
||||
|
||||
for (var w of this.getAllChildren()) {
|
||||
w.container.classList.add('not-editable')
|
||||
}
|
||||
this.cloneLock = false
|
||||
|
||||
}
|
||||
|
||||
unbindTarget() {
|
||||
|
||||
if (!this.cloneTarget) return
|
||||
|
||||
this.cloneTarget.removeEventContext(this)
|
||||
|
||||
this.cloneTarget = null
|
||||
|
||||
}
|
||||
|
||||
bindTarget(target) {
|
||||
|
||||
if (!target) return
|
||||
|
||||
this.cloneTarget = target
|
||||
|
||||
// listen for cloneTarget's deletion
|
||||
// if it is just edited, its recreation will be catched by the global 'widget-created' event handler
|
||||
this.cloneTarget.on('widget-removed', (e)=>{
|
||||
if (this.cloneTarget === e.widget) {
|
||||
this.cloneTarget = null
|
||||
this.cleanClone()
|
||||
this.unbindTarget()
|
||||
}
|
||||
}, {context: this})
|
||||
|
||||
|
|
@ -184,15 +172,77 @@ class Clone extends Container {
|
|||
parent = parent.parent
|
||||
}
|
||||
|
||||
this.createClone()
|
||||
resize.check(this.container)
|
||||
this.updateClone()
|
||||
|
||||
}, {context: this})
|
||||
|
||||
}
|
||||
|
||||
updateClone() {
|
||||
|
||||
if (this.cloneLock) return
|
||||
|
||||
this.cloneLock = true
|
||||
|
||||
var data = {...deepCopy(this.cloneTarget.props), ...this.getProp('props')},
|
||||
clone = this.children[0],
|
||||
childrenChanged = false
|
||||
|
||||
var delta = diff.diff(clone.props, data) || {},
|
||||
changedProps = Object.keys(delta)
|
||||
|
||||
clone.props = data
|
||||
|
||||
if (changedProps.some(x => !clone.constructor.dynamicProps.includes(x))) {
|
||||
|
||||
clone.reCreateWidget({reuseChildren: false})
|
||||
this.updateContainer(false)
|
||||
|
||||
} else {
|
||||
|
||||
clone.updateProps(changedProps, this)
|
||||
this.updateContainer(true)
|
||||
|
||||
}
|
||||
|
||||
|
||||
this.cloneLock = false
|
||||
|
||||
}
|
||||
|
||||
updateContainer(checkResize) {
|
||||
|
||||
|
||||
if (this.cloneTarget) {
|
||||
|
||||
var classes = [...this.cloneTarget.container.classList].filter(i=>excludedCloneClasses.indexOf(i) === -1)
|
||||
|
||||
if (!deepEqual(classes, this.cloneClass)) {
|
||||
this.container.classList.remove(...this.cloneClass)
|
||||
this.cloneClass = classes
|
||||
this.container.classList.add(...this.cloneClass)
|
||||
}
|
||||
|
||||
this.container.classList.remove('empty')
|
||||
|
||||
for (var w of this.getAllChildren()) {
|
||||
w.container.classList.add('not-editable')
|
||||
}
|
||||
|
||||
if (checkResize) resize.check(this.widget)
|
||||
|
||||
} else if (this.cloneClass.length) {
|
||||
|
||||
this.container.classList.remove(...this.cloneClass)
|
||||
this.container.classList.add('empty')
|
||||
this.cloneClass = []
|
||||
this.widget.innerHTML = ''
|
||||
widgetManager.removeWidgets(this.getAllChildren())
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onPropChanged(propName, options, oldPropValue) {
|
||||
|
||||
if (super.onPropChanged(...arguments)) return true
|
||||
|
|
@ -200,9 +250,9 @@ class Clone extends Container {
|
|||
switch (propName) {
|
||||
|
||||
case 'widgetId':
|
||||
this.cleanClone()
|
||||
this.cloneTarget = null
|
||||
this.getCloneTarget()
|
||||
this.unbindTarget()
|
||||
this.updateContainer(false)
|
||||
this.bindTarget(this.getCloneTarget())
|
||||
if (this.cloneTarget) this.createClone()
|
||||
return
|
||||
|
||||
|
|
@ -212,10 +262,7 @@ class Clone extends Container {
|
|||
|
||||
onRemove(){
|
||||
|
||||
if (this.cloneTarget) {
|
||||
this.cloneTarget.removeEventContext(this)
|
||||
}
|
||||
|
||||
this.unbindTarget()
|
||||
super.onRemove()
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue