forked from ethereum/mist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmistAPI.js
More file actions
270 lines (225 loc) · 7.86 KB
/
Copy pathmistAPI.js
File metadata and controls
270 lines (225 loc) · 7.86 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
/**
@module MistAPI
*/
(function () {
'use strict';
var postMessage = function(payload) {
if(typeof payload === 'object') {
payload = JSON.stringify(payload);
}
window.postMessage(payload, (!location.origin || location.origin === "null" ) ? '*' : location.origin);
};
var queue = [];
const prefix = 'entry_';
const MIST_SUBMENU_LIMIT = 100;
// todo: error handling
const filterAdd = function(options) {
if (!(options instanceof Object)) { return false; }
return ['name'].every(e => e in options);
};
// filterId the id to only contain a-z A-Z 0-9
const filterId = function(str) {
const filteredStr = String(str);
let newStr = '';
if (filteredStr) {
for (let i = 0; i < filteredStr.length; i += 1) {
if (/[a-zA-Z0-9_-]/.test(filteredStr.charAt(i))) {
newStr += filteredStr.charAt(i);
}
}
}
return newStr;
};
/**
Mist API
Provides an API for all dapps, which specifically targets features from the Mist browser
@class mist
@constructor
*/
const mist = {
callbacks: {},
version: '__version__',
license: '__license__',
platform: '__platform__',
requestAccount(callback) {
if (callback) {
if (!this.callbacks.connectAccount) {
this.callbacks.connectAccount = [];
}
this.callbacks.connectAccount.push(callback);
}
postMessage({
type: 'mistAPI_requestAccount'
});
},
solidity: {
version: '__solidityVersion__',
},
sounds: {
bip: function playSound() {
postMessage({
type: 'mistAPI_sound',
message: 'bip'
});
},
bloop: function playSound() {
postMessage({
type: 'mistAPI_sound',
message: 'bloop'
});
},
invite: function playSound() {
postMessage({
type: 'mistAPI_sound',
message: 'invite'
});
},
},
menu: {
entries: {},
/**
Sets the badge text for the apps menu button
Example
mist.menu.setBadge('Some Text')
@method setBadge
@param {String} text
*/
setBadge(text) {
postMessage({
type: 'mistAPI_setBadge',
message: text
});
},
/**
Adds/Updates a menu entry
Example
mist.menu.add('tkrzU', {
name: 'My Meny Entry',
badge: 50,
position: 1,
selected: true
}, function(){
// Router.go('/chat/1245');
})
@method add
@param {String} id The id of the menu, has to be the same accross page reloads.
@param {Object} options The menu options like {badge: 23, name: 'My Entry'}
@param {Function} callback Change the callback to be called when the menu is pressed.
*/
add(id, options, callback) {
const args = Array.prototype.slice.call(arguments);
callback = (typeof args[args.length - 1] === 'function') ? args.pop() : null;
options = (typeof args[args.length - 1] === 'object') ? args.pop() : null;
id = (typeof args[args.length - 1] === 'string') || isFinite(args[args.length - 1]) ? args.pop() : null;
if (!filterAdd(options)) { return false; }
const filteredId = prefix + filterId(id);
// restricting to 100 menu entries
if (!(filteredId in this.entries) &&
Object.keys(this.entries).length >= MIST_SUBMENU_LIMIT) {
return false;
}
const entry = {
id: filteredId || 'mist_defaultId',
position: options.position,
selected: !!options.selected,
name: options.name,
badge: options.badge,
};
queue.push({
action: 'addMenu',
entry,
});
if (callback) {
entry.callback = callback;
}
this.entries[filteredId] = entry;
return true;
},
/**
Updates a menu entry from the mist sidebar.
@method update
@param {String} id The id of the menu, has to be the same accross page reloads.
@param {Object} options The menu options like {badge: 23, name: 'My Entry'}
@param {Function} callback Change the callback to be called when the menu is pressed.
*/
update() {
this.add.apply(this, arguments);
},
/**
Removes a menu entry from the mist sidebar.
@method remove
@param {String} id
@param {String} id The id of the menu, has to be the same accross page reloads.
@param {Object} options The menu options like {badge: 23, name: 'My Entry'}
@param {Function} callback Change the callback to be called when the menu is pressed.
*/
remove(id) {
const filteredId = prefix + filterId(id);
delete this.entries[filteredId];
queue.push({
action: 'removeMenu',
filteredId,
});
},
/**
Marks a menu entry as selected
@method select
@param {String} id
*/
select(id) {
const filteredId = prefix + filterId(id);
queue.push({ action: 'selectMenu', id: filteredId });
for (const e in this.entries) {
if ({}.hasOwnProperty.call(this.entries, e)) {
this.entries[e].selected = (e === filteredId);
}
}
},
/**
Removes all menu entries.
@method clear
*/
clear() {
this.entries = {};
queue.push({ action: 'clearMenu' });
},
},
};
// Wait for response messages
window.addEventListener('message', function(event) {
var data;
try {
data = JSON.parse(event.data);
} catch(e){
data = event.data;
}
if(typeof data !== 'object') {
return;
}
if (data.type === 'mistAPI_callMenuFunction') {
var id = data.message;
if (mist.menu.entries[id] && mist.menu.entries[id].callback) {
mist.menu.entries[id].callback();
}
} else if (data.type === 'uiAction_windowMessage') {
var params = data.message;
if (mist.callbacks[params.type]) {
mist.callbacks[params.type].forEach(function(cb) {
cb(params.error, params.value);
});
delete mist.callbacks[params.type];
}
}
});
// work up queue every 500ms
setInterval(function() {
if (queue.length > 0) {
postMessage({
type: 'mistAPI_menuChanges',
message: queue
});
queue = [];
}
}, 500);
window.mist = mist;
})();