import.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Abstract root for all import popup screens.
  3. * Descendants must define:
  4. * - getMethodName(): return the Meteor method to call for import, passing json data decoded as object
  5. * and additional data (see below)
  6. * - getAdditionalData(): return object containing additional data passed to Meteor method
  7. * (like list ID and position for a card import)
  8. * - getLabel(): i18n key for the text displayed in the popup, usually to explain how to get the data out of the
  9. * source system.
  10. */
  11. const ImportPopup = BlazeComponent.extendComponent({
  12. template() {return 'importPopup';},
  13. events() {
  14. return [{
  15. 'submit': (evt) => {
  16. evt.preventDefault();
  17. const dataJson = $(evt.currentTarget).find('textarea').val();
  18. let dataObject;
  19. try {
  20. dataObject = JSON.parse(dataJson);
  21. } catch (e) {
  22. this.setError('error-json-malformed');
  23. return;
  24. }
  25. Meteor.call(this.getMethodName(), dataObject, this.getAdditionalData(),
  26. (error, response) => {
  27. if (error) {
  28. this.setError(error.error);
  29. } else {
  30. Filter.addException(response);
  31. this.onFinish(response);
  32. }
  33. }
  34. );
  35. },
  36. }];
  37. },
  38. onCreated() {
  39. this.error = new ReactiveVar('');
  40. },
  41. setError(error) {
  42. this.error.set(error);
  43. },
  44. onFinish() {
  45. Popup.close();
  46. },
  47. });
  48. ImportPopup.extendComponent({
  49. getAdditionalData() {
  50. const listId = this.data()._id;
  51. const firstCardDom = $(`#js-list-${this.currentData()._id} .js-minicard:first`).get(0);
  52. const sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  53. const result = {listId, sortIndex};
  54. return result;
  55. },
  56. getMethodName() {
  57. return 'importTrelloCard';
  58. },
  59. getLabel() {
  60. return 'import-card-trello-instruction';
  61. },
  62. }).register('listImportCardPopup');
  63. ImportPopup.extendComponent({
  64. getAdditionalData() {
  65. const result = {};
  66. return result;
  67. },
  68. getMethodName() {
  69. return 'importTrelloBoard';
  70. },
  71. getLabel() {
  72. return 'import-board-trello-instruction';
  73. },
  74. onFinish(response) {
  75. Utils.goBoardId(response);
  76. },
  77. }).register('boardImportBoardPopup');