ll-database-binding: allow binding to build with older gdbm libraries
[apps/agl-service-data-persistence.git] / htdocs / persistence / persistence-binding.js
1 var afb = new AFB("api", "HELLO");
2 var ws;
3
4 function add_debbug_panel() {
5         
6         if (document.getElementById("debug-panel"))
7                 return;
8
9         var itm = document.getElementById("debug-panel-container");
10         if (itm)
11         {
12                 var pnl =
13                         "<div id=\"debug-panel\" class=\"expanded\">\n" +
14                         "   <button id=\"debug-panel-collapse\" onclick=\"debug_panel_collapse();\">&gt;</button>\n" +
15                         "    <button id=\"debug-panel-expand\" onclick=\"debug_panel_expand();\">&lt;</button>\n" +
16                         "    <div id=\"debug-panel-content\">\n" +
17                         "        <h1>Debug</h1>\n" +
18                         "        <h2>Call</h2><div id=\"debug-panel-call\">\n" +
19                         "            <ul>\n" +
20                         "                <li><strong>api : </strong><span id=\"debug-panel-call-id\"></span></li>\n" +
21                         "                <li><strong>verb : </strong><span id=\"debug-panel-call-verb\"></span></li>\n" +
22                         "                <li><strong>query : </strong></li>\n" +
23                         "            </ul>\n" +
24                         "            <pre id=\"debug-panel-call-query\"></pre>\n" +
25                         "        </div>\n" +
26                         "        <h2>Response</h2><pre id=\"debug-panel-response\"></pre>\n" +
27                         "        <h2>Event</h2><pre id=\"debug-panel-event\"></pre>\n" +
28                         "    </div>\n" +
29                         "</div>\n";
30                 itm.insertAdjacentHTML("afterbegin", pnl);
31         }
32 }
33
34 function createClass(name,rules) {
35         var style = document.createElement('style');
36         style.type = 'text/css';
37         document.getElementsByTagName('head')[0].appendChild(style);
38         if(!(style.sheet||{}).insertRule) 
39                 (style.styleSheet || style.sheet).addRule(name, rules);
40         else
41                 style.sheet.insertRule(name+"{"+rules+"}",0);
42 }
43
44 function syntaxHighlight(json) {
45         if (typeof json != 'string')
46                 json = JSON.stringify(json, undefined, 2);
47         
48         json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
49         return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
50                 var cls = 'json-number';
51                 if (/^"/.test(match)) {
52                         if (/:$/.test(match)) {
53                                 cls = 'json-key';
54                         } else {
55                                 cls = 'json-string';
56                         }
57                 } else if (/true|false/.test(match)) {
58                         cls = 'json-boolean';
59                 } else if (/null/.test(match)) {
60                         cls = 'json-null';
61                 }
62                 return '<span class="' + cls + '">' + match + '</span>';
63         });
64 }
65
66 function set_item_html(id, text)
67 {
68         var itm = document.getElementById(id);
69         if (itm) itm.innerHTML = text;
70 }
71
72 function set_item_text(id, text)
73 {
74         var itm = document.getElementById(id);
75         if (itm) itm.innerText = text;
76 }
77
78 function debug_panel_collapse() {
79         var pnl = document.getElementById('debug-panel');
80         if (pnl)
81         {
82                 pnl.classList.remove('expanded');
83                 pnl.classList.add('collapsed');
84         }
85 }
86
87 function debug_panel_expand() {
88         var pnl = document.getElementById('debug-panel');
89         if (pnl)
90         {
91                 pnl.classList.remove('collapsed');
92                 pnl.classList.add('expanded');
93         }
94 }
95
96 function init() {
97         add_debbug_panel();
98         ws = new afb.ws(onopen, onabort);
99 }
100
101 function onopen() {
102         //callbinder("ll-auth", "getuser", "");
103         ws.onevent("*", gotevent);
104 }
105
106 function onabort() {
107 }
108
109 function replyok(obj) {
110         console.log("replyok:" + JSON.stringify(obj));
111         set_item_html("debug-panel-response", syntaxHighlight(JSON.stringify(obj, null, 4)));
112 }
113
114 function replyerr(obj) {
115         console.log("replyerr:" + JSON.stringify(obj));
116         set_item_html("debug-panel-response", syntaxHighlight(JSON.stringify(obj, null, 4)));
117 }
118
119 function gotevent(obj) {
120         console.log("gotevent:" + JSON.stringify(obj));
121         set_item_html("debug-panel-event", syntaxHighlight(JSON.stringify(obj, null, 4)));
122 }
123
124 function callbinder(api, verb, query) {
125         console.log ("subscribe api="+api+" verb="+verb+" query=" +query);
126
127         set_item_text("debug-panel-call-api", api);
128         set_item_text("debug-panel-call-verb", verb);
129         set_item_html("debug-panel-call-query", syntaxHighlight(JSON.stringify(query, null, 4)));
130
131         ws.call(api+"/"+verb, query).then(replyok, replyerr);
132 }
133
134 function value(id){
135         return document.getElementById(id).value;
136 }
137
138 function readData() {
139         callbinder("persistence", "read", { "key": value("read-key") });
140 }
141
142 function deleteData() {
143         callbinder("persistence", "delete", { "key": value("read-key")});
144 }
145
146 function writeData() {
147         callbinder("persistence", "update", {
148                 "key": value("write-key"),
149                 "value": value("write-data")
150         });
151 }
152
153 function readProfile() {
154         var vin = value("profile-vin")
155         var token = value("get-profile-key")
156         var url = "https://agl-graphapi.forgerocklabs.org:443/getuserprofilefromtoken?vin=" + vin + "&kind=nfc&keytoken=" + token
157
158         callbinder("persistence", "read", {
159                 "key": {
160                         "url": url,
161                         "vin": vin,
162                         "kind": "nfc",
163                         "key": token
164                 }
165         });
166 }
167
168 function deleteProfile() {
169         var vin = value("profile-vin")
170         var token = value("get-profile-key")
171         var url = "https://agl-graphapi.forgerocklabs.org:443/getuserprofilefromtoken?vin=" + vin + "&kind=nfc&keytoken=" + token
172
173         callbinder("persistence", "delete", {
174                 "key": {
175                         "url": url,
176                         "vin": vin,
177                         "kind": "nfc",
178                         "key": token
179                 }
180         });
181 }
182
183 function writeProfile() {
184         var vin = value("profile-vin")
185         var token = value("set-profile-key")
186         var url = "https://agl-graphapi.forgerocklabs.org:443/getuserprofilefromtoken?vin=" + vin + "&kind=nfc&keytoken=" + token
187
188         callbinder("persistence", "update", {
189                 "key": {
190                         "url": url,
191                         "vin": vin,
192                         "kind": "nfc",
193                         "key": token
194                 },
195                 "value": {
196                         "keytoken": token,
197                         "name": value("set-profile-login"),
198                         "first_name": value("set-profile-first-name"),
199                         "last_name": value("set-profile-last-name"),
200                         "graphPreferredLanguage": value("set-profile-language")
201                 }
202         });
203 }