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. count: 0,
  16. };
  17. },
  18. methods: {
  19. async nextPage() {
  20. this.pageNumber += 1;
  21. if (this.pageNumber === this.docs.length) {
  22. if (this.next) {
  23. this.url = this.next;
  24. await this.search();
  25. this.pageNumber = 0;
  26. } else {
  27. this.pageNumber = this.docs.length - 1;
  28. }
  29. }
  30. },
  31. async prevPage() {
  32. this.pageNumber -= 1;
  33. if (this.pageNumber === -1) {
  34. if (this.prev) {
  35. this.url = this.prev;
  36. await this.search();
  37. this.pageNumber = this.docs.length - 1;
  38. } else {
  39. this.pageNumber = 0;
  40. }
  41. }
  42. },
  43. async search() {
  44. await HTTP.get(this.url).then((response) => {
  45. this.docs = response.data.results;
  46. this.next = response.data.next;
  47. this.prev = response.data.previous;
  48. this.count = response.data.count;
  49. this.annotations = [];
  50. for (let i = 0; i < this.docs.length; i++) {
  51. const doc = this.docs[i];
  52. this.annotations.push(doc.annotations);
  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;