瀏覽代碼

add the current view to the app state

crobi 9 年之前
父節點
當前提交
f37092cd0e

+ 10
- 0
client/src/actions/actions.ts 查看文件

5
     export class ActionReset implements Action {
5
     export class ActionReset implements Action {
6
         constructor() {}
6
         constructor() {}
7
     }
7
     }
8
+    
9
+    /* View actions */
10
+   export class ActionSetView implements Action {
11
+        constructor(private _view: ViewState, private _params: EntityId[]) {}
12
+        get view(): ViewState {return this._view}
13
+        get params(): EntityId[] {return this._params}
14
+    }
8
 
15
 
9
     /* Deck actions */
16
     /* Deck actions */
10
     export class ActionNewDeck implements Action {
17
     export class ActionNewDeck implements Action {
86
         public reset(): void {
93
         public reset(): void {
87
             this._dispatcher.dispatch(new ActionReset());
94
             this._dispatcher.dispatch(new ActionReset());
88
         }
95
         }
96
+        public setView(view:ViewState, params:EntityId[]): void {
97
+            this._dispatcher.dispatch(new ActionSetView(view, params));
98
+        }
89
         public newDeck(): void {
99
         public newDeck(): void {
90
             this._dispatcher.dispatch(new ActionNewDeck());
100
             this._dispatcher.dispatch(new ActionNewDeck());
91
         }
101
         }

+ 7
- 23
client/src/app.ts 查看文件

1
 /// <reference path="./dispatcher/dispatcher.ts"/>
1
 /// <reference path="./dispatcher/dispatcher.ts"/>
2
 /// <reference path="./stores/store.ts"/>
2
 /// <reference path="./stores/store.ts"/>
3
 /// <reference path="./actions/actions.ts"/>
3
 /// <reference path="./actions/actions.ts"/>
4
-/// <reference path="./views/header.ts"/>
5
 /// <reference path="./views/view.ts"/>
4
 /// <reference path="./views/view.ts"/>
5
+/// <reference path="./views/ui.ts"/>
6
 /// <reference path="./external/page/page.d.ts"/>
6
 /// <reference path="./external/page/page.d.ts"/>
7
 
7
 
8
+/// <reference path="./routes.ts"/>
8
 /// <reference path="./mock.ts"/>
9
 /// <reference path="./mock.ts"/>
9
 
10
 
10
 module rpgcards {
11
 module rpgcards {
33
             this.dispatcher = new Dispatcher();
34
             this.dispatcher = new Dispatcher();
34
             this.actions = new Actions(this.dispatcher);
35
             this.actions = new Actions(this.dispatcher);
35
             this.store = new Store(this.dispatcher);
36
             this.store = new Store(this.dispatcher);
37
+            this.view = renderUI;
36
             this.container = ()=>document.body;
38
             this.container = ()=>document.body;
37
-            
38
-            // Map URLs to views
39
-            this.setupRoutes();
40
-    
39
+
41
             // Each state change triggers a re-render
40
             // Each state change triggers a re-render
42
             this.store.addChangeListener(() => this.refresh());
41
             this.store.addChangeListener(() => this.refresh());
43
-            
44
-            // Trigger an initial action
45
-            this.actions.reset();
42
+             
43
+            // Map URLs to views (triggers an initial action)
44
+            setupRoutes(this.actions);
46
         }
45
         }
47
         
46
         
48
         // Refresh
47
         // Refresh
51
             // the application state (Store) to React elements
50
             // the application state (Store) to React elements
52
             React.render(this.view(this.store), this.container());
51
             React.render(this.view(this.store), this.container());
53
         }
52
         }
54
-        
55
-        // Routing
56
-        setupRoutes() {
57
-            page('/', () => {
58
-                this.view = renderDecks;
59
-            });
60
-            page('/decks', () => {
61
-                this.view = renderDecks;
62
-            });
63
-            page('*', () => {
64
-                this.view = renderDecks;
65
-            });
66
-            
67
-            page();
68
-        }
69
     }
53
     }
70
     
54
     
71
 
55
 

+ 20
- 0
client/src/routes.ts 查看文件

1
+
2
+/// <reference path="./views/view.ts"/>
3
+/// <reference path="./external/page/page.d.ts"/>
4
+
5
+module rpgcards {
6
+    
7
+    export function setupRoutes(actions: Actions) {
8
+        page('/', () => {
9
+            actions.setView(ViewState.MainMenu, []);
10
+        });
11
+        page('/decks', () => {
12
+            actions.setView(ViewState.DeckList, []);
13
+        });
14
+        page('*', () => {
15
+            actions.setView(ViewState.MainMenu, []);
16
+        });
17
+        
18
+        page();
19
+    }
20
+}

+ 18
- 4
client/src/stores/store.ts 查看文件

1
 /// <reference path="./card.ts"/>
1
 /// <reference path="./card.ts"/>
2
 /// <reference path="./deck.ts"/>
2
 /// <reference path="./deck.ts"/>
3
+/// <reference path="./viewState.ts"/>
3
 /// <reference path="./eventEmitter.ts"/>
4
 /// <reference path="./eventEmitter.ts"/>
4
 /// <reference path="./asyncT.ts"/>
5
 /// <reference path="./asyncT.ts"/>
5
 /// <reference path="../dispatcher/dispatcher.ts"/>
6
 /// <reference path="../dispatcher/dispatcher.ts"/>
21
         private _datasets: CardDataSet[];
22
         private _datasets: CardDataSet[];
22
         private _records: CardData[];
23
         private _records: CardData[];
23
         private _templates: CardTemplate[];
24
         private _templates: CardTemplate[];
25
+        private _view: ViewState;
26
+        private _viewParams: EntityId[];
24
 
27
 
25
         constructor(dispatcher: Dispatcher) {
28
         constructor(dispatcher: Dispatcher) {
26
             super();
29
             super();
27
-            this._decks = [];
28
-            this._datasets = [];
29
-            this._records = [];
30
-            this._templates = [];
30
+            this._reset();
31
 
31
 
32
             dispatcher.register((action) => {
32
             dispatcher.register((action) => {
33
                 /* Generic */
33
                 /* Generic */
34
                 if(action instanceof ActionReset) {
34
                 if(action instanceof ActionReset) {
35
                     this._reset()
35
                     this._reset()
36
                 }
36
                 }
37
+                else if(action instanceof ActionSetView) {
38
+                    this._view = action.view;
39
+                    this._viewParams = action.params;
40
+                }
37
                 /* Deck */
41
                 /* Deck */
38
                 else if(action instanceof ActionNewDeck) {
42
                 else if(action instanceof ActionNewDeck) {
39
                     this._newDeck()
43
                     this._newDeck()
102
         // ---------------------------------------------------------------------
106
         // ---------------------------------------------------------------------
103
         // Accessing data
107
         // Accessing data
104
         // ---------------------------------------------------------------------
108
         // ---------------------------------------------------------------------
109
+        
110
+        getViewState(): ViewState {
111
+            return this._view;
112
+        }
113
+        
114
+        getViewParams(): EntityId[] {
115
+            return this._viewParams;
116
+        }
105
 
117
 
106
         getDatasetList(): AsyncT<string[]> {
118
         getDatasetList(): AsyncT<string[]> {
107
             return AsyncT.just(
119
             return AsyncT.just(
145
             this._datasets = [];
157
             this._datasets = [];
146
             this._records = [];
158
             this._records = [];
147
             this._templates = [];
159
             this._templates = [];
160
+            this._view = ViewState.MainMenu;
161
+            this._viewParams = [];
148
         }
162
         }
149
         
163
         
150
         private _newDeck(): void {
164
         private _newDeck(): void {

+ 17
- 0
client/src/stores/viewState.ts 查看文件

1
+module rpgcards {
2
+	
3
+	/**
4
+	 * List of states the UI can be in
5
+	 */
6
+	export const enum ViewState {
7
+		MainMenu,
8
+		DeckList,
9
+		DeckEdit,
10
+		DeckAddDataset,
11
+		TemplateList,
12
+		TemplateEdit,
13
+		DatasetList,
14
+		DatasetEdit,
15
+		DatasetEditRecord
16
+	}
17
+}

+ 2
- 0
client/src/views/components/deck.ts 查看文件

1
+/// <reference path="../../external/react/react.d.ts"/>
2
+
1
 module rpgcards {
3
 module rpgcards {
2
 
4
 
3
     export interface DeckTileProps extends React.Props<any> {
5
     export interface DeckTileProps extends React.Props<any> {

+ 7
- 4
client/src/views/decks.ts 查看文件

1
 /// <reference path="../external/react/react.d.ts"/>
1
 /// <reference path="../external/react/react.d.ts"/>
2
+/// <reference path="./header.ts"/>
2
 
3
 
3
 module rpgcards {
4
 module rpgcards {
4
 
5
 
69
             pending: () => [React.DOM.div({}, "loading...")]
70
             pending: () => [React.DOM.div({}, "loading...")]
70
         });
71
         });
71
 
72
 
72
-        return React.DOM.div({ className: 'decks' },
73
-            decks,
74
-            newDeckTile()
75
-        );
73
+        return React.DOM.div({}
74
+            , renderHeader(store)
75
+            , React.DOM.div({ className: 'decks' }
76
+                , decks
77
+                , newDeckTile()
78
+            ));
76
     }
79
     }
77
 }
80
 }

+ 12
- 0
client/src/views/ui.ts 查看文件

1
+/// <reference path="./view.ts"/>
2
+/// <reference path="./decks.ts"/>
3
+
4
+module rpgcards {
5
+	
6
+	export function renderUI(store: Store): React.ReactElement<any> {
7
+		switch(store.getViewState()) {
8
+			case ViewState.MainMenu: return renderDecks(store);
9
+			case ViewState.DeckEdit: return renderDecks(store);
10
+		}
11
+	}
12
+}

+ 4
- 1
client/tsconfig.json 查看文件

18
     "files": [
18
     "files": [
19
         "./src/app.ts",
19
         "./src/app.ts",
20
         "./src/mock.ts",
20
         "./src/mock.ts",
21
+        "./src/routes.ts",
21
         "./src/actions/actions.ts",
22
         "./src/actions/actions.ts",
22
         "./src/dispatcher/dispatcher.ts",
23
         "./src/dispatcher/dispatcher.ts",
23
         "./src/dispatcher/invariant.ts",
24
         "./src/dispatcher/invariant.ts",
33
         "./src/stores/id.ts",
34
         "./src/stores/id.ts",
34
         "./src/stores/store.ts",
35
         "./src/stores/store.ts",
35
         "./src/stores/template.ts",
36
         "./src/stores/template.ts",
37
+        "./src/stores/viewState.ts",
36
         "./src/views/components/deck.ts",
38
         "./src/views/components/deck.ts",
39
+        "./src/views/view.ts",
37
         "./src/views/decks.ts",
40
         "./src/views/decks.ts",
38
         "./src/views/header.ts",
41
         "./src/views/header.ts",
39
-        "./src/views/view.ts"
42
+        "./src/views/ui.ts"
40
     ]
43
     ]
41
 }
44
 }

Loading…
取消
儲存