mistAPIBackend.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. @module MistAPI Backend
  3. */
  4. var allowedBrowserBarStyles = ['transparent'];
  5. /**
  6. Filters a id the id to only contain a-z A-Z 0-9 _ -.
  7. @method filterId
  8. */
  9. var filterId = function(str) {
  10. var newStr = '';
  11. var i;
  12. for (i = 0; i < str.length; i += 1) {
  13. if (/[a-zA-Z0-9_-]/.test(str.charAt(i))) {
  14. newStr += str.charAt(i);
  15. }
  16. }
  17. return newStr;
  18. };
  19. var sound = document.createElement('audio');
  20. /**
  21. The backend side of the mist API.
  22. @method mistAPIBackend
  23. */
  24. mistAPIBackend = function(event) {
  25. var template = this.template;
  26. var webview = this.webview;
  27. var arg = event.args[0];
  28. // console.trace('mistAPIBackend event', event);
  29. if (event.channel === 'setWebviewId') {
  30. Tabs.update(template.data._id, {
  31. $set: { webviewId: webview.getWebContents().id }
  32. });
  33. }
  34. // Send TEST DATA
  35. if (event.channel === 'sendTestData') {
  36. var tests = Tabs.findOne('tests');
  37. if (tests) {
  38. web3.eth.getCoinbase(function(e, coinbase) {
  39. webview.send('uiAction_sendTestData', tests.permissions, coinbase);
  40. });
  41. }
  42. }
  43. // SET FAVICON
  44. if (event.channel === 'favicon') {
  45. Tabs.update(template.data._id, {
  46. $set: {
  47. icon: Blaze._escape(arg || '')
  48. }
  49. });
  50. }
  51. // SET APPBAR
  52. if (event.channel === 'appBar') {
  53. var appBarClass = Blaze._escape(arg || '');
  54. Tabs.update(template.data._id, {
  55. $set: {
  56. appBar: _.contains(allowedBrowserBarStyles, appBarClass)
  57. ? appBarClass
  58. : null
  59. }
  60. });
  61. }
  62. if (event.channel === 'mistAPI_sound') {
  63. sound.pause();
  64. sound.src = Blaze._escape('file://' + dirname + '/sounds/' + arg + '.mp3');
  65. sound.play();
  66. }
  67. // STOP HERE, IF BROWSER
  68. if (template.data._id === 'browser') {
  69. return;
  70. }
  71. // Actions: --------
  72. if (event.channel === 'mistAPI_setBadge') {
  73. Tabs.update(template.data._id, {
  74. $set: {
  75. badge: arg
  76. }
  77. });
  78. }
  79. if (event.channel === 'mistAPI_menuChanges' && arg instanceof Array) {
  80. arg.forEach(function(eventArg) {
  81. var query;
  82. if (eventArg.action === 'addMenu') {
  83. // filter ID
  84. if (eventArg.entry && eventArg.entry.id) {
  85. eventArg.entry.id = filterId(eventArg.entry.id);
  86. }
  87. query = { $set: {} };
  88. if (eventArg.entry.id) {
  89. query.$set['menu.' + eventArg.entry.id + '.id'] = eventArg.entry.id;
  90. }
  91. query.$set['menu.' + eventArg.entry.id + '.selected'] = !!eventArg.entry
  92. .selected;
  93. if (!_.isUndefined(eventArg.entry.position)) {
  94. query.$set['menu.' + eventArg.entry.id + '.position'] =
  95. eventArg.entry.position;
  96. }
  97. if (!_.isUndefined(eventArg.entry.name)) {
  98. query.$set['menu.' + eventArg.entry.id + '.name'] =
  99. eventArg.entry.name;
  100. }
  101. if (!_.isUndefined(eventArg.entry.badge)) {
  102. query.$set['menu.' + eventArg.entry.id + '.badge'] =
  103. eventArg.entry.badge;
  104. }
  105. Tabs.update(template.data._id, query);
  106. }
  107. if (eventArg.action === 'selectMenu') {
  108. var tab = Tabs.findOne(template.data._id);
  109. for (var e in tab.menu) {
  110. if ({}.hasOwnProperty.call(tab.menu, e)) {
  111. tab.menu[e].selected = e === eventArg.id;
  112. }
  113. }
  114. Tabs.update(template.data._id, { $set: { menu: tab.menu } });
  115. }
  116. if (eventArg.action === 'removeMenu') {
  117. var removeQuery = { $unset: {} };
  118. removeQuery.$unset['menu.' + eventArg.id] = '';
  119. Tabs.update(template.data._id, removeQuery);
  120. }
  121. if (eventArg.action === 'clearMenu') {
  122. Tabs.update(template.data._id, { $set: { menu: {} } });
  123. }
  124. });
  125. }
  126. };