You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.2 KiB

  1. import HTTP from './http';
  2. const annotationMixin = {
  3. data() {
  4. return {
  5. pageNumber: 0,
  6. items: [{
  7. id: null,
  8. text: '',
  9. labels: [],
  10. }],
  11. labels: [],
  12. guideline: '',
  13. total: 0,
  14. remaining: 0,
  15. searchQuery: '',
  16. url: '',
  17. picked: 'all',
  18. annotations: [],
  19. };
  20. },
  21. methods: {
  22. async nextPage() {
  23. this.pageNumber += 1;
  24. if (this.pageNumber === this.items.length) {
  25. if (this.next) {
  26. this.url = this.next;
  27. await this.search();
  28. this.pageNumber = 0;
  29. } else {
  30. this.pageNumber = this.items.length - 1;
  31. }
  32. }
  33. this.showMessage(this.pageNumber);
  34. },
  35. async prevPage() {
  36. this.pageNumber -= 1;
  37. if (this.pageNumber === -1) {
  38. if (this.prev) {
  39. this.url = this.prev;
  40. await this.search();
  41. this.pageNumber = this.items.length - 1;
  42. } else {
  43. this.pageNumber = 0;
  44. }
  45. }
  46. this.showMessage(this.pageNumber);
  47. },
  48. async search() {
  49. await HTTP.get(this.url).then((response) => {
  50. this.items = response.data.results;
  51. this.next = response.data.next;
  52. this.prev = response.data.previous;
  53. });
  54. for (let i = 0; i < this.items.length; i++) {
  55. const docId = this.items[i].id;
  56. HTTP.get(`docs/${docId}/annotations/`).then((response) => {
  57. this.annotations.push(response.data);
  58. });
  59. }
  60. },
  61. showMessage(index) {
  62. this.pageNumber = index;
  63. },
  64. getState() {
  65. if (this.picked === 'all') {
  66. return '';
  67. }
  68. if (this.picked === 'active') {
  69. return 'true';
  70. }
  71. return 'false';
  72. },
  73. async submit() {
  74. const state = this.getState();
  75. this.url = `docs/?q=${this.searchQuery}&is_checked=${state}`;
  76. await this.search();
  77. this.pageNumber = 0;
  78. },
  79. removeLabel(label) {
  80. const docId = this.items[this.pageNumber].id;
  81. HTTP.delete(`docs/${docId}/annotations/${label.id}`).then((response) => {
  82. const index = this.annotations[this.pageNumber].indexOf(response.data);
  83. this.annotations[this.pageNumber].splice(index, 1);
  84. });
  85. },
  86. },
  87. watch: {
  88. picked() {
  89. this.submit();
  90. },
  91. items() {
  92. // fetch progress info.
  93. HTTP.get('progress').then((response) => {
  94. this.total = response.data.total;
  95. this.remaining = response.data.remaining;
  96. });
  97. },
  98. },
  99. created() {
  100. HTTP.get('labels').then((response) => {
  101. this.labels = response.data;
  102. });
  103. this.submit();
  104. },
  105. computed: {
  106. achievement() {
  107. const done = this.total - this.remaining;
  108. const percentage = Math.round(done / this.total * 100);
  109. return this.total > 0 ? percentage : 0;
  110. },
  111. id2label() {
  112. let id2label = {};
  113. for (let i = 0; i < this.labels.length; i++) {
  114. const label = this.labels[i];
  115. id2label[label.id] = label;
  116. }
  117. return id2label;
  118. },
  119. progressColor() {
  120. if (this.achievement < 30) {
  121. return 'is-danger';
  122. }
  123. if (this.achievement < 70) {
  124. return 'is-warning';
  125. }
  126. return 'is-primary';
  127. },
  128. },
  129. };
  130. export default annotationMixin;