Merge branch 'master' into v1
This commit is contained in:
commit
6c9220d0ab
103 changed files with 94 additions and 59 deletions
254
src/client/managers/session.js
Normal file
254
src/client/managers/session.js
Normal file
|
|
@ -0,0 +1,254 @@
|
|||
var ipc = require('../ipc/'),
|
||||
parser = require('../parser'),
|
||||
editor = require('../editor/'),
|
||||
lobby = require('../ui/lobby'),
|
||||
{loading, icon, Popup, upload, remoteBrowse} = require('../ui/utils'),
|
||||
{saveAs} = require('file-saver'),
|
||||
widgetManager = require('./widgets'),
|
||||
html = require('nanohtml'),
|
||||
raw = require('nanohtml/raw'),
|
||||
locales = require('../locales')
|
||||
|
||||
|
||||
var SessionManager = class SessionManager {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.session = null
|
||||
this.lock = false
|
||||
this.lastDir = null
|
||||
this.setSessionPath('')
|
||||
|
||||
ipc.on('connect', ()=>{
|
||||
ipc.send('sessionSetPath', {path: this.sessionPath})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
load(session, callback) {
|
||||
|
||||
if (this.lock) return
|
||||
|
||||
this.lock = true
|
||||
|
||||
var container = DOM.get('#osc-container')[0],
|
||||
loader = loading(locales('loading_session'))
|
||||
|
||||
setTimeout(()=>{
|
||||
try {
|
||||
|
||||
// backward compat
|
||||
if (Array.isArray(session)) session = session[0]
|
||||
|
||||
// session object must be a root widget
|
||||
if (session.type !== 'root') throw new Error(locales('session_malformed'))
|
||||
|
||||
// ok
|
||||
this.session = session
|
||||
container.innerHTML = ''
|
||||
parser.reset()
|
||||
parser.parse({
|
||||
data: this.session,
|
||||
parentNode: DOM.get('#osc-container')[0]
|
||||
})
|
||||
editor.clearHistory()
|
||||
|
||||
} catch (err) {
|
||||
loader.close()
|
||||
new Popup({title: locales('session_parsingerror'), content: err.message, icon: 'exclamation-triangle', closable:true})
|
||||
this.lock = false
|
||||
ipc.send('sessionSetPath', {path: this.sessionPath})
|
||||
throw err
|
||||
}
|
||||
|
||||
for (var h in widgetManager.widgets) {
|
||||
if (widgetManager.widgets[h].value !== undefined) {
|
||||
widgetManager.trigger('change', {
|
||||
widget: widgetManager.widgets[h],
|
||||
id: widgetManager.widgets[h].getProp('id'),
|
||||
linkId: widgetManager.widgets[h].getProp('linkId'),
|
||||
options: {}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (editor.enabled) editor.disable()
|
||||
|
||||
DOM.dispatchEvent(window, 'resize')
|
||||
|
||||
setTimeout(()=>{
|
||||
lobby.close()
|
||||
loader.close()
|
||||
container.classList.add('show')
|
||||
this.lock = false
|
||||
editor.unsavedSession = false
|
||||
this.setSessionPath('')
|
||||
if (callback) callback()
|
||||
}, 25)
|
||||
|
||||
}, 25)
|
||||
|
||||
}
|
||||
|
||||
save(path) {
|
||||
|
||||
if (!this.session || READ_ONLY) return
|
||||
|
||||
if (path) this.setSessionPath(path)
|
||||
|
||||
if (!this.sessionPath) return this.saveAs()
|
||||
|
||||
ipc.send('sessionSave', {
|
||||
session: JSON.stringify(this.session, null, ' '),
|
||||
path: this.sessionPath
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
saveAs() {
|
||||
|
||||
if (!this.session || READ_ONLY) return
|
||||
|
||||
remoteBrowse({extension: 'json', save:true, directory: this.lastDir}, (path)=>{
|
||||
this.lastDir = path[0]
|
||||
this.save(path)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export() {
|
||||
|
||||
var sessionfile = JSON.stringify(this.session,null,' '),
|
||||
blob = new Blob([sessionfile],{type : 'application/json'})
|
||||
|
||||
saveAs(blob, new Date().toJSON().slice(0,10)+'_'+new Date().toJSON().slice(11,16) + '.json')
|
||||
|
||||
editor.unsavedSession = false
|
||||
|
||||
}
|
||||
|
||||
list(data) {
|
||||
|
||||
for (let i in data) {
|
||||
|
||||
var max = 50
|
||||
var path = data[i],
|
||||
file = path.split('\\').pop().split('/').pop(),
|
||||
length = data[i].length
|
||||
|
||||
path = path.replace(file,'')
|
||||
|
||||
if (length > max - 3) {
|
||||
path = path.substr(0,Math.floor((path.length)/2)-(length-max)/2) + '...' + path.substr(Math.ceil((path.length)/2)+(length-max)/2, path.length)
|
||||
}
|
||||
|
||||
lobby.list.appendChild(html`
|
||||
<a href="#" tabIndex="0" class="btn load" data-session="${data[i]}">
|
||||
${file} <em style="opacity:0.45">(${path})</em>
|
||||
${READ_ONLY? '' : html`<span>${raw(icon('times'))}</span>`}
|
||||
</a>
|
||||
`)
|
||||
|
||||
}
|
||||
|
||||
lobby.list.addEventListener('click', function(e){
|
||||
e.preventDefault()
|
||||
if (e.target.hasAttribute('data-session')) {
|
||||
ipc.send('sessionOpen',{path:e.target.getAttribute('data-session')})
|
||||
} else if (!READ_ONLY && e.target.tagName == 'SPAN') {
|
||||
ipc.send('sessionRemoveFromHistory',e.target.parentNode.getAttribute('data-session'))
|
||||
lobby.list.removeChild(e.target.parentNode)
|
||||
}
|
||||
})
|
||||
|
||||
if (!READ_ONLY) {
|
||||
|
||||
var brw = lobby.footer.appendChild(html`<a href="#" tabindex="0" class="btn browse">${raw(icon('folder-open'))} ${locales('session_browse')}</a>`)
|
||||
var imp = lobby.footer.appendChild(html`<a href="#" tabindex="0" class="btn import">${raw(icon('upload'))} ${locales('editor_import')}</a>`)
|
||||
var nws = lobby.footer.appendChild(html`<a href="#" tabindex="0" class="btn new">${raw(icon('file'))} ${locales('session_new')}</a>`)
|
||||
|
||||
brw.addEventListener('click', (e)=>{
|
||||
this.browse()
|
||||
})
|
||||
|
||||
imp.addEventListener('click', (e)=>{
|
||||
this.import()
|
||||
})
|
||||
|
||||
nws.addEventListener('click', (e)=>{
|
||||
this.create()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
DOM.addEventListener(lobby.html, 'mousedown touchstart', function(e){
|
||||
if (e.type == 'mousedown') e.preventDefault()
|
||||
e.target.blur()
|
||||
})
|
||||
|
||||
lobby.open()
|
||||
|
||||
}
|
||||
|
||||
open(data) {
|
||||
|
||||
this.load(data.session, ()=>{
|
||||
this.setSessionPath(data.path)
|
||||
ipc.send('sessionOpened', {path: data.path})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
browse() {
|
||||
|
||||
if (READ_ONLY) return
|
||||
|
||||
remoteBrowse({extension: 'json', directory: this.lastDir}, (path)=>{
|
||||
if (editor.unsavedSession && !confirm(locales('session_unsaved'))) return
|
||||
this.lastDir = path[0]
|
||||
ipc.send('sessionOpen',{path:path})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
import() {
|
||||
|
||||
if (READ_ONLY) return
|
||||
|
||||
upload('.json', (path, result)=>{
|
||||
var session
|
||||
try {
|
||||
session = JSON.parse(result)
|
||||
} catch (err) {
|
||||
new Popup({title: locales('session_parsingerror'), content: err, icon: 'exclamation-triangle', closable:true})
|
||||
}
|
||||
if (editor.unsavedSession && !confirm(locales('session_unsaved'))) return
|
||||
if (session) sessionManager.load(session, ()=>{
|
||||
ipc.send('sessionSetPath', {path: 'imported'})
|
||||
})
|
||||
|
||||
}, ()=>{
|
||||
new Popup({title: locales('error'), content: locales('session_uploaderror'), icon: 'exclamation-triangle', closable:true})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
create() {
|
||||
this.load({type: 'root'},function(){
|
||||
require('../ui/sidepanel').open()
|
||||
editor.enable()
|
||||
editor.select(widgetManager.getWidgetById('root'))
|
||||
})
|
||||
}
|
||||
|
||||
setSessionPath(path) {
|
||||
|
||||
this.sessionPath = path
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var sessionManager = new SessionManager()
|
||||
|
||||
module.exports = sessionManager
|
||||
251
src/client/managers/state.js
Normal file
251
src/client/managers/state.js
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
var ipc = require('../ipc'),
|
||||
widgetManager = require('./widgets'),
|
||||
{upload, remoteBrowse} = require('../ui/utils'),
|
||||
notifications = require('../ui/notifications'),
|
||||
{saveAs} = require('file-saver'),
|
||||
locales = require('../locales'),
|
||||
{deepCopy} = require('../utils')
|
||||
|
||||
|
||||
var StateManager = class StateManager {
|
||||
|
||||
constructor() {
|
||||
|
||||
this.statePath = ''
|
||||
this.lastDir = null
|
||||
|
||||
this.quickState = []
|
||||
|
||||
this.valueStateQueue = {}
|
||||
this.valueOldPropQueue = {}
|
||||
this.valueNewPropQueue = {}
|
||||
this.queueCounter = 0
|
||||
|
||||
}
|
||||
|
||||
get(filter) {
|
||||
|
||||
var data = {}
|
||||
|
||||
for (let i in widgetManager.widgets) {
|
||||
|
||||
var widget = widgetManager.widgets[i]
|
||||
|
||||
if (filter && !filter(widget)) continue
|
||||
|
||||
if (widget.setValue && widget.getValue) {
|
||||
|
||||
var v = widget.getValue()
|
||||
|
||||
if (v !== undefined) {
|
||||
|
||||
data[widget.getProp('id')] = v
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
}
|
||||
|
||||
set(preset,send) {
|
||||
|
||||
for (let id in preset) {
|
||||
|
||||
var value = preset[id],
|
||||
widgets = widgetManager.getWidgetById(id)
|
||||
|
||||
if (widgets.length) {
|
||||
|
||||
for (var j = widgets.length - 1; j >= 0; j--) {
|
||||
|
||||
if (widgets[j].setValue) {
|
||||
|
||||
widgets[j].setValue(value, {send:send, sync:true, fromState:true})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
send(options) {
|
||||
|
||||
for (let i in widgetManager.widgets) {
|
||||
|
||||
var widget = widgetManager.widgets[i]
|
||||
|
||||
if (widget.sendValue) {
|
||||
widget.sendValue(null, options)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
save(path) {
|
||||
|
||||
if (path) this.statePath = path
|
||||
|
||||
if (!this.statePath) return this.saveAs()
|
||||
|
||||
ipc.send('stateSave', {
|
||||
session: JSON.stringify(this.get(), null, ' '),
|
||||
path: this.statePath
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
saveAs() {
|
||||
|
||||
remoteBrowse({extension: 'state', save:true, directory: this.lastDir}, (path)=>{
|
||||
this.lastDir = path[0]
|
||||
this.save(path)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
browse() {
|
||||
|
||||
remoteBrowse({extension: 'state', directory: this.lastDir}, (path)=>{
|
||||
this.lastDir = path[0]
|
||||
ipc.send('stateOpen',{path: path})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
load(state, send, path) {
|
||||
|
||||
if (!state) return
|
||||
|
||||
this.setStatePath(path)
|
||||
|
||||
try {
|
||||
if (typeof state === 'string') {
|
||||
state = JSON.parse(state)
|
||||
}
|
||||
|
||||
if (Array.isArray(state)) {
|
||||
// backward compatibility
|
||||
var ostate = {}
|
||||
for (var i in state) {
|
||||
ostate[state[i][0]] = state[i][1]
|
||||
}
|
||||
state = ostate
|
||||
}
|
||||
|
||||
this.set(state, send)
|
||||
notifications.add({
|
||||
icon: 'sliders-h',
|
||||
message: locales('state_recallsuccess')
|
||||
})
|
||||
} catch(e) {
|
||||
this.loadError()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
loadError(){
|
||||
|
||||
notifications.add({
|
||||
class: 'error',
|
||||
message: locales('state_uploaderror')
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
import() {
|
||||
|
||||
upload('.state', (path, result)=>{
|
||||
this.load(result, true)
|
||||
}, (e)=>{
|
||||
this.loadError()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export() {
|
||||
|
||||
var state = JSON.stringify(this.get(),null,' ')
|
||||
var blob = new Blob([state],{type : 'application/json'})
|
||||
saveAs(blob, new Date().toJSON().slice(0,10)+'_'+new Date().toJSON().slice(11,16) + '.state')
|
||||
|
||||
}
|
||||
|
||||
quickSave() {
|
||||
|
||||
this.quickState = deepCopy(this.get())
|
||||
|
||||
}
|
||||
|
||||
quickLoad() {
|
||||
|
||||
this.load(this.quickState, true)
|
||||
|
||||
}
|
||||
|
||||
setStatePath(path) {
|
||||
|
||||
if (!path) return
|
||||
this.statePath = path
|
||||
|
||||
}
|
||||
|
||||
pushValueState(id, value) {
|
||||
this.valueStateQueue[id] = value
|
||||
}
|
||||
|
||||
pushValueOldProp(id, value) {
|
||||
this.valueOldPropQueue[id] = typeof value == 'object' ? JSON.stringify(value) : value
|
||||
}
|
||||
|
||||
pushValueNewProp(id, value) {
|
||||
this.valueNewPropQueue[id] = typeof value == 'object' ? JSON.stringify(value) : value
|
||||
if (this.queueCounter == 0) this.flush()
|
||||
}
|
||||
|
||||
flush(){
|
||||
|
||||
for (let id in this.valueStateQueue) {
|
||||
if (this.valueStateQueue[id] !== undefined) {
|
||||
for (let w of widgetManager.getWidgetById(id)) {
|
||||
w.setValue(this.valueStateQueue[id], {sync: true, fromState:true})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let id in this.valueNewPropQueue) {
|
||||
if (this.valueNewPropQueue[id] != this.valueOldPropQueue[id]) {
|
||||
for (let w of widgetManager.getWidgetById(id)) {
|
||||
w.setValue(w.getProp('value'), {sync: true, fromState:true})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.valueStateQueue = {}
|
||||
this.valueOldPropQueue = {}
|
||||
this.valueNewPropQueue = {}
|
||||
}
|
||||
|
||||
incrementQueue() {
|
||||
this.queueCounter++
|
||||
}
|
||||
|
||||
decrementQueue() {
|
||||
this.queueCounter--
|
||||
if (this.queueCounter == 0) {
|
||||
this.flush()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var stateManager = new StateManager()
|
||||
|
||||
module.exports = stateManager
|
||||
337
src/client/managers/widgets.js
Normal file
337
src/client/managers/widgets.js
Normal file
|
|
@ -0,0 +1,337 @@
|
|||
var EventEmitter = require('../events/event-emitter'),
|
||||
ipc = require('../ipc/')
|
||||
|
||||
var WidgetManager = class WidgetManager extends EventEmitter {
|
||||
|
||||
constructor() {
|
||||
|
||||
super()
|
||||
|
||||
this.widgets = {}
|
||||
this.children = []
|
||||
|
||||
this.addressRoute = {}
|
||||
this.idRoute = {}
|
||||
this.linkIdRoute = {}
|
||||
|
||||
this.linkIdLock = []
|
||||
this.linkIdLockDepth = 0
|
||||
|
||||
this.scrollingWidgets = []
|
||||
|
||||
this.preArgsSeparator = '||||'
|
||||
|
||||
this.on('change', this.onChange.bind(this))
|
||||
|
||||
ipc.on('connect', ()=>{
|
||||
for (var hash in this.widgets) {
|
||||
this.registerWidget(this.widgets[hash])
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
onChange(e) {
|
||||
|
||||
var {id, widget, linkId, options} = e
|
||||
|
||||
if (linkId) {
|
||||
if (!Array.isArray(linkId)) linkId = [linkId]
|
||||
linkId = linkId.map(x=>x.replace(/^>>\s*/, '')).filter(x=>x.indexOf('<<') < 0)
|
||||
linkId = linkId.filter(x=>this.linkIdLock.indexOf(x)<0)
|
||||
this.linkIdLock = this.linkIdLock.concat(linkId)
|
||||
this.linkIdLockDepth++
|
||||
}
|
||||
|
||||
var widgetsById = this.getWidgetById(id),
|
||||
widgetsByLinkId = linkId ? this.getWidgetByLinkId(linkId) : []
|
||||
|
||||
// Widget that share the same id will update each other
|
||||
// without sending any extra osc message
|
||||
if (widgetsById.length > 1) {
|
||||
let v = widget.getValue()
|
||||
for (let i in widgetsById) {
|
||||
if (widgetsById[i] !== widget) {
|
||||
widgetsById[i].setValue(v,{send:false,sync:false, id})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// widgets that share the same linkId will update each other.
|
||||
// Updated widgets will send osc messages normally
|
||||
if (widgetsByLinkId.length > 0) {
|
||||
let v = widget.getValue()
|
||||
for (let i in widgetsByLinkId) {
|
||||
if (widgetsByLinkId[i] !== widget) {
|
||||
widgetsByLinkId[i].setValue(v,{send: options.send,sync: true, id})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (linkId && this.linkIdLockDepth) {
|
||||
this.linkIdLockDepth--
|
||||
if (!this.linkIdLockDepth) {
|
||||
this.linkIdLock = []
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
createAddressRef(widget, preArgs, address) {
|
||||
|
||||
preArgs = widget ? widget.getProp('preArgs') : preArgs
|
||||
address = widget ? widget.getProp('address') : address
|
||||
|
||||
if (!Array.isArray(preArgs) && preArgs !== '') preArgs = [preArgs]
|
||||
if (preArgs === '') preArgs = []
|
||||
|
||||
preArgs = preArgs.map(x=>typeof x === 'object' && x !== null && x.value !== undefined ? x.value : x)
|
||||
|
||||
return preArgs && preArgs.length ?
|
||||
address + this.preArgsSeparator + preArgs.join(this.preArgsSeparator)
|
||||
: address
|
||||
|
||||
}
|
||||
|
||||
addWidget(widget) {
|
||||
|
||||
var hash = widget.hash,
|
||||
address = this.createAddressRef(widget),
|
||||
id = widget.getProp('id'),
|
||||
linkId = widget.getProp('linkId'),
|
||||
scroll = widget.getProp('scroll')
|
||||
|
||||
|
||||
this.widgets[hash] = widget
|
||||
|
||||
if (!this.idRoute[id]) this.idRoute[id] = []
|
||||
this.idRoute[id].push(hash)
|
||||
|
||||
if (address) {
|
||||
if (!this.addressRoute[address]) this.addressRoute[address] = []
|
||||
this.addressRoute[address].push(hash)
|
||||
}
|
||||
|
||||
if (linkId) {
|
||||
if (!Array.isArray(linkId)) linkId = [linkId]
|
||||
linkId = linkId.map(x=>x.replace(/^<<\s*/, '')).filter(x=>x.indexOf('>>') < 0)
|
||||
for (var i in linkId) {
|
||||
if (linkId[i]) {
|
||||
if (!this.linkIdRoute[linkId[i]]) this.linkIdRoute[linkId[i]] = []
|
||||
this.linkIdRoute[linkId[i]].push(hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (scroll) {
|
||||
this.scrollingWidgets.push(hash)
|
||||
}
|
||||
|
||||
this.registerWidget(widget)
|
||||
|
||||
}
|
||||
|
||||
registerWidget(widget, updatedData, oldData) {
|
||||
|
||||
ipc.send('addWidget', {
|
||||
hash: widget.hash,
|
||||
data: updatedData || {
|
||||
precision: widget.getProp('precision'),
|
||||
preArgs: widget.getProp('preArgs'),
|
||||
split: widget.getSplit(),
|
||||
target: widget.getProp('target'),
|
||||
address: widget.getProp('address'),
|
||||
noSync: widget.getProp('noSync'),
|
||||
}
|
||||
})
|
||||
|
||||
if (updatedData && oldData) {
|
||||
var address = this.createAddressRef(null, oldData.preArgs, oldData.address),
|
||||
newAddress = this.createAddressRef(widget),
|
||||
hash = widget.hash
|
||||
|
||||
if (address && this.addressRoute[address].indexOf(hash) != -1) this.addressRoute[address].splice(this.addressRoute[address].indexOf(hash), 1)
|
||||
|
||||
if (!this.addressRoute[newAddress]) this.addressRoute[newAddress] = []
|
||||
this.addressRoute[newAddress].push(hash)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
removeWidget(widget) {
|
||||
|
||||
var hash = widget.hash,
|
||||
address = this.createAddressRef(widget),
|
||||
linkId = widget.getProp('linkId'),
|
||||
id = widget.getProp('id'),
|
||||
scroll = widget.getProp('scroll')
|
||||
|
||||
if (this.widgets[hash]) {
|
||||
if (this.widgets[hash].onRemove) this.widgets[hash].onRemove()
|
||||
|
||||
delete this.widgets[hash]
|
||||
|
||||
ipc.send('removeWidget', {
|
||||
hash:hash
|
||||
})
|
||||
|
||||
widget.trigger('widget-removed', {widget: widget})
|
||||
|
||||
}
|
||||
if (id && this.idRoute[id].indexOf(hash) != -1) this.idRoute[id].splice(this.idRoute[id].indexOf(hash), 1)
|
||||
if (address && this.addressRoute[address].indexOf(hash) != -1) this.addressRoute[address].splice(this.addressRoute[address].indexOf(hash), 1)
|
||||
if (scroll && this.scrollingWidgets.indexOf(hash) != -1) this.scrollingWidgets.splice(this.scrollingWidgets.indexOf(hash), 1)
|
||||
|
||||
if (!Array.isArray(linkId)) linkId = [linkId]
|
||||
for (var i in linkId) {
|
||||
if (linkId[i] && this.linkIdRoute[linkId[i]] && this.linkIdRoute[linkId[i]].indexOf(hash) != -1) this.linkIdRoute[linkId[i]].splice(this.linkIdRoute[linkId[i]].indexOf(hash), 1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
removeWidgets(widgets, nested) {
|
||||
|
||||
for (let i in widgets) {
|
||||
|
||||
if (Array.isArray(widgets[i])) {
|
||||
|
||||
this.removeWidgets(widgets[i], true)
|
||||
|
||||
} else if (this.widgets[widgets[i].hash]) {
|
||||
|
||||
this.removeWidget(widgets[i])
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!nested) this.purge()
|
||||
|
||||
}
|
||||
|
||||
purge() {
|
||||
|
||||
for (let route of [this.addressRoute, this.idRoute, this.linkIdRoute]) {
|
||||
for (let key in route) {
|
||||
for (let i=route[key].length-1; i>=0; i--) {
|
||||
let hash = route[key][i]
|
||||
if (!this.widgets[hash]) {
|
||||
route[key].splice(i, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i in this.scrollingWidgets) {
|
||||
if (!this.widgets[this.scrollingWidgets[i]]) {
|
||||
this.scrollingWidgets.splice(i, 1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
reset() {
|
||||
|
||||
this.removeWidgets(Object.values(this.widgets))
|
||||
|
||||
this.off()
|
||||
|
||||
this.on('change', this.onChange.bind(this))
|
||||
|
||||
}
|
||||
|
||||
|
||||
getWidgetBy(key, dict, widgets = []) {
|
||||
|
||||
var hash, w
|
||||
|
||||
if (Array.isArray(key)) {
|
||||
for (let i in key) {
|
||||
this.getWidgetBy(key[i], dict, widgets)
|
||||
}
|
||||
return widgets
|
||||
}
|
||||
|
||||
if (dict[key]) {
|
||||
for (let i = dict[key].length-1; i>=0; i--) {
|
||||
hash = dict[key][i]
|
||||
w = this.widgets[hash]
|
||||
if (!w) {
|
||||
dict[key].splice(i,1)
|
||||
} else if (widgets.indexOf(w) == -1) {
|
||||
widgets.push(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return widgets
|
||||
|
||||
}
|
||||
|
||||
getWidgetById(id) {
|
||||
|
||||
return this.getWidgetBy(id, this.idRoute)
|
||||
|
||||
}
|
||||
|
||||
getWidgetByLinkId(linkId) {
|
||||
|
||||
return this.getWidgetBy(linkId, this.linkIdRoute)
|
||||
|
||||
}
|
||||
|
||||
getWidgetByAddress(address) {
|
||||
|
||||
return this.getWidgetBy(address, this.addressRoute)
|
||||
|
||||
}
|
||||
|
||||
getWidgetByAddressAndArgs(address, args) {
|
||||
|
||||
var addressref = address,
|
||||
restArgs = args
|
||||
|
||||
if (typeof args == 'object' && args != null) {
|
||||
for (var i = args.length; i >= 0; i--) {
|
||||
|
||||
var ref = this.createAddressRef(null, args.slice(0,i), address)
|
||||
|
||||
if (this.getWidgetByAddress(ref).length) {
|
||||
addressref = ref
|
||||
restArgs = args.slice(i, args.length)
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
restArgs = args
|
||||
}
|
||||
|
||||
|
||||
if (restArgs == null || restArgs.length == 0) restArgs = null
|
||||
else if (restArgs.length == 1) restArgs = restArgs[0]
|
||||
|
||||
|
||||
var widgets = this.getWidgetByAddress(addressref)
|
||||
|
||||
return [
|
||||
widgets,
|
||||
restArgs
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
getWidgetByElement(e, filter = '') {
|
||||
var element = e ? e.closest('[data-widget]' + filter) : undefined
|
||||
if (element) {
|
||||
return element._widget_instance ?
|
||||
element._widget_instance :
|
||||
this.widgets[element.getAttribute('data-widget')]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var widgetManager = new WidgetManager()
|
||||
|
||||
module.exports = widgetManager
|
||||
Loading…
Add table
Add a link
Reference in a new issue