-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.5 Mediator.ts
181 lines (148 loc) · 5.11 KB
/
3.5 Mediator.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
/*
* This is an example of a Mediator design pattern usage.
* The code snippet represents business logic of a dialog window with font settings.
*
* An abstract class Colleague is extended into DialogButtons, SingleSelectionListBox,
* Input field and Checkbox. A DialogModerator class handles interactions between colleagues.
* The dialog window consists of:
* - one SingleSelectionListBox for font family;
* - two Inputs for font size and weight;
* - one Checkbox for italics;
* - two DialogButtons: 'OK' and 'Cancel'.
*
* After the dialog is closed the font information is either updated ('OK') or stays
* without modifications ('Cancel').
*/
abstract class Colleague {
dialogMediator: DialogMediator;
constructor(dialogMediator: DialogMediator) {
this.dialogMediator = dialogMediator;
}
changed() {
this.dialogMediator.widgetChanged(this);
}
}
class DialogButton extends Colleague {
text: string;
constructor(dialogMediator: DialogMediator, text: string) {
super(dialogMediator);
this.text = text;
}
click(): void {
this.changed();
}
}
class SingleSelectionListBox extends Colleague {
options: string[];
selected: number;
constructor(dialogMediator: DialogMediator, options: string[]) {
super(dialogMediator);
this.options = options;
this.selected = 0;
}
click(e: any): void {
this.selected = e.clickedItem.index; // get the value from the event of an actually drawn listbox
this.changed();
}
getSelection(): string {
return this.options[this.selected];
}
}
class Input extends Colleague {
text: string;
constructor(dialogMediator: DialogMediator) {
super(dialogMediator);
this.text = '';
}
input(e: any): void {
this.text = e.text; // get the value from the event of an actually drawn input
this.changed();
}
getText(): string {
return this.text;
}
}
class Checkbox extends Colleague {
label: string;
checked: boolean;
constructor(dialogMediator: DialogMediator, label: string) {
super(dialogMediator);
this.label = label;
this.checked = false;
}
click(): void {
this.checked = !this.checked; // get the value from the event of an actually drawn input
this.changed();
}
getValue(): boolean {
return this.checked;
}
}
abstract class DialogMediator {
abstract showDialog(): void;
abstract createWidgets(): void;
abstract widgetChanged(collegue: Colleague): void;
abstract apply(): void;
abstract close(): void;
}
class FontSettingsDialogMediator extends DialogMediator {
fontListBox!: SingleSelectionListBox;
fontSizeInput!: Input;
fontWeightInput!: Input;
italicCheckbox!: Checkbox;
okButton!: DialogButton;
cancelButton!: DialogButton;
fontFamily: string;
fontSize: number;
fontWeight: number;
italic: boolean;
constructor(fontFamily: string, fontSize: number, fontWeight: number, italic: boolean) {
super();
this.fontFamily = fontFamily;
this.fontSize = fontSize;
this.fontWeight = fontWeight;
this.italic = italic;
}
showDialog(): void {
this.createWidgets();
console.log('Drawing the dialog window and positioning all widgets inside...');
}
createWidgets(): void {
this.fontListBox = new SingleSelectionListBox(this, ['Helvetica', 'Times', 'Fira Sans']);
this.fontSizeInput = new Input(this);
this.fontWeightInput = new Input(this);
this.italicCheckbox = new Checkbox(this, 'Italic');
this.okButton = new DialogButton(this, 'OK');
this.cancelButton = new DialogButton(this, 'Cancel');
console.log('Initializing widgets with additionally required values...');
}
widgetChanged(colleague: Colleague): void {
if (colleague === this.fontListBox) {
this.fontFamily = this.fontListBox.getSelection();
} else if (colleague === this.fontSizeInput) {
this.fontSize = parseInt(this.fontSizeInput.getText(), 10);
} else if (colleague === this.fontWeightInput) {
this.fontWeight = parseInt(this.fontWeightInput.getText(), 10);
} else if (colleague === this.italicCheckbox) {
this.italic = this.italicCheckbox.getValue();
} else if (colleague === this.okButton) {
this.apply();
this.close();
} else if (colleague === this.cancelButton) {
this.close();
}
}
apply(): void {
console.log(`Applied settings:\nFont family: ${this.fontFamily}\nFont size: ${this.fontSize}\nFont weight: ${this.fontWeight}\nItalic: ${this.italic}`);
}
close(): void {
console.log('FontSettingsDialog closed!');
}
}
const fontDialog = new FontSettingsDialogMediator('Helvetica', 14, 300, false);
fontDialog.showDialog();
fontDialog.fontListBox.click({ clickedItem: { index: 2 } });
fontDialog.fontSizeInput.input({ text: '10' });
fontDialog.fontWeightInput.input({ text: '600' });
fontDialog.italicCheckbox.click();
fontDialog.okButton.click();