attachments.js 3.5 KB

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