Skip to content

Commit 1c4894a

Browse files
authored
Merge pull request doccano#276 from CatalystCode/enhancement/remove-demo-components
Enhancement/Remove demo components
2 parents 67aae2e + 4515228 commit 1c4894a

17 files changed

+342
-694
lines changed

app/server/static/components/annotationMixin.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as marked from 'marked';
22
import VueJsonPretty from 'vue-json-pretty';
33
import isEmpty from 'lodash.isempty';
4-
import HTTP, { rootUrl, newHttpClient } from './http';
5-
6-
const httpClient = newHttpClient();
4+
import HTTP, { defaultHttpClient } from './http';
75

86
const getOffsetFromUrl = (url) => {
97
const offsetMatch = url.match(/[?#].*offset=(\d+)/);
@@ -124,7 +122,7 @@ export default {
124122
},
125123

126124
replaceNull(shortcut) {
127-
if (shortcut === null) {
125+
if (shortcut == null) {
128126
shortcut = '';
129127
}
130128
shortcut = shortcut.split(' ');
@@ -176,7 +174,7 @@ export default {
176174
HTTP.get().then((response) => {
177175
this.guideline = response.data.guideline;
178176
});
179-
httpClient.get(`${rootUrl}/v1/me`).then((response) => {
177+
defaultHttpClient.get('/v1/me').then((response) => {
180178
this.isSuperuser = response.data.is_superuser;
181179
});
182180
this.submit();

app/server/static/components/demo/demo_annotator.vue

Lines changed: 0 additions & 158 deletions
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import MockAdatper from 'axios-mock-adapter';
2+
import HTTP, { defaultHttpClient } from '../http';
3+
4+
function newId() {
5+
return Number((Math.random() * 100000).toFixed(0));
6+
}
7+
8+
function parseOffset(url) {
9+
const offset = url.match(/offset=(\d+)/);
10+
return offset ? Number(offset[1]) : 0;
11+
}
12+
13+
function parseDocId(url) {
14+
return Number(url.split('/')[5]);
15+
}
16+
17+
function parseAnnotationId(url) {
18+
return Number(url.split('/')[7]);
19+
}
20+
21+
export default class DemoApi {
22+
constructor(data, labelField) {
23+
this.data = data;
24+
this.labelField = labelField;
25+
this.mocks = [new MockAdatper(HTTP), new MockAdatper(defaultHttpClient)];
26+
this.pageSize = 5;
27+
}
28+
29+
getMe() {
30+
return [200, this.data.me];
31+
}
32+
33+
getLabels() {
34+
return [200, this.data.labels];
35+
}
36+
37+
getStatistics() {
38+
return [200, {
39+
total: this.data.docs.length,
40+
remaining: this.data.docs.filter(doc => doc.annotations.length === 0).length,
41+
}];
42+
}
43+
44+
getDocs(config) {
45+
const offset = parseOffset(config.url);
46+
47+
return [200, {
48+
results: this.data.docs.slice(Math.max(offset - 1, 0), this.pageSize),
49+
count: this.data.docs.length,
50+
next: offset + this.pageSize <= this.data.docs.length
51+
? config.url.replace(`offset=${offset}`, `offset=${offset + this.pageSize}`)
52+
: null,
53+
previous: offset - this.pageSize >= 0
54+
? config.url.replace(`offset=${offset}`, `offset=${offset - this.pageSize}`)
55+
: null,
56+
}];
57+
}
58+
59+
getProject() {
60+
return [200, this.data.project];
61+
}
62+
63+
postAnnotations(config) {
64+
const docId = parseDocId(config.url);
65+
const body = JSON.parse(config.data);
66+
67+
const doc = this.data.docs.find(_ => _.id === docId);
68+
if (!doc) {
69+
return [404, {}];
70+
}
71+
72+
let annotation = doc.annotations.find(_ => _[this.labelField] === body[this.labelField]);
73+
if (!annotation) {
74+
annotation = { id: newId(), ...body };
75+
doc.annotations.push(annotation);
76+
}
77+
78+
return [200, annotation];
79+
}
80+
81+
deleteAnnotations(config) {
82+
const docId = parseDocId(config.url);
83+
const annotationId = parseAnnotationId(config.url);
84+
85+
const doc = this.data.docs.find(_ => _.id === docId);
86+
if (!doc) {
87+
return [404, {}];
88+
}
89+
90+
doc.annotations = doc.annotations.filter(el => el.id !== annotationId);
91+
return [200, {}];
92+
}
93+
94+
start() {
95+
this.mocks.forEach((mock) => {
96+
mock.onGet(/\/v1\/.*/g).reply((config) => {
97+
if (config.url.endsWith('/me')) {
98+
return this.getMe();
99+
}
100+
101+
if (config.url.endsWith('/labels')) {
102+
return this.getLabels();
103+
}
104+
105+
if (config.url.endsWith('/statistics')) {
106+
return this.getStatistics();
107+
}
108+
109+
if (config.url.indexOf('/docs') !== -1) {
110+
return this.getDocs(config);
111+
}
112+
113+
return this.getProject();
114+
});
115+
116+
mock.onPost(/\/v1\/.*/g).reply((config) => {
117+
if (config.url.endsWith('/annotations')) {
118+
return this.postAnnotations(config);
119+
}
120+
121+
return [404, {}];
122+
});
123+
124+
mock.onDelete(/\/v1\/.*/g).reply((config) => {
125+
if (config.url.indexOf('/annotations') !== -1) {
126+
return this.deleteAnnotations(config);
127+
}
128+
129+
return [404, {}];
130+
});
131+
});
132+
}
133+
134+
stop() {
135+
this.mocks.forEach(mock => mock.reset());
136+
}
137+
}

0 commit comments

Comments
 (0)