-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
173 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,197 +1,197 @@ | ||
"use strict"; | ||
'use strict'; | ||
|
||
define(["./versioning", "./sandbox"], function(Versioning, Sandbox) { | ||
let API; | ||
let evaluatedScripts; | ||
define(['./versioning', './sandbox'], function (Versioning, Sandbox) { | ||
let API; | ||
let evaluatedScripts; | ||
|
||
function doScripts() { | ||
const data = getActionScripts(); | ||
function doScripts() { | ||
const data = getActionScripts(); | ||
|
||
if (!data || evaluatedScripts) { | ||
return; | ||
} | ||
if (!data || evaluatedScripts) { | ||
return; | ||
} | ||
|
||
const evalSandbox = new Sandbox(); | ||
evalSandbox.setContext({ API }); | ||
const evaled = {}; | ||
const evalSandbox = new Sandbox(); | ||
evalSandbox.setContext({ API }); | ||
const evaled = {}; | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
const action = data[i].groups.action[0]; | ||
evaled[action.name[0]] = evalSandbox.run( | ||
`(function(value) { ${action.script[0]} })` | ||
); | ||
} | ||
|
||
evaluatedScripts = evaled; | ||
for (let i = 0; i < data.length; i++) { | ||
const action = data[i].groups.action[0]; | ||
evaled[action.name[0]] = evalSandbox.run( | ||
`(function(value) { ${action.script[0]} })` | ||
); | ||
} | ||
|
||
function getActionScripts() { | ||
return Versioning.getView().actionscripts || []; | ||
} | ||
evaluatedScripts = evaled; | ||
} | ||
|
||
function setActionScripts(form) { | ||
evaluatedScripts = undefined; | ||
Versioning.getView().actionscripts = form; // Keeps track of the scripts in the view | ||
doScripts(form); | ||
} | ||
function getActionScripts() { | ||
return Versioning.getView().actionscripts || []; | ||
} | ||
|
||
let actionsFiles; | ||
function setActionScripts(form) { | ||
evaluatedScripts = undefined; | ||
Versioning.getView().actionscripts = form; // Keeps track of the scripts in the view | ||
doScripts(form); | ||
} | ||
|
||
function setActionFiles(form) { | ||
Versioning.getView().actionfiles = form; | ||
doFiles(); | ||
} | ||
let actionsFiles; | ||
|
||
function doFiles() { | ||
const files = Versioning.getView().actionfiles; | ||
function setActionFiles(form) { | ||
Versioning.getView().actionfiles = form; | ||
doFiles(); | ||
} | ||
|
||
actionsFiles = {}; | ||
function doFiles() { | ||
const files = Versioning.getView().actionfiles; | ||
|
||
if (!files) { | ||
return; | ||
} | ||
actionsFiles = {}; | ||
|
||
for (let i = 0, l = files[0].groups.action[0].length; i < l; i++) { | ||
actionsFiles[files[0].groups.action[0][i].name] = | ||
actionsFiles[files[0].groups.action[0][i].name] || []; | ||
actionsFiles[files[0].groups.action[0][i].name].push({ | ||
file: files[0].groups.action[0][i].file, | ||
mode: files[0].groups.action[0][i].mode | ||
}); | ||
} | ||
if (!files) { | ||
return; | ||
} | ||
|
||
function executeActionFile(file, value) { | ||
switch (file.mode) { | ||
case "amd": { | ||
require([file.file], function(File) { | ||
File(value); | ||
}); | ||
break; | ||
} | ||
case "worker": { | ||
const worker = new Worker(file.file); | ||
|
||
worker.postMessage({ method: "actionValue", value: value }); | ||
|
||
worker.onmessage = function(event) { | ||
if (!event.data.method) { | ||
return; | ||
} | ||
|
||
switch (event.data.method) { | ||
case "getVar": { | ||
if (!Array.isArray(event.data.variables)) { | ||
return; | ||
} | ||
|
||
const variables = {}; | ||
const varArray = event.data.variables; | ||
for (let i = 0; i < varArray.length; i++) { | ||
variables[varArray[i]] = API.getVar( | ||
varArray[i] | ||
); | ||
} | ||
|
||
worker.postMessage({ | ||
method: "getVar", | ||
variables: variables | ||
}); | ||
break; | ||
} | ||
case "setVar": { | ||
if (!event.data.variables) { | ||
return; | ||
} | ||
|
||
for (const i in event.data.variables) { | ||
API.setVar(i, event.data.variables[i]); | ||
} | ||
break; | ||
} | ||
case "terminate": { | ||
worker.terminate(); | ||
break; | ||
} | ||
case "sendAction": { | ||
if (!event.data.actionName) { | ||
return; | ||
} | ||
|
||
API.doAction( | ||
event.data.actionName, | ||
event.data.actionValue | ||
); | ||
break; | ||
} | ||
case "highlight": { | ||
if (!event.data.highlightId) { | ||
return; | ||
} | ||
|
||
API.highlightId( | ||
event.data.highlightId, | ||
event.data.highlightValue || false | ||
); | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
break; | ||
} | ||
} | ||
for (let i = 0, l = files[0].groups.action[0].length; i < l; i++) { | ||
actionsFiles[files[0].groups.action[0][i].name] = | ||
actionsFiles[files[0].groups.action[0][i].name] || []; | ||
actionsFiles[files[0].groups.action[0][i].name].push({ | ||
file: files[0].groups.action[0][i].file, | ||
mode: files[0].groups.action[0][i].mode | ||
}); | ||
} | ||
|
||
return { | ||
setScriptsFromForm(form) { | ||
setActionScripts(form); | ||
}, | ||
|
||
setFilesFromForm(form) { | ||
setActionFiles(form); | ||
}, | ||
|
||
viewHasChanged(view) { | ||
setActionScripts(view.actionscripts); | ||
doFiles(); | ||
}, | ||
|
||
getScriptsForm() { | ||
return Versioning.getView().actionscripts || []; | ||
}, | ||
|
||
getFilesForm() { | ||
return Versioning.getView().actionfiles || []; | ||
}, | ||
|
||
listActions() { | ||
let actions = []; | ||
if (actionsFiles) { | ||
actions.push(...Object.keys(actionsFiles)); | ||
} | ||
|
||
function executeActionFile(file, value) { | ||
switch (file.mode) { | ||
case 'amd': { | ||
require([file.file], function (File) { | ||
File(value); | ||
}); | ||
break; | ||
} | ||
case 'worker': { | ||
const worker = new Worker(file.file); | ||
|
||
worker.postMessage({ method: 'actionValue', value: value }); | ||
|
||
worker.onmessage = function (event) { | ||
if (!event.data.method) { | ||
return; | ||
} | ||
|
||
switch (event.data.method) { | ||
case 'getVar': { | ||
if (!Array.isArray(event.data.variables)) { | ||
return; | ||
} | ||
|
||
const variables = {}; | ||
const varArray = event.data.variables; | ||
for (let i = 0; i < varArray.length; i++) { | ||
variables[varArray[i]] = API.getVar( | ||
varArray[i] | ||
); | ||
} | ||
|
||
worker.postMessage({ | ||
method: 'getVar', | ||
variables: variables | ||
}); | ||
break; | ||
} | ||
if (evaluatedScripts) { | ||
actions.push(...Object.keys(evaluatedScripts)); | ||
case 'setVar': { | ||
if (!event.data.variables) { | ||
return; | ||
} | ||
|
||
for (const i in event.data.variables) { | ||
API.setVar(i, event.data.variables[i]); | ||
} | ||
break; | ||
} | ||
return actions; | ||
}, | ||
|
||
execute(actionName, actionValue) { | ||
if (evaluatedScripts && evaluatedScripts[actionName]) { | ||
setImmediate(function() { | ||
evaluatedScripts[actionName](actionValue); | ||
}); | ||
case 'terminate': { | ||
worker.terminate(); | ||
break; | ||
} | ||
|
||
if (actionsFiles && actionsFiles[actionName]) { | ||
for (let i = 0; i < actionsFiles[actionName].length; i++) { | ||
executeActionFile(actionsFiles[actionName][i], actionValue); | ||
} | ||
case 'sendAction': { | ||
if (!event.data.actionName) { | ||
return; | ||
} | ||
|
||
API.doAction( | ||
event.data.actionName, | ||
event.data.actionValue | ||
); | ||
break; | ||
} | ||
case 'highlight': { | ||
if (!event.data.highlightId) { | ||
return; | ||
} | ||
|
||
API.highlightId( | ||
event.data.highlightId, | ||
event.data.highlightValue || false | ||
); | ||
break; | ||
} | ||
}, | ||
} | ||
}; | ||
|
||
setAPI(value) { | ||
API = value; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return { | ||
setScriptsFromForm(form) { | ||
setActionScripts(form); | ||
}, | ||
|
||
setFilesFromForm(form) { | ||
setActionFiles(form); | ||
}, | ||
|
||
viewHasChanged(view) { | ||
setActionScripts(view.actionscripts); | ||
doFiles(); | ||
}, | ||
|
||
getScriptsForm() { | ||
return Versioning.getView().actionscripts || []; | ||
}, | ||
|
||
getFilesForm() { | ||
return Versioning.getView().actionfiles || []; | ||
}, | ||
|
||
listActions() { | ||
let actions = []; | ||
if (actionsFiles) { | ||
actions.push(...Object.keys(actionsFiles)); | ||
} | ||
if (evaluatedScripts) { | ||
actions.push(...Object.keys(evaluatedScripts)); | ||
} | ||
return actions; | ||
}, | ||
|
||
execute(actionName, actionValue) { | ||
if (evaluatedScripts && evaluatedScripts[actionName]) { | ||
setImmediate(function () { | ||
evaluatedScripts[actionName](actionValue); | ||
}); | ||
} | ||
|
||
if (actionsFiles && actionsFiles[actionName]) { | ||
for (let i = 0; i < actionsFiles[actionName].length; i++) { | ||
executeActionFile(actionsFiles[actionName][i], actionValue); | ||
} | ||
}; | ||
} | ||
}, | ||
|
||
setAPI(value) { | ||
API = value; | ||
} | ||
}; | ||
}); |