Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 84x 84x 84x 84x 15x 84x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 28x 16x 16x 16x 32x 28x 28x 28x 7x 6x 6x 6x 5x 5x 4x 4x 4x 4x 4x 28x 6x 6x 5x 5x 5x 4x 4x 3x 3x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 4x 4x 4x 4x 4x 3x 1x 3x 3x 3x 7x 5x | import { ChangeDetectionStrategy, Component, HostBinding, Input } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RouterLink, RouterLinkActive } from '@angular/router';
import { delay } from 'lodash-es';
import { DateTime } from 'luxon';
import { catchError, defaultIfEmpty, defer, EMPTY, Observable, of, switchMap } from 'rxjs';
import { Page } from '../../model/page';
import { Ref, RefPageArgs, RefSort } from '../../model/ref';
import { RefService } from '../../service/api/ref.service';
import { BookmarkService } from '../../service/bookmark.service';
import { QueryStore } from '../../store/query';
import { Store } from '../../store/store';
import { defaultDesc, withStableDateSort } from '../../util/query';
const CURSOR_PAGE_PADDING = 1;
const DATE_SORT_FIELDS = ['created', 'modified', 'published'] as const;
type DateSortField = typeof DATE_SORT_FIELDS[number];
type SortDirection = 'ASC' | 'DESC';
type CursorBound = 'Before' | 'After';
type CursorFilter = `${DateSortField}${CursorBound}`;
interface DateSort {
field: DateSortField;
direction: SortDirection;
}
interface CursorMove {
anchor: Ref;
bound: CursorBound;
reverse: boolean;
target: number;
}
interface CursorPlan {
anchor: Ref;
request: RefPageArgs;
fallback: RefPageArgs;
initialSize: number;
reverse: boolean;
contentSize: number;
page: Page<Ref>['page'];
}
function isDateSortField(value: string | undefined): value is DateSortField {
return DATE_SORT_FIELDS.some(field => field === value);
}
@Component({
selector: 'app-page-controls',
templateUrl: './page-controls.component.html',
styleUrls: ['./page-controls.component.scss'],
host: { 'class': 'page-controls' },
changeDetection: ChangeDetectionStrategy.Eager,
imports: [
RouterLink,
RouterLinkActive,
ReactiveFormsModule,
FormsModule,
]
})
export class PageControlsComponent {
@Input()
page?: Page<any>;
@Input()
showPageLast = true;
@Input()
hideCols = false;
@Input()
showPrev = true;
pageSizes = [6, 24, 48, 96, 480];
colSizes = [1, 2, 3, 4, 5, 6];
colsChanged = false;
constructor(
public store: Store,
private bookmarks: BookmarkService,
private query: QueryStore,
private refs: RefService,
) { }
@HostBinding('class.print-hide')
get fullResults() {
return this.page?.page.totalPages === 1;
}
@Input()
set defaultCols(value: number | undefined) {
this.colsChanged ||= value !== undefined;
}
get hasQuery() {
return this.store.view.pageNumber !== undefined;
}
get prev() {
return Math.max(0, this.page!.page.number - 1);
}
get next() {
return Math.max(0, Math.min(this.last, this.page!.page.number + 1));
}
get last() {
return Math.max(0, this.page!.page.totalPages - 1);
}
get pageSize() {
return this.store.view.pageSize;
}
set pageSize(value: number) {
this.bookmarks.pageSize = value;
}
get cols() {
Iif (this.store.view.cols) {
this.colsChanged = true;
}
return this.store.view.cols;
}
set cols(value: number) {
this.bookmarks.cols = value;
}
scrollUp() {
delay(() => window.scrollTo(0, 0), 400);
}
cursorPage(target: number, event?: MouseEvent) {
if (!this.plainClick(event)) return;
const page = this.currentPage();
const args = this.query.args;
if (!page || !args) return;
const sort = this.dateSort(args);
if (!sort) return;
const move = this.cursorMove(page, target, sort);
Iif (!move) return;
const plan = this.cursorPlan(page, args, sort, move);
Iif (!plan) return;
this.query.queueCursorPage(target, this.loadOrFallback(plan));
}
outOfPageSizeRange(size: number) {
return !this.pageSizes.includes(size);
}
outOfColSizeRange(size: number) {
return !this.colSizes.includes(size);
}
private currentPage(): Page<Ref> | undefined {
const page = this.page as Page<Ref> | undefined;
if (!page || page !== this.query.page || page.content.length === 0) return undefined;
return page;
}
private dateSort(args: RefPageArgs): DateSort | undefined {
const [field, direction] = args.sort?.[0]?.split(',') || [];
if (!isDateSortField(field)) return undefined;
return {
field,
direction: direction === 'ASC' ? 'ASC' : 'DESC',
};
}
private cursorMove(page: Page<Ref>, target: number, sort: DateSort): CursorMove | undefined {
if (target === page.page.number + 1) {
const anchor = page.content.at(-1)!;
return {
anchor,
bound: sort.direction === 'DESC' ? 'Before' : 'After',
reverse: false,
target,
};
}
Eif (target === page.page.number - 1) {
const anchor = page.content[0];
return {
anchor,
bound: sort.direction === 'DESC' ? 'After' : 'Before',
reverse: true,
target,
};
}
return undefined;
}
private cursorPlan(
page: Page<Ref>,
args: RefPageArgs,
sort: DateSort,
move: CursorMove,
): CursorPlan | undefined {
const contentSize = this.contentSize(page, move.target);
Iif (contentSize <= 0) return undefined;
const cursor = this.cursorValue(move.anchor, sort.field, move.bound);
Iif (!cursor) return undefined;
const stableArgs = withStableDateSort(args);
const stableSort = stableArgs.sort ?? [];
const filter = `${sort.field}${move.bound}` as CursorFilter;
return {
anchor: move.anchor,
request: {
...stableArgs,
page: undefined,
sort: move.reverse ? this.reverseSort(stableSort) : stableSort,
[filter]: cursor,
},
fallback: { ...stableArgs, page: move.target },
initialSize: contentSize + CURSOR_PAGE_PADDING,
reverse: move.reverse,
contentSize,
page: { ...page.page, number: move.target },
};
}
private cursorValue(ref: Ref, field: DateSortField, bound: CursorBound) {
const value = field === 'modified' ? ref.modifiedString || ref.modified : ref[field];
const date = typeof value === 'string' ? DateTime.fromISO(value) : value;
Iif (!date?.isValid) return undefined;
return (bound === 'Before'
? date.plus({ milliseconds: 1 })
: date.minus({ milliseconds: 1 })
).toUTC().toISO() || undefined;
}
private contentSize(page: Page<Ref>, target: number) {
const pageSize = page.page.size || page.content.length;
const firstElement = target * pageSize;
const remainingElements = page.page.totalElements - firstElement;
return Math.min(pageSize, remainingElements);
}
private loadOrFallback(plan: CursorPlan): Observable<Page<Ref>> {
return defer(() => this.loadCursorPage(plan)).pipe(
catchError(() => EMPTY),
defaultIfEmpty(undefined),
switchMap(cursorPage => cursorPage
? of(cursorPage)
: this.refs.page(plan.fallback)),
);
}
private loadCursorPage(plan: CursorPlan): Observable<Page<Ref>> {
const args = {
...plan.request,
size: plan.initialSize,
};
return this.refs.page(args).pipe(
switchMap(response => {
const page = this.reconstruct(response.content, plan);
return page ? of(page) : EMPTY;
}),
);
}
private reconstruct(content: Ref[], plan: CursorPlan): Page<Ref> | undefined {
const anchorIndex = content.findIndex(ref => this.sameRef(ref, plan.anchor));
Iif (anchorIndex < 0) return undefined;
const contentStart = anchorIndex + 1;
const contentEnd = contentStart + plan.contentSize;
const pageContent = content.slice(contentStart, contentEnd);
if (pageContent.length !== plan.contentSize) return undefined;
return {
content: plan.reverse ? pageContent.reverse() : pageContent,
page: { ...plan.page },
};
}
private reverseSort(sort: RefSort[]): RefSort[] {
return sort.map(value => {
const [field, direction] = value.split(',');
const reverseDirection = direction
? direction === 'DESC' ? 'ASC' : 'DESC'
: defaultDesc(field) ? 'ASC' : 'DESC';
return `${field},${reverseDirection}` as RefSort;
});
}
private plainClick(event?: MouseEvent) {
return !event || (
event.button === 0 &&
!event.altKey &&
!event.ctrlKey &&
!event.metaKey &&
!event.shiftKey
);
}
private sameRef(a: Ref, b: Ref) {
return a.url === b.url && (a.origin || '') === (b.origin || '');
}
}
|