scoping: first id scoping implementation
This commit is contained in:
parent
593ec4c53d
commit
39c9d4a46d
5 changed files with 90 additions and 6 deletions
|
|
@ -260,7 +260,20 @@ class WidgetManager extends EventEmitter {
|
|||
|
||||
}
|
||||
|
||||
getWidgetById(id) {
|
||||
getWidgetById(id, scope) {
|
||||
|
||||
/*
|
||||
scopable: cache id prefixes (clone scopes), from neaest to farthest
|
||||
*/
|
||||
if (scope) {
|
||||
for (var prefix of scope) {
|
||||
var prefixedId = Array.isArray(id) ? id.map(i=>prefix + i) : prefix + id
|
||||
var widgets = this.getWidgetBy(prefixedId, this.idRoute)
|
||||
|
||||
// return matching widgets for the first matching scope
|
||||
if (widgets.length) return widgets
|
||||
}
|
||||
}
|
||||
|
||||
return this.getWidgetBy(id, this.idRoute)
|
||||
|
||||
|
|
|
|||
|
|
@ -417,6 +417,24 @@ class Widget extends EventEmitter {
|
|||
|
||||
createPropsCache() {
|
||||
|
||||
/*
|
||||
scopable: cache id prefixes (clone scopes), from neaest to farthest
|
||||
*/
|
||||
this.idPrefix = []
|
||||
if (this.meta.scopable) {
|
||||
var parent = this.parent
|
||||
do {
|
||||
if (parent.getProp('scopedId')) {
|
||||
this.idPrefix.push(parent.scopeId)
|
||||
}
|
||||
parent = parent.parent
|
||||
} while (parent.meta.scopable || parent.getProp('scopedId'))
|
||||
for (var i=0; i<this.idPrefix.length; i++) {
|
||||
this.idPrefix[i] = this.idPrefix.slice(i).reverse().join('/')
|
||||
}
|
||||
this.idPrefix.push('')
|
||||
}
|
||||
|
||||
// @{props} links lists
|
||||
this.linkedProps = {}
|
||||
this.nestedLinkedProps = {}
|
||||
|
|
@ -528,6 +546,23 @@ class Widget extends EventEmitter {
|
|||
|
||||
if (widget === this) id = 'this'
|
||||
|
||||
/*
|
||||
scopable: attempt to resolve widget updates against scoped ids
|
||||
*/
|
||||
if (this.meta.scopable) {
|
||||
var unprefixedId
|
||||
for (var p of this.idPrefix) {
|
||||
if (p === '') continue
|
||||
if (id.includes(p)) {
|
||||
unprefixedId = id.replace(p, '')
|
||||
if (this.nestedLinkedProps[unprefixedId] || linkedProps[unprefixedId]) {
|
||||
id = unprefixedId
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.nestedLinkedProps[id]) {
|
||||
this.updateLinkedPropsWithNesting(id)
|
||||
}
|
||||
|
|
@ -618,7 +653,7 @@ class Widget extends EventEmitter {
|
|||
|
||||
var widgets = id == 'parent' && this.parent ?
|
||||
[this.parent] : id == 'this' ? [this] :
|
||||
widgetManager.getWidgetById(id)
|
||||
widgetManager.getWidgetById(id, this.idPrefix)
|
||||
|
||||
if (!widgets.length) {
|
||||
var parent = this.parent
|
||||
|
|
@ -909,8 +944,27 @@ class Widget extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
return propValue
|
||||
|
||||
return this.transformProp(propName, propValue)
|
||||
|
||||
|
||||
}
|
||||
|
||||
transformProp(propName, propValue) {
|
||||
|
||||
switch (propName) {
|
||||
case 'id':
|
||||
case 'linkId':
|
||||
/*
|
||||
scopable: prefix id & linkId
|
||||
*/
|
||||
if (this.meta.scopable) {
|
||||
propValue = this.idPrefix[0] + propValue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return propValue
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@ class Clone extends Container() {
|
|||
|
||||
class_specific: {
|
||||
widgetId: {type: 'string', value: '', help: '`id` of the widget to clone'},
|
||||
scopedId: {type: 'boolean|string', value: false, help: [
|
||||
'Set to `true` to prefix all cloned `id`s inside the clone with a unique identifier and make the clone independent from its original. All `@{}` blocks and scripts will be prefixed automatically so that they resolve to widgets within the clone if possible, falling back to unprefixed ids otherwise. `linkId` will be prefixed as well but will only resolve within the clone.',
|
||||
'Set to a string to define the `ìd` prefix manually.'
|
||||
]},
|
||||
props: {type: 'object', value: {}, editor: 'javascript', syntaxChecker: false, help: 'Cloned widget\'s properties to override'},
|
||||
},
|
||||
value: null,
|
||||
|
|
@ -49,6 +53,8 @@ class Clone extends Container() {
|
|||
this.cloneTarget = null
|
||||
this.cloneClass = []
|
||||
|
||||
this.scopeId = this.getProp('scopedId') === true ? this.hash + '/' : this.getProp('scopedId') === false ? '' : this.getProp('scopedId')
|
||||
|
||||
this.modalBreakout = 0
|
||||
|
||||
this.container.classList.add('empty')
|
||||
|
|
@ -174,7 +180,8 @@ class Clone extends Container() {
|
|||
parent: this,
|
||||
meta: {
|
||||
not_editable: true,
|
||||
clone: true
|
||||
clone: true,
|
||||
scopable: true
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@ class Fragment extends Container() {
|
|||
class_specific: {
|
||||
file: {type: 'string', value: '', help: 'Fragment file path (relative to the session or theme file location by default, falling back to absolute path)'},
|
||||
fallback: {type: 'string', value: '', help: 'Fallack fragment file path, loaded if `file` can\'t be opened'},
|
||||
scopedId: {type: 'boolean|string', value: false, help: [
|
||||
'Set to `true` to prefix all widget `id`s inside the clone with a unique identifier and make the fragment self-contained. All `@{}` blocks and scripts will be prefixed automatically so that they resolve to widgets within the fragment if possible, falling back to unprefixed ids otherwise. `linkId` will be prefixed as well but will only resolve within the fragment.',
|
||||
'Set to a string to define the `ìd` prefix manually.'
|
||||
]},
|
||||
props: {type: 'object', value: {}, editor: 'javascript', syntaxChecker: false, help: 'Fragment widget\'s properties to override'},
|
||||
},
|
||||
value: null,
|
||||
|
|
@ -51,6 +55,8 @@ class Fragment extends Container() {
|
|||
|
||||
this.noValueState = true
|
||||
|
||||
this.scopeId = this.getProp('scopedId') === true ? this.hash + '/' : this.getProp('scopedId') === false ? '' : this.getProp('scopedId')
|
||||
|
||||
this.modalBreakout = 0
|
||||
|
||||
this.fragmentClass = []
|
||||
|
|
@ -115,7 +121,11 @@ class Fragment extends Container() {
|
|||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this,
|
||||
meta: {not_editable: true}
|
||||
meta: {
|
||||
not_editable: true,
|
||||
fragment: true,
|
||||
scopable: true
|
||||
}
|
||||
})
|
||||
|
||||
this.updateContainer(!init)
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ class ScriptVm extends Vm {
|
|||
|
||||
if (id === 'this') return [widget]
|
||||
else if (id === 'parent' && widget.parent !== widgetManager) return [widget.parent]
|
||||
else return widgetManager.getWidgetById(id)
|
||||
else return widgetManager.getWidgetById(id, widget.meta.scopable ? widget.idPrefix : null)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue