attachments.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. Template.attachmentsGalery.events({
  2. 'click .js-add-attachment': Popup.open('cardAttachments'),
  3. // If we let this event bubble, FlowRouter will handle it and empty the page
  4. // content, see #101.
  5. 'click .js-download'(event) {
  6. event.stopPropagation();
  7. },
  8. 'click .js-open-attachment-menu': Popup.open('attachmentActions'),
  9. });
  10. Template.attachmentsGalery.helpers({
  11. isBoardAdmin() {
  12. return Meteor.user().isBoardAdmin();
  13. },
  14. fileSize(size) {
  15. return Math.round(size / 1024);
  16. },
  17. });
  18. Template.cardAttachmentsPopup.events({
  19. 'change .js-attach-file'(event) {
  20. const card = this;
  21. const files = event.currentTarget.files;
  22. if (files) {
  23. let finished = [];
  24. for (const file of files) {
  25. const fileId = Random.id();
  26. const config = {
  27. file: file,
  28. fileId: fileId,
  29. meta: Utils.getCommonAttachmentMetaFrom(card),
  30. chunkSize: 'dynamic',
  31. };
  32. config.meta.fileId = fileId;
  33. const uploader = Attachments.insert(
  34. config,
  35. false,
  36. );
  37. uploader.on('uploaded', (error, fileRef) => {
  38. if (!error) {
  39. if (fileRef.isImage) {
  40. card.setCover(fileRef._id);
  41. }
  42. }
  43. });
  44. uploader.on('end', (error, fileRef) => {
  45. Popup.back();
  46. });
  47. uploader.start();
  48. }
  49. }
  50. },
  51. 'click .js-computer-upload'(event, templateInstance) {
  52. templateInstance.find('.js-attach-file').click();
  53. event.preventDefault();
  54. },
  55. 'click .js-upload-clipboard-image': Popup.open('previewClipboardImage'),
  56. });
  57. const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
  58. const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
  59. let pastedResults = null;
  60. Template.previewClipboardImagePopup.onRendered(() => {
  61. // we can paste image from clipboard
  62. const handle = results => {
  63. if (results.dataURL.startsWith('data:image/')) {
  64. const direct = results => {
  65. $('img.preview-clipboard-image').attr('src', results.dataURL);
  66. pastedResults = results;
  67. };
  68. if (MAX_IMAGE_PIXEL) {
  69. // if has size limitation on image we shrink it before uploading
  70. Utils.shrinkImage({
  71. dataurl: results.dataURL,
  72. maxSize: MAX_IMAGE_PIXEL,
  73. ratio: COMPRESS_RATIO,
  74. callback(changed) {
  75. if (changed !== false && !!changed) {
  76. results.dataURL = changed;
  77. }
  78. direct(results);
  79. },
  80. });
  81. } else {
  82. direct(results);
  83. }
  84. }
  85. };
  86. $(document.body).pasteImageReader(handle);
  87. // we can also drag & drop image file to it
  88. $(document.body).dropImageReader(handle);
  89. });
  90. Template.previewClipboardImagePopup.events({
  91. 'click .js-upload-pasted-image'() {
  92. const card = this;
  93. if (pastedResults && pastedResults.file) {
  94. const file = pastedResults.file;
  95. window.oPasted = pastedResults;
  96. const fileId = Random.id();
  97. const config = {
  98. file,
  99. fileId: fileId,
  100. meta: Utils.getCommonAttachmentMetaFrom(card),
  101. fileName: file.name || file.type.replace('image/', 'clipboard.'),
  102. chunkSize: 'dynamic',
  103. };
  104. config.meta.fileId = fileId;
  105. const uploader = Attachments.insert(
  106. config,
  107. false,
  108. );
  109. uploader.on('uploaded', (error, fileRef) => {
  110. if (!error) {
  111. if (fileRef.isImage) {
  112. card.setCover(fileRef._id);
  113. }
  114. }
  115. });
  116. uploader.on('end', (error, fileRef) => {
  117. pastedResults = null;
  118. $(document.body).pasteImageReader(() => {});
  119. Popup.back();
  120. });
  121. uploader.start();
  122. }
  123. },
  124. });
  125. BlazeComponent.extendComponent({
  126. isCover() {
  127. const ret = Cards.findOne(this.data().meta.cardId).coverId == this.data()._id;
  128. return ret;
  129. },
  130. events() {
  131. return [
  132. {
  133. 'click .js-rename': Popup.open('attachmentRename'),
  134. 'click .js-confirm-delete': Popup.afterConfirm('attachmentDelete', function() {
  135. Attachments.remove(this._id);
  136. Popup.back(2);
  137. }),
  138. 'click .js-add-cover'() {
  139. Cards.findOne(this.data().meta.cardId).setCover(this.data()._id);
  140. Popup.back();
  141. },
  142. 'click .js-remove-cover'() {
  143. Cards.findOne(this.data().meta.cardId).unsetCover();
  144. Popup.back();
  145. },
  146. 'click .js-move-storage-fs'() {
  147. Meteor.call('moveAttachmentToStorage', this.data()._id, "fs");
  148. Popup.back();
  149. },
  150. 'click .js-move-storage-gridfs'() {
  151. Meteor.call('moveAttachmentToStorage', this.data()._id, "gridfs");
  152. Popup.back();
  153. },
  154. }
  155. ]
  156. }
  157. }).register('attachmentActionsPopup');
  158. BlazeComponent.extendComponent({
  159. getNameWithoutExtension() {
  160. const ret = this.data().name.replace(new RegExp("\." + this.data().extension + "$"), "");
  161. return ret;
  162. },
  163. events() {
  164. return [
  165. {
  166. 'keydown input.js-edit-attachment-name'(evt) {
  167. // enter = save
  168. if (evt.keyCode === 13) {
  169. this.find('button[type=submit]').click();
  170. }
  171. },
  172. 'click button.js-submit-edit-attachment-name'(event) {
  173. // save button pressed
  174. event.preventDefault();
  175. const name = this.$('.js-edit-attachment-name')[0]
  176. .value
  177. .trim() + this.data().extensionWithDot;
  178. Meteor.call('renameAttachment', this.data()._id, name);
  179. Popup.back(2);
  180. },
  181. }
  182. ]
  183. }
  184. }).register('attachmentRenamePopup');