This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
167 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,164 +1,174 @@ | ||
/** | ||
@module MistAPI | ||
*/ | ||
module.exports = function(isWallet) { | ||
|
||
const ipc = require('ipc'); | ||
const ipc = require('ipc'); | ||
|
||
var queue = []; | ||
var prefix = 'entry_'; | ||
var queue = []; | ||
var prefix = 'entry_'; | ||
|
||
// filterId the id to only contain a-z A-Z 0-9 | ||
var filterId = function(str) { | ||
var newStr = ''; | ||
for (var i = 0; i < str.length; i++) { | ||
if(/[a-zA-Z0-9_-]/.test(str.charAt(i))) | ||
newStr += str.charAt(i); | ||
// filterId the id to only contain a-z A-Z 0-9 | ||
var filterId = function(str) { | ||
var newStr = ''; | ||
for (var i = 0; i < str.length; i++) { | ||
if(/[a-zA-Z0-9_-]/.test(str.charAt(i))) | ||
newStr += str.charAt(i); | ||
}; | ||
return newStr; | ||
}; | ||
return newStr; | ||
}; | ||
|
||
ipc.on('mistAPI_callMenuFunction', function(id) { | ||
if(mist.menu.entries[id] && mist.menu.entries[id].callback) | ||
mist.menu.entries[id].callback(); | ||
}); | ||
|
||
ipc.on('windowMessage', function(type, error, value) { | ||
if(mist.callbacks[type]) { | ||
mist.callbacks[type].forEach(function(cb){ | ||
cb(error, value); | ||
}); | ||
delete mist.callbacks[type]; | ||
} | ||
}); | ||
|
||
|
||
// work up queue every 500ms | ||
setInterval(function(){ | ||
if(queue.length > 0) { | ||
ipc.sendToHost('mistAPI_menuChanges', queue); | ||
queue = []; | ||
} | ||
}, 200); | ||
|
||
// preparing sounds | ||
var sound = { | ||
bip: document.createElement('audio'), | ||
bloop: document.createElement('audio'), | ||
invite: document.createElement('audio'), | ||
}; | ||
sound.bip.src = 'file://'+ __dirname + '/../sounds/bip.mp3'; | ||
sound.bloop.src = 'file://'+ __dirname + '/../sounds/bloop.mp3'; | ||
sound.invite.src = 'file://'+ __dirname + '/../sounds/invite.mp3'; | ||
|
||
ipc.on('mistAPI_callMenuFunction', function(id) { | ||
if(mist.menu.entries[id] && mist.menu.entries[id].callback) | ||
mist.menu.entries[id].callback(); | ||
}); | ||
|
||
/** | ||
Mist API | ||
TODO: queue up all changes and send them all together, to prevent multiple update calls in the mist ui db? | ||
@class mist | ||
@constructor | ||
*/ | ||
var mist = { | ||
callbacks: {}, | ||
platform: process.platform, | ||
requestAccount: function(callback){ | ||
if(callback) { | ||
if(!this.callbacks['requestAccount']) | ||
this.callbacks['requestAccount'] = []; | ||
this.callbacks['requestAccount'].push(callback); | ||
ipc.on('windowMessage', function(type, error, value) { | ||
if(mist.callbacks[type]) { | ||
mist.callbacks[type].forEach(function(cb){ | ||
cb(error, value); | ||
}); | ||
delete mist.callbacks[type]; | ||
} | ||
}); | ||
|
||
|
||
ipc.send('mistAPI_requestAccount'); | ||
}, | ||
sounds: { | ||
bip: function(){ | ||
sound.bip.play(); | ||
// work up queue every 500ms | ||
setInterval(function(){ | ||
if(queue.length > 0) { | ||
ipc.sendToHost('mistAPI_menuChanges', queue); | ||
queue = []; | ||
} | ||
}, | ||
menu: { | ||
entries: {}, | ||
/** | ||
Sets the badge text for the apps menu button | ||
}, 200); | ||
|
||
// preparing sounds | ||
// if wallet | ||
if(isWallet) { | ||
var sound = { | ||
bip: document.createElement('audio'), | ||
bloop: document.createElement('audio'), | ||
invite: document.createElement('audio'), | ||
}; | ||
sound.bip.src = 'file://'+ __dirname + '/../sounds/bip.mp3'; | ||
sound.bloop.src = 'file://'+ __dirname + '/../sounds/bloop.mp3'; | ||
sound.invite.src = 'file://'+ __dirname + '/../sounds/invite.mp3'; | ||
} | ||
|
||
Example | ||
|
||
mist.menu.setBadge('Some Text') | ||
/** | ||
Mist API | ||
@method setBadge | ||
@param {String} text | ||
*/ | ||
setBadge: function(text){ | ||
ipc.sendToHost('mistAPI_setBadge', 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': function(id, options, callback){ | ||
id = prefix + filterId(id); | ||
|
||
var entry = { | ||
id: id, | ||
position: options.position, | ||
selected: !!options.selected, | ||
name: options.name, | ||
badge: options.badge, | ||
}; | ||
|
||
queue.push({ | ||
action: 'addMenu', | ||
entry: entry | ||
}); | ||
TODO: queue up all changes and send them all together, to prevent multiple update calls in the mist ui db? | ||
if(callback) | ||
entry.callback = callback; | ||
@class mist | ||
@constructor | ||
*/ | ||
var mist = { | ||
callbacks: {}, | ||
platform: process.platform, | ||
requestAccount: function(callback){ | ||
if(callback) { | ||
if(!this.callbacks['requestAccount']) | ||
this.callbacks['requestAccount'] = []; | ||
this.callbacks['requestAccount'].push(callback); | ||
} | ||
|
||
this.entries[id] = entry; | ||
ipc.send('mistAPI_requestAccount'); | ||
}, | ||
'update': function(){ | ||
this.add.apply(this, arguments); | ||
sounds: { | ||
bip: function(){ | ||
// if wallet | ||
if(isWallet) | ||
sound.bip.play(); | ||
// if mist | ||
else | ||
ipc.sendToHost('mistAPI_sound', 'bip'); | ||
} | ||
}, | ||
/** | ||
Removes a menu entry from the mist sidebar. | ||
@method remove | ||
@param {String} id | ||
*/ | ||
'remove': function(id){ | ||
id = prefix + filterId(id); | ||
|
||
delete this.entries[id]; | ||
|
||
queue.push({ | ||
action: 'removeMenu', | ||
id: id | ||
}); | ||
menu: { | ||
entries: {}, | ||
/** | ||
Sets the badge text for the apps menu button | ||
Example | ||
mist.menu.setBadge('Some Text') | ||
@method setBadge | ||
@param {String} text | ||
*/ | ||
setBadge: function(text){ | ||
ipc.sendToHost('mistAPI_setBadge', 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': function(id, options, callback){ | ||
id = prefix + filterId(id); | ||
|
||
var entry = { | ||
id: id, | ||
position: options.position, | ||
selected: !!options.selected, | ||
name: options.name, | ||
badge: options.badge, | ||
}; | ||
|
||
queue.push({ | ||
action: 'addMenu', | ||
entry: entry | ||
}); | ||
|
||
if(callback) | ||
entry.callback = callback; | ||
|
||
this.entries[id] = entry; | ||
}, | ||
'update': function(){ | ||
this.add.apply(this, arguments); | ||
}, | ||
/** | ||
Removes a menu entry from the mist sidebar. | ||
@method remove | ||
@param {String} id | ||
*/ | ||
'remove': function(id){ | ||
id = prefix + filterId(id); | ||
|
||
delete this.entries[id]; | ||
|
||
queue.push({ | ||
action: 'removeMenu', | ||
id: id | ||
}); | ||
}, | ||
/** | ||
Removes all menu entries. | ||
@method clear | ||
*/ | ||
'clear': function(){ | ||
queue.push({action: 'clearMenu'}); | ||
} | ||
}, | ||
/** | ||
Removes all menu entries. | ||
@method clear | ||
*/ | ||
'clear': function(){ | ||
queue.push({action: 'clearMenu'}); | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports = mist; | ||
return mist; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters