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.

136 lines
3.0 KiB

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