-
Notifications
You must be signed in to change notification settings - Fork 18
/
d-link.ts
269 lines (237 loc) · 6.31 KB
/
d-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
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
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { DisplayObject, InteractionEvent, Texture } from "pixi.js";
import { DBase } from "./d-base";
import { DLinkMenu } from "./d-link-menu";
import { DLinkMenuItemId } from "./d-link-menu-item-id";
import { DLinkTarget } from "./d-link-target";
import { DMenu, DMenuOptions } from "./d-menu";
import { isFunction } from "./util/is-function";
import { isString } from "./util/is-string";
import { toEnum } from "./util/to-enum";
import { UtilClipboard } from "./util/util-clipboard";
import { DThemes } from "./theme/d-themes";
import { DBaseStateSet } from "./d-base-state-set";
export type DLinkUrlValue = string | null | undefined;
export type DLinkUrlMaker = () => DLinkUrlValue | Promise<DLinkUrlValue>;
export type DLinkTargetValue = DLinkTarget | null | undefined | keyof typeof DLinkTarget;
export type DLinkChecker = () => boolean | Promise<boolean>;
export interface DLinkOptions {
url?: DLinkUrlValue | DLinkUrlMaker;
target?: DLinkTargetValue;
checker?: DLinkChecker;
menu?: DMenuOptions<DLinkMenuItemId> | DMenu<DLinkMenuItemId>;
}
export interface DThemeLink {
getImageSource(state: DBaseStateSet): Texture | DisplayObject | null;
getMenuOptions(): DMenuOptions<DLinkMenuItemId>;
}
export class DLink {
protected static ANCHOR_ELEMENT?: HTMLAnchorElement;
protected static MENU?: DLinkMenu;
protected _options?: DLinkOptions;
protected _url: DLinkUrlValue | DLinkUrlMaker;
protected _target: DLinkTarget;
protected _checker?: DLinkChecker;
protected _menu?: DLinkMenu;
protected _isEnabled: boolean;
constructor(options?: DLinkOptions) {
this._options = options;
this._url = options?.url;
this._target = this.toNormalizedTarget(options?.target);
this._checker = options?.checker;
this._isEnabled = true;
}
get enable(): boolean {
return this._isEnabled;
}
set enable(enable: boolean) {
this._isEnabled = enable;
}
get url(): DLinkUrlValue | DLinkUrlMaker {
return this._url;
}
set url(url: DLinkUrlValue | DLinkUrlMaker) {
this._url = url;
}
get target(): DLinkTarget {
return this._target;
}
set target(target: DLinkTargetValue) {
this._target = this.toNormalizedTarget(target);
}
get checker(): DLinkChecker | undefined {
return this._checker;
}
set checker(checker: DLinkChecker | undefined) {
this._checker = checker;
}
get menu(): DLinkMenu {
let result = this._menu;
if (result == null) {
result = this.newMenu();
this._menu = result;
}
return result;
}
protected newMenu(): DLinkMenu {
const options = this._options;
if (options) {
const menu = options.menu;
if (menu) {
return new DLinkMenu(this, menu);
}
}
let result = DLink.MENU;
if (result == null) {
result = new DLinkMenu(
this,
DThemes.getInstance().get<DThemeLink>("DLink").getMenuOptions()
);
DLink.MENU = result;
}
return result;
}
protected toStringifiedUrl(
target: DLinkUrlValue | DLinkUrlMaker,
onResolved: (url: string) => void
): void {
const url = isFunction(target) ? target() : target;
if (url != null) {
if (isString(url)) {
onResolved(url);
} else {
url.then((resolved: DLinkUrlValue): void => {
if (resolved != null) {
onResolved(resolved);
}
});
}
}
}
protected toNormalizedUrl(url: string): string {
const a = DLink.ANCHOR_ELEMENT || document.createElement("a");
DLink.ANCHOR_ELEMENT = a;
a.href = url;
return a.href;
}
protected toNormalizedTarget(target: DLinkTargetValue): DLinkTarget {
return toEnum(target ?? DLinkTarget.AUTO, DLinkTarget);
}
/**
* Copys the URL to the clipboard.
*/
copy(): void {
this.toStringifiedUrl(this._url, (url: string): void => {
UtilClipboard.copy(this.toNormalizedUrl(url));
});
}
/**
* Checks and opens the URL.
*
* @param e An event object which triggered this method call.
*/
open(e?: InteractionEvent | KeyboardEvent | MouseEvent | TouchEvent): void;
/**
* Checks and opens the URL.
*
* @param inNewWindow True to open in a new window
*/
open(inNewWindow: boolean): void;
open(
x: InteractionEvent | KeyboardEvent | MouseEvent | TouchEvent | undefined | boolean
): void {
this.toStringifiedUrl(this._url, (url): void => {
const inNewWindow = x === true || x === false ? x : this.inNewWindow(x);
this.check(url, inNewWindow, (): void => {
this.exec(url, inNewWindow);
});
});
}
check(url: string, inNewWindow: boolean, onResolved: () => void): void {
const checker = this._checker;
if (checker) {
const checked = checker();
if (checked === true) {
onResolved();
} else if (checked === false) {
// DO NOTHING
} else {
checked.then((resolved: boolean): void => {
if (resolved) {
onResolved();
}
});
}
} else {
onResolved();
}
}
/**
* Opens the given URL.
*
* @param url An URL to be opened
* @param inNewWindow True to open in a new window.
*/
exec(url: string, inNewWindow: boolean): void {
if (inNewWindow) {
const a = document.createElement("a");
a.href = url;
a.target = "_blank";
a.style.display = "none";
a.rel = "noopener noreferrer";
document.body.appendChild(a);
a.click();
setTimeout((): void => {
document.body.removeChild(a);
}, 100);
} else {
window.location.href = url;
}
}
/**
* Returns true if the URL need to be opened in a new window.
*
* @param e An event object.
*/
inNewWindow(e?: InteractionEvent | KeyboardEvent | MouseEvent | TouchEvent): boolean {
switch (this._target) {
case DLinkTarget.NEW_WINDOW:
return true;
case DLinkTarget.THIS_WINDOW:
return false;
case DLinkTarget.AUTO:
if (e != null) {
const oe = e instanceof InteractionEvent ? e.data.originalEvent : e;
return (
oe.ctrlKey ||
oe.shiftKey ||
oe.altKey ||
oe.metaKey ||
("button" in oe && oe.button !== 0)
);
}
return false;
}
}
onClick(base: DBase, e: InteractionEvent): boolean {
if (this._target === DLinkTarget.AUTO) {
const oe = e.data.originalEvent;
if (("pointerType" in oe && oe.pointerType !== "mouse") || "touches" in oe) {
const menu = this.menu;
if (menu.enable) {
if (this._isEnabled && base.state.isActionable) {
menu.open(base);
}
return true;
}
}
}
if (this._isEnabled) {
return false;
}
return true;
}
}