-
Notifications
You must be signed in to change notification settings - Fork 18
/
d-table-body-cell-link.ts
149 lines (133 loc) · 3.96 KB
/
d-table-body-cell-link.ts
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
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { InteractionEvent } from "pixi.js";
import { DLink, DLinkChecker, DLinkOptions, DLinkUrlMaker } from "./d-link";
import { DLinkMenuItemId } from "./d-link-menu-item-id";
import { DLinkTarget } from "./d-link-target";
import { DLinks } from "./d-links";
import { DMenu, DMenuOptions } from "./d-menu";
import { DTableBodyCell, DTableBodyCellOnChange } from "./d-table-body-cell";
import {
DTableBodyCellButton,
DTableBodyCellButtonOptions,
DThemeTableBodyCellButton
} from "./d-table-body-cell-button";
import { DTableColumn } from "./d-table-column";
import { isString } from "./util/is-string";
export type DTableBodyCellLinkUrlMaker<ROW, VALUE> = (
row: ROW,
rowIndex: number,
columnIndex: number,
link: DTableBodyCell<ROW, VALUE | null>
) => string | null | Promise<string | null>;
export type DTableBodyCellLinkChecker<ROW, VALUE> = (
row: ROW,
rowIndex: number,
columnIndex: number,
link: DTableBodyCell<ROW, VALUE | null>
) => boolean | Promise<boolean>;
export interface DTableBodyCellLinkLinkOptions<ROW, VALUE> {
url?: string | DTableBodyCellLinkUrlMaker<ROW, VALUE>;
target?: DLinkTarget | keyof typeof DLinkTarget;
checker?: DTableBodyCellLinkChecker<ROW, VALUE>;
menu?: DMenuOptions<DLinkMenuItemId> | DMenu<DLinkMenuItemId>;
}
export interface DTableBodyCellLinkOptions<
ROW,
VALUE = unknown,
THEME extends DThemeTableBodyCellLink<VALUE> = DThemeTableBodyCellLink<VALUE>
> extends DTableBodyCellButtonOptions<ROW, VALUE, THEME> {
link?: DTableBodyCellLinkLinkOptions<ROW, VALUE>;
}
export interface DThemeTableBodyCellLink<VALUE = unknown>
extends DThemeTableBodyCellButton<VALUE> {}
export const toLinkOptions = <ROW, VALUE>(
cell: DTableBodyCell<ROW, VALUE | null>,
options?: DTableBodyCellLinkLinkOptions<ROW, VALUE>
): DLinkOptions | undefined => {
if (options) {
return {
url: toUrl(cell, options.url),
target: options.target,
checker: toChecker(cell, options.checker),
menu: options.menu
};
}
return undefined;
};
export const toUrl = <ROW, VALUE>(
cell: DTableBodyCell<ROW, VALUE | null>,
url?: string | DTableBodyCellLinkUrlMaker<ROW, VALUE>
): string | DLinkUrlMaker | undefined => {
if (isString(url) || url == null) {
return url;
} else {
return () => {
const row = cell.row;
if (row !== undefined) {
return url(row, cell.rowIndex, cell.columnIndex, cell);
}
return null;
};
}
};
export const toChecker = <ROW, VALUE>(
cell: DTableBodyCell<ROW, VALUE | null>,
checker?: DTableBodyCellLinkChecker<ROW, VALUE>
): DLinkChecker | undefined => {
if (checker != null) {
return (): boolean | Promise<boolean> => {
const row = cell.row;
if (row !== undefined) {
return checker(row, cell.rowIndex, cell.columnIndex, cell);
}
return false;
};
}
return undefined;
};
export class DTableBodyCellLink<
ROW,
VALUE = unknown,
THEME extends DThemeTableBodyCellLink<VALUE> = DThemeTableBodyCellLink<VALUE>,
OPTIONS extends DTableBodyCellLinkOptions<ROW, VALUE, THEME> = DTableBodyCellLinkOptions<
ROW,
VALUE,
THEME
>
> extends DTableBodyCellButton<ROW, VALUE, THEME, OPTIONS> {
protected _link?: DLink;
constructor(
columnIndex: number,
column: DTableColumn<ROW, VALUE | null>,
onChange: DTableBodyCellOnChange<ROW, VALUE | null>,
options: OPTIONS
) {
super(columnIndex, column, onChange, DLinks.toStateOptions(options?.link?.target, options));
}
protected onClick(e: InteractionEvent): void {
if (!this.link.onClick(this, e)) {
super.onClick(e);
}
}
get link(): DLink {
let result = this._link;
if (result == null) {
result = new DLink(toLinkOptions(this, this._options?.link));
this._link = result;
}
return result;
}
protected getType(): string {
return "DTableBodyCellLink";
}
protected onActivate(e?: InteractionEvent | KeyboardEvent | MouseEvent | TouchEvent): void {
super.onActivate(e);
this.link.open(e);
}
open(inNewWindow: boolean): void {
this.link.open(inNewWindow);
}
}