Refactor page handling

This commit is contained in:
gconsidine
2018-03-01 09:15:43 -05:00
committed by Jake McDermott
parent df84f822f6
commit b16d9a89e3
3 changed files with 238 additions and 160 deletions

View File

@@ -1,4 +1,4 @@
function JobPageService () {
function JobPageService ($q) {
this.page = null;
this.resource = null;
this.result = null;
@@ -13,7 +13,14 @@ function JobPageService () {
size: resource.page.size,
current: 0,
index: -1,
count: 0
count: 0,
first: 0,
last: 0,
bookmark: {
first: 0,
last: 0,
current: 0
}
};
this.result = {
@@ -28,14 +35,25 @@ function JobPageService () {
this.cache = [];
};
this.add = (page, position) => {
this.add = (page, position, bookmark) => {
page.events = page.events || [];
page.lines = page.lines || 0;
if (!position) {
if (position === 'first') {
this.cache.unshift(page);
this.page.first = page.number;
this.page.last = this.cache[this.cache.length -1].number;
} else {
this.cache.push(page);
this.page.last = page.number;
this.page.first = this.cache[0].number;
}
if (bookmark) {
this.page.bookmark.current = page.number;
}
this.page.current = page.number;
this.page.count++;
};
@@ -88,19 +106,33 @@ function JobPageService () {
return data;
};
this.emptyCache = () => {
this.page.first = this.page.current;
this.page.last = this.page.current;
this.cache = [];
};
this.isOverCapacity = () => {
return (this.cache.length - this.page.limit) > 0;
};
this.trim = () => {
this.trim = side => {
const count = this.cache.length - this.page.limit;
const ejected = this.cache.splice(0, count);
const linesRemoved = ejected.reduce((total, page) => total + page.lines, 0);
return linesRemoved;
let ejected;
if (side === 'left') {
ejected = this.cache.splice(0, count);
this.page.first = this.cache[0].number;
} else {
ejected = this.cache.splice(-count);
this.page.last = this.cache[this.cache.length - 1].number;
}
return ejected.reduce((total, page) => total + page.lines, 0);
};
this.getPageNumber = (page) => {
this.getPageNumber = page => {
let index;
if (page === 'first') {
@@ -114,22 +146,118 @@ function JobPageService () {
let index;
if (page === 'current') {
index = this.cache.length - 1;
index = this.cache.findIndex(item => item.number === this.page.current);
}
if (this.cache[index].lines) {
this.cache[index].lines += lines;
} else {
this.cache[index].lines = lines;
}
this.cache[index].lines += lines;
}
this.next = () => {
this.bookmark = () => {
console.log('b,current', this.page.current);
if (!this.page.bookmark.active) {
this.page.bookmark.first = this.page.first;
this.page.bookmark.last = this.page.last;
this.page.bookmark.current = this.page.current;
console.log('b,bookmark', this.page.bookmark.current);
this.page.bookmark.active = true;
} else {
this.page.bookmark.active = false;
}
};
this.prev = () => {
this.next = () => {
let page;
let bookmark;
if (this.page.bookmark.active) {
page = this.page.bookmark.current + 1;
bookmark = true;
} else {
page = this.page.last + 1;
}
const config = this.buildRequestConfig(page);
return this.resource.model.goToPage(config)
.then(data => {
if (!data || !data.results) {
return $q.resolve();
}
this.add({ number: data.page, events: [], lines: 0 }, 'last', bookmark);
return data.results;
});
};
this.previous = () => {
let page;
let bookmark;
if (this.page.bookmark.active) {
page = this.page.bookmark.current - 1;
bookmark = true;
} else {
page = this.page.first - 1;
}
const config = this.buildRequestConfig(page);
return this.resource.model.goToPage(config)
.then(data => {
if (!data || !data.results) {
return $q.resolve();
}
this.add({ number: data.page, events: [], lines: 0 }, 'first', bookmark);
return data.results;
});
};
this.last = () => {
const config = this.buildRequestConfig('last');
this.emptyCache();
return this.resource.model.goToPage(config)
.then(data => {
if (!data || !data.results) {
return $q.resolve();
}
this.add({ number: data.page, events: [], lines: 0 }, 'last');
return data.results;
});
};
this.first = () => {
const config = this.buildRequestConfig('first');
this.emptyCache();
return this.resource.model.goToPage(config)
.then(data => {
if (!data || !data.results) {
return $q.resolve();
}
this.add({ number: data.page, events: [], lines: 0 }, 'first');
return data.results;
});
};
this.buildRequestConfig = (page) => {
return {
page,
related: this.resource.related,
params: {
order_by: 'start_line'
}
};
};
this.current = () => {
@@ -137,4 +265,6 @@ function JobPageService () {
};
}
JobPageService.$inject = ['$q'];
export default JobPageService;