brew-js-react 0.2.1 → 0.2.4
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/dialog.js +10 -4
- package/dist/brew-js-react.js +252 -93
- package/dist/brew-js-react.js.map +1 -1
- package/dist/brew-js-react.min.js +1 -1
- package/dist/brew-js-react.min.js.map +1 -1
- package/hooks.d.ts +1 -1
- package/hooks.js +113 -84
- package/i18n.d.ts +9 -1
- package/i18n.js +5 -2
- package/include/brew-js/var.js +1 -0
- package/mixin.js +6 -2
- package/mixins/ClassNameMixin.js +0 -3
- package/mixins/FlyoutMixin.d.ts +5 -2
- package/mixins/FlyoutMixin.js +17 -3
- package/mixins/FlyoutToggleMixin.d.ts +4 -1
- package/mixins/FlyoutToggleMixin.js +6 -3
- package/mixins/FocusStateMixin.js +2 -2
- package/mixins/Mixin.d.ts +4 -0
- package/mixins/Mixin.js +2 -0
- package/mixins/StatefulMixin.js +10 -3
- package/package.json +2 -1
- package/view.d.ts +27 -0
- package/view.js +90 -25
package/view.js
CHANGED
|
@@ -2,7 +2,7 @@ import React, { useRef } from "react";
|
|
|
2
2
|
import { useAsync } from "zeta-dom-react";
|
|
3
3
|
import dom from "./include/zeta-dom/dom.js";
|
|
4
4
|
import { notifyAsync } from "./include/zeta-dom/domLock.js";
|
|
5
|
-
import { any, defineGetterProperty, definePrototype, each, extend, isFunction, keys, makeArray, noop, pick, randomId, setImmediate } from "./include/zeta-dom/util.js";
|
|
5
|
+
import { any, defineGetterProperty, definePrototype, each, exclude, extend, grep, isFunction, keys, makeArray, map, noop, pick, randomId, setImmediate, single } from "./include/zeta-dom/util.js";
|
|
6
6
|
import { animateIn, animateOut } from "./include/brew-js/anim.js";
|
|
7
7
|
import { app } from "./app.js";
|
|
8
8
|
import { ViewStateContainer } from "./hooks.js";
|
|
@@ -10,6 +10,7 @@ import { ViewStateContainer } from "./hooks.js";
|
|
|
10
10
|
const root = dom.root;
|
|
11
11
|
const routeMap = new Map();
|
|
12
12
|
const usedParams = {};
|
|
13
|
+
const sortedViews = [];
|
|
13
14
|
const StateContext = React.createContext(Object.freeze({ active: true }));
|
|
14
15
|
|
|
15
16
|
let stateId;
|
|
@@ -27,6 +28,7 @@ function ViewContainer() {
|
|
|
27
28
|
}
|
|
28
29
|
self.componentWillUnmount = app.on('navigate', function () {
|
|
29
30
|
if (self.mounted && self.getViewComponent()) {
|
|
31
|
+
self.isForceUpdate = true;
|
|
30
32
|
self.forceUpdate();
|
|
31
33
|
}
|
|
32
34
|
});
|
|
@@ -50,18 +52,19 @@ definePrototype(ViewContainer, React.Component, {
|
|
|
50
52
|
if (V && V !== self.currentViewComponent) {
|
|
51
53
|
self.currentViewComponent = V;
|
|
52
54
|
if (self.currentView && self.currentElement) {
|
|
55
|
+
var prevPath = self.currentPath;
|
|
56
|
+
var prevElement = self.currentElement;
|
|
53
57
|
self.prevView = self.currentView;
|
|
54
|
-
self.prevElement = self.currentElement;
|
|
55
58
|
self.currentElement = undefined;
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
app.emit('pageleave', prevElement, { pathname: prevPath }, true);
|
|
60
|
+
animateOut(prevElement, 'show').then(function () {
|
|
58
61
|
self.prevView = undefined;
|
|
59
62
|
self.forceUpdate();
|
|
60
63
|
});
|
|
61
64
|
}
|
|
62
65
|
var providerProps = {
|
|
63
66
|
key: routeMap.get(V).id,
|
|
64
|
-
value: {}
|
|
67
|
+
value: { view: V }
|
|
65
68
|
};
|
|
66
69
|
var view = React.createElement(StateContext.Provider, providerProps,
|
|
67
70
|
React.createElement(ViewStateContainer, null,
|
|
@@ -72,15 +75,21 @@ definePrototype(ViewContainer, React.Component, {
|
|
|
72
75
|
self.parentElement = element.parentElement;
|
|
73
76
|
setImmediate(function () {
|
|
74
77
|
resolve();
|
|
75
|
-
|
|
78
|
+
animateIn(element, 'show');
|
|
79
|
+
app.emit('pageenter', element, { pathname: app.path }, true);
|
|
76
80
|
});
|
|
77
81
|
}
|
|
78
82
|
})));
|
|
79
83
|
defineGetterProperty(providerProps.value, 'active', function () {
|
|
80
84
|
return self.currentView === view;
|
|
81
85
|
});
|
|
86
|
+
self.currentPath = app.path;
|
|
82
87
|
self.currentView = view;
|
|
83
88
|
} else {
|
|
89
|
+
if (self.isForceUpdate) {
|
|
90
|
+
self.isForceUpdate = false;
|
|
91
|
+
app.emit('pageenter', self.currentElement, { pathname: app.path }, true);
|
|
92
|
+
}
|
|
84
93
|
resolve();
|
|
85
94
|
}
|
|
86
95
|
notifyAsync(self.parentElement || root, promise);
|
|
@@ -88,22 +97,84 @@ definePrototype(ViewContainer, React.Component, {
|
|
|
88
97
|
},
|
|
89
98
|
getViewComponent: function () {
|
|
90
99
|
var props = this.props;
|
|
91
|
-
|
|
100
|
+
var matched = any(props.views, isViewMatched) || props.defaultView;
|
|
101
|
+
if (history.state === stateId) {
|
|
102
|
+
// ensure the current path actually corresponds to the matched view
|
|
103
|
+
// when some views are not included in the list of allowed views
|
|
104
|
+
var targetPath = linkTo(matched, getCurrentParams(matched, true));
|
|
105
|
+
if (targetPath !== app.path) {
|
|
106
|
+
app.navigate(targetPath, true);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return matched;
|
|
92
110
|
}
|
|
93
111
|
});
|
|
94
112
|
|
|
95
|
-
|
|
96
|
-
|
|
113
|
+
function getCurrentParams(view, includeAll, params) {
|
|
114
|
+
var state = routeMap.get(view);
|
|
115
|
+
if (!state.maxParams) {
|
|
116
|
+
var matchers = exclude(state.matchers, ['remainingSegments']);
|
|
117
|
+
var matched = map(app.routes, function (v) {
|
|
118
|
+
var route = app.parseRoute(v);
|
|
119
|
+
var matched = route.length && !any(matchers, function (v, i) {
|
|
120
|
+
var pos = route.params[i];
|
|
121
|
+
return (v ? !(pos >= 0) : pos < route.minLength) || (!isFunction(v) && !route.match(i, v));
|
|
122
|
+
});
|
|
123
|
+
return matched ? route : null;
|
|
124
|
+
});
|
|
125
|
+
if (matched[1]) {
|
|
126
|
+
matched = grep(matched, function (v) {
|
|
127
|
+
return !single(v.params, function (v, i) {
|
|
128
|
+
return usedParams[i] && !matchers[i];
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (matched[0]) {
|
|
133
|
+
var last = matched.slice(-1)[0];
|
|
134
|
+
state.maxParams = keys(extend.apply(0, [{}].concat(matched.map(function (v) {
|
|
135
|
+
return v.params;
|
|
136
|
+
}))));
|
|
137
|
+
state.minParams = map(last.params, function (v, i) {
|
|
138
|
+
return state.params[i] || v >= last.minLength ? null : i;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return pick(params || app.route, includeAll ? state.maxParams : state.minParams);
|
|
97
143
|
}
|
|
98
144
|
|
|
99
|
-
|
|
145
|
+
function sortViews(a, b) {
|
|
146
|
+
return (routeMap.get(b) || {}).matchCount - (routeMap.get(a) || {}).matchCount;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function matchViewParams(view, route) {
|
|
100
150
|
var params = routeMap.get(view);
|
|
101
|
-
return !!params &&
|
|
102
|
-
var value =
|
|
151
|
+
return !!params && !single(params.matchers, function (v, i) {
|
|
152
|
+
var value = route[i] || '';
|
|
103
153
|
return isFunction(v) ? !v(value) : (v || '') !== value;
|
|
104
154
|
});
|
|
105
155
|
}
|
|
106
156
|
|
|
157
|
+
export function useViewContainerState() {
|
|
158
|
+
return React.useContext(StateContext);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function isViewMatched(view) {
|
|
162
|
+
return matchViewParams(view, app.route);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function matchView(path, views) {
|
|
166
|
+
var route = app.route;
|
|
167
|
+
if (typeof path === 'string') {
|
|
168
|
+
route = route.parse(path);
|
|
169
|
+
} else {
|
|
170
|
+
views = path;
|
|
171
|
+
}
|
|
172
|
+
views = views ? makeArray(views).sort(sortViews) : sortedViews;
|
|
173
|
+
return any(views, function (v) {
|
|
174
|
+
return matchViewParams(v, route);
|
|
175
|
+
}) || undefined;
|
|
176
|
+
}
|
|
177
|
+
|
|
107
178
|
export function registerView(factory, routeParams) {
|
|
108
179
|
var Component = function (props) {
|
|
109
180
|
var state = useAsync(factory);
|
|
@@ -131,6 +202,8 @@ export function registerView(factory, routeParams) {
|
|
|
131
202
|
return typeof v === 'string';
|
|
132
203
|
})
|
|
133
204
|
});
|
|
205
|
+
sortedViews.push(Component);
|
|
206
|
+
sortedViews.sort(sortViews);
|
|
134
207
|
return Component;
|
|
135
208
|
}
|
|
136
209
|
|
|
@@ -138,24 +211,16 @@ export function renderView() {
|
|
|
138
211
|
var views = makeArray(arguments);
|
|
139
212
|
var rootProps = isFunction(views[0]) ? {} : views.shift();
|
|
140
213
|
var defaultView = views[0];
|
|
141
|
-
views.sort(
|
|
142
|
-
return (routeMap.get(b) || {}).matchCount - (routeMap.get(a) || {}).matchCount;
|
|
143
|
-
});
|
|
214
|
+
views.sort(sortViews);
|
|
144
215
|
return React.createElement(ViewContainer, { rootProps, views, defaultView });
|
|
145
216
|
}
|
|
146
217
|
|
|
147
218
|
export function linkTo(view, params) {
|
|
148
|
-
var
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (viewParams && i in viewParams) {
|
|
152
|
-
newParams[i] = viewParams[i];
|
|
153
|
-
} else if (params && i in params) {
|
|
154
|
-
newParams[i] = params[i];
|
|
155
|
-
} else if (!usedParams[i]) {
|
|
156
|
-
newParams[i] = app.route[i];
|
|
157
|
-
}
|
|
219
|
+
var state = routeMap.get(view);
|
|
220
|
+
if (!state) {
|
|
221
|
+
return '/';
|
|
158
222
|
}
|
|
223
|
+
var newParams = extend(getCurrentParams(view), getCurrentParams(view, true, params), state.params);
|
|
159
224
|
return app.route.getPath(newParams);
|
|
160
225
|
}
|
|
161
226
|
|