brew-js-react 0.1.11 → 0.2.0

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.
@@ -175,6 +175,7 @@ __webpack_require__.d(src_namespaceObject, {
175
175
  "Mixin": () => (Mixin),
176
176
  "ScrollableMixin": () => (ScrollableMixin),
177
177
  "StatefulMixin": () => (StatefulMixin),
178
+ "ViewStateContainer": () => (ViewStateContainer),
178
179
  "createDialog": () => (createDialog),
179
180
  "default": () => (src),
180
181
  "isViewMatched": () => (isViewMatched),
@@ -196,7 +197,8 @@ __webpack_require__.d(src_namespaceObject, {
196
197
  "useMixinRef": () => (useMixinRef),
197
198
  "useRouteParam": () => (useRouteParam),
198
199
  "useRouteState": () => (useRouteState),
199
- "useScrollableMixin": () => (useScrollableMixin)
200
+ "useScrollableMixin": () => (useScrollableMixin),
201
+ "useViewContainerState": () => (useViewContainerState)
200
202
  });
201
203
 
202
204
  // EXTERNAL MODULE: external {"commonjs":"brew-js","commonjs2":"brew-js","amd":"brew-js","root":"brew"}
@@ -458,6 +460,8 @@ function Dialog(props) {
458
460
  }, [dialog]);
459
461
  return /*#__PURE__*/external_commonjs_react_dom_commonjs2_react_dom_amd_react_dom_root_ReactDOM_.createPortal(props.children, dialog.root);
460
462
  }
463
+ // EXTERNAL MODULE: external "zeta-dom-react"
464
+ var external_zeta_dom_react_ = __webpack_require__(103);
461
465
  ;// CONCATENATED MODULE: ./tmp/brew-js/defaults.js
462
466
 
463
467
  var defaults_defaultExport = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.defaults;
@@ -476,11 +480,193 @@ install('react', function (app_) {
476
480
  app_app = app_;
477
481
  });
478
482
  brew_js_defaults.react = true;
483
+ ;// CONCATENATED MODULE: ./tmp/brew-js/anim.js
484
+
485
+ var animateIn = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.animateIn,
486
+ animateOut = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.animateOut,
487
+ addAnimateIn = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.addAnimateIn,
488
+ addAnimateOut = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.addAnimateOut;
489
+
490
+ ;// CONCATENATED MODULE: ./src/include/brew-js/anim.js
491
+
492
+ ;// CONCATENATED MODULE: ./src/view.js
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+ var routeMap = new Map();
501
+ var usedParams = {};
502
+ var StateContext = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createContext(Object.freeze({
503
+ active: true
504
+ }));
505
+ var stateId;
506
+
507
+ function ViewContainer() {
508
+ /** @type {any} */
509
+ var self = this;
510
+ external_commonjs_react_commonjs2_react_amd_react_root_React_.Component.apply(self, arguments);
511
+ self.mounted = false;
512
+
513
+ if (!stateId) {
514
+ stateId = history.state;
515
+ app_app.on('navigate', function () {
516
+ stateId = history.state;
517
+ });
518
+ }
519
+
520
+ self.componentWillUnmount = app_app.on('navigate', function () {
521
+ if (self.mounted && self.getViewComponent()) {
522
+ self.forceUpdate();
523
+ }
524
+ });
525
+ }
526
+
527
+ definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react_root_React_.Component, {
528
+ componentDidMount: function componentDidMount() {
529
+ this.mounted = true;
530
+ },
531
+ componentDidCatch: function componentDidCatch(error) {
532
+ zeta_dom_dom.emit('error', this.parentElement || zeta_dom_dom.root, {
533
+ error: error
534
+ }, true);
535
+ },
536
+ render: function render() {
537
+ /** @type {any} */
538
+ var self = this;
539
+ var V = self.getViewComponent();
540
+
541
+ if (V && V !== self.currentViewComponent) {
542
+ self.currentViewComponent = V;
543
+
544
+ if (self.currentView && self.currentElement) {
545
+ self.prevView = self.currentView;
546
+ self.prevElement = self.currentElement;
547
+ self.currentElement = undefined;
548
+ animateOut(self.prevElement, 'show').then(function () {
549
+ self.prevElement = undefined;
550
+ self.prevView = undefined;
551
+ self.forceUpdate();
552
+ });
553
+ }
554
+
555
+ var providerProps = {
556
+ key: routeMap.get(V).id,
557
+ value: {}
558
+ };
559
+ var view = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(StateContext.Provider, providerProps, /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(ViewStateContainer, null, /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(V, {
560
+ rootProps: self.props.rootProps,
561
+ onComponentLoaded: function onComponentLoaded(element) {
562
+ self.currentElement = element;
563
+ self.parentElement = element.parentElement;
564
+ util_setImmediate(function () {
565
+ return animateIn(element, 'show');
566
+ });
567
+ }
568
+ })));
569
+ defineGetterProperty(providerProps.value, 'active', function () {
570
+ return self.currentView === view;
571
+ });
572
+ self.currentView = view;
573
+ }
574
+
575
+ return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(external_commonjs_react_commonjs2_react_amd_react_root_React_.Fragment, null, self.prevView, self.currentView);
576
+ },
577
+ getViewComponent: function getViewComponent() {
578
+ var props = this.props;
579
+ return any(props.views, isViewMatched) || history.state === stateId && void redirectTo(props.defaultView);
580
+ }
581
+ });
582
+ function useViewContainerState() {
583
+ return external_commonjs_react_commonjs2_react_amd_react_root_React_.useContext(StateContext);
584
+ }
585
+ function isViewMatched(view) {
586
+ var params = routeMap.get(view);
587
+ return !!params && false === any(params.matchers, function (v, i) {
588
+ var value = app_app.route[i] || '';
589
+ return isFunction(v) ? !v(value) : (v || '') !== value;
590
+ });
591
+ }
592
+ function registerView(factory, routeParams) {
593
+ var Component = function Component(props) {
594
+ var Component = (0,external_zeta_dom_react_.useAsync)(factory)[0];
595
+ return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement('div', extend({}, props.rootProps, {
596
+ ref: function ref(element) {
597
+ if (element && Component) {
598
+ (props.onComponentLoaded || noop)(element);
599
+ }
600
+ },
601
+ children: Component && /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(Component["default"])
602
+ }));
603
+ };
604
+
605
+ routeParams = extend({}, routeParams);
606
+ each(routeParams, function (i, v) {
607
+ usedParams[i] = true;
608
+
609
+ if (v instanceof RegExp) {
610
+ routeParams[i] = v.test.bind(v);
611
+ }
612
+ });
613
+ routeMap.set(Component, {
614
+ id: randomId(),
615
+ matchCount: keys(routeParams).length,
616
+ matchers: routeParams,
617
+ params: pick(routeParams, function (v) {
618
+ return typeof v === 'string';
619
+ })
620
+ });
621
+ return Component;
622
+ }
623
+ function renderView() {
624
+ var views = makeArray(arguments);
625
+ var rootProps = isFunction(views[0]) ? {} : views.shift();
626
+ var defaultView = views[0];
627
+ views.sort(function (a, b) {
628
+ return (routeMap.get(b) || {}).matchCount - (routeMap.get(a) || {}).matchCount;
629
+ });
630
+ return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(ViewContainer, {
631
+ rootProps: rootProps,
632
+ views: views,
633
+ defaultView: defaultView
634
+ });
635
+ }
636
+ function linkTo(view, params) {
637
+ var viewParams = (routeMap.get(view) || {}).params;
638
+ var newParams = {};
639
+
640
+ for (var i in app_app.route) {
641
+ if (viewParams && i in viewParams) {
642
+ newParams[i] = viewParams[i];
643
+ } else if (params && i in params) {
644
+ newParams[i] = params[i];
645
+ } else if (!usedParams[i]) {
646
+ newParams[i] = app_app.route[i];
647
+ }
648
+ }
649
+
650
+ return app_app.route.getPath(newParams);
651
+ }
652
+ function navigateTo(view, params) {
653
+ return app_app.navigate(linkTo(view, params));
654
+ }
655
+ function redirectTo(view, params) {
656
+ return app_app.navigate(linkTo(view, params), true);
657
+ }
479
658
  ;// CONCATENATED MODULE: ./src/hooks.js
480
659
 
481
660
 
482
661
 
662
+
663
+
483
664
  var states = {};
665
+
666
+ function getCurrentStates() {
667
+ return states[history.state] || (states[history.state] = {});
668
+ }
669
+
484
670
  function useAppReady() {
485
671
  var sReady = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(false);
486
672
  var ready = sReady[0],
@@ -493,6 +679,7 @@ function useAppReady() {
493
679
  return ready;
494
680
  }
495
681
  function useRouteParam(name, defaultValue) {
682
+ var container = useViewContainerState();
496
683
  var route = app_app.route;
497
684
  var sValue = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(route[name]);
498
685
  var value = sValue[0],
@@ -503,7 +690,13 @@ function useRouteParam(name, defaultValue) {
503
690
  setValue(current);
504
691
 
505
692
  if (name in route) {
506
- return route.watch(name, setValue);
693
+ return route.watch(name, function (value) {
694
+ util_setImmediate(function () {
695
+ if (container.active) {
696
+ setValue(value);
697
+ }
698
+ });
699
+ });
507
700
  }
508
701
 
509
702
  console.error('Route parameter ' + name + ' does not exist');
@@ -516,13 +709,45 @@ function useRouteParam(name, defaultValue) {
516
709
  return value || '';
517
710
  }
518
711
  function useRouteState(key, defaultValue) {
519
- var cur = states[history.state] || (states[history.state] = {});
712
+ var container = useViewContainerState();
713
+ var cur = getCurrentStates();
520
714
  var state = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(key in cur ? cur[key] : defaultValue);
521
- cur[key] = state[0];
715
+
716
+ if (container.active) {
717
+ cur[key] = state[0];
718
+ }
719
+
522
720
  return state;
523
721
  }
524
- // EXTERNAL MODULE: external "zeta-dom-react"
525
- var external_zeta_dom_react_ = __webpack_require__(103);
722
+ function ViewStateContainer(props) {
723
+ var container = useViewContainerState();
724
+ var provider = (0,external_commonjs_react_commonjs2_react_amd_react_root_React_.useState)(function () {
725
+ var cache = {};
726
+ return {
727
+ getState: function getState(uniqueId, key) {
728
+ var cur = getCurrentStates();
729
+ var state = cache[uniqueId] || (cache[uniqueId] = {
730
+ value: cur[key] && cur[key].value,
731
+ get: function get() {
732
+ return state.value;
733
+ },
734
+ set: function set(value) {
735
+ state.value = value;
736
+ }
737
+ });
738
+
739
+ if (container.active) {
740
+ cur[key] = state;
741
+ }
742
+
743
+ return state;
744
+ }
745
+ };
746
+ })[0];
747
+ return /*#__PURE__*/(0,external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement)(external_zeta_dom_react_.ViewStateProvider, {
748
+ value: provider
749
+ }, props.children);
750
+ }
526
751
  // EXTERNAL MODULE: ./src/include/external/waterpipe.js
527
752
  var waterpipe = __webpack_require__(43);
528
753
  ;// CONCATENATED MODULE: ./src/i18n.js
@@ -1238,160 +1463,6 @@ function useMixinRef(mixin) {
1238
1463
  return mixin && mixin.getMixin().reset();
1239
1464
  }
1240
1465
 
1241
- ;// CONCATENATED MODULE: ./tmp/brew-js/anim.js
1242
-
1243
- var animateIn = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.animateIn,
1244
- animateOut = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.animateOut,
1245
- addAnimateIn = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.addAnimateIn,
1246
- addAnimateOut = external_commonjs_brew_js_commonjs2_brew_js_amd_brew_js_root_brew_.addAnimateOut;
1247
-
1248
- ;// CONCATENATED MODULE: ./src/include/brew-js/anim.js
1249
-
1250
- ;// CONCATENATED MODULE: ./src/view.js
1251
-
1252
-
1253
-
1254
-
1255
-
1256
- var routeMap = new Map();
1257
- var usedParams = {};
1258
- var stateId;
1259
-
1260
- function ViewContainer() {
1261
- /** @type {any} */
1262
- var self = this;
1263
- external_commonjs_react_commonjs2_react_amd_react_root_React_.Component.apply(self, arguments);
1264
- self.mounted = false;
1265
-
1266
- if (!stateId) {
1267
- stateId = history.state;
1268
- app_app.on('navigate', function () {
1269
- stateId = history.state;
1270
- });
1271
- }
1272
-
1273
- self.componentWillUnmount = app_app.on('navigate', function () {
1274
- if (self.mounted && self.getViewComponent()) {
1275
- self.forceUpdate();
1276
- }
1277
- });
1278
- }
1279
-
1280
- definePrototype(ViewContainer, external_commonjs_react_commonjs2_react_amd_react_root_React_.Component, {
1281
- componentDidMount: function componentDidMount() {
1282
- this.mounted = true;
1283
- },
1284
- render: function render() {
1285
- /** @type {any} */
1286
- var self = this;
1287
- var V = self.getViewComponent();
1288
-
1289
- if (V && V !== self.currentViewComponent) {
1290
- self.currentViewComponent = V;
1291
-
1292
- if (self.currentView && self.currentElement) {
1293
- self.prevView = self.currentView;
1294
- self.prevElement = self.currentElement;
1295
- self.currentElement = undefined;
1296
- animateOut(self.prevElement, 'show').then(function () {
1297
- self.prevElement = undefined;
1298
- self.prevView = undefined;
1299
- self.forceUpdate();
1300
- });
1301
- }
1302
-
1303
- self.currentView = /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(V, {
1304
- key: routeMap.get(V).id,
1305
- rootProps: self.props.rootProps,
1306
- onComponentLoaded: function onComponentLoaded(element) {
1307
- self.currentElement = element;
1308
- util_setImmediate(function () {
1309
- return animateIn(element, 'show');
1310
- });
1311
- }
1312
- });
1313
- }
1314
-
1315
- return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(external_commonjs_react_commonjs2_react_amd_react_root_React_.Fragment, null, self.prevView, self.currentView);
1316
- },
1317
- getViewComponent: function getViewComponent() {
1318
- var props = this.props;
1319
- return any(props.views, isViewMatched) || history.state === stateId && void redirectTo(props.defaultView);
1320
- }
1321
- });
1322
- function isViewMatched(view) {
1323
- var params = routeMap.get(view);
1324
- return !!params && false === any(params.matchers, function (v, i) {
1325
- var value = app_app.route[i] || '';
1326
- return isFunction(v) ? !v(value) : (v || '') !== value;
1327
- });
1328
- }
1329
- function registerView(factory, routeParams) {
1330
- var Component = function Component(props) {
1331
- var Component = (0,external_zeta_dom_react_.useAsync)(factory)[0];
1332
- return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement('div', extend({}, props.rootProps, {
1333
- ref: function ref(element) {
1334
- if (element && Component) {
1335
- (props.onComponentLoaded || noop)(element);
1336
- }
1337
- },
1338
- children: Component && /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(Component["default"])
1339
- }));
1340
- };
1341
-
1342
- routeParams = extend({}, routeParams);
1343
- each(routeParams, function (i, v) {
1344
- usedParams[i] = true;
1345
-
1346
- if (v instanceof RegExp) {
1347
- routeParams[i] = v.test.bind(v);
1348
- }
1349
- });
1350
- routeMap.set(Component, {
1351
- id: randomId(),
1352
- matchCount: keys(routeParams).length,
1353
- matchers: routeParams,
1354
- params: pick(routeParams, function (v) {
1355
- return typeof v === 'string';
1356
- })
1357
- });
1358
- return Component;
1359
- }
1360
- function renderView() {
1361
- var views = makeArray(arguments);
1362
- var rootProps = isFunction(views[0]) ? {} : views.shift();
1363
- var defaultView = views[0];
1364
- views.sort(function (a, b) {
1365
- return (routeMap.get(b) || {}).matchCount - (routeMap.get(a) || {}).matchCount;
1366
- });
1367
- return /*#__PURE__*/external_commonjs_react_commonjs2_react_amd_react_root_React_.createElement(ViewContainer, {
1368
- rootProps: rootProps,
1369
- views: views,
1370
- defaultView: defaultView
1371
- });
1372
- }
1373
- function linkTo(view, params) {
1374
- var viewParams = (routeMap.get(view) || {}).params;
1375
- var newParams = {};
1376
-
1377
- for (var i in app_app.route) {
1378
- if (viewParams && i in viewParams) {
1379
- newParams[i] = viewParams[i];
1380
- } else if (params && i in params) {
1381
- newParams[i] = params[i];
1382
- } else if (!usedParams[i]) {
1383
- newParams[i] = app_app.route[i];
1384
- }
1385
- }
1386
-
1387
- return app_app.route.getPath(newParams);
1388
- }
1389
- function navigateTo(view, params) {
1390
- return app_app.navigate(linkTo(view, params));
1391
- }
1392
- function redirectTo(view, params) {
1393
- return app_app.navigate(linkTo(view, params), true);
1394
- }
1395
1466
  ;// CONCATENATED MODULE: ./src/index.js
1396
1467
 
1397
1468
  /* harmony default export */ const src = (brew_js_app);