forked from gbtami/pychess-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.ts
123 lines (109 loc) · 4.42 KB
/
document.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
import { VNode, init, classModule, attributesModule, propsModule, eventListenersModule, styleModule } from 'snabbdom';
export const patch = init([classModule, attributesModule, propsModule, eventListenersModule, styleModule]);
export function downloadPgnText(filename: string) {
const text = (document.getElementById('pgntext') as HTMLInputElement).innerHTML;
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
export function getDocumentData(name: string) {
const elm = document.getElementById('pychess-variants');
if (elm) {
return elm.getAttribute('data-' + name.toLowerCase());
} else {
return "";
}
}
export function debounce(callback: any, wait: number) {
let timeout: ReturnType<typeof setTimeout>;
return function() {
const context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => callback.apply(context, args), wait);
};
}
export function getCookie(name: string) {
const cookies = document.cookie.split(';');
for(let i = 0; i < cookies.length; i++) {
const pair = cookies[i].trim().split('=');
if(pair[0] === name)
return pair[1];
}
return "";
}
export function setCookie(cname: string, cvalue: string, exdays: number) {
const d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
const expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function changeCSS(cssLinkIndex: number, cssFile: string) {
document.getElementsByTagName("link").item(cssLinkIndex)!.setAttribute("href", cssFile);
}
// css file index in templates/base.html
const BOARD_CSS_IDX = 1;
const PIECE_CSS_IDX = 2;
export function changeBoardCSS(assetUrl: string, family: string, cssFile: string) {
const sheet = document.styleSheets[BOARD_CSS_IDX];
const cssRules = sheet.cssRules;
for (let i = 0; i < cssRules.length; i++) {
const rule = cssRules[i];
if (!( rule instanceof CSSStyleRule)) {
continue;
}
if (rule.selectorText === `.${family} cg-board`) {
// console.log("changeBoardCSS", family, cssFile, i)
sheet.deleteRule(i)
const newRule = `.${family} cg-board {background-image: url(${assetUrl}/images/board/${cssFile})}`;
// console.log(newRule);
sheet.insertRule(newRule, i);
break;
}
}
}
export function changePieceCSS(assetUrl: string, family: string, cssFile: string) {
let cssLinkIndex = PIECE_CSS_IDX;
switch (family) {
case "standard": break;
case "seirawan": cssLinkIndex += 1; break;
case "makruk": cssLinkIndex += 2; break;
case "sittuyin": cssLinkIndex += 3; break;
case "asean": cssLinkIndex += 4; break;
case "shogi": cssLinkIndex += 5; break;
case "kyoto": cssLinkIndex += 6; break;
case "tori": cssLinkIndex += 7; break;
case "xiangqi": cssLinkIndex += 8; break;
case "capa": cssLinkIndex += 9; break;
case "shako": cssLinkIndex += 10; break;
case "shogun": cssLinkIndex += 11; break;
case "janggi": cssLinkIndex += 12; break;
case "orda": cssLinkIndex += 13; break;
case "synochess": cssLinkIndex += 14; break;
case "hoppel": cssLinkIndex += 15; break;
case "dobutsu": cssLinkIndex += 16; break;
case "shinobi": cssLinkIndex += 17; break;
case "empire": cssLinkIndex += 18; break;
case "ordamirror": cssLinkIndex += 19; break;
case "chak": cssLinkIndex += 20; break;
case "chennis": cssLinkIndex += 21; break;
default: throw "Unknown piece family " + family;
}
const newUrl = `${assetUrl}/piece/${family}/${cssFile}.css`;
// console.log("changePieceCSS", family, cssFile, newUrl)
changeCSS(cssLinkIndex, newUrl);
}
export function bind(eventName: string, f: (e: Event) => void, redraw: null | (() => void)) {
return {
insert(vnode: VNode) {
vnode.elm?.addEventListener(eventName, (e: Event) => {
const res = f(e);
if (redraw) redraw();
return res;
});
}
};
}