From de14bf39d775c65f654315a8250be06d80e9247e Mon Sep 17 00:00:00 2001 From: Martin Hermant Date: Wed, 10 Oct 2018 23:05:46 +0200 Subject: [PATCH 1/6] remove circular dependencies, and undefined behaviors --- .../js/app/widgets/containers/clone.js | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 6eddd030..0ee9cbc5 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -41,40 +41,56 @@ class Clone extends Widget { if (this.cloneTarget) this.createClone() widgetManager.on(`widget-created.${this.hash}`, (e)=>{ - var {id, widget} = e + var {id, widget, reCreate} = e + function hasParentId(ob,tid){ + let parent = ob + while(parent && parent.getProp && parent.parent !== widgetManager){ + if( parent.getProp('id')===tid){return parent} + parent = parent.parent + } + } + const potentialTarget = hasParentId(widget,this.getProp('widgetId')) if ( this.cloneTarget === null && - id === this.getProp('widgetId') && - this.isValidCloneTarget(widget) + ! this.contains(widget) && + potentialTarget ) { - this.cloneTarget = widget + this.cloneTarget = potentialTarget this.createClone() resize.check(this.container) } }) - - widgetManager.on(`widget-removed.${this.hash}`, (e)=>{ - if (this.cloneTarget === e.widget) { - this.cloneTarget = null - this.cleanClone() - } - }) - } getCloneTarget() { var widgets = widgetManager.getWidgetById(this.getProp('widgetId')) - for (var i in widgets) { - if ( - this.isValidCloneTarget(widgets[i]) - ) { + if(widgets.length==1){ + this.cloneTarget = widgets[0]; + return; + } + // when duplicating clones pointing to an object with a static id, + // each clone will recreate an object with the same id + // we try to get the original one if there is more than 2 + let foundDedupedClone = null; + function isInsideClone(wi){ + let parent = wi.parent; + return parent && parent.cachedProps.type==="clone" + } + for (var i in widgets) { + if (this.isValidCloneTarget(widgets[i])) { this.cloneTarget = widgets[i] - break + if(!isInsideClone(widgets[i]) ){ + foundDedupedClone = widgets[i] + break; + } } } - + this.cloneTarget = foundDedupedClone || this.cloneTarget + if(this.cloneTarget && !foundDedupedClone ){ + console.warn('no deduped clone found : ',this.getProp('widgetId')) + } } isValidCloneTarget(widget) { @@ -93,7 +109,7 @@ class Clone extends Widget { this.container.classList.remove(...this.cloneClass) this.container.classList.add('clone-container', 'empty') this.cloneClass = [] - if (this.cloneTarget) this.cloneTarget.off(`widget-created.${this.hash}`) + if (this.cloneTarget) widgetManager.off(`widget-removed.${this.hash}`) } @@ -107,8 +123,8 @@ class Clone extends Widget { this.cloneClass = [...this.cloneTarget.container.classList].filter(i=>excludedCloneClasses.indexOf(i) === -1) this.container.classList.remove('empty') this.container.classList.add(...this.cloneClass) - - var clone = parser.parse([{...deepCopy(this.cloneTarget.props), ...this.getProp('props')}], this.widget, this) + const propsCopy = deepCopy(this.cloneTarget.props) + var clone = parser.parse([{...propsCopy, ...this.getProp('props')}], this.widget, this,null,null,true) if (clone.getProp('id') === this.cloneTarget.getProp('id')) { widgetManager.trigger('change.*', [{ @@ -122,9 +138,13 @@ class Clone extends Widget { DOM.each(this.widget, '.widget', (el)=>{el.classList.add('not-editable')}) - this.cloneTarget.once(`widget-created.${this.hash}`, (e)=>{ - this.createClone(this.cloneTarget) - resize.check(this.container) + // will react to any of childs deletion + widgetManager.on(`widget-removed.${this.hash}`, (e)=>{ + var { widget} = e + if(this.cloneTarget && this.cloneTarget.contains(widget)){ + this.cleanClone() + this.cloneTarget = null + } }) this.cloneLock = false From e20fda493de415df53c69d63cb2671e10f0b5728 Mon Sep 17 00:00:00 2001 From: Martin Hermant Date: Thu, 11 Oct 2018 10:09:05 +0200 Subject: [PATCH 2/6] clone : reset parser call to original api --- src/browser/js/app/widgets/containers/clone.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 0ee9cbc5..a3abc52d 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -123,8 +123,8 @@ class Clone extends Widget { this.cloneClass = [...this.cloneTarget.container.classList].filter(i=>excludedCloneClasses.indexOf(i) === -1) this.container.classList.remove('empty') this.container.classList.add(...this.cloneClass) - const propsCopy = deepCopy(this.cloneTarget.props) - var clone = parser.parse([{...propsCopy, ...this.getProp('props')}], this.widget, this,null,null,true) + + var clone = parser.parse([{...deepCopy(this.cloneTarget.props), ...this.getProp('props')}], this.widget, this) if (clone.getProp('id') === this.cloneTarget.getProp('id')) { widgetManager.trigger('change.*', [{ From 3671f8317b80ebc9507549d1fc46574c99c3d94e Mon Sep 17 00:00:00 2001 From: Martin Hermant Date: Thu, 11 Oct 2018 14:36:10 +0200 Subject: [PATCH 3/6] better syntax and comments --- .../js/app/widgets/containers/clone.js | 83 ++++++++++++------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index a3abc52d..457e3ad1 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -40,21 +40,22 @@ class Clone extends Widget { this.getCloneTarget() if (this.cloneTarget) this.createClone() + // we need to listen globally if some kind of widgets gets created with the right id widgetManager.on(`widget-created.${this.hash}`, (e)=>{ - var {id, widget, reCreate} = e - function hasParentId(ob,tid){ - let parent = ob + var {id, widget} = e + if(this.cloneTarget !== null || !this.isValidCloneTarget(widget))return + //console.log('cb',id,",",this.getProp('id')) + // we could put getParentWithId inside widget class + function getParentWithId(ob,tid,includeThis){ + let parent = includeThis?ob:ob.parent while(parent && parent.getProp && parent.parent !== widgetManager){ - if( parent.getProp('id')===tid){return parent} + if(parent.getProp('id')===tid){return parent} parent = parent.parent } } - const potentialTarget = hasParentId(widget,this.getProp('widgetId')) - if ( - this.cloneTarget === null && - ! this.contains(widget) && - potentialTarget - ) { + // if some Child is created inside a container with the widgetId, we surely need to create the clone + const potentialTarget = id===this.getProp('widgetId')?widget:getParentWithId(widget,this.getProp('widgetId')) + if (potentialTarget) { this.cloneTarget = potentialTarget this.createClone() resize.check(this.container) @@ -64,31 +65,32 @@ class Clone extends Widget { getCloneTarget() { - var widgets = widgetManager.getWidgetById(this.getProp('widgetId')) + const widgets = widgetManager.getWidgetById(this.getProp('widgetId')) + .filter(el=>this.isValidCloneTarget(el)) + if(widgets.length===0){return} if(widgets.length==1){ this.cloneTarget = widgets[0]; - return; + return; } // when duplicating clones pointing to an object with a static id, - // each clone will recreate an object with the same id - // we try to get the original one if there is more than 2 - let foundDedupedClone = null; - function isInsideClone(wi){ - let parent = wi.parent; + // each clone will recreate an object with the same id internally + // we try to get the original one if there is more than 2 clone pointing to the same id (avoiding cloning the clone) + + function isCreatedByAClone(wi){ + const parent = wi.parent; return parent && parent.cachedProps.type==="clone" } - for (var i in widgets) { - if (this.isValidCloneTarget(widgets[i])) { - this.cloneTarget = widgets[i] - if(!isInsideClone(widgets[i]) ){ - foundDedupedClone = widgets[i] - break; - } + this.cloneTarget = widgets[0] + for (const w of widgets) { + if(!isCreatedByAClone(w) ){ + this.cloneTarget = w + break; } } - this.cloneTarget = foundDedupedClone || this.cloneTarget - if(this.cloneTarget && !foundDedupedClone ){ + + // we may want to remove this warning + if(isCreatedByAClone(this.cloneTarget)){ console.warn('no deduped clone found : ',this.getProp('widgetId')) } } @@ -102,7 +104,7 @@ class Clone extends Widget { cleanClone() { if (!this.childrenHashes.length) return - + //console.log('clear clone',this.getProp('id')) widgetManager.removeWidgets(this.childrenHashes) this.widget.innerHTML = '' this.childrenHashes = [] @@ -115,7 +117,11 @@ class Clone extends Widget { createClone() { - if (this.cloneLock) return + if (this.cloneLock) { + //console.warn('clone locked',this.id) + return + } + //console.log('create clone',this.getProp('id')) this.cloneLock = true this.cleanClone() @@ -138,10 +144,23 @@ class Clone extends Widget { DOM.each(this.widget, '.widget', (el)=>{el.classList.add('not-editable')}) - // will react to any of childs deletion + // will react to any widget deletion + // widget-removed are only triggered from the manager from now + // TODO : we could avoid useless callbacks if widgets were to trigger widget-removed events widgetManager.on(`widget-removed.${this.hash}`, (e)=>{ var { widget} = e - if(this.cloneTarget && this.cloneTarget.contains(widget)){ + // a clone should not react on its target's nested clones widgets + // this mechanism is handled by the inner clones + function containsNonCloned(self,wi){ + let insp = wi.parent; + while(insp && insp.parent!==widgetManager){ + if(insp===self){return true} + if(insp.cachedProps.type==="clone"){return false} + insp = insp.parent + } + return false + } + if(this.cloneTarget && containsNonCloned(this.cloneTarget,widget)){ this.cleanClone() this.cloneTarget = null } @@ -169,8 +188,8 @@ class Clone extends Widget { } onRemove(){ - - if (this.cloneTarget) this.cloneTarget.off(`widget-created.${this.hash}`) + this.cleanClone(); + this.cloneTarget = null; widgetManager.off(`widget-removed.${this.hash}`) super.onRemove() From 25944ad60b2c7c72963f2f3b41c48f4e18098cef Mon Sep 17 00:00:00 2001 From: Martin Hermant Date: Thu, 11 Oct 2018 14:38:54 +0200 Subject: [PATCH 4/6] add auto detection of id created automatically to allow to have different id when cloning (can be done via dynamic props too ) --- src/browser/js/app/widgets/containers/clone.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 457e3ad1..107ad956 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -129,8 +129,16 @@ class Clone extends Widget { this.cloneClass = [...this.cloneTarget.container.classList].filter(i=>excludedCloneClasses.indexOf(i) === -1) this.container.classList.remove('empty') this.container.classList.add(...this.cloneClass) - - var clone = parser.parse([{...deepCopy(this.cloneTarget.props), ...this.getProp('props')}], this.widget, this) + let clonedProps = deepCopy(this.cloneTarget.props) + function resetAutoIds(ob){ + if(ob.widgets) + for (const o of ob.widgets){resetAutoIds(o)} + if( ob.id && ob.id.startsWith(ob.type+'_')){ + ob.id = "auto" + } + } + resetAutoIds(clonedProps) + var clone = parser.parse([{...clonedProps, ...this.getProp('props')}], this.widget, this) if (clone.getProp('id') === this.cloneTarget.getProp('id')) { widgetManager.trigger('change.*', [{ From 0bd29cc3ea6ec24c2467943a723bc8c398282d71 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 13 Oct 2018 16:49:53 +0200 Subject: [PATCH 5/6] clone: don't reset auto ids (maybe to be added as an option) --- src/browser/js/app/widgets/containers/clone.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 107ad956..b0e223bd 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -130,14 +130,6 @@ class Clone extends Widget { this.container.classList.remove('empty') this.container.classList.add(...this.cloneClass) let clonedProps = deepCopy(this.cloneTarget.props) - function resetAutoIds(ob){ - if(ob.widgets) - for (const o of ob.widgets){resetAutoIds(o)} - if( ob.id && ob.id.startsWith(ob.type+'_')){ - ob.id = "auto" - } - } - resetAutoIds(clonedProps) var clone = parser.parse([{...clonedProps, ...this.getProp('props')}], this.widget, this) if (clone.getProp('id') === this.cloneTarget.getProp('id')) { From bc954552ce9693f1b3e41372dd1cf51276ff2da6 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 13 Oct 2018 16:50:32 +0200 Subject: [PATCH 6/6] clone: remove unnecessary clean on deletion --- src/browser/js/app/widgets/containers/clone.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index b0e223bd..37659b55 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -188,8 +188,7 @@ class Clone extends Widget { } onRemove(){ - this.cleanClone(); - this.cloneTarget = null; + widgetManager.off(`widget-removed.${this.hash}`) super.onRemove()