events: handle "widget-created" event without dom, make widget auto register their children

This commit is contained in:
jean-emmanuel 2018-01-12 12:39:39 +01:00
parent ae74946493
commit a4266e2e76
9 changed files with 30 additions and 73 deletions

View file

@ -20,7 +20,7 @@ var updateDom = function(container,data, remote) {
// save state
var scroll = $('#sidepanel').scrollTop(),
oldWidgets = container[0].abstract.hashes || [container[0].abstract.hash],
oldWidgets = container[0].abstract.childrenHashes.concat(container[0].abstract.hash),
wScroll = {}
stateManager.incrementQueue()
@ -44,12 +44,6 @@ var updateDom = function(container,data, remote) {
var newContainer = parser.parse([data], container[0].abstract.parentNode, container[0].abstract.parent)
container.replaceWith(newContainer)
if (data.type == 'tab') newContainer.trigger('tab-created')
if (newContainer[0].abstract.parent && newContainer[0].abstract.parent.registerHashes) {
newContainer[0].abstract.parent.registerHashes(true)
}
$('.editor-root').attr('data-widget', $('.root-container').attr('data-widget'))
newContainer.trigger('resize')

View file

@ -28,6 +28,7 @@ module.exports = class _widgets_base extends EventEmitter {
this.parent = options.parent
this.parentNode = options.parentNode
this.hash = _widgets_base.createHash()
this.childrenHashes = []
this.widget.abstract = this
if (this.container) this.container[0].abstract = this
@ -67,7 +68,7 @@ module.exports = class _widgets_base extends EventEmitter {
if (Object.keys(this.linkedProps).length) {
$('body').on(`widget-created.${this.hash}`, (e)=>{
widgetManager.on(`widget-created.${this.hash}`, (e)=>{
var {id} = e
if (this.linkedProps[id] && id != this.getProp('id')) {
this.checkPropsChanged(this.linkedProps[id])
@ -92,11 +93,22 @@ module.exports = class _widgets_base 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)
})
}
created() {
$('body').trigger({type: 'widget-created', id: this.getProp('id'), widget:this})
this.trigger(/widget-created(\..*)?/, [{
id: this.getProp('id'),
widget: this
}])
}
@ -279,7 +291,7 @@ module.exports = class _widgets_base extends EventEmitter {
}
onRemove(){
$('body').off(`widget-created.${this.hash}`)
widgetManager.off(`widget-created.${this.hash}`)
widgetManager.off(`change.${this.hash}`)
}

View file

@ -40,16 +40,15 @@ module.exports = class Clone extends _widgets_base {
super({...options, html: '<div class="clone"></div>'})
this.cloneHash = [0]
this.cloneHashes = []
this.cloneLock = false
this.cloneClass = ''
this.originalHash = false
this.createClone()
$('body').on(`widget-created.${this.hash}`, (e)=>{
widgetManager.on(`widget-created.${this.hash}`, (e)=>{
var {id, widget} = e
if (
(id == this.getProp('widgetId') && this.cloneHash.indexOf(widget.hash) == -1) &&
(id == this.getProp('widgetId') && this.cloneHashes.indexOf(widget.hash) == -1) &&
!(widget.parent && widget.parent.getProp('type') == 'clone')
) {
this.createClone(widget)
@ -66,7 +65,7 @@ module.exports = class Clone extends _widgets_base {
this.widget.empty()
this.container.removeClass(this.cloneClass)
purge(this.cloneHash)
purge(this.childrenHashes)
var widgets = widget ? [widget] : widgetManager.getWidgetById(this.getProp('widgetId'))
@ -74,14 +73,8 @@ module.exports = class Clone extends _widgets_base {
for (var i = widgets.length - 1; i>=0; i--) {
if (
if (!(widgets[i].parent && widgets[i].parent.getProp('type') == 'clone')) {
this.cloneHash.indexOf(widgets[i].hash) == -1 &&
!(widgets[i].parent && widgets[i].parent.getProp('type') == 'clone')
) {
this.originalHash = widgets[i].hash
this.cloneClass = widgets[i].container.attr('class').match(/[^\s]*-container/)[0]
this.container.addClass(this.cloneClass)
@ -89,14 +82,9 @@ module.exports = class Clone extends _widgets_base {
this.widget.find('.widget').addClass('not-editable')
this.hashes = [this.hash]
if (c[0].abstract.hashes) {
this.hashes = this.hashes.concat(c[0].abstract.hashes)
this.cloneHash = c[0].abstract.hashes
} else {
this.hashes.push(c[0].abstract.hash)
this.cloneHash = [c[0].abstract.hash]
}
widgets[i].once(`widget-created.${this.hash}`, (e)=>{
this.createClone()
})
break

View file

@ -73,16 +73,17 @@ module.exports = class Panel extends _widgets_base {
parser.parse(this.getProp('tabs'), this.wrapper, this, true)
this.createNavigation()
this.registerHashes()
this.navigation.on('fake-click', (e)=>{
if (!e.target.hasAttribute('data-widget')) return
this.setValue($(e.target).index(), {sync: true, send:this.value != $(e.target).index()})
})
this.wrapper.on('tab-created', (e)=>{
e.stopPropagation()
this.createNavigation()
this.on('widget-created', (e)=>{
if (e.widget == this) return
if (e.widget.parent == this && e.widget.getProp('type') == 'tab') {
this.createNavigation()
}
})
this.setValue(this.getProp('value') || 0)
@ -90,7 +91,6 @@ module.exports = class Panel extends _widgets_base {
} else if (this.getProp('widgets') && this.getProp('widgets').length) {
parser.parse(this.getProp('widgets'), this.widget, this)
this.registerHashes()
if (this.getProp('layout') != '') this.parseLayout()
}
@ -98,23 +98,6 @@ module.exports = class Panel extends _widgets_base {
}
registerHashes(edited) {
this.hashes = [this.hash]
var widgets = this.widget.add(this.wrapper).find('> .widget')
for (let widget of widgets) {
if (widget.abstract.hashes) {
this.hashes = this.hashes.concat(widget.abstract.hashes)
} else {
this.hashes.push(widget.abstract.hash)
}
}
if (edited) this.created()
if (edited && this.parent && this.parent.registerHashes) this.parent.registerHashes(true)
}
createNavigation() {
this.navigation.empty()

View file

@ -13,7 +13,7 @@ module.exports = class _matrices_base extends _widgets_base {
this.on('change',(e)=>{
if (e.widget == this) return
this.value[e.widget.container.index()] = e.widget.getValue()
this.changed(e.options)
@ -21,18 +21,6 @@ module.exports = class _matrices_base extends _widgets_base {
}
registerHashes() {
this.hashes = [this.hash]
var widgets = this.widget.find('.widget')
for (let widget of widgets) {
if (widget.abstract.hashes) {
this.hashes = this.hashes.concat(widget.abstract.hashes)
} else {
this.hashes.push(widget.abstract.hash)
}
}
}
// conflicts with switchers (double osc send)
// setValue(v, options={}) {
// if (v && v.length == this.value.length) {

View file

@ -92,8 +92,6 @@ module.exports = class Keyboard extends _matrices_base {
if (this.getProp('traversing')) this.widget.enableTraversingGestures()
this.registerHashes()
}
}

View file

@ -98,8 +98,6 @@ module.exports = class Multifader extends _matrices_base {
if (this.getProp('traversing')) this.widget.enableTraversingGestures({ctrlKeyCancel:true})
this.registerHashes()
}
}

View file

@ -76,8 +76,6 @@ module.exports = class Multipush extends _matrices_base {
if (this.getProp('traversing')) this.widget.enableTraversingGestures()
this.registerHashes()
}
}

View file

@ -76,8 +76,6 @@ module.exports = class Multitoggle extends _matrices_base {
if (this.getProp('traversing')) this.widget.enableTraversingGestures()
this.registerHashes()
}
}