attachments.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. FS.Utility.eachFile(event, f => {
  56. const file = new FS.File(f);
  57. if (card.isLinkedCard()) {
  58. file.boardId = Cards.findOne(card.linkedId).boardId;
  59. file.cardId = card.linkedId;
  60. } else {
  61. file.boardId = card.boardId;
  62. file.swimlaneId = card.swimlaneId;
  63. file.listId = card.listId;
  64. file.cardId = card._id;
  65. }
  66. file.userId = Meteor.userId();
  67. const attachment = Attachments.insert(file);
  68. if (attachment && attachment._id && attachment.isImage()) {
  69. card.setCover(attachment._id);
  70. }
  71. Popup.close();
  72. });
  73. },
  74. 'click .js-computer-upload'(event, templateInstance) {
  75. templateInstance.find('.js-attach-file').click();
  76. event.preventDefault();
  77. },
  78. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  79. });
  80. let pastedResults = null;
  81. Template.previewClipboardImagePopup.onRendered(() => {
  82. // we can paste image from clipboard
  83. $(document.body).pasteImageReader(results => {
  84. if (results.dataURL.startsWith('data:image/')) {
  85. $('img.preview-clipboard-image').attr('src', results.dataURL);
  86. pastedResults = results;
  87. }
  88. });
  89. // we can also drag & drop image file to it
  90. $(document.body).dropImageReader(results => {
  91. if (results.dataURL.startsWith('data:image/')) {
  92. $('img.preview-clipboard-image').attr('src', results.dataURL);
  93. pastedResults = results;
  94. }
  95. });
  96. });
  97. Template.previewClipboardImagePopup.events({
  98. 'click .js-upload-pasted-image'() {
  99. const results = pastedResults;
  100. if (results && results.file) {
  101. const card = this;
  102. const file = new FS.File(results.file);
  103. if (!results.name) {
  104. // if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
  105. if (typeof results.file.type === 'string') {
  106. file.name(results.file.type.replace('image/', 'clipboard.'));
  107. }
  108. }
  109. file.updatedAt(new Date());
  110. file.boardId = card.boardId;
  111. file.cardId = card._id;
  112. file.userId = Meteor.userId();
  113. const attachment = Attachments.insert(file);
  114. if (attachment && attachment._id && attachment.isImage()) {
  115. card.setCover(attachment._id);
  116. }
  117. pastedResults = null;
  118. $(document.body).pasteImageReader(() => {});
  119. Popup.close();
  120. }
  121. },
  122. });