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
|
var afb = new AFB("api", "HELLO");
var ws;
function add_debbug_panel() {
if (document.getElementById("debug-panel"))
return;
var itm = document.getElementById("debug-panel-container");
if (itm)
{
var pnl =
"<div id=\"debug-panel\" class=\"expanded\">\n" +
" <button id=\"debug-panel-collapse\" onclick=\"debug_panel_collapse();\">></button>\n" +
" <button id=\"debug-panel-expand\" onclick=\"debug_panel_expand();\"><</button>\n" +
" <div id=\"debug-panel-content\">\n" +
" <h1>Debug</h1>\n" +
" <h2>Call</h2><div id=\"debug-panel-call\">\n" +
" <ul>\n" +
" <li><strong>api : </strong><span id=\"debug-panel-call-id\"></span></li>\n" +
" <li><strong>verb : </strong><span id=\"debug-panel-call-verb\"></span></li>\n" +
" <li><strong>query : </strong></li>\n" +
" </ul>\n" +
" <pre id=\"debug-panel-call-query\"></pre>\n" +
" </div>\n" +
" <h2>Response</h2><pre id=\"debug-panel-response\"></pre>\n" +
" <h2>Event</h2><pre id=\"debug-panel-event\"></pre>\n" +
" </div>\n" +
"</div>\n";
itm.insertAdjacentHTML("afterbegin", pnl);
}
}
function createClass(name,rules) {
var style = document.createElement('style');
style.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(style);
if(!(style.sheet||{}).insertRule)
(style.styleSheet || style.sheet).addRule(name, rules);
else
style.sheet.insertRule(name+"{"+rules+"}",0);
}
function syntaxHighlight(json) {
if (typeof json != 'string')
json = JSON.stringify(json, undefined, 2);
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'json-number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'json-key';
} else {
cls = 'json-string';
}
} else if (/true|false/.test(match)) {
cls = 'json-boolean';
} else if (/null/.test(match)) {
cls = 'json-null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function set_item_html(id, text)
{
var itm = document.getElementById(id);
if (itm) itm.innerHTML = text;
}
function set_item_text(id, text)
{
var itm = document.getElementById(id);
if (itm) itm.innerText = text;
}
function debug_panel_collapse() {
var pnl = document.getElementById('debug-panel');
if (pnl)
{
pnl.classList.remove('expanded');
pnl.classList.add('collapsed');
}
}
function debug_panel_expand() {
var pnl = document.getElementById('debug-panel');
if (pnl)
{
pnl.classList.remove('collapsed');
pnl.classList.add('expanded');
}
}
function init() {
add_debbug_panel();
ws = new afb.ws(onopen, onabort);
}
function onopen() {
//callbinder("ll-auth", "getuser", "");
ws.onevent("*", gotevent);
}
function onabort() {
}
function replyok(obj) {
console.log("replyok:" + JSON.stringify(obj));
set_item_html("debug-panel-response", syntaxHighlight(JSON.stringify(obj, null, 4)));
}
function replyerr(obj) {
console.log("replyerr:" + JSON.stringify(obj));
set_item_html("debug-panel-response", syntaxHighlight(JSON.stringify(obj, null, 4)));
}
function gotevent(obj) {
console.log("gotevent:" + JSON.stringify(obj));
set_item_html("debug-panel-event", syntaxHighlight(JSON.stringify(obj, null, 4)));
}
function callbinder(api, verb, query) {
console.log ("subscribe api="+api+" verb="+verb+" query=" +query);
set_item_text("debug-panel-call-api", api);
set_item_text("debug-panel-call-verb", verb);
set_item_html("debug-panel-call-query", syntaxHighlight(JSON.stringify(query, null, 4)));
ws.call(api+"/"+verb, query).then(replyok, replyerr);
}
function value(id){
return document.getElementById(id).value;
}
function readData() {
callbinder("persistence", "read", { "key": value("read-key") });
}
function deleteData() {
callbinder("persistence", "delete", { "key": value("read-key")});
}
function writeData() {
callbinder("persistence", "update", {
"key": value("write-key"),
"value": value("write-data")
});
}
function readProfile() {
var vin = value("profile-vin")
var token = value("get-profile-key")
var url = "https://agl-graphapi.forgerocklabs.org:443/getuserprofilefromtoken?vin=" + vin + "&kind=nfc&keytoken=" + token
callbinder("persistence", "read", {
"key": {
"url": url,
"vin": vin,
"kind": "nfc",
"key": token
}
});
}
function deleteProfile() {
var vin = value("profile-vin")
var token = value("get-profile-key")
var url = "https://agl-graphapi.forgerocklabs.org:443/getuserprofilefromtoken?vin=" + vin + "&kind=nfc&keytoken=" + token
callbinder("persistence", "delete", {
"key": {
"url": url,
"vin": vin,
"kind": "nfc",
"key": token
}
});
}
function writeProfile() {
var vin = value("profile-vin")
var token = value("set-profile-key")
var url = "https://agl-graphapi.forgerocklabs.org:443/getuserprofilefromtoken?vin=" + vin + "&kind=nfc&keytoken=" + token
callbinder("persistence", "update", {
"key": {
"url": url,
"vin": vin,
"kind": "nfc",
"key": token
},
"value": {
"keytoken": token,
"name": value("set-profile-login"),
"first_name": value("set-profile-first-name"),
"last_name": value("set-profile-last-name"),
"graphPreferredLanguage": value("set-profile-language")
}
});
}
|