brew-js-react 0.2.3 → 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/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, either, extend, grep, isFunction, keys, makeArray, map, 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;
@@ -63,7 +64,7 @@ definePrototype(ViewContainer, React.Component, {
63
64
  }
64
65
  var providerProps = {
65
66
  key: routeMap.get(V).id,
66
- value: {}
67
+ value: { view: V }
67
68
  };
68
69
  var view = React.createElement(StateContext.Provider, providerProps,
69
70
  React.createElement(ViewStateContainer, null,
@@ -103,19 +104,19 @@ definePrototype(ViewContainer, React.Component, {
103
104
  var targetPath = linkTo(matched, getCurrentParams(matched, true));
104
105
  if (targetPath !== app.path) {
105
106
  app.navigate(targetPath, true);
106
- return;
107
107
  }
108
108
  }
109
109
  return matched;
110
110
  }
111
111
  });
112
112
 
113
- function getCurrentParams(view, includeAll) {
113
+ function getCurrentParams(view, includeAll, params) {
114
114
  var state = routeMap.get(view);
115
115
  if (!state.maxParams) {
116
+ var matchers = exclude(state.matchers, ['remainingSegments']);
116
117
  var matched = map(app.routes, function (v) {
117
118
  var route = app.parseRoute(v);
118
- var matched = route.length && !any(state.matchers, function (v, i) {
119
+ var matched = route.length && !any(matchers, function (v, i) {
119
120
  var pos = route.params[i];
120
121
  return (v ? !(pos >= 0) : pos < route.minLength) || (!isFunction(v) && !route.match(i, v));
121
122
  });
@@ -123,8 +124,8 @@ function getCurrentParams(view, includeAll) {
123
124
  });
124
125
  if (matched[1]) {
125
126
  matched = grep(matched, function (v) {
126
- return !any(v.params, function (v, i) {
127
- return usedParams[i] && !state.matchers[i];
127
+ return !single(v.params, function (v, i) {
128
+ return usedParams[i] && !matchers[i];
128
129
  });
129
130
  });
130
131
  }
@@ -138,21 +139,42 @@ function getCurrentParams(view, includeAll) {
138
139
  });
139
140
  }
140
141
  }
141
- return pick(app.route, includeAll ? state.maxParams : state.minParams);
142
+ return pick(params || app.route, includeAll ? state.maxParams : state.minParams);
142
143
  }
143
144
 
144
- export function useViewContainerState() {
145
- return React.useContext(StateContext);
145
+ function sortViews(a, b) {
146
+ return (routeMap.get(b) || {}).matchCount - (routeMap.get(a) || {}).matchCount;
146
147
  }
147
148
 
148
- export function isViewMatched(view) {
149
+ function matchViewParams(view, route) {
149
150
  var params = routeMap.get(view);
150
- return !!params && false === any(params.matchers, function (v, i) {
151
- var value = app.route[i] || '';
151
+ return !!params && !single(params.matchers, function (v, i) {
152
+ var value = route[i] || '';
152
153
  return isFunction(v) ? !v(value) : (v || '') !== value;
153
154
  });
154
155
  }
155
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
+
156
178
  export function registerView(factory, routeParams) {
157
179
  var Component = function (props) {
158
180
  var state = useAsync(factory);
@@ -180,6 +202,8 @@ export function registerView(factory, routeParams) {
180
202
  return typeof v === 'string';
181
203
  })
182
204
  });
205
+ sortedViews.push(Component);
206
+ sortedViews.sort(sortViews);
183
207
  return Component;
184
208
  }
185
209
 
@@ -187,9 +211,7 @@ export function renderView() {
187
211
  var views = makeArray(arguments);
188
212
  var rootProps = isFunction(views[0]) ? {} : views.shift();
189
213
  var defaultView = views[0];
190
- views.sort(function (a, b) {
191
- return (routeMap.get(b) || {}).matchCount - (routeMap.get(a) || {}).matchCount;
192
- });
214
+ views.sort(sortViews);
193
215
  return React.createElement(ViewContainer, { rootProps, views, defaultView });
194
216
  }
195
217
 
@@ -198,7 +220,7 @@ export function linkTo(view, params) {
198
220
  if (!state) {
199
221
  return '/';
200
222
  }
201
- var newParams = extend(getCurrentParams(view), params, state.params);
223
+ var newParams = extend(getCurrentParams(view), getCurrentParams(view, true, params), state.params);
202
224
  return app.route.getPath(newParams);
203
225
  }
204
226