-
Notifications
You must be signed in to change notification settings - Fork 0
/
stqry.js
379 lines (343 loc) · 11.2 KB
/
stqry.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
var STORAGE_KEY = 'stqry_dataset'
/**
* @param {string} key storage key
* @return {string}
*/
function getStoredData(key) {
var storedData = {}
try {
var dataStr = window.localStorage.getItem(key)
storedData = dataStr ? JSON.parse(dataStr) : {}
} catch (error) {
console.error(error)
}
return storedData
}
/**
* @param {string} key storage key
* @param {any} data data to storage
*/
function setStoredData(key, data) {
try {
var value = JSON.stringify(data)
window.localStorage.setItem(key, value)
} catch (error) {
console.error(error)
}
}
var appCallbacks = {}
var lastAppCallbackId = 0;
/**
* @param {string} action action to take in the parent app
* @param {any} data data to send to the parent app
* @param {function} callback callback for when the action is finished
*/
function callApp(action, data, callback) {
if (callback) {
var callbackId = lastAppCallbackId
var message = {
action: action,
version: 'v1',
data: data,
callbackId: callbackId,
}
appCallbacks[callbackId] = callback
lastAppCallbackId++
if (window.stqryRuntime === 'ReactNative') {
window.ReactNativeWebView.postMessage(JSON.stringify(message))
} else if (window.stqryRuntime === 'MobileWeb') {
window.parent.postMessage(JSON.stringify(message), '*')
} else if (window.stqryRuntime === 'Kiosk') {
window.parent.postMessage(message, '*')
}
} else {
var message = {
action: action,
version: 'v1',
data: data,
}
if (window.stqryRuntime === 'ReactNative') {
window.ReactNativeWebView.postMessage(JSON.stringify(message))
} else if (window.stqryRuntime === 'MobileWeb') {
window.parent.postMessage(JSON.stringify(message), '*')
} else if (window.stqryRuntime === 'Kiosk') {
window.parent.postMessage(message, '*')
}
}
}
window.addEventListener('message', onMessage)
document.addEventListener('message', onMessage)
function onMessage(event) {
var message
if (window.stqryRuntime === 'Kiosk') {
message = event.data
} else {
try {
message = JSON.parse(event.data)
} catch (error) {
// here can be any extension, so the error isn't logged
}
}
if (!message) {
return
}
var action = message.action
var version = message.version
if (version === 'v1' && action === 'callback') {
var callbackId = message.callbackId
var args = message.args
var callback = appCallbacks[callbackId]
if (callback) {
callback.apply(null, args)
delete appCallbacks[callbackId] // can only call back once
} else {
// callback is not set up at the moment
}
} else if (version === 'v1' && action === 'execute' && message.function) {
eval(message.function)
} else {
// malformed message, ignore
}
}
if (!window.stqryRuntime) {
window.stqryRuntime = 'NoRuntime'
}
window.stqry = {
/**
* loading parameter show state stqry scripts
*/
loading: true,
/**
* Call init function when stqry can't load oneself
*/
init: function () {
this.loading = false;
},
storage: {
/**
* @param {Object.<string, any>} changeset object - pair of key value
* @param {function()} callback callback function - use to handle when storage data saved
* @param {string} [customKey] storage key - add if you want to use custom storage key
*/
set: function (changeset, callback, customKey) {
var storageKey = customKey || STORAGE_KEY
if (window.stqryRuntime === 'NoRuntime') {
var storedData = getStoredData(storageKey)
var value = Object.assign(storedData, changeset)
setStoredData(storageKey, value)
console.warn('Saving into storage:')
console.log('[key]', storageKey)
console.log('[value]', value)
callback()
return
}
callApp('storage.set', { changeset: changeset, storageKey: storageKey }, callback)
},
/**
* @param {Array<string>} [keys] array of keys
* @param {function(Object.<string, any>)} callback callback function - use to handle when storage data saved
* @param {string} [customKey] storage key - add if you want to use custom storage key
*/
get: function (keys, callback, customKey) {
var storageKey = customKey || STORAGE_KEY
if (window.stqryRuntime === 'NoRuntime') {
var storedData = getStoredData(storageKey)
if (keys && Array.isArray(keys)) {
Object.keys(storedData).forEach(key => {
if (!keys.includes(key)) {
delete storedData[key]
}
})
}
console.warn('Getting from storage:')
console.log('[object keys]', keys)
console.log('[key]', storageKey)
console.log('[value]', storedData)
callback(storedData)
return
}
callApp('storage.get', { keys: keys, storageKey: storageKey }, callback)
},
/**
* @param {Array<string>} [keys] array of keys
* @param {function()} callback callback function - use to handle when storage data saved
* @param {string} [customKey] storage key - add if you want to use custom storage key
*/
remove: function (keys, callback, customKey) {
var storageKey = customKey || STORAGE_KEY
if (window.stqryRuntime === 'NoRuntime') {
if (keys && Array.isArray(keys)) {
var storedData = getStoredData(storageKey)
keys.forEach(key => {
delete storedData[key]
})
setStoredData(storageKey, storedData)
console.warn('Removing from storage:')
console.log('[object keys]', keys)
console.log('[key]', storageKey)
} else {
Object.keys(window.localStorage).forEach(function (key) {
if (key.startsWith(storageKey)) {
window.localStorage.removeItem(key)
}
})
console.warn('Removing from storage:')
console.log('[key]', storageKey)
}
callback()
return
}
callApp('storage.remove', { keys: keys, storageKey: storageKey }, callback)
}
},
linking: {
/**
* @param {Object} data internal link data
* @param {number} data.id collection or screen id
* @param {'collection'|'screen'} data.type item type
* @param {'list'|'tour'|'menu'|'web'|'story'|'panorama'} data.subtype item subtype
* @param {function()} callback callback function - calling after link openned
*/
openInternal: function (data, callback) {
if (window.stqryRuntime === 'NoRuntime') {
console.warn('Opening internal link: [id] [type] [subtype]', data)
if (callback) callback()
return
}
callApp('linking.openInternal', { params: data })
if (callback) callback()
},
/**
* @param {string} link external link
* @param {function()} callback callback function - calling after link openned
*/
openExternal: function (link, callback) {
if (window.stqryRuntime === 'NoRuntime') {
console.warn('Opening external link: ', link, window)
if (callback) callback()
return
}
callApp('linking.openExternal', { link: link })
if (callback) callback()
}
},
navigation: {
back: function () {
callApp('navigation.back')
}
},
camera: {
/**
* @param {String} videoTagId - id of video element
* @param {function(any)} callback callback function - calling after link openned
*/
enableBackground: function (videoTagId, callback) {
console.warn('Enable camera background', videoTagId)
// Making `this` available inside nested functions
var _this = this
_this._video = document.querySelector('#' + videoTagId);
function enableBackgroundInternal() {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' }})
.then(function (stream) {
_this._video.srcObject = stream;
if (callback) callback()
})
.catch(function (error) {
if (callback) callback(error)
});
}
else {
alert("Camera API is not available")
}
}
if (window.stqryRuntime === "ReactNative") {
callApp("camera.requestCameraPermission", {}, function(permissionStatus, error) {
if (error) console.error('camera.requestCameraPermission error', error)
if (permissionStatus !== "granted" && permissionStatus !== "bypassed") {
if (callback) callback(new Error("Camera permission is not granted or bypassed. Camera permission is '"+permissionStatus+"' instead."))
return
}
enableBackgroundInternal();
})
return
}
setTimeout(function () {
enableBackgroundInternal();
});
},
/**
* @param {function()} callback callback function - calling after link openned
*/
disableBackground: function (callback) {
console.warn('Disable camera background')
if (this._video) {
var stream = this._video.srcObject;
if (stream) {
var tracks = stream.getTracks();
for (var i = 0; i < tracks.length; i++) {
var track = tracks[i];
track.stop();
}
}
this._video.srcObject = null;
if (callback) callback()
}
}
},
media: {
/**
* Get media by id.
* @param {number} mediaId The id of the media.
* @param {string | undefined} language The language of the media. If not
* provided, the user's selected language will be used. If neither is
* provided, any language will be used.
* @param {function()} callback The callback function that will be called
* with the media item and a dictionary of responses for each file.
*/
get: function (mediaId, language, callback) {
if (window.stqryRuntime === 'Kiosk') {
callApp('media.get', { mediaId, language }, (item, files) => {
callback(
item,
Object.fromEntries(
Object.entries(files).map(([key, value]) => [
key,
new Response(value.blob, {
status: value.status,
statusText: value.statusText,
headers: value.headers,
}),
])
)
);
})
}
}
}
}
// Check if it's self page or react native webview
// If so, then don't need to call window.stqry.init()
if (window.self === window.top || window.stqryRuntime === 'ReactNative') {
window.stqry.loading = false
}
/**
* Check if stqry script is fully loaded
* Use that callback in frontend
* @param {function()} callback callback function - calling after link openned
*/
window.stqryOnLoad = function (successCallback) {
var attempt = 0
var loadingInterval = setInterval(function () {
if (attempt === 30) {
clearInterval(loadingInterval);
throw 'Maximum attempts exceeded';
}
if (!window.stqry.loading) {
successCallback();
clearInterval(loadingInterval);
} else {
attempt++;
}
}, 100);
}