forked from status-im/status-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.js
More file actions
367 lines (317 loc) · 8.57 KB
/
status.js
File metadata and controls
367 lines (317 loc) · 8.57 KB
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
var _status_catalog = {
commands: {},
responses: {},
functions: {},
subscriptions: {}
},
status = {};
function Command() {
}
function Response() {
}
Command.prototype.addToCatalog = function () {
_status_catalog.commands[this.name] = this;
};
Command.prototype.param = function (parameter) {
this.params.push(parameter);
return this;
};
Command.prototype.create = function (com) {
this.name = com.name;
this.title = com.title;
this.description = com.description;
this.handler = com.handler;
this["has-handler"] = com.handler != null;
this["registered-only"] = com.registeredOnly;
this.validator = com.validator;
this.color = com.color;
this.icon = com.icon;
this.params = com.params || [];
this.preview = com.preview;
this["short-preview"] = com.shortPreview;
this["on-send"] = com.onSend;
this.fullscreen = com.fullscreen;
this.actions = com.actions;
this.request = com.request;
this["execute-immediately?"] = com.executeImmediately;
this["sequential-params"] = com.sequentialParams;
this["hide-send-button"] = com.hideSendButton;
this.addToCatalog();
return this;
};
Response.prototype = Object.create(Command.prototype);
Response.prototype.addToCatalog = function () {
_status_catalog.responses[this.name] = this;
};
Response.prototype.onReceiveResponse = function (handler) {
this.onReceive = handler;
};
var context = {};
function addContext(key, value) {
context[status.message_id][key] = value;
}
function getContext(key) {
return context[status.message_id][key];
}
function call(pathStr, paramsStr) {
var params = JSON.parse(paramsStr),
path = JSON.parse(pathStr),
fn, callResult, message_id;
if (typeof params.context !== "undefined" &&
typeof params.context["message-id"] !== "undefined") {
message_id = params.context["message-id"];
} else {
message_id = null;
}
context[message_id] = {};
status.message_id = message_id;
fn = path.reduce(function (catalog, name) {
if (catalog && catalog[name]) {
return catalog[name];
}
},
_status_catalog
);
if (!fn) {
return null;
}
context.messages = [];
callResult = fn(params.parameters, params.context);
result = {
returned: callResult,
context: context[message_id],
messages: context.messages
};
return JSON.stringify(result);
}
function view(options, elements) {
return ['view', options].concat(elements);
}
function text(options, s) {
s = Array.isArray(s) ? s : [s];
return ['text', options].concat(s);
}
function textInput(options) {
return ['text-input', options];
}
function image(options) {
return ['image', options];
}
function qrCode(options) {
return ['qr-code', options];
}
function linking(options) {
return ['linking', options];
}
function slider(options) {
return ['slider', options];
}
function image(options) {
return ['image', options];
}
function touchable(options, element) {
return ['touchable', options, element];
}
function activityIndicator(options) {
return ['activity-indicator', options];
}
function scrollView(options, elements) {
return ['scroll-view', options].concat(elements);
}
function subscribe(path) {
return ['subscribe', path];
}
function dispatch(path) {
return ['dispatch', path];
}
function webView(url) {
return ['web-view', {
source: {
uri: url
},
javaScriptEnabled: true
}];
}
function bridgedWebView(url) {
return ['bridged-web-view', {
url: url
}];
}
function validationMessage(titleText, descriptionText) {
return ['validation-message', {
title: titleText,
description: descriptionText
}];
}
function chooseContact(titleText, botDbKey, argumentIndex) {
return ['choose-contact', {
title: titleText,
"bot-db-key": botDbKey,
index: argumentIndex
}];
}
function droppedPin() {
return ['dropped-pin'];
}
function placesSearch() {
return ['places-search'];
}
function currentLocationMap() {
return ['current-location-map'];
}
function currentLocation() {
return ['current-location'];
}
function placesNearby() {
return ['places-nearby'];
}
function separator() {
return ['separator'];
}
var status = {
command: function (h) {
var command = new Command();
return command.create(h);
},
response: function (h) {
var response = new Response();
return response.create(h);
},
addListener: function (name, fn) {
_status_catalog.functions[name] = fn;
},
localizeNumber: function (num, del, sep) {
return I18n.toNumber(
num.replace(",", "."),
{
precision: 10,
strip_insignificant_zeros: true,
delimiter: del,
separator: sep
});
},
types: {
TEXT: 'text',
NUMBER: 'number',
PHONE: 'phone',
PASSWORD: 'password'
},
events: {
SET_VALUE: 'set-value',
SET_COMMAND_ARGUMENT: 'set-command-argument',
UPDATE_DB: 'set',
SET_COMMAND_ARGUMENT_FROM_DB: 'set-command-argument-from-db',
SET_VALUE_FROM_DB: 'set-value-from-db',
FOCUS_INPUT: 'focus-input'
},
actions: {
WEB_VIEW_BACK: 'web-view-back',
WEB_VIEW_FORWARD: 'web-view-forward',
FULLSCREEN: 'fullscreen',
CUSTOM: 'custom',
},
components: {
view: view,
text: text,
textInput: textInput,
slider: slider,
image: image,
qrCode: qrCode,
linking: linking,
slider: slider,
touchable: touchable,
activityIndicator: activityIndicator,
scrollView: scrollView,
webView: webView,
validationMessage: validationMessage,
bridgedWebView: bridgedWebView,
chooseContact: chooseContact,
subscribe: subscribe,
dispatch: dispatch,
droppedPin: droppedPin,
placesSearch: placesSearch,
currentLocationMap: currentLocationMap,
currentLocation: currentLocation,
placesNearby: placesNearby,
separator: separator
},
showSuggestions: function (view) {
statusSignals.showSuggestions(JSON.stringify(view));
},
setDefaultDb: function (db) {
addContext("default-db", db);
},
updateDb: function (db) {
addContext("update-db", db)
},
sendMessage: function (message) {
if(typeof message !== "string") {
message = JSON.stringify(message);
}
statusSignals.sendMessage(message);
},
addLogMessage: function (type, message) {
var message = {
type: type,
message: JSON.stringify(message)
};
var logMessages = getContext("log-messages");
if (!logMessages) {
logMessages = [];
}
logMessages.push(message);
addContext("log-messages", logMessages);
},
defineSubscription: function (name, subscriptions, handler) {
_status_catalog.subscriptions[name] = {
subscriptions: subscriptions,
handler: handler
};
}
};
function calculateSubscription(parameters, context) {
var subscriptionConfig = _status_catalog.subscriptions[parameters.name];
if (!subscriptionConfig) {
return;
}
return subscriptionConfig.handler(parameters.subscriptions);
}
status.addListener("subscription", calculateSubscription);
console = (function (old) {
return {
log: function (text) {
old.log(text);
status.addLogMessage('log', text);
},
debug: function (text) {
old.debug(text);
status.addLogMessage('debug', text);
},
info: function (text) {
old.info(text);
status.addLogMessage('info', text);
},
warn: function (text) {
old.warn(text);
status.addLogMessage('warn', text);
},
error: function (text) {
old.error(text);
status.addLogMessage('error', text);
}
};
}(console));
localStorage.setItem = function(key, value) {
if(value === null) {
delete localStorageData[key];
} else {
localStorageData[key] = value;
}
localStorage.set(JSON.stringify(localStorageData));
};
localStorage.getItem = function (key) {
if (typeof localStorageData[key] === "undefined") {
return null;
}
return localStorageData[key];
};