1
|
|
|
import React from 'react'; |
2
|
|
|
import ListenerAdapter from '../Observer/ListenerAdapter'; |
3
|
|
|
import {IObserverAdapter} from '../Observer/Observer'; |
4
|
|
|
import {IPageData} from '../Router/Router'; |
5
|
|
|
import ApplicationView, {IAdapter as IViewAdapter} from './View/Application'; |
6
|
|
|
import Model from './View/Application/Model'; |
7
|
|
|
import {IPresenter as IApplicationPresenter} from './View/Application/Presenter'; |
8
|
|
|
|
9
|
|
|
export interface IModulePageData extends IPageData { |
10
|
|
|
module: string |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
export interface IAdapter extends IViewAdapter { |
14
|
|
|
onPageChanged(newValue: IPageData): void; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
export default class Application { |
18
|
|
|
private readonly adapter: IAdapter; |
19
|
|
|
private readonly presenter: IApplicationPresenter; |
20
|
|
|
private view: ApplicationView | undefined; |
21
|
|
|
private readonly renderCallback: OmitThisParameter<() => void>; |
22
|
|
|
|
23
|
|
|
constructor(adapter: IAdapter, presenter:IApplicationPresenter) { |
24
|
5 |
|
this.presenter = presenter; |
25
|
5 |
|
this.adapter = adapter; |
26
|
5 |
|
this.renderCallback = this.render.bind(this); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
attachToLanguage(adapter:ListenerAdapter<string>) { |
30
|
5 |
|
adapter.addListener(this.renderCallback); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
attachToModuleState(adapter:ListenerAdapter<typeof React.Component | null>) { |
34
|
5 |
|
adapter.addListener(this.renderCallback); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
attachToMenuOpenState(adapter: IObserverAdapter<boolean>) { |
38
|
5 |
|
adapter.onChange = this.renderCallback; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
attachToContainerNode(containerNode: Element | DocumentFragment | null) { |
42
|
4 |
|
if (containerNode == null) return; |
43
|
4 |
|
this.view = new ApplicationView(containerNode, this.adapter); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
render() { |
47
|
6 |
|
if (this.view == undefined) return; |
48
|
|
|
|
49
|
4 |
|
const model: Model = this.presenter.present(); |
50
|
4 |
|
this.view.render(model); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|