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.

123 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import Vue from 'vue';
  2. Vue.use(require('vue-shortkey'));
  3. axios.defaults.xsrfCookieName = 'csrftoken';
  4. axios.defaults.xsrfHeaderName = 'X-CSRFToken';
  5. var base_url = window.location.href.split('/').slice(3, 5).join('/');
  6. const HTTP = axios.create({
  7. baseURL: `/api/${base_url}/`
  8. })
  9. var vm = new Vue({
  10. el: '#mail-app',
  11. delimiters: ['[[', ']]'],
  12. data: {
  13. cur: 0,
  14. items: [{id: null, text: '', labels: []}],
  15. labels: [],
  16. guideline: 'Here is the Annotation Guideline Text',
  17. total: 0,
  18. remaining: 0,
  19. searchQuery: '',
  20. url: '',
  21. },
  22. methods: {
  23. addLabel: async function (label_id) {
  24. for (var i = 0; i < this.items[this.cur]['labels'].length; i++) {
  25. var item = this.items[this.cur]['labels'][i];
  26. if (label_id == item.label.id) {
  27. this.deleteLabel(i);
  28. return;
  29. }
  30. }
  31. var payload = {
  32. 'label_id': label_id
  33. };
  34. var doc_id = this.items[this.cur].id;
  35. await HTTP.post(`docs/${doc_id}/annotations/`, payload).then(response => {
  36. this.items[this.cur]['labels'].push(response.data);
  37. });
  38. this.updateProgress();
  39. },
  40. deleteLabel: async function (index) {
  41. var doc_id = this.items[this.cur].id;
  42. var annotation_id = this.items[this.cur]['labels'][index].id;
  43. HTTP.delete(`docs/${doc_id}/annotations/${annotation_id}`).then(response => {
  44. this.items[this.cur]['labels'].splice(index, 1)
  45. });
  46. this.updateProgress();
  47. },
  48. nextPage: async function () {
  49. this.cur += 1;
  50. if (this.cur == this.items.length) {
  51. if (this.next) {
  52. this.url = this.next;
  53. await this.search();
  54. this.cur = 0;
  55. } else {
  56. this.cur = this.items.length - 1;
  57. }
  58. }
  59. this.showMessage(this.cur);
  60. },
  61. prevPage: async function () {
  62. this.cur -= 1;
  63. if (this.cur == -1) {
  64. if (this.prev) {
  65. this.url = this.prev;
  66. await this.search();
  67. this.cur = this.items.length - 1;
  68. } else {
  69. this.cur = 0;
  70. }
  71. }
  72. this.showMessage(this.cur);
  73. },
  74. submit: async function () {
  75. this.url = `docs/?q=${this.searchQuery}`;
  76. await this.search();
  77. this.cur = 0;
  78. },
  79. search: async function () {
  80. await HTTP.get(this.url).then(response => {
  81. this.items = response.data['results'];
  82. this.next = response.data['next'];
  83. this.prev = response.data['previous'];
  84. })
  85. },
  86. showMessage: function (index) {
  87. this.cur = index;
  88. },
  89. updateProgress: function () {
  90. HTTP.get('progress').then(response => {
  91. this.total = response.data['total'];
  92. this.remaining = response.data['remaining'];
  93. })
  94. }
  95. },
  96. created: function () {
  97. HTTP.get('labels').then(response => {
  98. this.labels = response.data
  99. });
  100. this.updateProgress();
  101. this.submit();
  102. },
  103. computed: {
  104. achievement: function () {
  105. var done = this.total - this.remaining;
  106. var percentage = Math.round(done / this.total * 100);
  107. return this.total > 0 ? percentage : 0;
  108. },
  109. progressColor: function () {
  110. if (this.achievement < 30) {
  111. return 'is-danger'
  112. } else if (this.achievement < 70) {
  113. return 'is-warning'
  114. } else {
  115. return 'is-primary'
  116. }
  117. }
  118. }
  119. });