attachment_view.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. (function () {
  5. 'use strict';
  6. var FileView = Backbone.View.extend({
  7. tagName: 'a',
  8. initialize: function(dataUrl) {
  9. this.dataUrl = dataUrl;
  10. this.$el.text(i18n('unsupportedAttachment'));
  11. },
  12. render: function() {
  13. this.$el.attr('href', this.dataUrl);
  14. this.trigger('update');
  15. return this;
  16. }
  17. });
  18. var ImageView = Backbone.View.extend({
  19. tagName: 'img',
  20. initialize: function(dataUrl) {
  21. this.dataUrl = dataUrl;
  22. },
  23. events: {
  24. 'load': 'update',
  25. },
  26. update: function() {
  27. this.trigger('update');
  28. },
  29. render: function() {
  30. this.$el.attr('src', this.dataUrl);
  31. return this;
  32. }
  33. });
  34. var MediaView = Backbone.View.extend({
  35. initialize: function(dataUrl, contentType) {
  36. this.dataUrl = dataUrl;
  37. this.contentType = contentType;
  38. this.$el.attr('controls', '');
  39. },
  40. events: {
  41. 'canplay': 'canplay'
  42. },
  43. canplay: function() {
  44. this.trigger('update');
  45. },
  46. render: function() {
  47. var $el = $('<source>');
  48. $el.attr('src', this.dataUrl);
  49. $el.attr('type', this.contentType);
  50. this.$el.append($el);
  51. return this;
  52. }
  53. });
  54. var AudioView = MediaView.extend({ tagName: 'audio' });
  55. var VideoView = MediaView.extend({ tagName: 'video' });
  56. Whisper.AttachmentView = Backbone.View.extend({
  57. tagName: 'span',
  58. className: 'attachment',
  59. initialize: function(options) {
  60. this.blob = new Blob([this.model.data], {type: this.model.contentType});
  61. var parts = this.model.contentType.split('/');
  62. this.contentType = parts[0];
  63. this.fileType = parts[1];
  64. if (options.timestamp) {
  65. this.timestamp = options.timestamp;
  66. }
  67. },
  68. events: {
  69. 'click': 'onclick'
  70. },
  71. onclick: function(e) {
  72. switch (this.contentType) {
  73. case 'audio':
  74. case 'video':
  75. return;
  76. case 'image':
  77. var view = new Whisper.LightboxView({ model: this });
  78. view.render();
  79. view.$el.appendTo(this.el);
  80. view.$el.trigger('show');
  81. break;
  82. default:
  83. this.saveFile();
  84. }
  85. },
  86. suggestedName: function() {
  87. var suggestion = 'signal';
  88. if (this.timestamp) {
  89. suggestion += moment(this.timestamp).format('-YYYY-MM-DD-HHmmss');
  90. }
  91. if (this.fileType) {
  92. suggestion += '.' + this.fileType;
  93. }
  94. return suggestion;
  95. },
  96. saveFile: function() {
  97. var blob = this.blob;
  98. var w = extension.windows.getViews()[0];
  99. if (w && w.chrome && w.chrome.fileSystem) {
  100. w.chrome.fileSystem.chooseEntry({
  101. type: 'saveFile', suggestedName: this.suggestedName()
  102. }, function(entry) {
  103. if (!entry) {
  104. return;
  105. }
  106. entry.createWriter(function(fileWriter) {
  107. fileWriter.write(blob);
  108. });
  109. });
  110. } else {
  111. console.log('Failed to get window');
  112. }
  113. },
  114. render: function() {
  115. var View;
  116. switch(this.contentType) {
  117. case 'image': View = ImageView; break;
  118. case 'audio': View = AudioView; break;
  119. case 'video': View = VideoView; break;
  120. default : View = FileView; break;
  121. }
  122. if (!this.objectUrl) {
  123. this.objectUrl = window.URL.createObjectURL(this.blob);
  124. }
  125. var view = new View(this.objectUrl, this.model.contentType);
  126. view.$el.appendTo(this.$el);
  127. view.on('update', this.trigger.bind(this, 'update'));
  128. view.render();
  129. return this;
  130. }
  131. });
  132. Whisper.LightboxView = Whisper.View.extend({
  133. templateName: 'lightbox',
  134. className: 'modal lightbox',
  135. initialize: function() {
  136. this.window = extension.windows.getViews()[0];
  137. this.$document = $(this.window.document);
  138. this.listener = this.onkeyup.bind(this);
  139. this.$document.on('keyup', this.listener);
  140. },
  141. events: {
  142. 'click .save': 'save',
  143. 'click .close': 'remove',
  144. 'click': 'onclick'
  145. },
  146. save: function(e) {
  147. this.model.saveFile();
  148. },
  149. onclick: function(e) {
  150. var $el = this.$(e.target);
  151. if (!$el.hasClass('image') && !$el.closest('.controls').length ) {
  152. e.preventDefault();
  153. this.remove();
  154. return false;
  155. }
  156. },
  157. onkeyup: function(e) {
  158. if (e.keyCode === 27) {
  159. this.remove();
  160. this.$document.off('keyup', this.listener);
  161. }
  162. },
  163. render_attributes: function() {
  164. return { url: this.model.objectUrl };
  165. }
  166. });
  167. })();