From de14bf39d775c65f654315a8250be06d80e9247e Mon Sep 17 00:00:00 2001 From: Martin Hermant Date: Wed, 10 Oct 2018 23:05:46 +0200 Subject: [PATCH 01/10] 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 02/10] 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 03/10] 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 04/10] 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 05/10] 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 06/10] 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() From e5203117439bc74e1f4a4c588f48f9582385e7b4 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 14 Oct 2018 11:09:31 +0200 Subject: [PATCH 07/10] clone: adapt to commit 94da9103e68fd9cfcc8c8ee27d1b997600843cc8 --- src/browser/js/app/widgets/containers/clone.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 71142c1d..7844c6e5 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -103,15 +103,14 @@ class Clone extends Container { cleanClone() { - if (!this.childrenHashes.length) return + if (!this.children.length) return //console.log('clear clone',this.getProp('id')) - widgetManager.removeWidgets(this.childrenHashes) + widgetManager.removeWidgets(this.children) this.widget.innerHTML = '' - this.childrenHashes = [] + this.children = [] this.container.classList.remove(...this.cloneClass) this.container.classList.add('clone-container', 'empty') this.cloneClass = [] - if (this.cloneTarget) widgetManager.off(`widget-removed.${this.hash}`) } @@ -189,7 +188,6 @@ class Clone extends Container { onRemove(){ - widgetManager.off(`widget-removed.${this.hash}`) super.onRemove() } From 3e87c2f8ca2ab10c5f1355e83759ad4def8b1f29 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 14 Oct 2018 12:07:30 +0200 Subject: [PATCH 08/10] clone: fix cloneTarget updates watch --- .../js/app/widgets/containers/clone.js | 123 +++++++++++------- 1 file changed, 75 insertions(+), 48 deletions(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 7844c6e5..16286d43 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -40,59 +40,63 @@ class Clone extends Container { this.getCloneTarget() if (this.cloneTarget) this.createClone() - // we need to listen globally if some kind of widgets gets created with the right id + // global listenner to catch cloneTarget's creation if no target is locked widgetManager.on(`widget-created.${this.hash}`, (e)=>{ - 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} - parent = parent.parent + + if (this.cloneTarget === null) { + + var {id, widget} = e + + if (id === this.getProp('widgetId') && this.isValidCloneTarget(widget)) { + + this.cloneTarget = widget + this.createClone() + resize.check(this.container) + } + } - // 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) - } + }) + } getCloneTarget() { + // attempt to acquire a cloneTarget if 'widget-created' event has not been catched + // (the target might have been created before the clone itself) + 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; + + if (widgets.length === 0) return + 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 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" + const parent = wi.parent + return parent && parent.getProp('type') === 'clone' } + this.cloneTarget = widgets[0] for (const w of widgets) { - if(!isCreatedByAClone(w) ){ + if (!isCreatedByAClone(w)) { this.cloneTarget = w - break; + break } } - + // we may want to remove this warning - if(isCreatedByAClone(this.cloneTarget)){ + if (isCreatedByAClone(this.cloneTarget)) { console.warn('no deduped clone found : ',this.getProp('widgetId')) } + } isValidCloneTarget(widget) { @@ -103,15 +107,27 @@ class Clone extends Container { cleanClone() { + // 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.children) - this.widget.innerHTML = '' this.children = [] + + // 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.off(`widget-removed.${this.hash}`) + this.cloneTarget.off(`widget-created.${this.hash}`) + } + } createClone() { @@ -121,6 +137,7 @@ class Clone extends Container { return } //console.log('create clone',this.getProp('id')) + this.cloneLock = true this.cleanClone() @@ -143,26 +160,31 @@ class Clone extends Container { DOM.each(this.widget, '.widget', (el)=>{el.classList.add('not-editable')}) - // 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 - // 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 + // 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.${this.hash}`, (e)=>{ + if (this.cloneTarget === e.widget) { + this.cloneTarget = null + this.cleanClone() } - if(this.cloneTarget && containsNonCloned(this.cloneTarget,widget)){ - this.cleanClone() - this.cloneTarget = null - } + }) + + // listen for cloneTarget's content updates + this.cloneTarget.on(`widget-created.${this.hash}`, (e)=>{ + + var {widget} = e, + parent = widget.parent + + while (parent !== this.cloneTarget && parent !== widgetManager) { + // if updated widget is in a nested clone, ignore it: + // it will be handled by the nested clone that targets it + if (parent.getProp('type') === 'clone') return + parent = widget.parent + } + + this.createClone() + resize.check(this.container) + }) this.cloneLock = false @@ -188,6 +210,11 @@ class Clone extends Container { onRemove(){ + if (this.cloneTarget) { + this.cloneTarget.off(`widget-removed.${this.hash}`) + this.cloneTarget.off(`widget-created.${this.hash}`) + } + super.onRemove() } From 1d05bcbbebb191e7190fa5d03e9eaf97d1398ceb Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 14 Oct 2018 12:24:02 +0200 Subject: [PATCH 09/10] clone: use children list to disable edition instead of querying the DOM --- src/browser/js/app/widgets/containers/clone.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index 16286d43..a2f58597 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -158,7 +158,9 @@ class Clone extends Container { } - DOM.each(this.widget, '.widget', (el)=>{el.classList.add('not-editable')}) + for (var i = 0; i < this.children.length; i++) { + this.children[i].container.classList.add('not-editable') + } // listen for cloneTarget's deletion // if it is just edited, its recreation will be catched by the global 'widget-created' event handler From 19dd76e6bb6e00d867cbe7a28e621689c76e465f Mon Sep 17 00:00:00 2001 From: Tintamar Date: Mon, 15 Oct 2018 13:37:18 +0200 Subject: [PATCH 10/10] fix infinite loop typo --- src/browser/js/app/widgets/containers/clone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/browser/js/app/widgets/containers/clone.js b/src/browser/js/app/widgets/containers/clone.js index a2f58597..c0535f1b 100644 --- a/src/browser/js/app/widgets/containers/clone.js +++ b/src/browser/js/app/widgets/containers/clone.js @@ -181,7 +181,7 @@ class Clone extends Container { // if updated widget is in a nested clone, ignore it: // it will be handled by the nested clone that targets it if (parent.getProp('type') === 'clone') return - parent = widget.parent + parent = parent.parent } this.createClone()