-
Notifications
You must be signed in to change notification settings - Fork 32
/
dialog.html
133 lines (126 loc) · 5.17 KB
/
dialog.html
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
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'">
<title>Task</title>
<link rel=stylesheet href='style/ride-base.css'>
<link rel=stylesheet class=theme id=theme_dark href='style/dark-theme.css'>
<link rel=stylesheet class=theme id=theme_light href='style/light-theme.css'>
<script defer src="lib/fontawesome-all.min.js"></script>
</head>
<body>
<div id=gd class="dlg floating"><!--generic dialog for processing OptionsDialog,StringDialog,TaskDialog-->
<div class=dlg_content id=gd_content></div>
<div class=dlg_btns id=gd_btns ></div>
<div id="gd_footer">
<div id="gd_footer_btns"></div>
<input id=gd_footer_qn type="checkbox" name="question">
<label id=gd_footer_qn_lbl for=gd_footer_qn></label>
</div>
</div>
<script>
const ESC = { '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' };
const esc = s => s.replace(/[<>&'"]/g, x => ESC[x]);
const ipc = require('node-ipc').default;
const el = require('@electron/remote');
const bw = el.getCurrentWindow();
const [width] = bw.getContentSize();
const qp = require('querystring').parse(window.location.search.slice(1));
const iswin = /^win/i.test(process.platform);
ipc.config.id = 'dialog';
ipc.config.appspace = qp.appid;
ipc.config.retry = 1500;
ipc.config.silent = true;
let rm;
ipc.connectTo('ride_master', () => {
rm = ipc.of.ride_master;
rm.on('connect', () => {
window.onbeforeunload = (e) => {
e.returnValue = false;
rm.emit('dialogClose', [token, { index: task ? -1 : value }]);
};
rm.emit('dialogCreated');
});
rm.on('disconnect', () => {
ipc.log('disconnected from ride_master'.notice);
window.onbeforeunload = null;
window.close();
});
rm.on('show', (opts) => show(opts));
rm.on('setTheme', (theme) => {
document.getElementById('theme_dark').disabled = theme !== 'dark';
document.getElementById('theme_light').disabled = theme !== 'light';
});
});
const gd_content = document.getElementById('gd_content');
const gd_btns = document.getElementById('gd_btns');
const gd_footer = document.getElementById('gd_footer');
const gd_footer_btns = document.getElementById('gd_footer_btns');
const gd_footer_qn = document.getElementById('gd_footer_qn');
const gd_footer_qn_lbl = document.getElementById('gd_footer_qn_lbl');
let value;
let task;
let token;
function show(x) {
token = x.token;
document.title = x.title || 'Dyalog';
task = !!x.buttonText;
value = x.defaultValue || null;
gd_footer.hidden = !task;
if (task) {
gd_content.innerHTML = esc(x.text || '') + (x.subtext ? `<div class=task_subtext>${esc(x.subtext)}</div>` : '');
let btns = (x.buttonText || []).map((y) => {
const [caption, ...details] = esc(y).split('\n');
return '<button class=task><div class="btn_icon"><span class="fas fa-chevron-circle-right"></span></div>' +
`${caption}<br><div class="task_detail">${details.join('<br>')}</div></button>`;
}).join('');
gd_btns.innerHTML = btns;
gd_footer_btns.innerHTML = x.options.map((y) => `<button>${y}</button>`).join('');
gd_footer_qn_lbl.innerHTML = x.questionlabel;
gd_footer_qn.checked = false;
const ret = (index, questionkey) => {
[...gd_btns.getElementsByTagName('button')].forEach(x => x.onclick = null);
gd_footer_btns.onclick = null;
rm.emit('dialogClose', [token, { index, questionkey }]);
};
const clickCb = (e) => {
let t = e.currentTarget == gd_footer_btns ? e.target : e.currentTarget;
let i = e.currentTarget == gd_footer_btns ? -1 : 99;
while (t) { t = t.previousSibling; i += 1; }
ret(i, gd_footer_qn.checked && x.questionkey);
};
[...gd_btns.getElementsByTagName('button')].forEach(x => x.onclick = clickCb);
gd_footer_btns.onclick = clickCb;
gd_btns.querySelector('button').focus();
} else {
gd_content.innerText = x.text || '';
gd_content.insertAdjacentHTML('beforeend', `<br><input ${x.pass ? 'type=password' : ''}>`);
const inp = gd_content.querySelector('input');
inp.value = x.initialValue || '';
gd_btns.innerHTML = '<button>OK</button><button>Cancel</button>';
const ret = (index) => {
gd_btns.onclick = null;
rm.emit('dialogClose', [token, { index }]);
};
gd_btns.onclick = (e) => {
if (e.target.nodeName === 'BUTTON') {
ret(e.target.previousSibling ? value: inp.value);
}
};
inp.onkeydown = (e) => {
if (e.which === 13) {
e.preventDefault();
ret(inp.value);
} else if (e.which === 27) {
e.preventDefault();
ret(value);
}
};
setTimeout(() => { inp.focus(); }, 1);
}
bw.setContentSize(width, document.body.clientHeight);
}
</script>
</body>
</html>