Skip to content

Commit abbb957

Browse files
author
“coder1”
committed
issues #12 - applied discriminated unions in typing
1 parent 0683973 commit abbb957

File tree

8 files changed

+535
-432
lines changed

8 files changed

+535
-432
lines changed

src/plugins/rangeSliderFox/mvc/controller/Controller.ts

Lines changed: 40 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,17 @@ import { boundMethod } from 'autobind-decorator';
33
import { NAME_PLUGIN } from '@shared/constants';
44

55
import { RangeSliderOptions } from '../../globInterface';
6-
import Model from '../model/Model';
7-
import View from '../view/View';
8-
9-
interface DataAttributesProps extends RangeSliderOptions {
10-
key: 'DataAttributes';
11-
}
6+
import {
7+
ControllerEventProps,
8+
DataAttributesProps,
9+
} from './controllerInterface';
1210

13-
type SnapNumberProps = {
14-
key: 'SnapNumber';
15-
isResized: boolean;
16-
snapNumber: number[];
17-
}
18-
19-
type ClickMarkProps = {
20-
key: 'ClickMark';
21-
valueGrid: number;
22-
};
23-
24-
type CreateGridProps = {
25-
key: 'CreateGrid';
26-
valueMark: {
27-
value: number;
28-
position: number;
29-
}[];
30-
};
31-
32-
type ClickBarProps = {
33-
key: 'ClickBar';
34-
clientXY: number;
35-
};
36-
37-
type BarDataProps = {
38-
key: 'BarData';
39-
bar: boolean | null;
40-
};
41-
42-
type ClickLineProps = {
43-
key: 'ClickLine';
44-
clientXY: number;
45-
};
46-
47-
type DisabledDataProps = {
48-
key: 'DisabledData';
49-
disabled: boolean | null;
50-
};
51-
52-
type ThemeDataProps = {
53-
key: 'ThemeData';
54-
theme: string | null;
55-
};
56-
57-
type OrientationDataProps = {
58-
key: 'OrientationData';
59-
orientation: string | null;
60-
};
61-
62-
type GridDataProps = {
63-
key: 'GridData';
64-
grid: boolean | null;
65-
};
66-
67-
type GridSnapDataProps = {
68-
key: 'GridSnapData';
69-
}
11+
import {
12+
ViewEventProps,
13+
} from '../view/viewInterface';
7014

71-
type DotMoveProps = {
72-
key: 'DotMove';
73-
type: string | null;
74-
position: number;
75-
clientXY: number;
76-
shiftXY: number;
77-
};
78-
79-
type DotDataProps = {
80-
key: 'DotData';
81-
type: string | null;
82-
to: number | null;
83-
from: number | null;
84-
};
85-
86-
type DotKeyDownProps = {
87-
key: 'DotKeyDown';
88-
keyRepeat: boolean;
89-
keySign: string;
90-
dot: string;
91-
};
92-
93-
type RangeDataProps = {
94-
key: 'RangeData';
95-
min: number;
96-
max: number;
97-
};
98-
99-
type StartProps = {
100-
key: 'Start';
101-
};
102-
103-
type HintsProps = {
104-
key?: 'HintsData';
105-
type?: string | null;
106-
from?: number | null;
107-
to?: number | null;
108-
tipPrefix?: string | null;
109-
tipPostfix?: string | null;
110-
tipFromTo?: boolean | null;
111-
tipMinMax?: boolean | null;
112-
min?: number | null;
113-
max?: number | null;
114-
};
115-
116-
type EventProps = HintsProps |
117-
StartProps |
118-
DotDataProps |
119-
DotKeyDownProps |
120-
RangeDataProps |
121-
DotMoveProps |
122-
GridSnapDataProps |
123-
GridDataProps |
124-
OrientationDataProps |
125-
ThemeDataProps |
126-
DisabledDataProps |
127-
ClickLineProps |
128-
BarDataProps |
129-
ClickBarProps |
130-
CreateGridProps |
131-
ClickMarkProps |
132-
SnapNumberProps;
15+
import Model from '../model/Model';
16+
import View from '../view/View';
13317

13418
class Controller {
13519
private isStarted = false;
@@ -212,9 +96,18 @@ class Controller {
21296
return true;
21397
}
21498

215-
private static subscribe(
216-
talking: Model | View,
217-
items: ((options: EventProps) => boolean | Promise<boolean>)[],
99+
private static subscribeModel(
100+
talking: Model,
101+
items: ((options: ControllerEventProps) => boolean | Promise<boolean>)[],
102+
) {
103+
return items.forEach((item: any) => {
104+
talking.subscribeObserver(item);
105+
});
106+
}
107+
108+
private static subscribeView(
109+
talking: View,
110+
items: ((options: ViewEventProps) => boolean)[],
218111
) {
219112
return items.forEach((item) => {
220113
talking.subscribeObserver(item);
@@ -240,7 +133,7 @@ class Controller {
240133
this.handleGridCreation,
241134
];
242135

243-
Controller.subscribe(this.model, handlesModel);
136+
Controller.subscribeModel(this.model, handlesModel);
244137

245138
const handlesView = [
246139
this.handleDotMove,
@@ -252,13 +145,13 @@ class Controller {
252145
this.handleDataAttributes,
253146
];
254147

255-
Controller.subscribe(this.view, handlesView);
148+
Controller.subscribeView(this.view, handlesView);
256149

257150
return true;
258151
}
259152

260153
@boundMethod
261-
private handleStart(options: StartProps) {
154+
private handleStart(options: ControllerEventProps) {
262155
const { key } = options;
263156
const isStarted = key !== 'Start';
264157

@@ -291,7 +184,7 @@ class Controller {
291184
}
292185

293186
@boundMethod
294-
private handleRangeData(options: RangeDataProps) {
187+
private handleRangeData(options: ControllerEventProps) {
295188
const { key } = options;
296189
if (key !== 'RangeData') {
297190
return false;
@@ -321,7 +214,7 @@ class Controller {
321214
}
322215

323216
@boundMethod
324-
private handleDotKeyDown(options: DotKeyDownProps) {
217+
private handleDotKeyDown(options: ViewEventProps) {
325218
const { key } = options;
326219
const isDotKeyDown = key !== 'DotKeyDown';
327220

@@ -341,7 +234,7 @@ class Controller {
341234
}
342235

343236
@boundMethod
344-
private handleDotData(options: DotDataProps) {
237+
private handleDotData(options: ControllerEventProps) {
345238
const { key } = options;
346239
if (key !== 'DotData') {
347240
return false;
@@ -387,7 +280,7 @@ class Controller {
387280
}
388281

389282
@boundMethod
390-
private handleDotMove(options: DotMoveProps) {
283+
private handleDotMove(options: ViewEventProps) {
391284
const { key } = options;
392285
const isDotMove = key !== 'DotMove';
393286

@@ -409,7 +302,7 @@ class Controller {
409302
}
410303

411304
@boundMethod
412-
private handleGridSnapData(options: GridSnapDataProps) {
305+
private handleGridSnapData(options: ControllerEventProps) {
413306
const { key } = options;
414307
const isGridSnapData = key !== 'GridSnapData';
415308

@@ -422,7 +315,7 @@ class Controller {
422315
}
423316

424317
@boundMethod
425-
private handleGridData(options: GridDataProps) {
318+
private handleGridData(options: ControllerEventProps) {
426319
const { key } = options;
427320

428321
if (key !== 'GridData') {
@@ -447,7 +340,7 @@ class Controller {
447340
}
448341

449342
@boundMethod
450-
private async handleOrientationData(options: OrientationDataProps) {
343+
private async handleOrientationData(options: ControllerEventProps) {
451344
const { key } = options;
452345
if (key !== 'OrientationData') {
453346
return false;
@@ -472,7 +365,7 @@ class Controller {
472365
}
473366

474367
@boundMethod
475-
private handleThemeData(options: ThemeDataProps) {
368+
private handleThemeData(options: ControllerEventProps) {
476369
const { key } = options;
477370
const isThemeData = key !== 'ThemeData';
478371

@@ -485,7 +378,7 @@ class Controller {
485378
}
486379

487380
@boundMethod
488-
private handleHintsData(options: HintsProps) {
381+
private handleHintsData(options: ControllerEventProps) {
489382
const { key } = options;
490383
if (key !== 'HintsData') {
491384
return false;
@@ -553,7 +446,7 @@ class Controller {
553446
}
554447

555448
@boundMethod
556-
private handleDisabledData(options: DisabledDataProps) {
449+
private handleDisabledData(options: ControllerEventProps) {
557450
const { key } = options;
558451
const isDisabledData = key !== 'DisabledData';
559452

@@ -568,7 +461,7 @@ class Controller {
568461
}
569462

570463
@boundMethod
571-
private handleLineClick(options: ClickLineProps) {
464+
private handleLineClick(options: ViewEventProps) {
572465
const { key } = options;
573466
const isClickLine = key !== 'ClickLine';
574467

@@ -587,7 +480,7 @@ class Controller {
587480
}
588481

589482
@boundMethod
590-
private handleBarData(options: BarDataProps) {
483+
private handleBarData(options: ControllerEventProps) {
591484
const { key } = options;
592485
if (key !== 'BarData') {
593486
return false;
@@ -604,7 +497,7 @@ class Controller {
604497
}
605498

606499
@boundMethod
607-
private handleBarClick(options: ClickBarProps) {
500+
private handleBarClick(options: ViewEventProps) {
608501
const { key } = options;
609502
const isClickBar = key !== 'ClickBar';
610503

@@ -624,7 +517,7 @@ class Controller {
624517
}
625518

626519
@boundMethod
627-
private handleGridCreation(options: CreateGridProps) {
520+
private handleGridCreation(options: ControllerEventProps) {
628521
const { key } = options;
629522
const isCreateGrid = key !== 'CreateGrid';
630523

@@ -637,7 +530,7 @@ class Controller {
637530
}
638531

639532
@boundMethod
640-
private handleMarkClick(options: ClickMarkProps) {
533+
private handleMarkClick(options: ViewEventProps) {
641534
const { key } = options;
642535
const isClickMark = key !== 'ClickMark';
643536

@@ -653,7 +546,7 @@ class Controller {
653546
}
654547

655548
@boundMethod
656-
private handleSnapNumber(options: SnapNumberProps) {
549+
private handleSnapNumber(options: ViewEventProps) {
657550
const { key } = options;
658551
const isSnapNumber = key !== 'SnapNumber';
659552

0 commit comments

Comments
 (0)