attachments.js 6.3 KB

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