attachments.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.meta.cardId).setCover(this._id);
  17. },
  18. 'click .js-remove-cover'() {
  19. Cards.findOne(this.meta.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. meta: Utils.getCommonAttachmentMetaFrom(card),
  68. chunkSize: 'dynamic',
  69. },
  70. false,
  71. );
  72. uploader.on('uploaded', (error, fileRef) => {
  73. if (!error) {
  74. if (fileRef.isImage) {
  75. card.setCover(fileRef._id);
  76. }
  77. }
  78. });
  79. uploader.on('end', (error, fileRef) => {
  80. Popup.back();
  81. });
  82. uploader.start();
  83. }
  84. },
  85. 'click .js-computer-upload'(event, templateInstance) {
  86. templateInstance.find('.js-attach-file').click();
  87. event.preventDefault();
  88. },
  89. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  90. });
  91. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  92. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  93. let pastedResults = null;
  94. Template.previewClipboardImagePopup.onRendered(() => {
  95. // we can paste image from clipboard
  96. const handle = results => {
  97. if (results.dataURL.startsWith('data:image/')) {
  98. const direct = results => {
  99. $('img.preview-clipboard-image').attr('src', results.dataURL);
  100. pastedResults = results;
  101. };
  102. if (MAX_IMAGE_PIXEL) {
  103. // if has size limitation on image we shrink it before uploading
  104. Utils.shrinkImage({
  105. dataurl: results.dataURL,
  106. maxSize: MAX_IMAGE_PIXEL,
  107. ratio: COMPRESS_RATIO,
  108. callback(changed) {
  109. if (changed !== false && !!changed) {
  110. results.dataURL = changed;
  111. }
  112. direct(results);
  113. },
  114. });
  115. } else {
  116. direct(results);
  117. }
  118. }
  119. };
  120. $(document.body).pasteImageReader(handle);
  121. // we can also drag & drop image file to it
  122. $(document.body).dropImageReader(handle);
  123. });
  124. Template.previewClipboardImagePopup.events({
  125. 'click .js-upload-pasted-image'() {
  126. const card = this;
  127. if (pastedResults && pastedResults.file) {
  128. const file = pastedResults.file;
  129. window.oPasted = pastedResults;
  130. const uploader = Attachments.insert(
  131. {
  132. file,
  133. meta: Utils.getCommonAttachmentMetaFrom(card),
  134. fileName: file.name || file.type.replace('image/', 'clipboard.'),
  135. chunkSize: 'dynamic',
  136. },
  137. false,
  138. );
  139. uploader.on('uploaded', (error, fileRef) => {
  140. if (!error) {
  141. if (fileRef.isImage) {
  142. card.setCover(fileRef._id);
  143. }
  144. }
  145. });
  146. uploader.on('end', (error, fileRef) => {
  147. pastedResults = null;
  148. $(document.body).pasteImageReader(() => {});
  149. Popup.back();
  150. });
  151. uploader.start();
  152. }
  153. },
  154. });