attachments.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. const processFile = 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. FS.Utility.eachFile(event, f => {
  74. if (
  75. MAX_IMAGE_PIXEL > 0 &&
  76. typeof f.type === 'string' &&
  77. f.type.match(/^image/)
  78. ) {
  79. // is image
  80. const reader = new FileReader();
  81. reader.onload = function(e) {
  82. const dataurl = e && e.target && e.target.result;
  83. if (dataurl !== undefined) {
  84. shrinkImage({
  85. dataurl,
  86. maxSize: MAX_IMAGE_PIXEL,
  87. ratio: COMPRESS_RATIO,
  88. toBlob: true,
  89. callback(blob) {
  90. if (blob === false) {
  91. processFile(f);
  92. } else {
  93. blob.name = f.name;
  94. processFile(blob);
  95. }
  96. },
  97. });
  98. } else {
  99. // couldn't process it let other function handle it?
  100. processFile(f);
  101. }
  102. };
  103. reader.readAsDataURL(f);
  104. } else {
  105. processFile(f);
  106. }
  107. });
  108. },
  109. 'click .js-computer-upload'(event, templateInstance) {
  110. templateInstance.find('.js-attach-file').click();
  111. event.preventDefault();
  112. },
  113. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  114. });
  115. const MAX_IMAGE_PIXEL = Meteor.settings.public.MAX_IMAGE_PIXEL;
  116. const COMPRESS_RATIO = Meteor.settings.public.IMAGE_COMPRESS_RATIO;
  117. let pastedResults = null;
  118. const shrinkImage = function(options) {
  119. // shrink image to certain size
  120. const dataurl = options.dataurl,
  121. callback = options.callback,
  122. toBlob = options.toBlob;
  123. let canvas = document.createElement('canvas'),
  124. image = document.createElement('img');
  125. const maxSize = options.maxSize || 1024;
  126. const ratio = options.ratio || 1.0;
  127. const next = function(result) {
  128. image = null;
  129. canvas = null;
  130. if (typeof callback === 'function') {
  131. callback(result);
  132. }
  133. };
  134. image.onload = function() {
  135. let width = this.width,
  136. height = this.height;
  137. let changed = false;
  138. if (width > height) {
  139. if (width > maxSize) {
  140. height *= maxSize / width;
  141. width = maxSize;
  142. changed = true;
  143. }
  144. } else if (height > maxSize) {
  145. width *= maxSize / height;
  146. height = maxSize;
  147. changed = true;
  148. }
  149. canvas.width = width;
  150. canvas.height = height;
  151. canvas.getContext('2d').drawImage(this, 0, 0, width, height);
  152. if (changed === true) {
  153. const type = 'image/jpeg';
  154. if (toBlob) {
  155. canvas.toBlob(next, type, ratio);
  156. } else {
  157. next(canvas.toDataURL(type, ratio));
  158. }
  159. } else {
  160. next(changed);
  161. }
  162. };
  163. image.onerror = function() {
  164. next(false);
  165. };
  166. image.src = dataurl;
  167. };
  168. Template.previewClipboardImagePopup.onRendered(() => {
  169. // we can paste image from clipboard
  170. const handle = results => {
  171. if (results.dataURL.startsWith('data:image/')) {
  172. const direct = results => {
  173. $('img.preview-clipboard-image').attr('src', results.dataURL);
  174. pastedResults = results;
  175. };
  176. if (MAX_IMAGE_PIXEL) {
  177. // if has size limitation on image we shrink it before uploading
  178. shrinkImage({
  179. dataurl: results.dataURL,
  180. maxSize: MAX_IMAGE_PIXEL,
  181. ratio: COMPRESS_RATIO,
  182. callback(changed) {
  183. if (changed !== false && !!changed) {
  184. results.dataURL = changed;
  185. }
  186. direct(results);
  187. },
  188. });
  189. }
  190. }
  191. };
  192. $(document.body).pasteImageReader(handle);
  193. // we can also drag & drop image file to it
  194. $(document.body).dropImageReader(handle);
  195. });
  196. Template.previewClipboardImagePopup.events({
  197. 'click .js-upload-pasted-image'() {
  198. const results = pastedResults;
  199. if (results && results.file) {
  200. window.oPasted = pastedResults;
  201. const card = this;
  202. const file = new FS.File(results.file);
  203. if (!results.name) {
  204. // if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
  205. if (typeof results.file.type === 'string') {
  206. file.name(results.file.type.replace('image/', 'clipboard.'));
  207. }
  208. }
  209. file.updatedAt(new Date());
  210. file.boardId = card.boardId;
  211. file.cardId = card._id;
  212. file.userId = Meteor.userId();
  213. const attachment = Attachments.insert(file);
  214. if (attachment && attachment._id && attachment.isImage()) {
  215. card.setCover(attachment._id);
  216. }
  217. pastedResults = null;
  218. $(document.body).pasteImageReader(() => {});
  219. Popup.close();
  220. }
  221. },
  222. });