Skip to content

Commit 9bdedf9

Browse files
Federica Nocerafnocera
authored andcommitted
Add page-level navigation
1 parent 00db001 commit 9bdedf9

File tree

2 files changed

+109
-22
lines changed

2 files changed

+109
-22
lines changed

app/server/static/components/annotation.pug

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ div.columns(v-cloak="")
5353
| Completed
5454

5555
div.main.pt0.pb0.pr20.pl20
56-
span About {{ count }} results
56+
span About {{ count }} results (page {{ paginationPage }} of {{ paginationPages }})
5757

5858
div.main.sidebar-scrollable
5959
a.item(
@@ -141,22 +141,41 @@ div.columns(v-cloak="")
141141
preview(v-bind:url="documentMetadata.documentSourceUrl")
142142

143143
div.level.mt30
144-
a.button.level-left(
145-
v-shortkey="{ prev1: ['ctrl', 'p'], prev2: ['arrowup'], prev3: ['arrowleft'] }"
146-
v-on:click="prevPage"
147-
v-on:shortkey="prevPage"
148-
)
149-
span.icon
150-
i.fas.fa-chevron-left
151-
span Prev
152-
153-
span.button.level-center.is-static {{ offset + pageNumber + 1 }} / {{ count }}
154-
155-
a.button.level-right(
156-
v-shortkey="{ next1: ['ctrl', 'n'], next2: ['arrowdown'], next3: ['arrowright'] }"
157-
v-on:click="nextPage"
158-
v-on:shortkey="nextPage"
159-
)
160-
span Next
161-
span.icon
162-
i.fas.fa-chevron-right
144+
div.level-left
145+
div.buttons
146+
a.button(
147+
v-shortkey="{ prev1: ['shift', 'ctrl', 'p'], prev2: ['shift', 'arrowup'], prev3: ['shift', 'arrowleft'] }"
148+
v-on:click="prevPagination"
149+
v-on:shortkey="prevPagination"
150+
)
151+
span.icon.tooltip(data-tooltip="Previous page")
152+
i.fas.fa-arrow-left
153+
154+
a.button(
155+
v-shortkey="{ prev1: ['ctrl', 'p'], prev2: ['arrowup'], prev3: ['arrowleft'] }"
156+
v-on:click="prevPage"
157+
v-on:shortkey="prevPage"
158+
)
159+
span.icon.tooltip(data-tooltip="Previous document")
160+
i.fas.fa-chevron-left
161+
162+
div.level-center
163+
span.button.is-static {{ offset + pageNumber + 1 }} / {{ count }}
164+
165+
div.level-right
166+
div.buttons
167+
a.button(
168+
v-shortkey="{ next1: ['ctrl', 'n'], next2: ['arrowdown'], next3: ['arrowright'] }"
169+
v-on:click="nextPage"
170+
v-on:shortkey="nextPage"
171+
)
172+
span.icon.tooltip(data-tooltip="Next document")
173+
i.fas.fa-chevron-right
174+
175+
a.button(
176+
v-shortkey="{ next1: ['shift', 'ctrl', 'n'], next2: ['shift', 'arrowdown'], next3: ['shift', 'arrowright'] }"
177+
v-on:click="nextPagination"
178+
v-on:shortkey="nextPagination"
179+
)
180+
span.icon.tooltip(data-tooltip="Next page")
181+
i.fas.fa-arrow-right

app/server/static/components/annotationMixin.js

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ const getOffsetFromUrl = (url) => {
1313
return parseInt(offsetMatch[1], 10);
1414
};
1515

16+
const removeHost = (url) => {
17+
if (!url) {
18+
return url;
19+
}
20+
21+
const hostMatch = url.match(/^https?:\/\/[^/]*\/(.*)$/);
22+
if (hostMatch == null) {
23+
return url;
24+
}
25+
26+
return `${window.location.origin}/${hostMatch[1]}`;
27+
};
28+
1629
const storeOffsetInUrl = (offset) => {
1730
let href = window.location.href;
1831

@@ -36,6 +49,28 @@ const storeOffsetInUrl = (offset) => {
3649
window.location.href = href;
3750
};
3851

52+
const getLimitFromUrl = (url, prevLimit) => {
53+
try {
54+
const limitMatch = url.match(/[?#].*limit=(\d+)/);
55+
56+
return parseInt(limitMatch[1], 10);
57+
} catch (err) {
58+
return prevLimit;
59+
}
60+
};
61+
62+
const getSidebarTotal = (count, limit) => (
63+
count !== 0 && limit !== 0
64+
? Math.ceil(count / limit)
65+
: 0
66+
);
67+
68+
const getSidebarPage = (offset, limit) => (
69+
limit !== 0
70+
? Math.ceil(offset / limit) + 1
71+
: 0
72+
);
73+
3974
export default {
4075
components: { VueJsonPretty, Preview },
4176

@@ -53,6 +88,9 @@ export default {
5388
offset: getOffsetFromUrl(window.location.href),
5489
picked: 'all',
5590
count: 0,
91+
prevLimit: 0,
92+
paginationPages: 0,
93+
paginationPage: 0,
5694
isSuperuser: false,
5795
isMetadataActive: false,
5896
isAnnotationGuidelineActive: false,
@@ -97,14 +135,44 @@ export default {
97135
}
98136
},
99137

138+
async nextPagination() {
139+
if (this.next) {
140+
this.url = this.next;
141+
await this.search();
142+
this.pageNumber = 0;
143+
} else {
144+
this.pageNumber = this.docs.length - 1;
145+
}
146+
this.resetScrollbar();
147+
},
148+
149+
async prevPagination() {
150+
if (this.prev) {
151+
this.url = this.prev;
152+
await this.search();
153+
this.pageNumber = this.docs.length - this.limit;
154+
} else {
155+
this.pageNumber = 0;
156+
}
157+
this.resetScrollbar();
158+
},
159+
100160
async search() {
101161
await HTTP.get(this.url).then((response) => {
102162
this.docs = response.data.results;
103-
this.next = response.data.next;
104-
this.prev = response.data.previous;
163+
this.next = removeHost(response.data.next);
164+
this.prev = removeHost(response.data.previous);
105165
this.count = response.data.count;
106166
this.annotations = this.docs.map(doc => doc.annotations);
107167
this.offset = getOffsetFromUrl(this.url);
168+
this.prevLimit = this.limit;
169+
if (this.next || this.prevLimit) {
170+
this.limit = getLimitFromUrl(this.next, this.prevLimit);
171+
} else {
172+
this.limit = this.count;
173+
}
174+
this.paginationPages = getSidebarTotal(this.count, this.limit);
175+
this.paginationPage = getSidebarPage(this.offset, this.limit);
108176
});
109177
},
110178

0 commit comments

Comments
 (0)