2
0

attachments.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. Template.attachmentsGalery.events({
  2. 'click .js-add-attachment': Popup.open('cardAttachments'),
  3. 'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete',
  4. function() {
  5. Attachments.remove(this._id);
  6. Popup.close();
  7. }
  8. ),
  9. // If we let this event bubble, FlowRouter will handle it and empty the page
  10. // content, see #101.
  11. 'click .js-download'(event) {
  12. event.stopPropagation();
  13. },
  14. 'click .js-add-cover'() {
  15. Cards.findOne(this.cardId).setCover(this._id);
  16. },
  17. 'click .js-remove-cover'() {
  18. Cards.findOne(this.cardId).unsetCover();
  19. },
  20. 'click .js-preview-image'(evt) {
  21. Popup.open('previewAttachedImage').call(this, evt);
  22. // when multiple thumbnails, if click one then another very fast,
  23. // we might get a wrong width from previous img.
  24. // when popup reused, onRendered() won't be called, so we cannot get there.
  25. // here make sure to get correct size when this img fully loaded.
  26. const img = $('img.preview-large-image')[0];
  27. if (!img) return;
  28. const rePosPopup = () => {
  29. const w = img.width;
  30. const h = img.height;
  31. // if the image is too large, we resize & center the popup.
  32. if (w > 300) {
  33. $('div.pop-over').css({
  34. width: (w + 20),
  35. position: 'absolute',
  36. left: (window.innerWidth - w)/2,
  37. top: (window.innerHeight - h)/2,
  38. });
  39. }
  40. };
  41. const url = $(evt.currentTarget).attr('src');
  42. if (img.src === url && img.complete)
  43. rePosPopup();
  44. else
  45. img.onload = rePosPopup;
  46. },
  47. });
  48. Template.previewAttachedImagePopup.events({
  49. 'click .js-large-image-clicked'(){
  50. Popup.close();
  51. },
  52. });
  53. Template.cardAttachmentsPopup.events({
  54. 'change .js-attach-file'(evt) {
  55. const card = this;
  56. FS.Utility.eachFile(evt, (f) => {
  57. const file = new FS.File(f);
  58. if (card.isLinkedCard()) {
  59. file.boardId = Cards.findOne(card.linkedId).boardId;
  60. file.cardId = card.linkedId;
  61. } else {
  62. file.boardId = card.boardId;
  63. file.swimlaneId = card.swimlaneId;
  64. file.listId = card.listId;
  65. file.cardId = card._id;
  66. }
  67. file.userId = Meteor.userId();
  68. const attachment = Attachments.insert(file);
  69. if (attachment && attachment._id && attachment.isImage()) {
  70. card.setCover(attachment._id);
  71. }
  72. Popup.close();
  73. });
  74. },
  75. 'click .js-computer-upload'(evt, tpl) {
  76. tpl.find('.js-attach-file').click();
  77. evt.preventDefault();
  78. },
  79. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  80. });
  81. let pastedResults = null;
  82. Template.previewClipboardImagePopup.onRendered(() => {
  83. // we can paste image from clipboard
  84. $(document.body).pasteImageReader((results) => {
  85. if (results.dataURL.startsWith('data:image/')) {
  86. $('img.preview-clipboard-image').attr('src', results.dataURL);
  87. pastedResults = results;
  88. }
  89. });
  90. // we can also drag & drop image file to it
  91. $(document.body).dropImageReader((results) => {
  92. if (results.dataURL.startsWith('data:image/')) {
  93. $('img.preview-clipboard-image').attr('src', results.dataURL);
  94. pastedResults = results;
  95. }
  96. });
  97. });
  98. Template.previewClipboardImagePopup.events({
  99. 'click .js-upload-pasted-image'() {
  100. const results = pastedResults;
  101. if (results && results.file) {
  102. const card = this;
  103. const file = new FS.File(results.file);
  104. if (!results.name) {
  105. // if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
  106. if (typeof results.file.type === 'string') {
  107. file.name(results.file.type.replace('image/', 'clipboard.'));
  108. }
  109. }
  110. file.updatedAt(new Date());
  111. file.boardId = card.boardId;
  112. file.cardId = card._id;
  113. file.userId = Meteor.userId();
  114. const attachment = Attachments.insert(file);
  115. if (attachment && attachment._id && attachment.isImage()) {
  116. card.setCover(attachment._id);
  117. }
  118. pastedResults = null;
  119. $(document.body).pasteImageReader(() => {});
  120. Popup.close();
  121. }
  122. },
  123. });