attachments.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import Attachments from '../../../models/attachments';
  2. Template.attachmentsGalery.events({
  3. 'click .js-add-attachment': Popup.open('cardAttachments'),
  4. 'click .js-confirm-delete': Popup.afterConfirm(
  5. 'attachmentDelete',
  6. function() {
  7. Attachments.remove(this._id);
  8. Popup.back();
  9. },
  10. ),
  11. // If we let this event bubble, FlowRouter will handle it and empty the page
  12. // content, see #101.
  13. 'click .js-download'(event) {
  14. event.stopPropagation();
  15. },
  16. 'click .js-add-cover'() {
  17. Cards.findOne(this.cardId).setCover(this._id);
  18. },
  19. 'click .js-remove-cover'() {
  20. Cards.findOne(this.cardId).unsetCover();
  21. },
  22. 'click .js-preview-image'(event) {
  23. Popup.open('previewAttachedImage').call(this, event);
  24. // when multiple thumbnails, if click one then another very fast,
  25. // we might get a wrong width from previous img.
  26. // when popup reused, onRendered() won't be called, so we cannot get there.
  27. // here make sure to get correct size when this img fully loaded.
  28. const img = $('img.preview-large-image')[0];
  29. if (!img) return;
  30. const rePosPopup = () => {
  31. const w = img.width;
  32. const h = img.height;
  33. // if the image is too large, we resize & center the popup.
  34. if (w > 300) {
  35. $('div.pop-over').css({
  36. width: w + 20,
  37. position: 'absolute',
  38. left: (window.innerWidth - w) / 2,
  39. top: (window.innerHeight - h) / 2,
  40. });
  41. }
  42. };
  43. const url = $(event.currentTarget).attr('src');
  44. if (img.src === url && img.complete) rePosPopup();
  45. else img.onload = rePosPopup;
  46. },
  47. });
  48. Template.attachmentsGalery.helpers({
  49. isBoardAdmin() {
  50. return Meteor.user().isBoardAdmin();
  51. },
  52. fileSize(size) {
  53. return Math.round(size / 1024);
  54. },
  55. });
  56. Template.previewAttachedImagePopup.events({
  57. 'click .js-large-image-clicked'() {
  58. Popup.back();
  59. },
  60. });
  61. Template.cardAttachmentsPopup.events({
  62. 'change .js-attach-file'(event) {
  63. const card = this;
  64. if (event.currentTarget.files && event.currentTarget.files[0]) {
  65. const uploader = Attachments.insert(
  66. {
  67. file: event.currentTarget.files[0],
  68. meta: Utils.getCommonAttachmentMetaFrom(card),
  69. chunkSize: 'dynamic',
  70. },
  71. false,
  72. );
  73. uploader.on('uploaded', (error, fileRef) => {
  74. if (!error) {
  75. if (fileRef.isImage) {
  76. card.setCover(fileRef._id);
  77. }
  78. }
  79. });
  80. uploader.on('end', (error, fileRef) => {
  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 card = this;
  128. if (pastedResults && pastedResults.file) {
  129. const file = pastedResults.file;
  130. window.oPasted = pastedResults;
  131. const uploader = Attachments.insert(
  132. {
  133. file,
  134. meta: Utils.getCommonAttachmentMetaFrom(card),
  135. fileName: file.name || file.type.replace('image/', 'clipboard.'),
  136. chunkSize: 'dynamic',
  137. },
  138. false,
  139. );
  140. uploader.on('uploaded', (error, fileRef) => {
  141. if (!error) {
  142. if (fileRef.isImage) {
  143. card.setCover(fileRef._id);
  144. }
  145. }
  146. });
  147. uploader.on('end', (error, fileRef) => {
  148. pastedResults = null;
  149. $(document.body).pasteImageReader(() => {});
  150. Popup.back();
  151. });
  152. uploader.start();
  153. }
  154. },
  155. });