-
Notifications
You must be signed in to change notification settings - Fork 105
/
code.js
677 lines (572 loc) · 20.8 KB
/
code.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
var DEFAULT_APP_ID = 1089;
var DEFAULT_API_URL = 'frontend.binaryws.com';
var DEFAULT_LANGUAGE = 'EN';
var DEFAULT_BRAND = 'binary';
var VALID_LABELS = [
'beta',
'deprecated',
];
var api;
var $console;
// Add paths with same names as keys for fallback
require.config({
baseUrl: getBaseUrl(),
path: {
'/docson/docson' : '/docson/docson.js',
'/lib/binary-live-api': '/lib/binary-live-api.js',
'/lib/handlebars' : '/lib/handlebars.js',
'/lib/highlight' : '/lib/highlight.js',
'/lib/jquery' : '/lib/jquery.js',
'/lib/jsonpointer' : '/lib/jsonpointer.js',
'/lib/marked' : '/lib/marked.js',
'/lib/rainbow' : '/lib/rainbow.js',
'/lib/select2.min' : '/lib/select2.min.js',
'/lib/traverse' : '/lib/traverse.js',
}
});
require(["/lib/jquery.js"], function() {
require(["/docson/docson.js", "/lib/select2.min.js"], init);
});
function init(docson) {
$console = $('#playground-console');
docson.templateBaseUrl = '/docson';
$('[data-schema]').each(function() {
var $this = $(this);
loadAndDisplaySchema($this, $this.attr('data-schema'));
});
addEventListeners();
endpointNotification();
initEndpoint();
showDemoForLanguage('javascript');
updateApiDisplayed();
$('#api-token').val(sessionStorage.getItem('token'));
$('#playground').addClass(localStorage.getItem('console.theme'));
}
// -------------------------------
// ===== Connection Handling =====
// -------------------------------
function initConnection(language) {
if (api && api.disconnect) {
api.disconnect();
}
var LiveApi = window['binary-live-api'].LiveApi;
api = new LiveApi({
apiUrl : 'wss://' + getServerUrl() + '/websockets/v3',
language: getLanguage(),
appId : getAppId(),
brand : getBrand(),
});
api.socket.onopen = function(e) {
api.onOpen.apply(api, e);
};
api.socket.onclose = function(e) {
console.log('connection closed.'); // intended to help developers, not debugging
};
api.events.on('*', incomingMessageHandler);
}
function sendRequest(json) {
if (!api) {
initConnection();
}
var lang = getLanguage();
if (api.language !== lang) {
api.changeLanguage(lang);
}
if (api.socket.readyState === api.socket.CLOSED || api.socket.readyState === api.socket.CLOSING) {
api.connect();
}
api.sendRaw(json);
}
function incomingMessageHandler(json) {
var authorizationError = !!(json.error && json.error.code === "AuthorizationRequired"),
prettyJson = getFormattedJsonStr(json);
console.log(json); // intended to help developers, not for debugging, do not remove
$('.progress').remove();
appendToConsoleAndScrollIntoView(prettyJson);
$('#unauthorized-error').toggle(authorizationError);
if (!json.error) handleApplicationsResponse(json);
}
// --------------------------
// ===== API Playground =====
// --------------------------
function updateApiDisplayed() {
$('.sidebar-left a[href="' + window.location.pathname + '"]').parent().addClass('selected');
var $apiCallSelector = $('#api-call-selector');
if ($apiCallSelector.length === 0) return;
var apiToDisplay = getCurrentApi();
if (apiToDisplay) {
$apiCallSelector.val(apiToDisplay).change();
}
}
function getCurrentApi() {
var apiPageStrIdx = window.location.href.indexOf('/#');
if (!~apiPageStrIdx) return '';
return window.location.href.substr(apiPageStrIdx + 2);
}
function updatePlaygroundWithRequestAndResponse() {
try {
var json = JSON.parse($('#playground-request').val());
} catch (err) {
alert('Invalid JSON!');
return;
}
appendToConsoleAndScrollIntoView('<pre class="req">' + jsonToPretty(json) + '</pre>' + '<div class="progress"></div>');
sendRequest(json);
}
function getJsonPaths(method_name) {
url_path = getBaseUrl() + 'config/v3/' + method_name + '/';
return {
send : url_path + 'send.json',
receive: url_path + 'receive.json',
example: url_path + 'example.json',
};
}
function jsonToPretty(json, offset) {
var spaces = function(n) {
return new Array(n + 1).join(" ");
};
var span = function(val, className) {
return '<span class="' + className + '">' + escapeHtml(val) + '</span>';
};
var valToStr = function(val, offset) {
if (typeof val === 'string') {
return span('"' + val + '"', 'string');
} else if (typeof val === 'number') {
return span(val, 'number');
} else if (Array.isArray(val)) {
var elements = val.map(function(val) {
return spaces(offset + 2) + valToStr(val, offset + 2);
}).join(',\n');
return '[\n' + elements + '\n' + spaces(offset) + ']';
} else if (typeof val === 'boolean') {
return span(val, 'boolean');
} else {
return objToStr(val, offset);
}
};
var propsToStr = function(obj, offset) {
var keyStr = Object.keys(obj).map(function(key) {
return spaces(offset) + span('"' + key + '"', 'key') + ': ' + valToStr(obj[key], offset);
});
return keyStr.join(',\n');
};
var objToStr = function(obj, offset) {
if (!obj || Object.keys(obj).length === 0) return '{}';
return (
'{\n' +
propsToStr(obj, offset + 2) + '\n' +
spaces(offset) + '}'
)
};
return objToStr(json, offset || 0);
}
function getFormattedJsonStr(json) {
if (typeof json === 'string') {
json = JSON.parse(json);
}
return (json.error ? '<pre class="error">' : '<pre>') +
jsonToPretty(json) +
'</pre>';
}
function sortRequiredFirst(schema, method_name) {
if ((schema.required || []).length) {
var req_obj = {};
// Method name first, then Required properties
if (schema.properties[method_name]) {
req_obj[method_name] = schema.properties[method_name];
}
schema.required
.filter(function(prop) {
return prop !== method_name;
})
.sort(function(a, b) {
return a.localeCompare(b);
})
.forEach(function(prop) {
req_obj[prop] = schema.properties[prop];
});
schema.properties = Object.assign(req_obj, schema.properties);
}
}
function loadAndDisplaySchema($node, schema_url, method_name, required_first) {
$.get(schema_url, function(schema) {
if (required_first) sortRequiredFirst(schema, method_name);
docson.doc($node, schema, null, getBaseUrl());
setTimeout(function() {
$node.removeClass('labeled ' + VALID_LABELS.join(' '));
var label = VALID_LABELS.find(function(lbl) {
return schema[lbl];
});
if (label) {
$node.addClass('labeled ' + label);
}
linkToCallName();
}, 100);
});
}
function loadAndEditJson($node, jsonUrl) {
$.get(jsonUrl, function(exampleJson) {
$node.val(JSON.stringify(exampleJson, null, 2));
});
}
function customMatcher(params, data) {
var search_term = (params.term || '').trim();
if (!search_term) {
return data;
}
if (typeof data.children === 'undefined') {
return null;
}
var regexp = new RegExp(search_term, 'i');
var filtered_children = data.children.filter(function(child) {
return regexp.test([child.text, child.id]);
});
if (filtered_children.length) {
var modified_data = $.extend({}, data, true);
modified_data.children = filtered_children;
return modified_data;
}
return null;
}
function linkToCallName() {
$('.docson .desc code').each(function() {
var value = $(this).text();
if ($('#api-call-selector option[value="' + value + '"]').length) {
$(this).replaceWith($('<a/>', { href: '#' + value, html: $(this)[0].outerHTML }));
}
});
}
// ----------------------------------
// ===== Application Management =====
// ----------------------------------
function handleApplicationsResponse(response) {
if (response.msg_type === 'authorize' && $('#applications-table').length !== 0) {
sendRequest({ 'app_list': 1 });
} else if (response.msg_type === 'app_list' && response.app_list.length !== 0) {
listAllApplications(response);
} else if (response.msg_type === 'app_register') {
addApplication(response);
} else if (response.msg_type === 'app_delete') {
$('tr[id=' + response.echo_req.app_delete + ']').fadeOut(700).remove();
} else if (response.msg_type === 'app_update') {
updateApplication(response);
}
}
function listAllApplications(response) {
var applications = response.app_list;
for (i = 0; i < applications.length; i++) {
if ($('#' + applications[i].app_id).length === 0) {
applicationsTableRow(applications[i]);
}
}
$('#applications-table').show();
}
function addApplication(response) {
var application = response.app_register;
applicationsTableRow(application);
}
function applicationsTableRow(application) {
$('#applications-table').find('tbody').append(
'<tr class="flex-tr" id="' + application.app_id + '">' +
'<td class="flex-tr-child name">' + application.name + '</td>' +
'<td class="flex-tr-child app_id">' + application.app_id + '</td>' +
'<td class="flex-tr-child scopes">' + application.scopes.join(', ') + '</td>' +
'<td class="flex-tr-child redirect_uri">' + application.redirect_uri + '</td>' +
'<td class="flex-tr-child verification_uri" style="display:none">' + (application.verification_uri || '') + '</td>' +
'<td class="flex-tr-child homepage" style="display:none">' + application.homepage + '</td>' +
'<td class="flex-tr-child github" style="display:none">' + application.github + '</td>' +
'<td class="flex-tr-child googleplay" style="display:none">' + application.googleplay + '</td>' +
'<td class="flex-tr-child appstore" style="display:none">' + application.appstore + '</td>' +
'<td class="flex-tr-child app_markup_percentage" style="display:none">' + application.app_markup_percentage + '</td>' +
'<td class="action flex-tr-child"><button class="delete" id="' + application.app_id + '">Delete</button></td>' +
'<td class="action flex-tr-child"><button class="update" id="' + application.app_id + '">Update</button></td>' +
'</tr>'
).end().show();
$('button[id=' + application.app_id + '][class="delete"]').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
sendRequest({ 'app_delete': e.target.id });
});
$('button[id=' + application.app_id + '][class="update"]').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var $legend = $('#frmNewApplication').find('legend');
$legend.html($legend.html().replace('Register an', 'Update'));
$('#placeholder_app_id').text(' ' + e.target.id + ' ').attr('style', 'background:#ffffe0');
[
'name',
'redirect_uri',
'verification_uri',
'homepage',
'github',
'googleplay',
'appstore',
'app_markup_percentage',
].forEach(function(field) {
$('#application-' + field).val($('#' + e.target.id + ' .' + field).text());
});
var array = $('#' + e.target.id + ' .scopes').text().split(', '),
$scopes = $('.scopes input'),
i;
for (i = 0; i < array.length; i++) {
array[i] = array[i] + '-scope';
}
for (i = 0; i < $scopes.length; i++) {
if ($.inArray($scopes[i].id, array) > -1) {
$('.scopes input[id="' + $scopes[i].id + '"').prop('checked', true);
} else {
$('.scopes input[id="' + $scopes[i].id + '"').prop('checked', false);
}
}
// can't store btnupdate in a variable since it won't exist first
if ($('#btnUpdate').length === 0) {
$('.application_buttons').prepend('<button id="btnUpdate">Update</button> Or');
$('#btnRegister').text('Register as New Application');
$('#btnUpdate').on('click', function(e) {
e.preventDefault();
sendApplicationRequest(trim($('#placeholder_app_id').text()));
});
}
$("html, body").animate({ scrollTop: 280 }, "slow");
});
}
function sendApplicationRequest(app_id) {
var request = app_id
? { 'app_update' : app_id, 'scopes': [] }
: { 'app_register': 1, 'scopes': [] };
var fields = [
'name',
'redirect_uri',
'verification_uri',
'homepage',
'github',
'appstore',
'googleplay',
'app_markup_percentage',
];
fields.forEach(function(fld) {
var value = trim($('#application-' + fld).val());
if (value) {
request[fld] = value;
}
});
var scopesEl = $("form:first :input[type='checkbox']");
for (i = 0; i < scopesEl.length; i++) {
if (scopesEl[i].checked) {
request.scopes.push(scopesEl[i].value);
}
}
$('#playground-request').val(JSON.stringify(request, null, 2));
sendRequest(request);
}
function updateApplication(response) {
var application = response.app_update;
var columns = [
'name',
'app_id',
'redirect_uri',
'verification_uri',
];
columns.forEach(function(col) {
$('#' + application.app_id + ' .' + col).text(application[col]);
});
$('#' + application.app_id + ' .scopes').text(application.scopes.join(', '));
}
// --------------------
// ===== Endpoint =====
// --------------------
function initEndpoint() {
if (!/\/endpoint/i.test(window.location.href)) return;
showEndpointParams();
setEndpointValues();
$('#endpoint_btn-set').on('click', function(e) {
e.preventDefault();
setEndpointValues();
});
$('#endpoint_btn-reset').on('click', setEndpoint);
}
function addEndpointAPIEventListeners() {
api.socket.onopen = function(e) {
$('#endpoint_connecting').hide();
$('#endpoint_api-url').html(api.socket.url);
$('#endpoint_connected').show();
api.disconnect(); // because connected only for testing the new endpoint, the connection is not used here
};
api.socket.onerror = function() {
$('#endpoint_connecting').hide();
$('#endpoint_error').show();
api = undefined;
};
}
function showEndpointParams() {
$('#endpoint_txt-url').val(getServerUrl());
$('#endpoint_txt-appid').val(getAppId());
$('#endpoint_ddl-brand').val(getBrand());
}
function setEndpointValues() {
setEndpoint($('#endpoint_txt-url').val(), $('#endpoint_txt-appid').val(), $('#endpoint_ddl-brand').val());
}
function setEndpoint(server_url, app_id, brand) {
$('#endpoint_connecting').show();
$('#endpoint_connected').hide();
$('#endpoint_error').hide();
if (server_url && app_id) {
localStorage.setItem('config.server_url', server_url);
localStorage.setItem('config.app_id', app_id);
localStorage.setItem('config.brand', brand);
} else {
localStorage.removeItem('config.server_url');
localStorage.removeItem('config.app_id');
localStorage.removeItem('config.brand');
}
showEndpointParams();
endpointNotification();
initConnection();
addEndpointAPIEventListeners();
}
// -----------------------------
// ===== General Functions =====
// -----------------------------
function isProduction(url) {
return url && /(binary\.com|binary\.sx|binary\.me|pages\.dev)/i.test(url);
}
function isLocal(url) {
return /\/\/(localhost|127\.0\.0\.1)/.test(url);
}
function getBaseUrl(url) {
url = url || document.location.href;
return (isProduction(url) || isLocal(url) ? '' : '/' + url.split('/')[3]) + '/';
}
function getServerUrl() {
return window.localStorage.getItem('config.server_url') || DEFAULT_API_URL;
}
function getAppId() {
return window.localStorage.getItem('config.app_id') || DEFAULT_APP_ID;
}
function getBrand() {
return window.localStorage.getItem('config.brand') || DEFAULT_BRAND;
}
function getLanguage() {
return $('#api-language-selector').val() || DEFAULT_LANGUAGE;
}
function escapeHtml(unsafe) {
return unsafe.toString()
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function trim(str) {
return (str || '').trim();
}
function endpointNotification() {
var end_note = document.getElementById('end-note');
if (end_note) {
var server = getServerUrl();
var brand = getBrand();
if (server !== DEFAULT_API_URL || brand !== DEFAULT_BRAND) {
end_note.innerHTML = 'The server <a href="/endpoint/">endpoint</a> is: ' + server + ' (Brand: ' + brand + ')';
end_note.classList.remove('invisible');
} else {
end_note.innerHTML = '';
end_note.classList.add('invisible');
}
}
}
function scrollConsoleToBottom() {
$console.stop(false, true);
$console.animate({
scrollTop: $console[0].scrollHeight
}, 500);
}
function consoleShouldScroll() {
return Math.abs($console[0].scrollHeight - $console.scrollTop() - $console.outerHeight()) > 10;
}
function appendToConsoleAndScrollIntoView(html) {
$console.stop(false, true);
setTimeout(function() {
$console.append(html);
$('#toggle-theme').show();
if (consoleShouldScroll()) {
scrollConsoleToBottom();
setTimeout(function() {
if (consoleShouldScroll()) {
$console.animate({
scrollTop: $console[0].scrollHeight
}, 500);
}
}, 1500);
}
}, 0);
}
function showDemoForLanguage(lang) {
$('[data-language]').hide();
$('[data-language="' + lang + '"]').show();
}
function toggleTheme() {
$playground = $('#playground');
$playground.toggleClass('light');
localStorage.setItem('console.theme', $playground.hasClass('light') ? 'light' : 'dark');
}
function addEventListeners() {
$('#api-call-selector').select2({
matcher: customMatcher,
}).on('change', function() {
var method_name = $('#api-call-selector').val();
var json_paths = getJsonPaths(method_name);
loadAndDisplaySchema($('#playground-req-schema'), json_paths.send, method_name, true);
loadAndDisplaySchema($('#playground-res-schema'), json_paths.receive, method_name, false);
loadAndEditJson( $('#playground-request'), json_paths.example);
window.location.hash = method_name;
});
$('#playground-send-btn').on('click', function() {
updatePlaygroundWithRequestAndResponse();
});
$('#playground-reset-btn').on('click', function() {
$console.html('');
$('#toggle-theme').hide();
if (api) {
api.disconnect(); // the connection will be re-opened on demand
}
});
$('#api-token').on('change', function() {
sessionStorage.setItem('token', $(this).val());
});
$('#demo-language').on('change', function() {
showDemoForLanguage($(this).val());
});
$('#mobile-page-selector')
.val(window.location.pathname + window.location.hash)
.on('change', function(event) {
if (!event.originalEvent) return;
window.location.href = $(this).val();
});
$('#send-auth-manually-btn').on('click', function() {
var token = sessionStorage.getItem('token');
authReqStr = JSON.stringify({
authorize: token || ''
}, null, 2);
var $playgroundRequest = $('#playground-request');
var lastContents = $playgroundRequest.val();
$playgroundRequest.val(authReqStr);
if (token) {
$('#playground-send-btn').click();
$playgroundRequest.val(lastContents);
} else {
window.location.hash = 'authorize';
$playgroundRequest.focus();
}
});
$('#btnRegister').on('click', function(e) {
e.preventDefault();
sendApplicationRequest();
});
$('#scroll-to-bottom-btn').on('click', scrollConsoleToBottom);
$console.on('scroll', function() {
var shouldShow = consoleShouldScroll() && !$console.is(':animated');
$('#scroll-to-bottom-btn').toggle(shouldShow);
});
$('#toggle-theme').on('click', toggleTheme);
$(window).on('hashchange', updateApiDisplayed);
}