attachments.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.meta.cardId).setCover(this._id);
  17. },
  18. 'click .js-remove-cover'() {
  19. Cards.findOne(this.meta.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. url() {
  49. return Attachments.link(this, 'original', '/');
  50. },
  51. isUploaded() {
  52. return !this.meta.uploading;
  53. },
  54. isImage() {
  55. return !!this.isImage;
  56. },
  57. });
  58. Template.previewAttachedImagePopup.events({
  59. 'click .js-large-image-clicked'() {
  60. Popup.close();
  61. },
  62. });
  63. Template.previewAttachedImagePopup.helpers({
  64. url() {
  65. return Attachments.link(this, 'original', '/');
  66. }
  67. });
  68. // For uploading popup
  69. let uploadFileSize = new ReactiveVar('');
  70. let uploadProgress = new ReactiveVar(0);
  71. Template.cardAttachmentsPopup.events({
  72. 'change .js-attach-file'(event, instance) {
  73. const card = this;
  74. const callbacks = {
  75. onBeforeUpload: (err, fileData) => {
  76. Popup.open('uploading')(this.clickEvent);
  77. uploadFileSize.set('...');
  78. uploadProgress.set(0);
  79. return true;
  80. },
  81. onUploaded: (err, attachment) => {
  82. if (attachment && attachment._id && attachment.isImage) {
  83. card.setCover(attachment._id);
  84. }
  85. Popup.close();
  86. },
  87. onStart: (error, fileData) => {
  88. uploadFileSize.set(formatBytes(fileData.size));
  89. },
  90. onError: (err, fileObj) => {
  91. console.log('Error!', err);
  92. },
  93. onProgress: (progress, fileData) => {
  94. uploadProgress.set(progress);
  95. }
  96. };
  97. const processFile = f => {
  98. Utils.processUploadedAttachment(card, f, callbacks);
  99. };
  100. FS.Utility.eachFile(event, f => {
  101. if (
  102. MAX_IMAGE_PIXEL > 0 &&
  103. typeof f.type === 'string' &&
  104. f.type.match(/^image/)
  105. ) {
  106. // is image
  107. const reader = new FileReader();
  108. reader.onload = function(e) {
  109. const dataurl = e && e.target && e.target.result;
  110. if (dataurl !== undefined) {
  111. Utils.shrinkImage({
  112. dataurl,
  113. maxSize: MAX_IMAGE_PIXEL,
  114. ratio: COMPRESS_RATIO,
  115. toBlob: true,
  116. callback(blob) {
  117. if (blob === false) {
  118. processFile(f);
  119. } else {
  120. blob.name = f.name;
  121. processFile(blob);
  122. }
  123. },
  124. });
  125. } else {
  126. // couldn't process it let other function handle it?
  127. processFile(f);
  128. }
  129. };
  130. reader.readAsDataURL(f);
  131. } else {
  132. processFile(f);
  133. }
  134. });
  135. },
  136. 'click .js-computer-upload'(event, templateInstance) {
  137. this.clickEvent = event;
  138. templateInstance.find('.js-attach-file').click();
  139. event.preventDefault();
  140. },
  141. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  142. });
  143. Template.uploadingPopup.helpers({
  144. fileSize: () => {
  145. return uploadFileSize.get();
  146. },
  147. progress: () => {
  148. return uploadProgress.get();
  149. }
  150. });
  151. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  152. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  153. let pastedResults = null;
  154. Template.previewClipboardImagePopup.onRendered(() => {
  155. // we can paste image from clipboard
  156. const handle = results => {
  157. if (results.dataURL.startsWith('data:image/')) {
  158. const direct = results => {
  159. $('img.preview-clipboard-image').attr('src', results.dataURL);
  160. pastedResults = results;
  161. };
  162. if (MAX_IMAGE_PIXEL) {
  163. // if has size limitation on image we shrink it before uploading
  164. Utils.shrinkImage({
  165. dataurl: results.dataURL,
  166. maxSize: MAX_IMAGE_PIXEL,
  167. ratio: COMPRESS_RATIO,
  168. callback(changed) {
  169. if (changed !== false && !!changed) {
  170. results.dataURL = changed;
  171. }
  172. direct(results);
  173. },
  174. });
  175. } else {
  176. direct(results);
  177. }
  178. }
  179. };
  180. $(document.body).pasteImageReader(handle);
  181. // we can also drag & drop image file to it
  182. $(document.body).dropImageReader(handle);
  183. });
  184. Template.previewClipboardImagePopup.events({
  185. 'click .js-upload-pasted-image'() {
  186. const results = pastedResults;
  187. if (results && results.file) {
  188. window.oPasted = pastedResults;
  189. const card = this;
  190. const settings = {
  191. file: results.file,
  192. streams: 'dynamic',
  193. chunkSize: 'dynamic',
  194. };
  195. if (!results.name) {
  196. // if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
  197. if (typeof results.file.type === 'string') {
  198. settings.fileName =
  199. new Date().getTime() + results.file.type.replace('.+/', '');
  200. }
  201. }
  202. settings.meta = {};
  203. settings.meta.updatedAt = new Date().getTime();
  204. settings.meta.boardId = card.boardId;
  205. settings.meta.cardId = card._id;
  206. settings.meta.userId = Meteor.userId();
  207. const attachment = Attachments.insert(settings);
  208. if (attachment && attachment._id && attachment.isImage) {
  209. card.setCover(attachment._id);
  210. }
  211. pastedResults = null;
  212. $(document.body).pasteImageReader(() => {});
  213. Popup.close();
  214. }
  215. },
  216. });
  217. function formatBytes(bytes, decimals = 2) {
  218. if (bytes === 0) return '0 Bytes';
  219. const k = 1024;
  220. const dm = decimals < 0 ? 0 : decimals;
  221. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  222. const i = Math.floor(Math.log(bytes) / Math.log(k));
  223. return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
  224. }