-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathScript.js
More file actions
106 lines (95 loc) · 2.47 KB
/
Script.js
File metadata and controls
106 lines (95 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @license
* Copyright 2015 The FOAM Authors. All Rights Reserved.
* http://www.apache.org/licenses/LICENSE-2.0
*/
foam.CLASS({
package: 'foam.core.reflow',
name: 'Script',
imports: [
'data as block',
'eval_',
'scope',
'notify?'
],
documentation: `
In scripts, 'this' is bound to the containing block, so you can do things like:
this.tag(foam.core.reflow.Clock.create({x:400,y:400}));
As a result, log() will log to the Script output,
but this.log() will log to the block, ie. in the FLOW document itself.
`,
properties: [
{
class: 'Long',
name: 'id',
visibility: 'RO'
},
{
class: 'String',
name: 'scriptName'
},
{
class: 'String',
name: 'description'
},
{
class: 'String',
name: 'code',
reactive: false,
view: {
class: 'foam.u2.view.ObjAltView',
views: [
[ {class: 'foam.u2.view.CodeView', config: { width: '100%', mode: 'JAVASCRIPT', showGutter: false }}, 'Code'],
[ {class: 'foam.u2.tag.TextArea', rows: 16 }, 'Plain Text']
]
},
displayWidth: 60
},
{
class: 'String',
name: 'output',
transient: true,
reactive: false,
view: { class: 'foam.u2.tag.TextArea', rows: 8 },
displayWidth: 60
},
{ class: 'Boolean', name: 'autoRun', view: { class: 'foam.u2.Switch' } }
],
methods: [
function onLoad() {
if ( this.autoRun ) return this.run();
},
function log() {
this.output += Array.from(arguments).join(' ') + '\n';
}
],
actions: [
function run() {
let self = this;
var _scope = this.scope || {};
with ( _scope ) {
with ( { log: this.log.bind(this) } ) {
var ret = eval('(async function() {' + self.code + '})').call(self.block);
ret
.then(v => {
this.log(v);
try { this.notify && this.notify(`Script${this.scriptName ? ' "' + this.scriptName + '"' : ''} executed successfully`); } catch (e) {}
}, v => this.log(v))
.catch(e => this.log(e.stack));
return ret;
}
}
},
function clearOutput() {
this.output = '';
},
{
name: 'createTest',
availablePermissions: [ 'command.read.test' ],
code: async function() {
var name = this.block.flowName;
this.eval_(`test(${name}.output, 'Test script output for ${name}')`);
}
}
]
});