@pihanga2/core 0.3.3 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/card.d.ts +3 -2
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +17 -8
- package/dist/card.js.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -3
- package/dist/index.js.map +1 -1
- package/dist/reducer.d.ts.map +1 -1
- package/dist/reducer.js +65 -18
- package/dist/reducer.js.map +1 -1
- package/dist/redux.d.ts.map +1 -1
- package/dist/redux.js.map +1 -1
- package/dist/register.d.ts +1 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +28 -0
- package/dist/register.js.map +1 -0
- package/dist/register_cards.d.ts +2 -2
- package/dist/register_cards.d.ts.map +1 -1
- package/dist/register_cards.js +3 -3
- package/dist/register_cards.js.map +1 -1
- package/dist/router.d.ts +11 -5
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +33 -28
- package/dist/router.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/card.tsx +17 -9
- package/src/index.ts +72 -4
- package/src/reducer.ts +133 -76
- package/src/redux.ts +26 -25
- package/src/register.ts +31 -0
- package/src/register_cards.ts +27 -26
- package/src/router.ts +129 -100
- package/src/types.ts +10 -7
package/src/router.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { registerActions
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import {Update, createBrowserHistory, Location} from "history";
|
|
2
|
+
import {getLogger} from "./logger";
|
|
3
|
+
import {createOnAction, registerActions} from "./redux";
|
|
4
|
+
import {
|
|
5
|
+
DEF_REDUCER_PRIORITY,
|
|
6
|
+
DispatchF,
|
|
7
|
+
PathQuery,
|
|
8
|
+
PiReducer,
|
|
9
|
+
ReduceF,
|
|
10
|
+
ReduxAction,
|
|
11
|
+
ReduxState,
|
|
12
|
+
Route,
|
|
13
|
+
} from "./types";
|
|
14
|
+
import {PiRegister} from ".";
|
|
15
|
+
|
|
16
|
+
const logger = getLogger("router");
|
|
8
17
|
export const browserHistory = createBrowserHistory();
|
|
9
18
|
|
|
10
19
|
// type Route = {
|
|
@@ -14,56 +23,69 @@ export const browserHistory = createBrowserHistory();
|
|
|
14
23
|
// fromBrowser?: boolean
|
|
15
24
|
// }
|
|
16
25
|
|
|
17
|
-
export const ACTION_TYPES = registerActions(
|
|
26
|
+
export const ACTION_TYPES = registerActions("pi/router", [
|
|
27
|
+
"show_page",
|
|
28
|
+
"navigate_to_page",
|
|
29
|
+
]);
|
|
18
30
|
|
|
19
31
|
export type ShowPageEvent = {
|
|
20
|
-
path: string[]
|
|
21
|
-
query?: PathQuery
|
|
22
|
-
fromBrowser?: boolean
|
|
23
|
-
}
|
|
24
|
-
export const onShowPage = createOnAction<ShowPageEvent>(ACTION_TYPES.SHOW_PAGE)
|
|
32
|
+
path: string[];
|
|
33
|
+
query?: PathQuery;
|
|
34
|
+
fromBrowser?: boolean;
|
|
35
|
+
};
|
|
36
|
+
export const onShowPage = createOnAction<ShowPageEvent>(ACTION_TYPES.SHOW_PAGE);
|
|
25
37
|
|
|
26
|
-
export const showPage = (
|
|
27
|
-
dispatch
|
|
28
|
-
|
|
38
|
+
export const showPage = (
|
|
39
|
+
dispatch: DispatchF,
|
|
40
|
+
path: string[],
|
|
41
|
+
query?: PathQuery
|
|
42
|
+
) => {
|
|
43
|
+
dispatch(createShowPageAction(path, query));
|
|
44
|
+
};
|
|
29
45
|
|
|
30
|
-
export const createShowPageAction = (
|
|
46
|
+
export const createShowPageAction = (
|
|
47
|
+
path: string[],
|
|
48
|
+
query?: PathQuery
|
|
49
|
+
): ReduxAction & ShowPageEvent => ({
|
|
31
50
|
type: ACTION_TYPES.SHOW_PAGE,
|
|
32
|
-
path,
|
|
33
|
-
|
|
51
|
+
path,
|
|
52
|
+
query,
|
|
53
|
+
fromBrowser: false,
|
|
54
|
+
});
|
|
34
55
|
|
|
35
56
|
export type NavigateToPageEvent = {
|
|
36
|
-
url: string
|
|
37
|
-
fromBrowser: boolean
|
|
38
|
-
}
|
|
39
|
-
export const onNavigateToPage = createOnAction<NavigateToPageEvent>(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
export
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
url: string;
|
|
58
|
+
fromBrowser: boolean;
|
|
59
|
+
};
|
|
60
|
+
export const onNavigateToPage = createOnAction<NavigateToPageEvent>(
|
|
61
|
+
ACTION_TYPES.NAVIGATE_TO_PAGE
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
export const ON_INIT_ACTION = "pi/start";
|
|
65
|
+
export const onInit = createOnAction<{}>(ON_INIT_ACTION);
|
|
66
|
+
|
|
67
|
+
export function currentRoute(pathPrefix = ""): Route {
|
|
68
|
+
const f = route_functions(pathPrefix);
|
|
69
|
+
const r2 = f.url2route(window.location.href);
|
|
70
|
+
const r = f.location2route(browserHistory.location);
|
|
71
|
+
return r;
|
|
50
72
|
}
|
|
51
73
|
|
|
52
|
-
export function init(reducer: PiReducer, pathPrefix =
|
|
53
|
-
let workingURL: string
|
|
54
|
-
const f = route_functions(pathPrefix)
|
|
74
|
+
export function init(reducer: PiReducer, pathPrefix = ""): Route {
|
|
75
|
+
let workingURL: string;
|
|
76
|
+
const f = route_functions(pathPrefix);
|
|
55
77
|
|
|
56
|
-
browserHistory.listen(({
|
|
78
|
+
browserHistory.listen(({action, location}: Update) => {
|
|
57
79
|
// location is an object like window.location
|
|
58
|
-
const {
|
|
80
|
+
const {url} = f.location2route(location);
|
|
59
81
|
if (workingURL !== url) {
|
|
60
|
-
logger.info(
|
|
61
|
-
setTimeout(() => navigateToPage(url, action ===
|
|
82
|
+
logger.info("browser history:", url, action);
|
|
83
|
+
setTimeout(() => navigateToPage(url, action === "POP"));
|
|
62
84
|
}
|
|
63
85
|
});
|
|
64
86
|
|
|
65
87
|
function browserPath(): Route {
|
|
66
|
-
return f.location2route(browserHistory.location)
|
|
88
|
+
return f.location2route(browserHistory.location);
|
|
67
89
|
}
|
|
68
90
|
|
|
69
91
|
function navigateToPage(url: string, fromBrowser = false) {
|
|
@@ -76,9 +98,9 @@ export function init(reducer: PiReducer, pathPrefix = ''): Route {
|
|
|
76
98
|
|
|
77
99
|
reducer.register<ReduxState, ReduxAction & NavigateToPageEvent>(
|
|
78
100
|
ACTION_TYPES.NAVIGATE_TO_PAGE,
|
|
79
|
-
(state, {
|
|
80
|
-
const r = f.url2route(url)
|
|
81
|
-
r.fromBrowser = fromBrowser
|
|
101
|
+
(state, {url, fromBrowser}, dispatch) => {
|
|
102
|
+
const r = f.url2route(url);
|
|
103
|
+
r.fromBrowser = fromBrowser;
|
|
82
104
|
if (workingURL && state.route?.url === r.url) {
|
|
83
105
|
return state;
|
|
84
106
|
}
|
|
@@ -86,16 +108,19 @@ export function init(reducer: PiReducer, pathPrefix = ''): Route {
|
|
|
86
108
|
type: ACTION_TYPES.SHOW_PAGE,
|
|
87
109
|
...r,
|
|
88
110
|
});
|
|
89
|
-
return state
|
|
90
|
-
}
|
|
111
|
+
return state;
|
|
112
|
+
},
|
|
113
|
+
DEF_REDUCER_PRIORITY,
|
|
114
|
+
"@builtin:router:NAVIGATE_TO_PAGE"
|
|
115
|
+
);
|
|
91
116
|
|
|
92
117
|
reducer.register<ReduxState, ReduxAction & ShowPageEvent>(
|
|
93
118
|
ACTION_TYPES.SHOW_PAGE,
|
|
94
|
-
(state, {
|
|
95
|
-
const route = f.pathl2route(path, query)
|
|
96
|
-
workingURL = route.url
|
|
119
|
+
(state, {path, query = {}, fromBrowser = false}) => {
|
|
120
|
+
const route = f.pathl2route(path, query);
|
|
121
|
+
workingURL = route.url;
|
|
97
122
|
if (!fromBrowser) {
|
|
98
|
-
const hp = browserPath()
|
|
123
|
+
const hp = browserPath();
|
|
99
124
|
if (route.url !== hp.url) {
|
|
100
125
|
browserHistory.push(route.url);
|
|
101
126
|
}
|
|
@@ -103,69 +128,73 @@ export function init(reducer: PiReducer, pathPrefix = ''): Route {
|
|
|
103
128
|
state.route = {
|
|
104
129
|
...route,
|
|
105
130
|
fromBrowser,
|
|
106
|
-
}
|
|
107
|
-
return state
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
};
|
|
131
|
+
};
|
|
132
|
+
return state;
|
|
133
|
+
},
|
|
134
|
+
DEF_REDUCER_PRIORITY,
|
|
135
|
+
"@builtin:router:SHOW_PAGE"
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
reducer.register(
|
|
139
|
+
"@@INIT",
|
|
140
|
+
(state) => {
|
|
141
|
+
const url = browserPath().url;
|
|
142
|
+
logger.info(`Request navigation to '${url}'`);
|
|
143
|
+
setTimeout(() => navigateToPage(url, true));
|
|
144
|
+
return state;
|
|
145
|
+
},
|
|
146
|
+
DEF_REDUCER_PRIORITY,
|
|
147
|
+
"@builtin:router:@@INIT"
|
|
148
|
+
);
|
|
149
|
+
return f.location2route(browserHistory.location);
|
|
150
|
+
}
|
|
118
151
|
|
|
119
|
-
function route_functions(pathPrefix =
|
|
152
|
+
function route_functions(pathPrefix = "") {
|
|
120
153
|
const location2route = (location: Location): Route => {
|
|
121
|
-
const [p, s] = [location.pathname, location.search]
|
|
122
|
-
const url = s ? `${p}${s}` : p
|
|
123
|
-
return url2route(url)
|
|
124
|
-
}
|
|
154
|
+
const [p, s] = [location.pathname, location.search];
|
|
155
|
+
const url = s ? `${p}${s}` : p;
|
|
156
|
+
return url2route(url);
|
|
157
|
+
};
|
|
125
158
|
|
|
126
159
|
const url2route = (url: string): Route => {
|
|
127
|
-
const [pn, search] = url.split("?")
|
|
128
|
-
|
|
129
|
-
const path = pn
|
|
130
|
-
|
|
131
|
-
|
|
160
|
+
const [pn, search] = url.split("?");
|
|
161
|
+
|
|
162
|
+
const path = pn
|
|
163
|
+
.substring(pathPrefix.length)
|
|
164
|
+
.split("/")
|
|
165
|
+
.filter((s) => s !== "");
|
|
166
|
+
const query = {} as PathQuery;
|
|
167
|
+
const s = search;
|
|
132
168
|
if (s && s.length > 0) {
|
|
133
|
-
s.split(
|
|
134
|
-
const [k, v] = el.split(
|
|
169
|
+
s.split("&").forEach((el) => {
|
|
170
|
+
const [k, v] = el.split("=");
|
|
135
171
|
query[decodeURI(k)] = v ? decodeURI(v) : true;
|
|
136
172
|
});
|
|
137
173
|
}
|
|
138
|
-
return {
|
|
139
|
-
}
|
|
174
|
+
return {url, path, query};
|
|
175
|
+
};
|
|
140
176
|
|
|
141
177
|
const pathl2route = (path: string[], query: PathQuery): Route => {
|
|
142
|
-
let url = `${pathPrefix}/${path.join(
|
|
178
|
+
let url = `${pathPrefix}/${path.join("/")}`;
|
|
143
179
|
if (query) {
|
|
144
180
|
const qa = Object.entries(query);
|
|
145
181
|
if (qa.length > 0) {
|
|
146
|
-
const s = qa
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
182
|
+
const s = qa
|
|
183
|
+
.map(([k, v]) => {
|
|
184
|
+
const n = encodeURI(k);
|
|
185
|
+
if (typeof v === "boolean") {
|
|
186
|
+
return n;
|
|
187
|
+
} else if (typeof v === "number") {
|
|
188
|
+
return `${n}=${v}`;
|
|
189
|
+
} else {
|
|
190
|
+
return `${n}=${encodeURI(v)}`;
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
.join("&");
|
|
194
|
+
url = `${url}?${s}`;
|
|
157
195
|
}
|
|
158
196
|
}
|
|
159
|
-
return {
|
|
160
|
-
}
|
|
161
|
-
return {
|
|
197
|
+
return {url, path, query};
|
|
198
|
+
};
|
|
199
|
+
return {location2route, url2route, pathl2route};
|
|
162
200
|
}
|
|
163
|
-
|
|
164
|
-
function createOnAction<E>(actionType: string): <S extends ReduxState>(
|
|
165
|
-
register: PiRegister,
|
|
166
|
-
f: ReduceF<S, ReduxAction & E>,
|
|
167
|
-
) => void {
|
|
168
|
-
return (register, f) => {
|
|
169
|
-
register.reducer.register(actionType, f)
|
|
170
|
-
}
|
|
171
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type ReduxState = {
|
|
2
2
|
route: Route;
|
|
3
3
|
|
|
4
|
-
pihanga?: {
|
|
4
|
+
pihanga?: {[key: string]: any};
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
export type Route = {
|
|
@@ -10,7 +10,7 @@ export type Route = {
|
|
|
10
10
|
url: string;
|
|
11
11
|
fromBrowser?: boolean;
|
|
12
12
|
};
|
|
13
|
-
export type PathQuery = {
|
|
13
|
+
export type PathQuery = {[k: string]: string | number | boolean};
|
|
14
14
|
|
|
15
15
|
export type ReduxAction = {
|
|
16
16
|
type: string;
|
|
@@ -23,7 +23,7 @@ export type CardAction = ReduxAction & {
|
|
|
23
23
|
export type PiRegisterComponent = {
|
|
24
24
|
name: string;
|
|
25
25
|
component: any; // ReactComponent
|
|
26
|
-
events?: {
|
|
26
|
+
events?: {[key: string]: string};
|
|
27
27
|
// defaults?: { [key: string]: any }
|
|
28
28
|
};
|
|
29
29
|
|
|
@@ -48,11 +48,14 @@ export interface PiReducer {
|
|
|
48
48
|
dispatchFromReducer: DispatchF;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
export const DEF_REDUCER_PRIORITY = 0;
|
|
52
|
+
|
|
51
53
|
export type PiRegisterReducerF = <S extends ReduxState, A extends ReduxAction>(
|
|
52
54
|
eventType: string,
|
|
53
55
|
mapper: ReduceF<S, A>, // (state: S, action: A, dispatch: DispatchF) => S,
|
|
54
56
|
priority?: number,
|
|
55
|
-
key?: string
|
|
57
|
+
key?: string,
|
|
58
|
+
targetMapper?: ReduceF<S, A>
|
|
56
59
|
) => PiReducerCancelF;
|
|
57
60
|
|
|
58
61
|
export type PiReducerCancelF = () => void;
|
|
@@ -69,7 +72,7 @@ export type PiRegisterOneShotReducerF = <
|
|
|
69
72
|
// CARDS
|
|
70
73
|
|
|
71
74
|
// context props given to <Card> in parent card
|
|
72
|
-
export type PiDefCtxtProps = {
|
|
75
|
+
export type PiDefCtxtProps = {[k: string]: any};
|
|
73
76
|
|
|
74
77
|
// type for <Card .../>
|
|
75
78
|
export type CardProp = {
|
|
@@ -95,7 +98,7 @@ export type PiCardProps<P, E = {}> = P & {
|
|
|
95
98
|
[Key in keyof E]: (ev: E[Key]) => void;
|
|
96
99
|
};
|
|
97
100
|
|
|
98
|
-
export type CSSModuleClasses = {
|
|
101
|
+
export type CSSModuleClasses = {readonly [key: string]: string};
|
|
99
102
|
|
|
100
103
|
export type PiCardRef = string | PiCardDef;
|
|
101
104
|
|
|
@@ -150,7 +153,7 @@ export type PiCardDef = {
|
|
|
150
153
|
export type PiRegisterMetaCard = {
|
|
151
154
|
type: string;
|
|
152
155
|
mapper: MetaCardMapperF;
|
|
153
|
-
events?: {
|
|
156
|
+
events?: {[key: string]: string};
|
|
154
157
|
};
|
|
155
158
|
|
|
156
159
|
export type RegisterCardF = (name: string, parameters: PiCardDef) => PiCardRef;
|