import.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /// Abstract root for all import popup screens.
  2. /// Descendants must define:
  3. /// - getMethodName(): return the Meteor method to call for import, passing json
  4. /// data decoded as object and additional data (see below);
  5. /// - getAdditionalData(): return object containing additional data passed to
  6. /// Meteor method (like list ID and position for a card import);
  7. /// - getLabel(): i18n key for the text displayed in the popup, usually to
  8. /// explain how to get the data out of the source system.
  9. const ImportPopup = BlazeComponent.extendComponent({
  10. template() {
  11. return 'importPopup';
  12. },
  13. events() {
  14. return [{
  15. submit(evt) {
  16. evt.preventDefault();
  17. const dataJson = $(evt.currentTarget).find('.js-import-json').val();
  18. let dataObject;
  19. try {
  20. dataObject = JSON.parse(dataJson);
  21. this.setError('');
  22. } catch (e) {
  23. this.setError('error-json-malformed');
  24. return;
  25. }
  26. if(dataObject.members.length > 0) {
  27. this.data().toImport = dataObject;
  28. members.forEach(
  29. // todo if there is a Wekan user with same name, add it as a field 'wekanUser'
  30. );
  31. this.data().members = dataObject.members;
  32. // we bind to preserve data context
  33. Popup.open('mapMembers').bind(this)(evt);
  34. } else {
  35. Meteor.call(this.getMethodName(), dataObject, this.getAdditionalData(),
  36. (error, response) => {
  37. if (error) {
  38. this.setError(error.error);
  39. } else {
  40. Filter.addException(response);
  41. this.onFinish(response);
  42. }
  43. }
  44. );
  45. }
  46. },
  47. }];
  48. },
  49. onCreated() {
  50. this.error = new ReactiveVar('');
  51. this.dataToImport = '';
  52. },
  53. setError(error) {
  54. this.error.set(error);
  55. },
  56. onFinish() {
  57. Popup.close();
  58. },
  59. });
  60. ImportPopup.extendComponent({
  61. getAdditionalData() {
  62. const listId = this.data()._id;
  63. const selector = `#js-list-${this.currentData()._id} .js-minicard:first`;
  64. const firstCardDom = $(selector).get(0);
  65. const sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  66. const result = {listId, sortIndex};
  67. return result;
  68. },
  69. getMethodName() {
  70. return 'importTrelloCard';
  71. },
  72. getLabel() {
  73. return 'import-card-trello-instruction';
  74. },
  75. }).register('listImportCardPopup');
  76. ImportPopup.extendComponent({
  77. getAdditionalData() {
  78. const result = {};
  79. return result;
  80. },
  81. getMethodName() {
  82. return 'importTrelloBoard';
  83. },
  84. getLabel() {
  85. return 'import-board-trello-instruction';
  86. },
  87. onFinish(response) {
  88. Utils.goBoardId(response);
  89. },
  90. }).register('boardImportBoardPopup');
  91. BlazeComponent.extendComponent({
  92. events() {
  93. return [{
  94. 'submit': (evt) => {
  95. evt.preventDefault();
  96. console.log(this.data());
  97. },
  98. }];
  99. },
  100. }).register('mapMembersPopup');