attachments.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.back();
  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.attachmentsGalery.helpers({
  48. isBoardAdmin() {
  49. return Meteor.user().isBoardAdmin();
  50. },
  51. fileSize(size) {
  52. return Math.round(size / 1024);
  53. },
  54. });
  55. Template.previewAttachedImagePopup.events({
  56. 'click .js-large-image-clicked'() {
  57. Popup.back();
  58. },
  59. });
  60. Template.cardAttachmentsPopup.events({
  61. 'change .js-attach-file'(event) {
  62. const card = this;
  63. if (event.currentTarget.files && event.currentTarget.files[0]) {
  64. const uploader = Attachments.insert(
  65. {
  66. file: event.currentTarget.files[0],
  67. streams: 'dynamic',
  68. chunkSize: 'dynamic',
  69. },
  70. false,
  71. );
  72. uploader.on('uploaded', (error, fileObj) => {
  73. if (!error) {
  74. if (fileObj.isImage) {
  75. card.setCover(fileObj._id);
  76. }
  77. Utils.addCommonMetaToAttachment(card, fileObj);
  78. }
  79. });
  80. uploader.on('end', (error, fileObj) => {
  81. Popup.back();
  82. });
  83. uploader.start();
  84. }
  85. },
  86. 'click .js-computer-upload'(event, templateInstance) {
  87. templateInstance.find('.js-attach-file').click();
  88. event.preventDefault();
  89. },
  90. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  91. });
  92. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  93. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  94. let pastedResults = null;
  95. Template.previewClipboardImagePopup.onRendered(() => {
  96. // we can paste image from clipboard
  97. const handle = results => {
  98. if (results.dataURL.startsWith('data:image/')) {
  99. const direct = results => {
  100. $('img.preview-clipboard-image').attr('src', results.dataURL);
  101. pastedResults = results;
  102. };
  103. if (MAX_IMAGE_PIXEL) {
  104. // if has size limitation on image we shrink it before uploading
  105. Utils.shrinkImage({
  106. dataurl: results.dataURL,
  107. maxSize: MAX_IMAGE_PIXEL,
  108. ratio: COMPRESS_RATIO,
  109. callback(changed) {
  110. if (changed !== false && !!changed) {
  111. results.dataURL = changed;
  112. }
  113. direct(results);
  114. },
  115. });
  116. } else {
  117. direct(results);
  118. }
  119. }
  120. };
  121. $(document.body).pasteImageReader(handle);
  122. // we can also drag & drop image file to it
  123. $(document.body).dropImageReader(handle);
  124. });
  125. Template.previewClipboardImagePopup.events({
  126. 'click .js-upload-pasted-image'() {
  127. const results = pastedResults;
  128. if (results && results.file) {
  129. window.oPasted = pastedResults;
  130. const card = this;
  131. const file = new FS.File(results.file);
  132. if (!results.name) {
  133. // if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
  134. if (typeof results.file.type === 'string') {
  135. file.name(results.file.type.replace('image/', 'clipboard.'));
  136. }
  137. }
  138. file.updatedAt(new Date());
  139. file.boardId = card.boardId;
  140. file.cardId = card._id;
  141. file.userId = Meteor.userId();
  142. const attachment = Attachments.insert(file);
  143. if (attachment && attachment._id && attachment.isImage()) {
  144. card.setCover(attachment._id);
  145. }
  146. pastedResults = null;
  147. $(document.body).pasteImageReader(() => {});
  148. Popup.back();
  149. }
  150. },
  151. });