brew-js-react 0.2.2 → 0.2.3

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.
@@ -1,14 +1,17 @@
1
- import { defineAliasProperty, definePrototype, each, extend, makeArray } from "../include/zeta-dom/util.js";
1
+ import { defineAliasProperty, definePrototype, each, extend, kv, makeArray, randomId } from "../include/zeta-dom/util.js";
2
+ import { closeFlyout, openFlyout } from "../include/brew-js/domAction.js";
3
+ import { declareVar, getVar } from "../include/brew-js/var.js";
2
4
  import { app } from "../app.js";
3
5
  import ClassNameMixin from "./ClassNameMixin.js";
4
6
  import FlyoutToggleMixin from "./FlyoutToggleMixin.js";
5
7
 
6
8
  const FlyoutMixinSuper = ClassNameMixin.prototype;
9
+ const varname = '__flyout' + randomId();
7
10
  var flyoutMixinCounter = 0;
8
11
 
9
12
  export default function FlyoutMixin() {
10
13
  var self = this;
11
- ClassNameMixin.call(self, ['open', 'closing', 'tweening-in', 'tweening-out']);
14
+ ClassNameMixin.call(self, ['open', 'closing', 'visible', 'tweening-in', 'tweening-out']);
12
15
  self.modal = false;
13
16
  self.tabThrough = false;
14
17
  self.isFlyoutOpened = false;
@@ -45,10 +48,17 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
45
48
  'animate-out': ''
46
49
  });
47
50
  },
51
+ open: function (value) {
52
+ return openFlyout(this.elements()[0], kv(varname, value));
53
+ },
54
+ close: function (value) {
55
+ return closeFlyout(this.elements()[0], value);
56
+ },
48
57
  onOpen: function (callback) {
58
+ var element = this.elements()[0];
49
59
  return this.onToggleState(function (opened) {
50
60
  if (opened) {
51
- return callback();
61
+ return callback(getVar(element, varname));
52
62
  }
53
63
  });
54
64
  },
@@ -63,6 +73,7 @@ definePrototype(FlyoutMixin, ClassNameMixin, {
63
73
  FlyoutMixinSuper.initElement.call(self, element, state);
64
74
  if (!element.id) {
65
75
  element.id = 'flyout-' + (++flyoutMixinCounter);
76
+ declareVar(element, varname, undefined);
66
77
  }
67
78
  app.on(element, {
68
79
  animationstart: function () {
@@ -1,3 +1,6 @@
1
1
  import ClassNameMixin from "./ClassNameMixin";
2
2
 
3
- export default class FlyoutToggleMixin extends ClassNameMixin { }
3
+ export default class FlyoutToggleMixin extends ClassNameMixin {
4
+ open(state?: any): Promise<any>;
5
+ close(state?: any): Promise<void>;
6
+ }
@@ -9,13 +9,16 @@ export default function FlyoutToggleMixin(mixin) {
9
9
  }
10
10
 
11
11
  definePrototype(FlyoutToggleMixin, ClassNameMixin, {
12
+ open: function (value) {
13
+ return this.flyoutMixin.open(value);
14
+ },
15
+ close: function (value) {
16
+ return this.flyoutMixin.close(value);
17
+ },
12
18
  getCustomAttributes: function () {
13
19
  var element = this.flyoutMixin.elements()[0];
14
20
  return extend({}, FlyoutToggleMixinSuper.getCustomAttributes.call(this), {
15
21
  'toggle': element && ('#' + element.id)
16
22
  });
17
- },
18
- clone: function () {
19
- return extend(FlyoutToggleMixinSuper.clone.call(this), { flyoutMixin: this.flyoutMixin });
20
23
  }
21
24
  });
@@ -1,82 +1,83 @@
1
- import { createPrivateStore, definePrototype, each, inherit, randomId, values } from "../include/zeta-dom/util.js";
2
- import Mixin from "./Mixin.js";
3
-
4
- const _ = createPrivateStore();
5
-
6
- function MixinRefImpl(mixin) {
7
- this.mixin = mixin;
8
- }
9
-
10
- definePrototype(MixinRefImpl, {
11
- getMixin: function () {
12
- return this.mixin;
13
- }
14
- });
15
-
16
- export default function StatefulMixin() {
17
- Mixin.call(this);
18
- _(this, {
19
- states: {},
20
- prefix: '',
21
- counter: 0
22
- });
23
- }
24
-
25
- definePrototype(StatefulMixin, Mixin, {
26
- get ref() {
27
- const self = this;
28
- const state = self.state;
29
- self.next();
30
- return state.ref || (state.ref = new MixinRefImpl(self.clone()));
31
- },
32
- get state() {
33
- const obj = _(this);
34
- const key = obj.prefix + obj.counter;
35
- return obj.states[key] || (obj.states[key] = this.initState());
36
- },
37
- reset: function () {
38
- _(this).counter = 0;
39
- return this;
40
- },
41
- next: function () {
42
- _(this).counter++;
43
- return this;
44
- },
45
- getRef: function () {
46
- const self = this;
47
- const state = self.state;
48
- return function (current) {
49
- if (current && current !== state.element) {
50
- state.element = current;
51
- self.initElement(current, state);
52
- }
53
- };
54
- },
55
- elements: function () {
56
- return values(_(this).states).map(function (v) {
57
- return v.element;
58
- }).filter(function (v) {
59
- return v;
60
- });
61
- },
62
- initState: function () {
63
- return { element: null };
64
- },
65
- initElement: function (element, state) {
66
- },
67
- clone: function () {
68
- const clone = inherit(Object.getPrototypeOf(this));
69
- _(clone, {
70
- states: _(this).states,
71
- prefix: randomId() + '.',
72
- counter: 0
73
- });
74
- return clone;
75
- },
76
- dispose: function () {
77
- var states = _(this).states;
78
- each(states, function (i, v) {
79
- delete states[i];
80
- });
81
- }
82
- });
1
+ import { createPrivateStore, definePrototype, each, inherit, randomId, values } from "../include/zeta-dom/util.js";
2
+ import Mixin from "./Mixin.js";
3
+
4
+ const _ = createPrivateStore();
5
+
6
+ function MixinRefImpl(mixin) {
7
+ this.mixin = mixin;
8
+ }
9
+
10
+ definePrototype(MixinRefImpl, {
11
+ getMixin: function () {
12
+ return this.mixin;
13
+ }
14
+ });
15
+
16
+ export default function StatefulMixin() {
17
+ Mixin.call(this);
18
+ _(this, {
19
+ states: {},
20
+ prefix: '',
21
+ counter: 0
22
+ });
23
+ }
24
+
25
+ definePrototype(StatefulMixin, Mixin, {
26
+ get ref() {
27
+ const self = this;
28
+ const state = self.state;
29
+ self.next();
30
+ return state.ref || (state.ref = new MixinRefImpl(self.clone()));
31
+ },
32
+ get state() {
33
+ const obj = _(this);
34
+ const key = obj.prefix + obj.counter;
35
+ return obj.states[key] || (obj.states[key] = this.initState());
36
+ },
37
+ reset: function () {
38
+ _(this).counter = 0;
39
+ return this;
40
+ },
41
+ next: function () {
42
+ _(this).counter++;
43
+ return this;
44
+ },
45
+ getRef: function () {
46
+ const self = this;
47
+ const state = self.state;
48
+ return function (current) {
49
+ if (current && current !== state.element) {
50
+ state.element = current;
51
+ self.initElement(current, state);
52
+ }
53
+ };
54
+ },
55
+ elements: function () {
56
+ return values(_(this).states).map(function (v) {
57
+ return v.element;
58
+ }).filter(function (v) {
59
+ return v;
60
+ });
61
+ },
62
+ initState: function () {
63
+ return { element: null };
64
+ },
65
+ initElement: function (element, state) {
66
+ },
67
+ clone: function () {
68
+ const self = this;
69
+ const clone = inherit(Object.getPrototypeOf(self), self);
70
+ _(clone, {
71
+ states: _(self).states,
72
+ prefix: randomId() + '.',
73
+ counter: 0
74
+ });
75
+ return clone;
76
+ },
77
+ dispose: function () {
78
+ var states = _(this).states;
79
+ each(states, function (i, v) {
80
+ delete states[i];
81
+ });
82
+ }
83
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brew-js-react",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -40,6 +40,7 @@
40
40
  "cross-env": "^7.0.2",
41
41
  "glob": "^7.2.0",
42
42
  "jest": "^27.0.6",
43
+ "jest-environment-jsdom": "^27.4.6",
43
44
  "ncp": "^2.0.0",
44
45
  "regenerator-runtime": "^0.13.9",
45
46
  "webpack": "^5.3.0",
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, either, extend, grep, isFunction, keys, makeArray, map, noop, pick, randomId, setImmediate } 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";
@@ -27,6 +27,7 @@ function ViewContainer() {
27
27
  }
28
28
  self.componentWillUnmount = app.on('navigate', function () {
29
29
  if (self.mounted && self.getViewComponent()) {
30
+ self.isForceUpdate = true;
30
31
  self.forceUpdate();
31
32
  }
32
33
  });
@@ -84,7 +85,10 @@ definePrototype(ViewContainer, React.Component, {
84
85
  self.currentPath = app.path;
85
86
  self.currentView = view;
86
87
  } else {
87
- app.emit('pageenter', self.currentElement, { pathname: app.path }, true);
88
+ if (self.isForceUpdate) {
89
+ self.isForceUpdate = false;
90
+ app.emit('pageenter', self.currentElement, { pathname: app.path }, true);
91
+ }
88
92
  resolve();
89
93
  }
90
94
  notifyAsync(self.parentElement || root, promise);
@@ -92,10 +96,51 @@ definePrototype(ViewContainer, React.Component, {
92
96
  },
93
97
  getViewComponent: function () {
94
98
  var props = this.props;
95
- return any(props.views, isViewMatched) || (history.state === stateId && void redirectTo(props.defaultView));
99
+ var matched = any(props.views, isViewMatched) || props.defaultView;
100
+ if (history.state === stateId) {
101
+ // ensure the current path actually corresponds to the matched view
102
+ // when some views are not included in the list of allowed views
103
+ var targetPath = linkTo(matched, getCurrentParams(matched, true));
104
+ if (targetPath !== app.path) {
105
+ app.navigate(targetPath, true);
106
+ return;
107
+ }
108
+ }
109
+ return matched;
96
110
  }
97
111
  });
98
112
 
113
+ function getCurrentParams(view, includeAll) {
114
+ var state = routeMap.get(view);
115
+ if (!state.maxParams) {
116
+ var matched = map(app.routes, function (v) {
117
+ var route = app.parseRoute(v);
118
+ var matched = route.length && !any(state.matchers, function (v, i) {
119
+ var pos = route.params[i];
120
+ return (v ? !(pos >= 0) : pos < route.minLength) || (!isFunction(v) && !route.match(i, v));
121
+ });
122
+ return matched ? route : null;
123
+ });
124
+ if (matched[1]) {
125
+ matched = grep(matched, function (v) {
126
+ return !any(v.params, function (v, i) {
127
+ return usedParams[i] && !state.matchers[i];
128
+ });
129
+ });
130
+ }
131
+ if (matched[0]) {
132
+ var last = matched.slice(-1)[0];
133
+ state.maxParams = keys(extend.apply(0, [{}].concat(matched.map(function (v) {
134
+ return v.params;
135
+ }))));
136
+ state.minParams = map(last.params, function (v, i) {
137
+ return state.params[i] || v >= last.minLength ? null : i;
138
+ });
139
+ }
140
+ }
141
+ return pick(app.route, includeAll ? state.maxParams : state.minParams);
142
+ }
143
+
99
144
  export function useViewContainerState() {
100
145
  return React.useContext(StateContext);
101
146
  }
@@ -149,17 +194,11 @@ export function renderView() {
149
194
  }
150
195
 
151
196
  export function linkTo(view, params) {
152
- var viewParams = (routeMap.get(view) || {}).params;
153
- var newParams = {};
154
- for (var i in app.route) {
155
- if (viewParams && i in viewParams) {
156
- newParams[i] = viewParams[i];
157
- } else if (params && i in params) {
158
- newParams[i] = params[i];
159
- } else if (!usedParams[i]) {
160
- newParams[i] = app.route[i];
161
- }
197
+ var state = routeMap.get(view);
198
+ if (!state) {
199
+ return '/';
162
200
  }
201
+ var newParams = extend(getCurrentParams(view), params, state.params);
163
202
  return app.route.getPath(newParams);
164
203
  }
165
204