-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressions-proxy.coffee
108 lines (78 loc) · 3.39 KB
/
expressions-proxy.coffee
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
107
108
request = require('browser-request')
class ExpressionEngine
onMessage: (event) =>
@source = event.source
@origin = event.origin
try
message = JSON.parse(event.data)
catch
return
@id = message.id
@[message.name](event, message)
prepare: (event, message) ->
$$runtime.form = message.form
$$prepare()
@setupHostFunctions()
@respond(name: 'prepare')
expressions: (event, message) ->
$$runtime.expressions = message.expressions
@respond(name: 'expressions')
values: (event, message) ->
$$runtime.values = message.values or {}
if message.customVariables
$$runtime.customVariables = message.customVariables
if message.featureVariables
for key of message.featureVariables
$$runtime[key] = message.featureVariables[key]
@respond(name: 'values')
globals: (event, message) ->
for key of message.globals
$$runtime[key] = message.globals[key]
@respond(name: 'globals')
evaluate: (event, message) ->
results = $$evaluate()
@respond(name: 'evaluate', results: results)
triggerDataEvent: (event, message) ->
$$runtime.event = message.event or null
results = $$trigger()
@respond(name: 'triggerDataEvent', results: results)
finishAsyncCallback: (event, message) ->
$$runtime.callbackID = message.callbackID or null
$$runtime.callbackArguments = message.callbackArguments or []
results = $$finishAsync()
@respond(name: 'finishAsyncCallback', results: results)
respond: (message) ->
try
message.id ?= @id
response = JSON.stringify(message)
catch e
response = null
console.log 'Error sending object', message
@source.postMessage(response, @origin)
httpRequest: (event, message) ->
# not supported
delete message.options.followRedirect
request message.options, (err, res, body) =>
@respond(id: message.id, name: 'httpRequest', result: [err, res, body])
setupHostFunctions: ->
$$runtime["$$httpRequest"] = (options, callbackID) =>
@respond(name: 'hostFunction', functionName: 'httpRequest', argumentList: [options, callbackID])
$$runtime["$$messageBox"] = (options, callbackID) =>
@respond(name: 'hostFunction', functionName: 'messageBox', argumentList: [options, callbackID])
$$runtime["$$setTimeout"] = (delay, callbackID) =>
@respond(name: 'hostFunction', functionName: 'setTimeout', argumentList: [delay, callbackID])
$$runtime["$$inference"] = (options, callbackID) =>
@respond(name: 'hostFunction', functionName: 'inference', argumentList: [options, callbackID])
$$runtime["$$loadFile"] = (options, callbackID) =>
@respond(name: 'hostFunction', functionName: 'loadFile', argumentList: [options, callbackID])
$$runtime["$$loadRecords"] = (options, callbackID) =>
@respond(name: 'hostFunction', functionName: 'loadRecords', argumentList: [options, callbackID])
$$runtime["$$loadForm"] = (options, callbackID) =>
@respond(name: 'hostFunction', functionName: 'loadForm', argumentList: [options, callbackID])
$$runtime["$$recognizeText"] = (options, callbackID) =>
@respond(name: 'hostFunction', functionName: 'recognizeText', argumentList: [options, callbackID])
proxy = new ExpressionEngine
if window.addEventListener
window.addEventListener('message', proxy.onMessage.bind(proxy), false)
else
window.attachEvent('message', (event) -> proxy.onMessage(event))