From 4720c87023bd10c47aec7c1281afb91dcd98ec66 Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Mon, 2 Nov 2015 21:48:33 +0100 Subject: [PATCH] moved sounds to mist interface --- interface/client/mistAPIBackend.js | 14 +- modules/mistAPI.js | 296 +++++++++++++++-------------- modules/preloader/dapps.js | 2 +- modules/preloader/wallet.js | 3 +- 4 files changed, 167 insertions(+), 148 deletions(-) diff --git a/interface/client/mistAPIBackend.js b/interface/client/mistAPIBackend.js index 2f3e64562..0bb0d700a 100644 --- a/interface/client/mistAPIBackend.js +++ b/interface/client/mistAPIBackend.js @@ -17,6 +17,16 @@ var filterId = function(str) { }; +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'; + + /** The backend side of the mist API. @@ -52,8 +62,8 @@ mistAPIBackend = function(event) { // Actions: -------- - if(event.channel === 'mistAPI_createAccount') { - console.log('Create Account'); + if(event.channel === 'mistAPI_sound') { + sound[arg].play(); } if(event.channel === 'mistAPI_setBadge') { diff --git a/modules/mistAPI.js b/modules/mistAPI.js index 6f6d029fa..bb581d412 100644 --- a/modules/mistAPI.js +++ b/modules/mistAPI.js @@ -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; \ No newline at end of file + return mist; +}; \ No newline at end of file diff --git a/modules/preloader/dapps.js b/modules/preloader/dapps.js index f2774f504..269f14752 100644 --- a/modules/preloader/dapps.js +++ b/modules/preloader/dapps.js @@ -28,6 +28,6 @@ document.addEventListener('click', function(e) { -window.mist = mist; +window.mist = mist(); window.BigNumber = BigNumber; window.web3 = new Web3(new Web3.providers.IpcProvider('', ipcProviderWrapper)); \ No newline at end of file diff --git a/modules/preloader/wallet.js b/modules/preloader/wallet.js index d2443364b..9801c1c6b 100644 --- a/modules/preloader/wallet.js +++ b/modules/preloader/wallet.js @@ -1,7 +1,6 @@ /** @module preloader wallet */ - const mist = require('../mistAPI.js'); const shell = require('shell'); const BigNumber = require('bignumber.js'); @@ -26,7 +25,7 @@ window.web3 = new Web3(new Web3.providers.IpcProvider('', ipcProviderWrapper)); web3Admin.extend(window.web3); -window.mist = mist; +window.mist = mist(true); window.platform = process.platform; setTimeout(function(){