Sanity-check decaffeinate app.views.PaginatedList

pull/1441/head
Simon Legner 1 year ago
parent 415a68b793
commit 1fe6544c82

@ -1,153 +1,133 @@
// TODO: This file was created by bulk-decaffeinate. app.views.PaginatedList = class PaginatedList extends app.View {
// Sanity-check the conversion and remove this comment. static PER_PAGE = app.config.max_results;
/*
* decaffeinate suggestions: constructor(data) {
* DS102: Remove unnecessary code created because of implicit returns super();
* DS104: Avoid inline assignments this.data = data;
* DS202: Simplify dynamic range loops this.constructor.events = this.constructor.events || {};
* DS206: Consider reworking classes to avoid initClass if (this.constructor.events.click == null) {
* DS207: Consider shorter variations of null checks this.constructor.events.click = "onClick";
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md }
*/ }
(function () {
let PER_PAGE = undefined; renderPaginated() {
app.views.PaginatedList = class PaginatedList extends app.View { this.page = 0;
static initClass() {
PER_PAGE = app.config.max_results; if (this.totalPages() > 1) {
} this.paginateNext();
} else {
constructor(data) { this.html(this.renderAll());
super(); }
this.data = data; }
this.constructor.events = this.constructor.events || {};
if (this.constructor.events.click == null) { // render: (dataSlice) -> implemented by subclass
this.constructor.events.click = "onClick";
} renderAll() {
} return this.render(this.data);
}
renderPaginated() {
this.page = 0; renderPage(page) {
return this.render(
if (this.totalPages() > 1) { this.data.slice(
this.paginateNext(); (page - 1) * PaginatedList.PER_PAGE,
} else { page * PaginatedList.PER_PAGE,
this.html(this.renderAll()); ),
} );
} }
// render: (dataSlice) -> implemented by subclass renderPageLink(count) {
return this.tmpl("sidebarPageLink", count);
renderAll() { }
return this.render(this.data);
} renderPrevLink(page) {
return this.renderPageLink((page - 1) * PaginatedList.PER_PAGE);
renderPage(page) { }
return this.render(
this.data.slice((page - 1) * PER_PAGE, page * PER_PAGE), renderNextLink(page) {
); return this.renderPageLink(
} this.data.length - page * PaginatedList.PER_PAGE,
);
renderPageLink(count) { }
return this.tmpl("sidebarPageLink", count);
} totalPages() {
return Math.ceil(this.data.length / PaginatedList.PER_PAGE);
renderPrevLink(page) { }
return this.renderPageLink((page - 1) * PER_PAGE);
} paginate(link) {
$.lockScroll(link.nextSibling || link.previousSibling, () => {
renderNextLink(page) { $.batchUpdate(this.el, () => {
return this.renderPageLink(this.data.length - page * PER_PAGE); if (link.nextSibling) {
} this.paginatePrev(link);
} else {
totalPages() { this.paginateNext(link);
return Math.ceil(this.data.length / PER_PAGE);
}
paginate(link) {
$.lockScroll(link.nextSibling || link.previousSibling, () => {
$.batchUpdate(this.el, () => {
if (link.nextSibling) {
this.paginatePrev(link);
} else {
this.paginateNext(link);
}
});
});
}
paginateNext() {
if (this.el.lastChild) {
this.remove(this.el.lastChild);
} // remove link
if (this.page >= 2) {
this.hideTopPage();
} // keep previous page into view
this.page++;
this.append(this.renderPage(this.page));
if (this.page < this.totalPages()) {
this.append(this.renderNextLink(this.page));
}
}
paginatePrev() {
this.remove(this.el.firstChild); // remove link
this.hideBottomPage();
this.page--;
this.prepend(this.renderPage(this.page - 1)); // previous page is offset by one
if (this.page >= 3) {
this.prepend(this.renderPrevLink(this.page - 1));
}
}
paginateTo(object) {
const index = this.data.indexOf(object);
if (index >= PER_PAGE) {
for (
let i = 0, end = Math.floor(index / PER_PAGE), asc = 0 <= end;
asc ? i < end : i > end;
asc ? i++ : i--
) {
this.paginateNext();
} }
} });
} });
}
hideTopPage() {
const n = this.page <= 2 ? PER_PAGE : PER_PAGE + 1; // remove link paginateNext() {
for ( if (this.el.lastChild) {
let i = 0, end = n, asc = 0 <= end; this.remove(this.el.lastChild);
asc ? i < end : i > end; } // remove link
asc ? i++ : i-- if (this.page >= 2) {
) { this.hideTopPage();
this.remove(this.el.firstChild); } // keep previous page into view
} this.page++;
this.prepend(this.renderPrevLink(this.page)); this.append(this.renderPage(this.page));
} if (this.page < this.totalPages()) {
this.append(this.renderNextLink(this.page));
hideBottomPage() { }
const n = }
this.page === this.totalPages()
? this.data.length % PER_PAGE || PER_PAGE paginatePrev() {
: PER_PAGE + 1; // remove link this.remove(this.el.firstChild); // remove link
this.hideBottomPage();
this.page--;
this.prepend(this.renderPage(this.page - 1)); // previous page is offset by one
if (this.page >= 3) {
this.prepend(this.renderPrevLink(this.page - 1));
}
}
paginateTo(object) {
const index = this.data.indexOf(object);
if (index >= PaginatedList.PER_PAGE) {
for ( for (
let i = 0, end = n, asc = 0 <= end; let i = 0, end = Math.floor(index / PaginatedList.PER_PAGE);
asc ? i < end : i > end; i < end;
asc ? i++ : i-- i++
) { ) {
this.remove(this.el.lastChild); this.paginateNext();
}
this.append(this.renderNextLink(this.page - 1));
}
onClick(event) {
const target = $.eventTarget(event);
if (target.tagName === "SPAN") {
// link
$.stopEvent(event);
this.paginate(target);
} }
} }
}; }
app.views.PaginatedList.initClass();
return app.views.PaginatedList; hideTopPage() {
})(); const n =
this.page <= 2 ? PaginatedList.PER_PAGE : PaginatedList.PER_PAGE + 1; // remove link
for (let i = 0, end = n; i < end; i++) {
this.remove(this.el.firstChild);
}
this.prepend(this.renderPrevLink(this.page));
}
hideBottomPage() {
const n =
this.page === this.totalPages()
? this.data.length % PaginatedList.PER_PAGE || PaginatedList.PER_PAGE
: PaginatedList.PER_PAGE + 1; // remove link
for (let i = 0, end = n; i < end; i++) {
this.remove(this.el.lastChild);
}
this.append(this.renderNextLink(this.page - 1));
}
onClick(event) {
const target = $.eventTarget(event);
if (target.tagName === "SPAN") {
// link
$.stopEvent(event);
this.paginate(target);
}
}
};

Loading…
Cancel
Save