attachments.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. });
  22. Template.attachmentsGalery.helpers({
  23. isBoardAdmin() {
  24. return Meteor.user().isBoardAdmin();
  25. },
  26. fileSize(size) {
  27. return Math.round(size / 1024);
  28. },
  29. });
  30. Template.cardAttachmentsPopup.events({
  31. 'change .js-attach-file'(event) {
  32. const card = this;
  33. if (event.currentTarget.files && event.currentTarget.files[0]) {
  34. const uploader = Attachments.insert(
  35. {
  36. file: event.currentTarget.files[0],
  37. meta: Utils.getCommonAttachmentMetaFrom(card),
  38. chunkSize: 'dynamic',
  39. },
  40. false,
  41. );
  42. uploader.on('uploaded', (error, fileRef) => {
  43. if (!error) {
  44. if (fileRef.isImage) {
  45. card.setCover(fileRef._id);
  46. }
  47. }
  48. });
  49. uploader.on('end', (error, fileRef) => {
  50. Popup.back();
  51. });
  52. uploader.start();
  53. }
  54. },
  55. 'click .js-computer-upload'(event, templateInstance) {
  56. templateInstance.find('.js-attach-file').click();
  57. event.preventDefault();
  58. },
  59. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  60. });
  61. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  62. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  63. let pastedResults = null;
  64. Template.previewClipboardImagePopup.onRendered(() => {
  65. // we can paste image from clipboard
  66. const handle = results => {
  67. if (results.dataURL.startsWith('data:image/')) {
  68. const direct = results => {
  69. $('img.preview-clipboard-image').attr('src', results.dataURL);
  70. pastedResults = results;
  71. };
  72. if (MAX_IMAGE_PIXEL) {
  73. // if has size limitation on image we shrink it before uploading
  74. Utils.shrinkImage({
  75. dataurl: results.dataURL,
  76. maxSize: MAX_IMAGE_PIXEL,
  77. ratio: COMPRESS_RATIO,
  78. callback(changed) {
  79. if (changed !== false && !!changed) {
  80. results.dataURL = changed;
  81. }
  82. direct(results);
  83. },
  84. });
  85. } else {
  86. direct(results);
  87. }
  88. }
  89. };
  90. $(document.body).pasteImageReader(handle);
  91. // we can also drag & drop image file to it
  92. $(document.body).dropImageReader(handle);
  93. });
  94. Template.previewClipboardImagePopup.events({
  95. 'click .js-upload-pasted-image'() {
  96. const card = this;
  97. if (pastedResults && pastedResults.file) {
  98. const file = pastedResults.file;
  99. window.oPasted = pastedResults;
  100. const uploader = Attachments.insert(
  101. {
  102. file,
  103. meta: Utils.getCommonAttachmentMetaFrom(card),
  104. fileName: file.name || file.type.replace('image/', 'clipboard.'),
  105. chunkSize: 'dynamic',
  106. },
  107. false,
  108. );
  109. uploader.on('uploaded', (error, fileRef) => {
  110. if (!error) {
  111. if (fileRef.isImage) {
  112. card.setCover(fileRef._id);
  113. }
  114. }
  115. });
  116. uploader.on('end', (error, fileRef) => {
  117. pastedResults = null;
  118. $(document.body).pasteImageReader(() => {});
  119. Popup.back();
  120. });
  121. uploader.start();
  122. }
  123. },
  124. });