editor: reuse children when possible
This commit is contained in:
parent
8de0bb90f7
commit
079e9588bf
15 changed files with 236 additions and 38 deletions
|
|
@ -2,13 +2,102 @@ var widgetManager = require('../managers/widgets'),
|
|||
resize = require('../events/resize'),
|
||||
stateManager = require('../managers/state'),
|
||||
parser = require('../parser'),
|
||||
Panel,
|
||||
sidepanel,
|
||||
editor
|
||||
|
||||
var updateWidget = function(widget, options = {}) {
|
||||
DOM.ready(()=>{
|
||||
sidepanel = DOM.get('#sidepanel')[0]
|
||||
})
|
||||
|
||||
function updateWidget(widget, options={}) {
|
||||
|
||||
// save state
|
||||
var sidepanel = DOM.get('#sidepanel')[0],
|
||||
scroll = sidepanel.scrollTop,
|
||||
stateManager.incrementQueue()
|
||||
var toSave = [widget],
|
||||
sidepanelScroll = sidepanel.scrollTop,
|
||||
scrollState = {}
|
||||
if (options.reuseChildren === false) {
|
||||
toSave = toSave.concat(widget.getAllChildren())
|
||||
}
|
||||
for (let w of toSave) {
|
||||
if (widgetManager.widgets[w.hash]) {
|
||||
let id = w.getProp('id'),
|
||||
value = w.getValue(),
|
||||
valueProp = w.getProp('value')
|
||||
|
||||
stateManager.pushValueState(id, value)
|
||||
if (valueProp !== '' && valueProp !== undefined) stateManager.pushValueOldProp(id, valueProp)
|
||||
}
|
||||
if (widgetManager.scrollingWidgets.indexOf(w.hash) > -1 && w.scroll) {
|
||||
scrollState[w.getProp('id')] = w.scroll()
|
||||
}
|
||||
}
|
||||
|
||||
// remove old widgets
|
||||
var reuseChildren = options.reuseChildren !== false && widget instanceof Panel
|
||||
var removedWidgets = reuseChildren ?
|
||||
(options.removedChildren || []).map(x => x.getAllChildren()).concat(widget) :
|
||||
widget.getAllChildren().concat(widget)
|
||||
|
||||
widgetManager.removeWidgets(removedWidgets)
|
||||
|
||||
|
||||
// create new widget
|
||||
var newWidget = parser.parse({
|
||||
data: widget.props,
|
||||
parent: widget.parent,
|
||||
parentNode: widget.parentNode,
|
||||
reCreateOptions: options.reCreateOptions,
|
||||
children: reuseChildren ? widget.children : undefined
|
||||
})
|
||||
|
||||
widget.container.parentNode.replaceChild(newWidget.container, widget.container)
|
||||
|
||||
if (newWidget.getProp('type') == 'tab') newWidget.parent.trigger('tab-created', [{widget: widget}])
|
||||
if (newWidget.getProp('id') == 'root') DOM.get('.editor-root')[0].setAttribute('data-widget', newWidget.hash)
|
||||
|
||||
|
||||
if (reuseChildren && !(newWidget instanceof Panel)) {
|
||||
// remove remaining children if widget is not a container anymore
|
||||
widgetManager.removeWidgets(widget.getAllChildren())
|
||||
}
|
||||
|
||||
if (reuseChildren) {
|
||||
// children don't listen to their parent's 'widget-created' event
|
||||
// so we have to let them know it's been updated
|
||||
widgetManager.trigger('prop-changed', [{
|
||||
id: newWidget.getProp('id'),
|
||||
props: [],
|
||||
widget: newWidget,
|
||||
options: {}
|
||||
}])
|
||||
}
|
||||
|
||||
resize.check(newWidget.container)
|
||||
|
||||
// restore state
|
||||
stateManager.decrementQueue()
|
||||
for (let id in scrollState) {
|
||||
for (let w of widgetManager.getWidgetById(id)) {
|
||||
if (w.scroll) w.scroll(scrollState[id])
|
||||
}
|
||||
}
|
||||
sidepanel.scrollTop = sidepanelScroll
|
||||
|
||||
if (editor.selectedWidgets.includes(widget) && !options.preventSelect) {
|
||||
editor.select(newWidget)
|
||||
}
|
||||
|
||||
return newWidget
|
||||
|
||||
}
|
||||
|
||||
|
||||
var updateWidgetOld = function(widget, options = {}) {
|
||||
|
||||
// save state
|
||||
var scroll = sidepanel.scrollTop,
|
||||
oldWidgets = widget.getAllChildren().concat(widget),
|
||||
wasSelected = editor.selectedWidgets.includes(widget),
|
||||
wScroll = {}
|
||||
|
|
@ -144,3 +233,4 @@ module.exports = {
|
|||
}
|
||||
|
||||
editor = require('./')
|
||||
Panel = require('../widgets/containers/panel')
|
||||
|
|
|
|||
|
|
@ -146,6 +146,7 @@ module.exports = function editField(editor, widget, propName, defaultValue){
|
|||
break
|
||||
|
||||
case 'remove':
|
||||
widget.children.splice(DOM.index(e.target.closest('li')),1)
|
||||
widget.props[propName].splice(DOM.index(e.target.closest('li')),1)
|
||||
change = true
|
||||
break
|
||||
|
|
@ -175,6 +176,7 @@ module.exports = function editField(editor, widget, propName, defaultValue){
|
|||
var index = $(ui.item).index()
|
||||
|
||||
widget.props[propName].splice(index, 0, widget.props[propName].splice(oldindex, 1)[0])
|
||||
widget.children.splice(index, 0, widget.children.splice(oldindex, 1)[0])
|
||||
updateWidget(widget)
|
||||
editor.pushHistory()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -515,10 +515,11 @@ var Editor = class Editor {
|
|||
|
||||
if (!this.selectedWidgets.length) return
|
||||
|
||||
var index = this.selectedWidgets.map((w)=>DOM.index(w.container)).sort((a,b)=>{return b-a}),
|
||||
var type = this.selectedWidgets[0].props.type == 'tab' ? 'tab' : 'widget',
|
||||
parent = this.selectedWidgets[0].parent,
|
||||
index = this.selectedWidgets.map(w => parent.children.indexOf(w)).sort((a,b)=>{return b-a}),
|
||||
data = this.selectedWidgets.map((w)=>w.props),
|
||||
type = this.selectedWidgets[0].props.type == 'tab' ? 'tab' : 'widget',
|
||||
parent = this.selectedWidgets[0].parent
|
||||
removedChildren = []
|
||||
|
||||
if (type !== 'widget') return
|
||||
|
||||
|
|
@ -531,10 +532,14 @@ var Editor = class Editor {
|
|||
}
|
||||
|
||||
for (var i of index) {
|
||||
removedChildren.push(parent.children.splice(i,1)[0])
|
||||
parent.props.widgets.splice(i,1)
|
||||
}
|
||||
|
||||
this.select(updateWidget(parent, {preventSelect: true}))
|
||||
this.select(updateWidget(parent, {
|
||||
preventSelect: true,
|
||||
removedChildren
|
||||
}))
|
||||
this.pushHistory()
|
||||
|
||||
}
|
||||
|
|
@ -606,23 +611,29 @@ var Editor = class Editor {
|
|||
|
||||
if (!this.selectedWidgets.length) return
|
||||
|
||||
var index = this.selectedWidgets.map((w)=>DOM.index(w.container)).sort((a,b)=>{return b-a}),
|
||||
type = this.selectedWidgets[0].props.type == 'tab' ? 'tab' : 'widget',
|
||||
parent = this.selectedWidgets[0].parent
|
||||
var type = this.selectedWidgets[0].props.type == 'tab' ? 'tab' : 'widget',
|
||||
parent = this.selectedWidgets[0].parent,
|
||||
index = this.selectedWidgets.map(w => parent.children.indexOf(w)).sort((a,b)=>{return b-a}),
|
||||
removedChildren = []
|
||||
|
||||
if (this.selectedWidgets[0].getProp('id') === 'root') return
|
||||
|
||||
if (type === 'widget') {
|
||||
for (let i of index) {
|
||||
removedChildren.push(parent.children.splice(i,1)[0])
|
||||
parent.props.widgets.splice(i,1)
|
||||
}
|
||||
} else {
|
||||
for (let i of index) {
|
||||
removedChildren.push(parent.children.splice(i,1)[0])
|
||||
parent.props.tabs.splice(i,1)
|
||||
}
|
||||
}
|
||||
|
||||
this.select(updateWidget(parent, {preventSelect: true}))
|
||||
this.select(updateWidget(parent, {
|
||||
preventSelect: true,
|
||||
removedChildren
|
||||
}))
|
||||
this.pushHistory()
|
||||
|
||||
}
|
||||
|
|
@ -708,7 +719,7 @@ var Editor = class Editor {
|
|||
|
||||
|
||||
|
||||
pushHistory() {
|
||||
pushHistory(infos) {
|
||||
|
||||
if (this.historyState > -1) {
|
||||
this.history.splice(0, this.historyState + 1)
|
||||
|
|
@ -721,7 +732,7 @@ var Editor = class Editor {
|
|||
for (var c of deepCopy(d)) {
|
||||
diff.applyChange(this.historySession, null, c)
|
||||
}
|
||||
this.history.unshift(deepCopy(d))
|
||||
this.history.unshift([deepCopy(d), infos])
|
||||
if (this.history.length > HISTORY_SIZE) this.history.pop()
|
||||
}
|
||||
|
||||
|
|
@ -741,8 +752,9 @@ var Editor = class Editor {
|
|||
|
||||
this.historyState += 1
|
||||
|
||||
var d1 = deepCopy(this.history[this.historyState]),
|
||||
d2 = deepCopy(this.history[this.historyState]),
|
||||
var [patch, infos] = this.history[this.historyState],
|
||||
d1 = deepCopy(patch),
|
||||
d2 = deepCopy(patch),
|
||||
path
|
||||
|
||||
for (var i = d1.length - 1; i > -1; i--) {
|
||||
|
|
@ -776,8 +788,9 @@ var Editor = class Editor {
|
|||
|
||||
if (this.historyState === -1) return
|
||||
|
||||
var d1 = deepCopy(this.history[this.historyState]),
|
||||
d2 = deepCopy(this.history[this.historyState]),
|
||||
var [patch, infos] = this.history[this.historyState],
|
||||
d1 = deepCopy(patch),
|
||||
d2 = deepCopy(patch),
|
||||
path
|
||||
|
||||
for (var i = 0; i < d1.length; i++) {
|
||||
|
|
@ -802,6 +815,7 @@ var Editor = class Editor {
|
|||
path.splice(n, path.length - n)
|
||||
}
|
||||
|
||||
|
||||
this.updateWidgetByPath(path)
|
||||
|
||||
this.historyState -= 1
|
||||
|
|
@ -810,6 +824,8 @@ var Editor = class Editor {
|
|||
|
||||
updateWidgetByPath(path) {
|
||||
|
||||
//TODO traverse widget.children instead of querying the dom
|
||||
|
||||
var req = '.root-container'
|
||||
for (var j = 1; j < path.length; j++) {
|
||||
var item = path[j]
|
||||
|
|
@ -834,7 +850,9 @@ var Editor = class Editor {
|
|||
w = widgetManager.getWidgetById('root')[0]
|
||||
}
|
||||
|
||||
updateWidget(w)
|
||||
updateWidget(w, {
|
||||
reuseChildren: false
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,10 @@ var SessionManager = class SessionManager {
|
|||
|
||||
}
|
||||
|
||||
parser.parse(this.session, DOM.get('#container')[0])
|
||||
parser.parse({
|
||||
data: this.session,
|
||||
parentNode: DOM.get('#container')[0]
|
||||
})
|
||||
editor.clearHistory()
|
||||
|
||||
} catch (err) {
|
||||
|
|
|
|||
|
|
@ -188,19 +188,23 @@ var WidgetManager = class WidgetManager extends EventEmitter {
|
|||
|
||||
}
|
||||
|
||||
removeWidgets(widgets) {
|
||||
removeWidgets(widgets, nested) {
|
||||
|
||||
for (let i in widgets) {
|
||||
|
||||
if (
|
||||
this.widgets[widgets[i].hash]
|
||||
) {
|
||||
if (Array.isArray(widgets[i])) {
|
||||
|
||||
this.removeWidgets(widgets[i], true)
|
||||
|
||||
} else if (this.widgets[widgets[i].hash]) {
|
||||
|
||||
this.removeWidget(widgets[i])
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.purge()
|
||||
if (!nested) this.purge()
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,11 @@ var Parser = class Parser {
|
|||
|
||||
}
|
||||
|
||||
parse(data, parentNode, parentWidget, tab, reCreateOptions) {
|
||||
parse(options) {
|
||||
|
||||
var {data, parentNode, parent, tab, reCreateOptions, children} = options
|
||||
|
||||
if (!Array.isArray(data)) data = [data]
|
||||
|
||||
for (let i in data) {
|
||||
|
||||
|
|
@ -65,7 +69,14 @@ var Parser = class Parser {
|
|||
}
|
||||
|
||||
// create widget
|
||||
var widget = new this.widgets[props.type]({props:props, container:true, parent:parentWidget, parentNode:parentNode, reCreateOptions})
|
||||
var widget = new this.widgets[props.type]({
|
||||
container:true,
|
||||
props,
|
||||
parent,
|
||||
parentNode,
|
||||
reCreateOptions,
|
||||
children
|
||||
})
|
||||
|
||||
widgetManager.addWidget(widget)
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,10 @@ var callbacks = {
|
|||
data[k] = newdata[k]
|
||||
}
|
||||
|
||||
updateWidget(widget, {remote: true})
|
||||
updateWidget(widget, {
|
||||
remote: true,
|
||||
reuseChildren: !(newdata.widgets || newdata.tabs)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +50,10 @@ var callbacks = {
|
|||
|
||||
deepExtend(data, newdata)
|
||||
|
||||
updateWidget(widget, {remote: true})
|
||||
updateWidget(widget, {
|
||||
remote: true,
|
||||
reuseChildren: !(newdata.widgets || newdata.tabs)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -145,8 +145,11 @@ class Clone extends Container {
|
|||
this.container.classList.remove('empty')
|
||||
this.container.classList.add(...this.cloneClass)
|
||||
let clonedProps = deepCopy(this.cloneTarget.props)
|
||||
var clone = parser.parse([{...clonedProps, ...this.getProp('props')}], this.widget, this)
|
||||
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -50,7 +50,25 @@ class Panel extends Container {
|
|||
|
||||
this.wrapper = this.widget.appendChild(DOM.create('<div class="tabs-wrapper"></div>'))
|
||||
|
||||
parser.parse(this.getProp('tabs'), this.wrapper, this, true)
|
||||
if (options.children && options.children.length) {
|
||||
// reuse old children
|
||||
this.children = options.children
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
this.wrapper.appendChild(this.children[i].container)
|
||||
this.children[i].parent = this
|
||||
this.children[i].parentNode = this.wrapper
|
||||
}
|
||||
}
|
||||
if (this.getProp('tabs').length > this.children.length) {
|
||||
// parse new children
|
||||
parser.parse({
|
||||
data: this.getProp('tabs').slice(this.children.length),
|
||||
parentNode: this.wrapper,
|
||||
parent: this,
|
||||
tab: true
|
||||
})
|
||||
}
|
||||
|
||||
this.createNavigation()
|
||||
|
||||
this.navigation.addEventListener('fast-click', (e)=>{
|
||||
|
|
@ -68,7 +86,23 @@ class Panel extends Container {
|
|||
|
||||
} else if (this.getProp('widgets') && this.getProp('widgets').length) {
|
||||
|
||||
parser.parse(this.getProp('widgets'), this.widget, this)
|
||||
if (options.children && options.children.length) {
|
||||
// reuse old children
|
||||
this.children = options.children
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
this.widget.appendChild(this.children[i].container)
|
||||
this.children[i].parent = this
|
||||
this.children[i].parentNode = this.widget
|
||||
}
|
||||
}
|
||||
if (this.getProp('widgets').length > this.children.length) {
|
||||
// parse new children
|
||||
parser.parse({
|
||||
data: this.getProp('widgets').slice(this.children.length),
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -77,7 +77,12 @@ module.exports = class Keyboard extends _matrix_base {
|
|||
data.preArgs = '@{parent.preArgs}'
|
||||
}
|
||||
|
||||
var key = parser.parse([data], this.widget, this)
|
||||
var key = parser.parse({
|
||||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
|
||||
key.container.classList.add('not-editable')
|
||||
|
||||
if (pattern[i % 12] == 'w') {
|
||||
|
|
|
|||
|
|
@ -50,7 +50,11 @@ class Matrix extends _matrix_base {
|
|||
...this.resolveProp('props', undefined, false, false, false, {'$':i})
|
||||
}
|
||||
|
||||
var widget = parser.parse([data], this.widget, this)
|
||||
var widget = parser.parse({
|
||||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
|
||||
widget.container.classList.add('not-editable')
|
||||
|
||||
|
|
|
|||
|
|
@ -84,7 +84,11 @@ module.exports = class Multifader extends _matrix_base {
|
|||
data.preArgs = '@{parent.preArgs}'
|
||||
}
|
||||
|
||||
var fader = parser.parse([data], this.widget, this)
|
||||
var fader = parser.parse({
|
||||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
fader.container.classList.add('not-editable')
|
||||
|
||||
this.value[i-this.start] = this.getProp('range').min
|
||||
|
|
|
|||
|
|
@ -74,7 +74,12 @@ module.exports = class Multipush extends _matrix_base {
|
|||
data.preArgs = '@{parent.preArgs}'
|
||||
}
|
||||
|
||||
var push = parser.parse([data], this.widget, this)
|
||||
var push = parser.parse({
|
||||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
|
||||
push.container.classList.add('not-editable')
|
||||
|
||||
this.value[i-this.start] = this.getProp('off')
|
||||
|
|
|
|||
|
|
@ -74,7 +74,11 @@ module.exports = class Multitoggle extends _matrix_base {
|
|||
data.preArgs = '@{parent.preArgs}'
|
||||
}
|
||||
|
||||
var toggle = parser.parse([data], this.widget, this)
|
||||
var toggle = parser.parse({
|
||||
data: data,
|
||||
parentNode: this.widget,
|
||||
parent: this
|
||||
})
|
||||
toggle.container.classList.add('not-editable')
|
||||
|
||||
this.value[i-this.start] = this.getProp('off')
|
||||
|
|
|
|||
|
|
@ -96,7 +96,12 @@ module.exports = class Fader extends Slider {
|
|||
pips:false,
|
||||
dashed:true
|
||||
}
|
||||
var meter = parser.parse([data],this.wrapper, this)
|
||||
var meter = parser.parse({
|
||||
data: data,
|
||||
parentNode: this.wrapper,
|
||||
parent: this
|
||||
})
|
||||
|
||||
meter.container.classList.add('not-editable')
|
||||
|
||||
this.widget.classList.add('has-meter')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue