attachments.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. Template.attachmentsGalery.events({
  2. 'click .js-add-attachment': Popup.open('cardAttachments'),
  3. 'click .js-confirm-delete': Popup.afterConfirm(
  4. 'attachmentDelete',
  5. function() {
  6. Attachments.remove(this._id);
  7. Popup.close();
  8. },
  9. ),
  10. // If we let this event bubble, FlowRouter will handle it and empty the page
  11. // content, see #101.
  12. 'click .js-download'(event) {
  13. event.stopPropagation();
  14. },
  15. 'click .js-add-cover'() {
  16. Cards.findOne(this.cardId).setCover(this._id);
  17. },
  18. 'click .js-remove-cover'() {
  19. Cards.findOne(this.cardId).unsetCover();
  20. },
  21. 'click .js-preview-image'(event) {
  22. Popup.open('previewAttachedImage').call(this, event);
  23. // when multiple thumbnails, if click one then another very fast,
  24. // we might get a wrong width from previous img.
  25. // when popup reused, onRendered() won't be called, so we cannot get there.
  26. // here make sure to get correct size when this img fully loaded.
  27. const img = $('img.preview-large-image')[0];
  28. if (!img) return;
  29. const rePosPopup = () => {
  30. const w = img.width;
  31. const h = img.height;
  32. // if the image is too large, we resize & center the popup.
  33. if (w > 300) {
  34. $('div.pop-over').css({
  35. width: w + 20,
  36. position: 'absolute',
  37. left: (window.innerWidth - w) / 2,
  38. top: (window.innerHeight - h) / 2,
  39. });
  40. }
  41. };
  42. const url = $(event.currentTarget).attr('src');
  43. if (img.src === url && img.complete) rePosPopup();
  44. else img.onload = rePosPopup;
  45. },
  46. });
  47. Template.previewAttachedImagePopup.events({
  48. 'click .js-large-image-clicked'() {
  49. Popup.close();
  50. },
  51. });
  52. Template.cardAttachmentsPopup.events({
  53. 'change .js-attach-file'(event) {
  54. const card = this;
  55. const processFile = f => {
  56. Utils.processUploadedAttachment(card, f, attachment => {
  57. if (attachment && attachment._id && attachment.isImage()) {
  58. card.setCover(attachment._id);
  59. }
  60. Popup.close();
  61. });
  62. };
  63. FS.Utility.eachFile(event, f => {
  64. if (
  65. MAX_IMAGE_PIXEL > 0 &&
  66. typeof f.type === 'string' &&
  67. f.type.match(/^image/)
  68. ) {
  69. // is image
  70. const reader = new FileReader();
  71. reader.onload = function(e) {
  72. const dataurl = e && e.target && e.target.result;
  73. if (dataurl !== undefined) {
  74. Utils.shrinkImage({
  75. dataurl,
  76. maxSize: MAX_IMAGE_PIXEL,
  77. ratio: COMPRESS_RATIO,
  78. toBlob: true,
  79. callback(blob) {
  80. if (blob === false) {
  81. processFile(f);
  82. } else {
  83. blob.name = f.name;
  84. processFile(blob);
  85. }
  86. },
  87. });
  88. } else {
  89. // couldn't process it let other function handle it?
  90. processFile(f);
  91. }
  92. };
  93. reader.readAsDataURL(f);
  94. } else {
  95. processFile(f);
  96. }
  97. });
  98. },
  99. 'click .js-computer-upload'(event, templateInstance) {
  100. templateInstance.find('.js-attach-file').click();
  101. event.preventDefault();
  102. },
  103. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  104. });
  105. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  106. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  107. let pastedResults = null;
  108. Template.previewClipboardImagePopup.onRendered(() => {
  109. // we can paste image from clipboard
  110. const handle = results => {
  111. if (results.dataURL.startsWith('data:image/')) {
  112. const direct = results => {
  113. $('img.preview-clipboard-image').attr('src', results.dataURL);
  114. pastedResults = results;
  115. };
  116. if (MAX_IMAGE_PIXEL) {
  117. // if has size limitation on image we shrink it before uploading
  118. Utils.shrinkImage({
  119. dataurl: results.dataURL,
  120. maxSize: MAX_IMAGE_PIXEL,
  121. ratio: COMPRESS_RATIO,
  122. callback(changed) {
  123. if (changed !== false && !!changed) {
  124. results.dataURL = changed;
  125. }
  126. direct(results);
  127. },
  128. });
  129. } else {
  130. direct(results);
  131. }
  132. }
  133. };
  134. $(document.body).pasteImageReader(handle);
  135. // we can also drag & drop image file to it
  136. $(document.body).dropImageReader(handle);
  137. });
  138. Template.previewClipboardImagePopup.events({
  139. 'click .js-upload-pasted-image'() {
  140. const results = pastedResults;
  141. if (results && results.file) {
  142. window.oPasted = pastedResults;
  143. const card = this;
  144. const file = new FS.File(results.file);
  145. if (!results.name) {
  146. // if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
  147. if (typeof results.file.type === 'string') {
  148. file.name(results.file.type.replace('image/', 'clipboard.'));
  149. }
  150. }
  151. file.updatedAt(new Date());
  152. file.boardId = card.boardId;
  153. file.cardId = card._id;
  154. file.userId = Meteor.userId();
  155. const attachment = Attachments.insert(file);
  156. if (attachment && attachment._id && attachment.isImage()) {
  157. card.setCover(attachment._id);
  158. }
  159. pastedResults = null;
  160. $(document.body).pasteImageReader(() => {});
  161. Popup.close();
  162. }
  163. },
  164. });