-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormItInterface.js
More file actions
186 lines (173 loc) · 7.1 KB
/
Copy pathFormItInterface.js
File metadata and controls
186 lines (173 loc) · 7.1 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
FormItInterface = {};
const WINDOWS = "Windows";
const WEB = "Web";
if (window.location.href.indexOf("?Web") > -1)
{
FormItInterface.Platform = WEB;
}
else
{
FormItInterface.Platform = WINDOWS;
// Add the qwebchannel script only for Windows.
document.write('<script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>');
}
FormItInterface.CallMethod = function(method, args, callbackMethod)
{
if(FormItInterface.Platform == WEB)
{
postRobot.send(parent, 'FormIt.PluginMsg', {'method': method, 'args': args},{ timeout: 60000 }).then(function(event) {
//console.log('Event Data: ', JSON.stringify(event.data));
if (callbackMethod)
{
callbackMethod(event.data);
}
}).catch(function(err) {
// Handle any errors that stopped our call from going through
console.error(err);
});
}
else
{
console.log("Platform has not been integrated yet!");
}
}
FormItInterface.CallMethodDirect = function(args, callbackMethod)
{
if(FormItInterface.Platform == WEB)
{
postRobot.send(parent, 'FormIt.PluginMsgDirect', {'args': args}).then(function(event) {
if (callbackMethod)
{
callbackMethod(event.data);
}
}).catch(function(err) {
// Handle any errors that stopped our call from going through
console.error(err);
});
}
else
{
console.log("Platform has not been integrated yet!");
}
}
//TODO(hauswij): Remove this once all the plugins are converted to use FormItInterface.Initialize.
FormItInterface.AddEventListener = function(eventSignal, callbackMethod)
{
FormItInterface.Initialize(callbackMethod);
}
FormItInterface.Initialize = function(callbackMethod)
{
FormItInterface.PluginWindow = window;
//console.log(`---FormItInterface.AddEventListener: ${"DOMContentLoaded"}`);
if(FormItInterface.Platform == WINDOWS)
{
document.addEventListener("DOMContentLoaded", function ()
{
//console.log("new QWebChannel");
new QWebChannel(qt.webChannelTransport, function (channel)
{
FormItInterface.EmitMessage = channel.objects.FormItInterface.EmitMessage;
// This will display the message in both the web debugger connected to the HTML UI
// and the console in FormIt.
FormItInterface.ConsoleLog = function(msg)
{
console.log(msg);
channel.objects.FormItInterface.ConsoleLog(msg);
}
FormItInterface.SubscribeMessage = function(msg, handler)
{
channel.objects.FormItInterface.SubscribeMessage(msg);
if (handler)
{
channel.objects.FormItInterface.FormItMessage[msg] = handler;
if (!channel.objects.FormItInterface.FormItMessage.connected)
{
var msgFunc = function(arg)
{
var jsonArg = JSON.parse(arg);
channel.objects.FormItInterface.FormItMessage[jsonArg.msg](jsonArg.payload)
};
channel.objects.FormItInterface.FormItMessage.connect(msgFunc);
channel.objects.FormItInterface.FormItMessage.connected = true;
}
}
}
FormItInterface.UnsubscribeMessage = channel.objects.FormItInterface.UnsubscribeMessage;
FormItInterface.CallMethod = function(method, args, callbackMethod)
{
var stringArgs = JSON.stringify(args);
if (callbackMethod == null)
{
channel.objects.FormItInterface.CallMethod(method, stringArgs);
}
else
{
channel.objects.FormItInterface.CallMethod(method, stringArgs, callbackMethod);
}
}
FormItInterface.CallMethodDirect = function(args, callbackMethod)
{
var stringArgs = JSON.stringify(args);
channel.objects.FormItInterface.CallMethodDirect(stringArgs, callbackMethod);
}
window.PluginDialog = channel.objects.PluginDialog;
if (callbackMethod)
{
callbackMethod();
}
});
});
} else
{
// Add the post-robot script only for Web.
const script = document.createElement('script');
script.src = "https://formit3d.github.io/FormItExamplePlugins/SharedPluginFiles/post-robot.js";
script.type = 'text/javascript';
script.onload = () => {
postRobot.CONFIG.LOG_LEVEL = 'error';
postRobot.CONFIG.ACK_TIMEOUT = 60000;
FormItInterface.ConsoleLog = console.log;
FormItInterface.SubscribeMessage = function(msg, handler)
{
if (!FormItInterface.MessageHandlers)
{
FormItInterface.MessageHandlers = [];
}
FormItInterface.MessageHandlers[msg] = handler;
//console.log("Subscribing to:" + msg + "\n");
postRobot.send(parent, 'FormIt.PubSub', msg).then(function(event) {
//console.log('Event Data: ', JSON.stringify(event.data));
}).catch(function(err) {
// Handle any errors that stopped our call from going through
console.error(err);
});
};
FormItInterface.UnsubscribeMessage = function(msg) { FormItInterface.MessageHandlers[msg] = undefined; };
if (!postRobot['FormItPluginMsgEventInitialized'])
{
postRobot.on('FormIt.PluginMsgEvent',
function(event) {
//console.log('(Web side) msg: ', event.data);
var jsonMessage = JSON.parse(event.data);
var msgHandler = FormItInterface.MessageHandlers[jsonMessage.msg];
if (!!msgHandler)
{
msgHandler(event.data);
}
});
postRobot.FormItPluginMsgEventInitialized = true;
}
if (callbackMethod)
{
callbackMethod();
}
};
script.onabort = () => { throw new Error((`load '${script.src}' - aborted`)) };
script.onerror = () => { throw new Error((`load '${script.src}' - error`)) };
document.head.appendChild(script);
window.PluginDialog = window.PluginDialog || {};
window.PluginDialog.Close = function(dialogNamespace) {
FormItInterface.CallMethod(`${dialogNamespace}.Close`, "");
};
}
}