panel_controller.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*global $, Whisper, Backbone, textsecure, extension*/
  2. /*
  3. * vim: ts=4:sw=4:expandtab
  4. */
  5. // This script should only be included in background.html
  6. (function () {
  7. 'use strict';
  8. window.Whisper = window.Whisper || {};
  9. window.isFocused = function() {
  10. return inboxFocused;
  11. };
  12. window.isOpen = function() {
  13. return inboxOpened;
  14. };
  15. window.drawAttention = function() {
  16. if (inboxOpened && !inboxFocused) {
  17. extension.windows.drawAttention(inboxWindowId);
  18. }
  19. };
  20. window.clearAttention = function() {
  21. extension.windows.clearAttention(inboxWindowId);
  22. };
  23. /* Inbox window controller */
  24. var inboxFocused = false;
  25. var inboxOpened = false;
  26. var inboxWindowId = 'inbox';
  27. var appWindow = null;
  28. window.openInbox = function() {
  29. console.log('open inbox');
  30. if (inboxOpened === false) {
  31. inboxOpened = true;
  32. extension.windows.open({
  33. id: 'inbox',
  34. url: 'index.html',
  35. focused: true,
  36. width: 580,
  37. height: 440,
  38. minWidth: 600,
  39. minHeight: 360
  40. }, function (windowInfo) {
  41. appWindow = windowInfo;
  42. inboxWindowId = appWindow.id;
  43. appWindow.contentWindow.addEventListener('load', function() {
  44. setUnreadCount(storage.get("unreadCount", 0));
  45. });
  46. appWindow.onClosed.addListener(function () {
  47. inboxOpened = false;
  48. appWindow = null;
  49. });
  50. appWindow.contentWindow.addEventListener('blur', function() {
  51. inboxFocused = false;
  52. });
  53. appWindow.contentWindow.addEventListener('focus', function() {
  54. inboxFocused = true;
  55. clearAttention();
  56. });
  57. // close the inbox if background.html is refreshed
  58. extension.windows.onSuspend(function() {
  59. // TODO: reattach after reload instead of closing.
  60. extension.windows.remove(inboxWindowId);
  61. });
  62. });
  63. } else if (inboxOpened === true) {
  64. extension.windows.focus(inboxWindowId, function (error) {
  65. if (error) {
  66. inboxOpened = false;
  67. openInbox();
  68. }
  69. });
  70. }
  71. };
  72. window.setUnreadCount = function(count) {
  73. if (count > 0) {
  74. extension.navigator.setBadgeText(count);
  75. if (inboxOpened === true && appWindow) {
  76. appWindow.contentWindow.document.title = "Cable (" + count + ")";
  77. }
  78. } else {
  79. extension.navigator.setBadgeText("");
  80. if (inboxOpened === true && appWindow) {
  81. appWindow.contentWindow.document.title = "Cable";
  82. }
  83. }
  84. };
  85. var open;
  86. window.openConversation = function(conversation) {
  87. if (inboxOpened === true) {
  88. appWindow.contentWindow.openConversation(conversation);
  89. } else {
  90. open = conversation;
  91. }
  92. openInbox();
  93. };
  94. window.getOpenConversation = function() {
  95. var o = open;
  96. open = null;
  97. return o;
  98. };
  99. })();