attachments.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.attachmentsGalery.helpers({
  48. isBoardAdmin() {
  49. return Meteor.user().isBoardAdmin();
  50. },
  51. });
  52. Template.previewAttachedImagePopup.events({
  53. 'click .js-large-image-clicked'() {
  54. Popup.close();
  55. },
  56. });
  57. Template.cardAttachmentsPopup.events({
  58. 'change .js-attach-file'(event) {
  59. const card = this;
  60. const processFile = f => {
  61. Utils.processUploadedAttachment(card, f, attachment => {
  62. if (attachment && attachment._id && attachment.isImage()) {
  63. card.setCover(attachment._id);
  64. }
  65. Popup.close();
  66. });
  67. };
  68. FS.Utility.eachFile(event, f => {
  69. if (
  70. MAX_IMAGE_PIXEL > 0 &&
  71. typeof f.type === 'string' &&
  72. f.type.match(/^image/)
  73. ) {
  74. // is image
  75. const reader = new FileReader();
  76. reader.onload = function(e) {
  77. const dataurl = e && e.target && e.target.result;
  78. if (dataurl !== undefined) {
  79. Utils.shrinkImage({
  80. dataurl,
  81. maxSize: MAX_IMAGE_PIXEL,
  82. ratio: COMPRESS_RATIO,
  83. toBlob: true,
  84. callback(blob) {
  85. if (blob === false) {
  86. processFile(f);
  87. } else {
  88. blob.name = f.name;
  89. processFile(blob);
  90. }
  91. },
  92. });
  93. } else {
  94. // couldn't process it let other function handle it?
  95. processFile(f);
  96. }
  97. };
  98. reader.readAsDataURL(f);
  99. } else {
  100. processFile(f);
  101. }
  102. });
  103. },
  104. 'click .js-computer-upload'(event, templateInstance) {
  105. templateInstance.find('.js-attach-file').click();
  106. event.preventDefault();
  107. },
  108. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  109. });
  110. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  111. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  112. let pastedResults = null;
  113. Template.previewClipboardImagePopup.onRendered(() => {
  114. // we can paste image from clipboard
  115. const handle = results => {
  116. if (results.dataURL.startsWith('data:image/')) {
  117. const direct = results => {
  118. $('img.preview-clipboard-image').attr('src', results.dataURL);
  119. pastedResults = results;
  120. };
  121. if (MAX_IMAGE_PIXEL) {
  122. // if has size limitation on image we shrink it before uploading
  123. Utils.shrinkImage({
  124. dataurl: results.dataURL,
  125. maxSize: MAX_IMAGE_PIXEL,
  126. ratio: COMPRESS_RATIO,
  127. callback(changed) {
  128. if (changed !== false && !!changed) {
  129. results.dataURL = changed;
  130. }
  131. direct(results);
  132. },
  133. });
  134. } else {
  135. direct(results);
  136. }
  137. }
  138. };
  139. $(document.body).pasteImageReader(handle);
  140. // we can also drag & drop image file to it
  141. $(document.body).dropImageReader(handle);
  142. });
  143. Template.previewClipboardImagePopup.events({
  144. 'click .js-upload-pasted-image'() {
  145. const results = pastedResults;
  146. if (results && results.file) {
  147. window.oPasted = pastedResults;
  148. const card = this;
  149. const file = new FS.File(results.file);
  150. if (!results.name) {
  151. // if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
  152. if (typeof results.file.type === 'string') {
  153. file.name(results.file.type.replace('image/', 'clipboard.'));
  154. }
  155. }
  156. file.updatedAt(new Date());
  157. file.boardId = card.boardId;
  158. file.cardId = card._id;
  159. file.userId = Meteor.userId();
  160. const attachment = Attachments.insert(file);
  161. if (attachment && attachment._id && attachment.isImage()) {
  162. card.setCover(attachment._id);
  163. }
  164. pastedResults = null;
  165. $(document.body).pasteImageReader(() => {});
  166. Popup.close();
  167. }
  168. },
  169. });