attachments.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import Attachments, { fileStoreStrategyFactory } from '/models/attachments';
  2. const filesize = require('filesize');
  3. BlazeComponent.extendComponent({
  4. subscription: null,
  5. showMoveAttachments: new ReactiveVar(false),
  6. sessionId: null,
  7. onCreated() {
  8. this.error = new ReactiveVar('');
  9. this.loading = new ReactiveVar(false);
  10. },
  11. events() {
  12. return [
  13. {
  14. 'click a.js-move-attachments': this.switchMenu,
  15. },
  16. ];
  17. },
  18. switchMenu(event) {
  19. const target = $(event.target);
  20. if (!target.hasClass('active')) {
  21. this.loading.set(true);
  22. this.showMoveAttachments.set(false);
  23. if (this.subscription) {
  24. this.subscription.stop();
  25. }
  26. $('.side-menu li.active').removeClass('active');
  27. target.parent().addClass('active');
  28. const targetID = target.data('id');
  29. if ('move-attachments' === targetID) {
  30. this.showMoveAttachments.set(true);
  31. this.subscription = Meteor.subscribe('attachmentsList', () => {
  32. this.loading.set(false);
  33. });
  34. }
  35. }
  36. },
  37. }).register('attachments');
  38. BlazeComponent.extendComponent({
  39. getBoardsWithAttachments() {
  40. this.attachments = Attachments.find().get();
  41. this.attachmentsByBoardId = _.chain(this.attachments)
  42. .groupBy(fileObj => fileObj.meta.boardId)
  43. .value();
  44. const ret = Object.keys(this.attachmentsByBoardId)
  45. .map(boardId => {
  46. const boardAttachments = this.attachmentsByBoardId[boardId];
  47. _.each(boardAttachments, _attachment => {
  48. _attachment.flatVersion = Object.keys(_attachment.versions)
  49. .map(_versionName => {
  50. const _version = Object.assign(_attachment.versions[_versionName], {"versionName": _versionName});
  51. _version.storageName = fileStoreStrategyFactory.getFileStrategy(_attachment, _versionName).getStorageName();
  52. return _version;
  53. });
  54. });
  55. const board = Boards.findOne(boardId);
  56. board.attachments = boardAttachments;
  57. return board;
  58. })
  59. return ret;
  60. },
  61. getBoardData(boardid) {
  62. const ret = Boards.findOne(boardId);
  63. return ret;
  64. },
  65. events() {
  66. return [
  67. {
  68. 'click button.js-move-all-attachments-to-fs'(event) {
  69. this.attachments.forEach(_attachment => {
  70. Meteor.call('moveAttachmentToStorage', _attachment._id, "fs");
  71. });
  72. },
  73. 'click button.js-move-all-attachments-to-gridfs'(event) {
  74. this.attachments.forEach(_attachment => {
  75. Meteor.call('moveAttachmentToStorage', _attachment._id, "gridfs");
  76. });
  77. },
  78. }
  79. ]
  80. }
  81. }).register('moveAttachments');
  82. BlazeComponent.extendComponent({
  83. events() {
  84. return [
  85. {
  86. 'click button.js-move-all-attachments-of-board-to-fs'(event) {
  87. this.data().attachments.forEach(_attachment => {
  88. Meteor.call('moveAttachmentToStorage', _attachment._id, "fs");
  89. });
  90. },
  91. 'click button.js-move-all-attachments-of-board-to-gridfs'(event) {
  92. this.data().attachments.forEach(_attachment => {
  93. Meteor.call('moveAttachmentToStorage', _attachment._id, "gridfs");
  94. });
  95. },
  96. }
  97. ]
  98. },
  99. }).register('moveBoardAttachments');
  100. BlazeComponent.extendComponent({
  101. fileSize(size) {
  102. const ret = filesize(size);
  103. return ret;
  104. },
  105. events() {
  106. return [
  107. {
  108. 'click button.js-move-storage-fs'(event) {
  109. Meteor.call('moveAttachmentToStorage', this.data()._id, "fs");
  110. },
  111. 'click button.js-move-storage-gridfs'(event) {
  112. Meteor.call('moveAttachmentToStorage', this.data()._id, "gridfs");
  113. },
  114. }
  115. ]
  116. },
  117. }).register('moveAttachment');