-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffxivoceanfishing_pi.js
273 lines (234 loc) · 8.76 KB
/
ffxivoceanfishing_pi.js
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
// this is our global websocket, used to communicate from/to Stream Deck software
// and some info about our plugin, as sent by Stream Deck software
var websocket = null,
uuid = null,
actionInfo = {};
// stored menu info
routesJson = {};
menuheadersJson = {};
targetsJson = {};
globalSettingsInitialized = false;
settingsInitialized = false;
isInitialized = false;
function connectElgatoStreamDeckSocket(inPort, inUUID, inRegisterEvent, inInfo, inActionInfo) {
uuid = inUUID;
// please note: the incoming arguments are of type STRING, so
// in case of the inActionInfo, we must parse it into JSON first
actionInfo = JSON.parse(inActionInfo); // cache the info
websocket = new WebSocket('ws://localhost:' + inPort);
// if connection was established, the websocket sends
// an 'onopen' event, where we need to register our PI
websocket.onopen = function () {
var json = {
event: inRegisterEvent,
uuid: inUUID
};
// register property inspector to Stream Deck
websocket.send(JSON.stringify(json));
reloadAllSettings();
}
// retrieve saved settings if there are any
websocket.onmessage = function (evt) {
// Received message from Stream Deck
const jsonObj = JSON.parse(evt.data);
if (jsonObj.event === 'didReceiveSettings') {
const payload = jsonObj.payload.settings;
const menu = document.getElementById("Tracker")
if (payload.hasOwnProperty('Route') && payload.Route != null)
{
document.getElementById('Routes').value = payload.Route;
removeAllChildOptions(document.getElementById('Tracker'), '>>Select Tracker<<');
}
// create all the headers, this should arrive from plugin in the order we want the headers
// to be displayed in the dropdown menu
// ie: Blue Fish
if (payload.hasOwnProperty('menuheaders') && payload.menuheaders != null) {
menuheadersJson = payload.menuheaders; // save the menus globally
// note this loop has to use .length, without it extra elements like 0,1,2,3
// get put into the menu
for (var i = 0; i < payload.menuheaders.length; i++) {
insertOptGroup(menu, payload.menuheaders[i]);
}
}
// insert the targets under the appropriate headers
// ie: Elasomosaurus -> Blue Fish
if (payload.hasOwnProperty('targets') && payload.targets != null) {
targetsJson = payload.targets; // save the targets globally
for (targetName in payload.targets) {
insertOptionToOptGroup(menu, payload.targets[targetName], targetName, targetName);
}
}
if (payload.hasOwnProperty('Name') && payload.Name != null)
document.getElementById('Tracker').value = payload.Name;
if (payload.DateOrTime != null && payload.DateOrTime)
document.getElementById('select_date').checked = true;
else
document.getElementById('select_countdown').checked = true;
if (payload.Priority != null && !payload.Priority)
document.getElementById('select_bluefish').checked = true;
else
document.getElementById('select_achievement').checked = true;
if (payload.hasOwnProperty('Skips') && payload.Skips != null)
document.getElementById('set_skips').value = payload.Skips;
if( payload.hasOwnProperty('url') && payload.url != null)
document.getElementById('set_url').value = payload.url;
settingsInitialized = true;
}
if (jsonObj.event === 'didReceiveGlobalSettings') {
const payload = jsonObj.payload.settings;
// create route dropdown list
if (payload.hasOwnProperty('Routes')) {
routesJson = payload.Routes;
el = document.getElementById("Routes");
removeAllChildOptions(el, ">>Select Route<<");
for (var i = 0; i < payload.Routes.length; i++) {
insertOption(el, payload.Routes[i], payload.Routes[i]);
}
}
if (payload.Timekeeping24HMode != null && payload.Timekeeping24HMode)
document.getElementById('select_24h').checked = true;
else
document.getElementById('select_12h').checked = true;
globalSettingsInitialized = true;
}
if (jsonObj.event === 'sendToPropertyInspector') {
const payload = jsonObj.payload;
if (payload.action = "InitRoutes") {
if (payload.hasOwnProperty('Routes'))
routesJson = payload.Routes;
updateGlobalSettings();
reloadAllSettings();
}
}
if (globalSettingsInitialized && settingsInitialized && !isInitialized) {
sendSettingsToPlugin();
isInitialized = true;
}
};
}
function reloadAllSettings() {
json = {
"event": "getGlobalSettings",
"context": uuid,
};
websocket.send(JSON.stringify(json));
json = {
"event": "getSettings",
"context": uuid,
};
websocket.send(JSON.stringify(json));
}
// pass settings to the plugin
function sendSettingsToPlugin() {
route_el = document.getElementById('Routes');
tracker_el = document.getElementById('Tracker');
group = null;
if (tracker_el.selectedIndex >= 0)
group = tracker_el.options[tracker_el.selectedIndex].parentElement.getAttribute('label');
payload = {
'Route': route_el.value,
'Name': tracker_el.value,
'Tracker': group,
'DateOrTime': document.getElementById('select_date').checked,
'Priority': document.getElementById('select_achievement').checked,
'Skips': document.getElementById('set_skips').value,
'url': document.getElementById('set_url').value
};
if (websocket) {
const json = {
"action": actionInfo['action'],
"event": "sendToPlugin",
"context": uuid,
"payload": payload
};
websocket.send(JSON.stringify(json));
}
payload['menuheaders'] = menuheadersJson;
payload['targets'] = targetsJson;
saveValues(payload);
}
// updates global settings
function updateGlobalSettings()
{
globalPayload = {
'Routes': routesJson,
'Timekeeping24HMode': document.getElementById('select_24h').checked
};
saveGlobalValues(globalPayload);
}
// saves a payload
function saveValues(obj) {
if (websocket) {
const json = {
"event": "setSettings",
"context": uuid,
"payload": obj
};
websocket.send(JSON.stringify(json));
}
}
// saves a global payload
function saveGlobalValues(payload) {
if (websocket) {
const json = {
"event": "setGlobalSettings",
"context": uuid,
"payload": payload
};
websocket.send(JSON.stringify(json));
}
}
// inserts an optgroup
function insertOptGroup(el, optgroupID) {
if (el.querySelector("optgroup[label='" + optgroupID + "']") == null)
{
grp = document.createElement('OPTGROUP');
grp.label = optgroupID;
el.appendChild(grp);
}
}
// inserts an option to specified optgroup ID
function insertOptionToOptGroup(el, optgroupID, name, value) {
insertOptGroup(el, optgroupID);
const optgroup = el.querySelector("optgroup[label='" + optgroupID + "']");
insertOption(optgroup, name, value);
}
// inserts an option to specified element
function insertOption(optgroup, name, value) {
opt = document.createElement('OPTION');
opt.textContent = name;
opt.value = value;
optgroup.appendChild(opt);
}
// opens a url in the default browser
function openUrl(url) {
if (websocket) {
const json = {
"event": "openUrl",
"payload": {
"url": url
}
};
websocket.send(JSON.stringify(json));
}
}
// remove all child options in a dropdown menu
// and then inserts a default placeholder
function removeAllChildOptions(el, placeholderText) {
// delete all the child options
while (el.firstChild) {
el.removeChild(el.firstChild);
}
// insert a placeholder option
createPlaceholderOption(el, placeholderText)
}
// insert a placeholder option into an element
function createPlaceholderOption(el, text) {
opt = document.createElement('OPTION');
opt.disabled = "disabled";
opt.selected = "selected";
opt.hidden = "hidden";
opt.value = "";
opt.textContent = text;
el.appendChild(opt);
}