-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.ts
196 lines (149 loc) · 6.17 KB
/
dialog.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
module dialog {
export class EntryBase<T> extends qx.ui.container.Composite {
caption: string;
firstFocusable: qx.ui.core.Widget = null;
constructor() {
super(null);
}
getData(): T {
return null;
}
setData() {
}
handler(caption: string) {
}
}
export class DialogWindow<T> extends qx.ui.window.Window {
callback: (name: string, data: T) => void;
entry: EntryBase<T>;
buttons: Array<ButtonData>;
firstFocusable: qx.ui.core.Widget = null;
constructor() {
super("");
this.setLayout(new qx.ui.layout.VBox(10));
var root = this.getApplicationRoot();
root.setBlockerColor("black");
root.setBlockerOpacity(0.4);
this.addListener("appear", () => {
this.center();
if (this.firstFocusable)
this.firstFocusable.focus();
}, this);
this.setModal(true);
}
createForm = () => {
this.add(this.entry, { flex: 1 });
var bpLayout = new qx.ui.layout.HBox(10);
bpLayout.setAlignX("center");
var buttonPane = new qx.ui.container.Composite(bpLayout);
this.add(buttonPane);
this.firstFocusable = this.entry.firstFocusable;
var noFocus = (this.firstFocusable === null);
this.buttons.forEach(e => {
var button = newButton(this, e.caption, e.color, 70, (c) => this.handler(c));
buttonPane.add(button);
if (noFocus)
this.firstFocusable == button;
});
}
handler(caption: string) {
this.hide();
if (this.callback) {
this.callback(caption, this.entry.getData());
}
}
}
export function showDialog<T>(entry: () => EntryBase<T>, buttons: () => Array<ButtonData>, focus: number,
callback?: (name: string, data: T) => void) {
var dialog = new DialogWindow<T>();
dialog.entry = entry();
dialog.setCaption(dialog.entry.caption);
dialog.buttons = buttons();
dialog.callback = callback;
dialog.createForm();
dialog.show();
}
export function showAbout() {
showDialog<any>(() => new EntryAbout(),
() => okButton(), 0);
}
export function showOk(message: string, callback?: (name: string, data: string) => void) {
showDialog<string>(() => new EntryLabel(message, "Info", "icon/48/status/dialog-information.png"),
() => okButton(), 0, callback);
}
export function showError(message: any, callback?: (name: string, data: string) => void) {
showDialog<string>(() => new EntryLabel(message, "Error", "icon/48/status/dialog-error.png"),
() => okButton(), 0, callback);
}
export function showWarning(message: string, callback?: (name: string, data: string) => void) {
showDialog<string>(() => new EntryLabel(message, "Warning", "icon/48/status/dialog-warning.png"),
() => okButton(), 0, callback);
}
export function showOkCancel(message: string, callback?: (name: string, data: string) => void) {
showDialog<string>(() => new EntryLabel(message, "Confirm", "icon/48/status/dialog-warning.png"),
() => okCancelButtons(), 0, callback);
}
export function showYesNo(message: string, callback?: (name: string, data: string) => void) {
showDialog<string>(() => new EntryLabel(message, "Confirm", "icon/48/status/dialog-warning.png"),
() => [
{ caption: "Yes", color: ColorType.Success },
{ caption: "No", color: ColorType.Warning }
], 1, callback);
}
export function showText(prompt: string, defValue: string, minWidth: number = 100) {
showDialog<string>(() => new EntryText(prompt, "Info", defValue, minWidth, true),
() => okButton(), 1);
}
export function inputText(prompt: string, defValue: string, minWidth: number = 100, callback: (d: string) => void = null): void {
showDialog<string>(() => new EntryText(prompt, "Input", defValue, minWidth, false),
() => okCancelButtons(), 1, (n, d) => {
if (n === "ok") {
if (callback) {
callback(d);
}
}
});
}
export function okButton(): ButtonData[]{
return [{ caption: "OK", color: ColorType.Info }];
}
export function okCancelButtons(): ButtonData[]{
return [{ caption: "OK", color: ColorType.Success },
{ caption: "Cancel", color: ColorType.Warning }];
}
class EntryLabel extends EntryBase<string> {
constructor(message: string, caption: string, icon: string) {
super();
this.setLayout(new qx.ui.layout.HBox(10));
this.caption = caption;
this.add(new qx.ui.basic.Image(icon));
var msg = new qx.ui.basic.Label(message);
msg.setRich(true);
msg.setMaxWidth(window.innerWidth * 2/3);
// msg.setAllowStretchX(true, true);
this.add(msg, { flex: 1 });
}
}
class EntryText extends EntryBase<string> {
entry: qx.ui.form.TextField;
constructor(message: string, caption: string, defValue: string, minWidth: number = 100, readOnly: boolean = false) {
super();
var layout = new qx.ui.layout.VBox(10);
this.setLayout(layout);
this.caption = caption;
var msg = new qx.ui.basic.Label(message);
this.add(msg);
this.entry = new qx.ui.form.TextField();
this.entry.setReadOnly(readOnly);
this.entry.setMinWidth(minWidth);
this.entry.setValue(defValue);
this.entry.setAllowStretchX(true, true);
this.add(this.entry, { flex: 1 });
}
getData(): string {
return this.entry.getValue();
}
setData() {
}
}
}