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.

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