-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterEditor.js
More file actions
299 lines (266 loc) · 10.1 KB
/
ParameterEditor.js
File metadata and controls
299 lines (266 loc) · 10.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Communication Interface with the Surface Editor*/
class ParameterEditor {
static loaded = false;
static initialized = false;
// Constants which set when to
static UPDATE_MODES = {
ON_UPDATE: 'ON_UPDATE', // update external software every time output is updated
BY_REQUEST: 'BY_REQUEST' // only update external software when specifically requested
}
static OUTPUT_FORMATS = {
JAVASCRIPT_ARRAY: 'JAVASCRIPT_ARRAY',
JSON: 'JSON', // same as JAVASCRIPT_ARRAY but just converted to a string
CSV: 'CSV'
}
static id;
static iframe;
static update_mode;
static listeners;
/** Initialize Connection with Parameter Editor
*
* @param {Object} options (optional) {
* (optional) id {string}, // id of iframe to connect with
* (optional) output_format {string}, // format for output values
* (optional) output_steps_x {number}, // number of steps along x-axis to take surface output
* (optional) output_steps_y {number}, // number of steps along y-axis to take surface output
* (optional) listeners {Array<Function>} // listeners to receive editor data on updates
* }
*/
static init(options={}) {
// options to be sent to iframe
this._init_options = {
output_format: options.output_format,
output_steps_x: options.output_steps_x,
output_steps_y: options.output_steps_y,
};
this._setId(options.id);
this._initializeListeners(options.listeners);
this._initializeIframe();
}
/**
* This method asynchronously returns output data that can be used to save the editor's state
*
* @returns {Array<Object>} output_values : [
* {
* x: <number>, // The X coordinate or value.
* y: <number>, // The Y coordinate or value.
* z: <number>, // The Z coordinate or value.
* },
* // ... more x, y, z value coordinates
* ]
* Example usage:
* getOutput().then(output_values => {
* output_values.forEach(val => {
* console.log(val);
* });
* });
*/
static async getOutput() {
return await this._requestFromEditor({title: 'Get Output'}, 'Output');
}
/**
* Registers an event listener for updates to the editor's surface.
* When an update occurs, this listener invokes the provided callback function, `on_update`,
* passing it the event data structured as follows:
*
* @param {Function} on_update - Callback function to handle the event data. It accepts a single parameter:
* output_values: [
* {
* x: <number>, // The X coordinate or value.
* y: <number>, // The Y coordinate or value.
* z: <number>, // The Z coordinate or value.
* },
* // ... more x, y, z value coordinates
* ]
*
* Example usage:
* addUpdateListener(function(output_values) {
* output_values.forEach(val => {
* console.log(`X: ${val.x}, Y: ${val.y}, Z: ${val.z}`);
* });
* });
*/
static addUpdateListener(on_update) {
if (!on_update)
return;
if (!this.listeners.length)
this._listenForOutput();
this.listeners.push(on_update);
}
/**
* Registers an update event listener
* When an update occurs, this listener invokes the provided callback function, `on_update`,
* passing it the event data structured as follows:
*
* @param {Function} on_update - Callback function to identify which update event listener to remove
* Example usage:
* // update listener added earlier in code
* addUpdateListener(on_update);
* // ... later in code
* // remove update listener
* removeUpdateListener(on_update);
*/
static removeUpdateListener(on_update) {
this.listeners = this.listeners.filter(listener => listener != on_update);
if (!this.listeners.length)
this._stopListeningForOutput();
}
/**
* Set the format for outputs returned by the editor
*
* @param {string} output_format // value from ParameterEditor.OUTPUT_FORMATS
*/
static setOutputFormat(output_format=this.OUTPUT_FORMATS.JAVASCRIPT_ARRAY) {
this._sendMessageToEditor("Set Output Format", output_format);
}
/**
* Set the number of vertices in the x or y directions for outputs returned by the editor
*
* @param {number} output_steps_x // number of vertices in the x direction
* @param {number} output_steps_y // number of vertices in the y direction
*/
static setOutputSteps(output_steps_x=20, output_steps_y=20) {
this._sendMessageToEditor("Set Output Steps", {
output_steps_x: output_steps_x,
output_steps_y: output_steps_y
});
}
/*---------- Private ----------*/
static _editor_source_local = 'Editor'
static _editor_source = `${window.location.href}${this._editor_source_local}`;
static _init_options;
static _setId(id='editor') {
this.id = id;
}
static _initializeListeners(listeners=[]) {
this.listeners = listeners;
if (this.listeners.length) {
// start listening for updates
this._init_options.update_mode = this.UPDATE_MODES.ON_UPDATE;
window.addEventListener('message', ParameterEditor._receiveOutputFromEditor);
} else {
this._init_options.update_mode = this.UPDATE_MODES.BY_REQUEST;
}
}
static _initializeIframe() {
let el = document.getElementById(this.id);;
while (el && el.tagName != 'IFRAME') {
this._incrementId();
el = document.getElementById(this.id);
}
if (!el) {
el = document.createElement('iframe');
el.id = this.id;
el.style.width = '98vw';
el.style.height = '96vh';
el.style.margin = '0px';
document.body.append(el);
}
el.src = this._editor_source;
el.style.minWidth = '312px';
el.style.minHeight = '312px';
this.iframe = el.contentWindow;
this._waitForIframeLoad();
}
// element already exists with id
static _incrementId() {
const digit_regex = /\d+$/;
const digit_match = this.id.match(digit_regex)[0];
const id_num = digit_match ? digit_match[0] : 0;
this._setId(`editor${id_num}`);
}
static _waitForIframeLoad() {
const loaded_received = (event) => {
if (!this._validateMessage(event))
return;
if (event.data.title === "Loaded") {
this._iframeLoaded();
window.removeEventListener('message', loaded_received);
}
};
window.addEventListener('message', loaded_received);
}
static _iframeLoaded() {
this.loaded = true;
this._waitForInit();
}
static _waitForInit() {
this._requestFromEditor({title: 'Init', content: this._init_options}, 'Initialized')
.then(() => {
this._initialized();
})
}
static _initialized() {
this.initialized = true;
}
static _setUpdateMode(update_mode=this.UPDATE_MODES.BY_REQUEST) {
this.update_mode = update_mode;
this._sendMessageToEditor("Set Update Mode", update_mode);
}
static _outputReceived(data) {
this.listeners.forEach(on_update => {
on_update(data);
});
}
static _listenForOutput() {
if (this.update_mode === this.UPDATE_MODES.ON_UPDATE)
return;
this._setUpdateMode(this.UPDATE_MODES.ON_UPDATE);
window.addEventListener('message', ParameterEditor._receiveOutputFromEditor);
}
static _stopListeningForOutput() {
if (ParameterEditor.update_mode === ParameterEditor.UPDATE_MODES.BY_REQUEST)
return;
ParameterEditor._setUpdateMode(ParameterEditor.UPDATE_MODES.BY_REQUEST);
window.removeEventListener('message', ParameterEditor._receiveOutputFromEditor);
}
static _receiveOutputFromEditor(event) {
if (!ParameterEditor._validateMessage(event))
return;
if (event.data.title != "Output")
return;
ParameterEditor._outputReceived(event.data.content);
}
static async _requestFromEditor(send_message, receive_title) {
let content;
await new Promise((resolve) => {
const message_received = (event) => {
if (!this._validateMessage(event))
return;
if (event.data.title === receive_title) {
content = event.data.content;
window.removeEventListener('message', message_received);
resolve();
}
};
window.addEventListener('message', message_received);
this._sendMessageToEditor(send_message.title, send_message.content);
});
return content;
}
static _validateMessage(event) {
if (!event || !event.data || event.data.source != 'Parameter Editor')
return;
return true;
}
static _sendMessageToEditor(title, content='') {
this.iframe.postMessage({
source: 'External Software',
title: title,
content: content,
}, this._editor_source);
}
/* Bi-directional state serialization is not fully implemented for the editor. It should be fully implemented for the surface editor, but requires functionality for the 2D curve editor and the parent editor's ui needs to be updated */
// /**
// * This method asynchronously returns serialization data that can be used to save the editor's state
// *
// * @returns {Object} serialization_data
// * Example usage:
// * getStateSerialization().then(serialization_data => {
// * console.log(serialization_data);
// * });
// */
// static async getStateSerialization() {
// return await this._requestFromEditor({title: 'Get State Serialization'}, 'State Serialization');
// }
}