notifications.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. ;(function() {
  5. 'use strict';
  6. window.Whisper = window.Whisper || {};
  7. var SETTINGS = {
  8. OFF : 'off',
  9. COUNT : 'count',
  10. NAME : 'name',
  11. MESSAGE : 'message'
  12. };
  13. var sound = new Audio('/audio/NewMessage.mp3');
  14. Whisper.Notifications = new (Backbone.Collection.extend({
  15. initialize: function() {
  16. this.on('add', _.debounce(this.update.bind(this), 1000));
  17. this.on('remove', this.onRemove);
  18. },
  19. onclick: function() {
  20. var last = this.last();
  21. if (!last) {
  22. openInbox();
  23. return;
  24. }
  25. var conversation = ConversationController.create({
  26. id: last.get('conversationId')
  27. });
  28. openConversation(conversation);
  29. this.clear();
  30. },
  31. update: function() {
  32. console.log('updating notifications', this.length);
  33. extension.notification.clear();
  34. if (this.length === 0) {
  35. return;
  36. }
  37. var audioNotification = storage.get('audio-notification') || false;
  38. if (audioNotification) {
  39. sound.play();
  40. }
  41. var setting = storage.get('notification-setting') || 'message';
  42. if (setting === SETTINGS.OFF) {
  43. return;
  44. }
  45. var iconUrl = 'images/icon_128.png';
  46. var title = [
  47. this.length,
  48. this.length === 1 ? i18n('newMessage') : i18n('newMessages')
  49. ].join(' ');
  50. if (setting === SETTINGS.COUNT) {
  51. extension.notification.update({
  52. type : 'basic',
  53. title : title,
  54. iconUrl : iconUrl
  55. });
  56. return;
  57. }
  58. if (this.length > 1) {
  59. var conversationIds = _.uniq(this.map(function(m) {
  60. return m.get('conversationId');
  61. }));
  62. if (conversationIds.length === 1 && this.showSender()) {
  63. iconUrl = this.at(0).get('iconUrl');
  64. }
  65. extension.notification.update({
  66. type : 'list',
  67. iconUrl : iconUrl,
  68. title : title,
  69. message : 'Most recent from ' + this.last().get('title'),
  70. items : this.map(function(m) {
  71. var message, title;
  72. if (this.showMessage()) {
  73. return {
  74. title : m.get('title'),
  75. message : m.get('message')
  76. };
  77. } else if (this.showSender()) {
  78. return {
  79. title : m.get('title'),
  80. message : i18n('newMessage')
  81. };
  82. }
  83. }.bind(this)),
  84. buttons : [{
  85. title : 'Mark all as read',
  86. iconUrl : 'images/check.svg'
  87. }]
  88. });
  89. } else {
  90. var m = this.at(0);
  91. var type = 'basic';
  92. var message = i18n('newMessage');
  93. var imageUrl;
  94. if (this.showMessage()) {
  95. message = m.get('message');
  96. if (m.get('imageUrl')) {
  97. type = 'image';
  98. imageUrl = m.get('imageUrl');
  99. }
  100. }
  101. if (this.showSender()) {
  102. title = m.get('title');
  103. iconUrl = m.get('iconUrl');
  104. }
  105. extension.notification.update({
  106. type : type,
  107. title : title,
  108. message : message,
  109. iconUrl : iconUrl,
  110. imageUrl : imageUrl
  111. });
  112. }
  113. },
  114. getSetting: function() {
  115. return storage.get('notification-setting') || 'message';
  116. },
  117. showMessage: function() {
  118. return this.getSetting() === SETTINGS.MESSAGE;
  119. },
  120. showSender: function() {
  121. var setting = this.getSetting();
  122. return (setting === SETTINGS.MESSAGE || setting === SETTINGS.NAME);
  123. },
  124. onRemove: function() {
  125. console.log('remove notification');
  126. if (this.length === 0) {
  127. extension.notification.clear();
  128. return;
  129. }
  130. },
  131. clear: function() {
  132. this.reset([]);
  133. }
  134. }))();
  135. })();