jaxs 0.3.1 → 0.3.2

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/jaxs.js CHANGED
@@ -36,11 +36,9 @@ var setEventsOnElement = (element, events, publish) => {
36
36
  element.eventMaps = eventMaps;
37
37
  };
38
38
  var createNode = (type, document) => {
39
- document = document || window.document;
40
39
  return document.createElement(type);
41
40
  };
42
41
  var createTextNode = (value, document) => {
43
- document = document || window.document;
44
42
  return document.createTextNode(value);
45
43
  };
46
44
  var createDecoratedNode = (type, attributes, events, renderKit) => {
@@ -79,7 +77,7 @@ var namespace = "http://www.w3.org/2000/svg";
79
77
  var isSvgTag = (tagType) => tagType === "svg";
80
78
  var isSvg = (element) => element.namespaceURI === namespace;
81
79
  var createSvgNode = (type, attributes, renderKit) => {
82
- const document = renderKit && renderKit.document || window.document;
80
+ const { document } = renderKit;
83
81
  const node = document.createElementNS(namespace, type);
84
82
  for (const key in attributes) {
85
83
  if (key === "__self" || key === "xmlns")
@@ -1018,7 +1016,7 @@ var extractQueryParams = (queryString) => {
1018
1016
  return aggregate;
1019
1017
  }, {});
1020
1018
  };
1021
- var onLocationChange = (_payload, { publish, state: state2 }) => {
1019
+ var onLocationChange = (_payload, { publish, state: state2, window }) => {
1022
1020
  const { host, pathname, search } = window.location;
1023
1021
  const path = pathname;
1024
1022
  const query = extractQueryParams(search);
@@ -1030,7 +1028,7 @@ var onLocationChange = (_payload, { publish, state: state2 }) => {
1030
1028
  publish(routeChangeEvent, { host, path, query });
1031
1029
  };
1032
1030
  var setupHistory = (app) => {
1033
- const { publish, subscribe, state: state2 } = app;
1031
+ const { publish, subscribe, state: state2, window } = app;
1034
1032
  createRouteState(state2);
1035
1033
  window.addEventListener("popstate", () => publish(locationChangeEvent));
1036
1034
  subscribe(locationChangeEvent, onLocationChange);
@@ -1051,16 +1049,16 @@ var findHref = (node2) => {
1051
1049
  // src/navigation/setupNavigation.js
1052
1050
  var linkNavigationEvent = "goToHref";
1053
1051
  var programmaticNavigationEvent = "navigate";
1054
- var navigate = (path, { publish }) => {
1052
+ var navigate = (path, { publish, window }) => {
1055
1053
  window.history.pushState(null, "", path);
1056
1054
  publish(locationChangeEvent);
1057
1055
  };
1058
- var onLinkClick = (domEvent, { publish }) => {
1056
+ var onLinkClick = (domEvent, { publish, window }) => {
1059
1057
  if (!domEvent || !domEvent.target)
1060
1058
  return;
1061
1059
  domEvent.preventDefault();
1062
1060
  const href = findHref(domEvent.target);
1063
- navigate(href, { publish });
1061
+ navigate(href, { publish, window });
1064
1062
  };
1065
1063
  var setupNavigation = (app) => {
1066
1064
  const { subscribe } = app;
@@ -1081,14 +1079,19 @@ var setupState = (app) => {
1081
1079
  };
1082
1080
  var connectBusToState = (app) => {
1083
1081
  const { bus } = app;
1084
- bus.addListenerOptions({ state: app.state });
1082
+ bus.addListenerOptions({
1083
+ state: app.state,
1084
+ document: app.document,
1085
+ window: app.window
1086
+ });
1085
1087
  };
1086
- var setupRenderKit = (app, document) => {
1088
+ var setupRenderKit = (app) => {
1087
1089
  app.renderKit = {
1088
1090
  publish: app.publish,
1089
1091
  subscribe: app.subscribe,
1090
1092
  state: app.state,
1091
- document
1093
+ document: app.document,
1094
+ window: app.window
1092
1095
  };
1093
1096
  };
1094
1097
  var triggerRoute = (app) => {
@@ -1102,12 +1105,26 @@ var addRender = (app) => {
1102
1105
  return render(template, selector, app.renderKit);
1103
1106
  };
1104
1107
  };
1105
- var createApp = (document = window.document) => {
1108
+ var setupDomEnvironment = (app, domEnvironment) => {
1109
+ const { window, document } = domEnvironment;
1110
+ if (window) {
1111
+ app.window = window;
1112
+ app.document = window.document;
1113
+ } else if (document) {
1114
+ app.window = document.defaultView;
1115
+ app.document = document;
1116
+ } else {
1117
+ app.window = window;
1118
+ app.document = window?.document;
1119
+ }
1120
+ };
1121
+ var createApp = (domEnvironment) => {
1106
1122
  const app = {};
1123
+ setupDomEnvironment(app, domEnvironment || {});
1107
1124
  setupBus(app);
1108
1125
  setupState(app);
1109
1126
  connectBusToState(app);
1110
- setupRenderKit(app, document);
1127
+ setupRenderKit(app);
1111
1128
  setupHistory(app);
1112
1129
  setupNavigation(app);
1113
1130
  triggerRoute(app);
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "jaxs",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Modular J/TSX application framework",
5
5
  "module": "src/jaxs.ts",
6
+ "exports": {
7
+ ".": "./dist/jaxs.js",
8
+ "./views": "./src/views.js"
9
+ },
6
10
  "type": "module",
7
11
  "devDependencies": {
8
12
  "@types/bun": "latest",
package/src/app.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { App, BusPublish, BusSubscribe } from './types';
1
+ import type { App, BusPublish, BusSubscribe, DomEnvironment } from './types';
2
2
  import { createBus } from './messageBus';
3
3
  import { State } from './state'
4
4
  import {
@@ -23,15 +23,20 @@ const setupState = (app: App) => {
23
23
 
24
24
  const connectBusToState = (app: App) => {
25
25
  const { bus } = app;
26
- bus.addListenerOptions({ state: app.state });
26
+ bus.addListenerOptions({
27
+ state: app.state,
28
+ document: app.document,
29
+ window: app.window,
30
+ });
27
31
  };
28
32
 
29
- const setupRenderKit = (app: App, document: Document) => {
33
+ const setupRenderKit = (app: App) => {
30
34
  app.renderKit = {
31
35
  publish: app.publish as BusPublish,
32
36
  subscribe: app.subscribe as BusSubscribe,
33
37
  state: app.state as State,
34
- document,
38
+ document: app.document,
39
+ window: app.window,
35
40
  };
36
41
  };
37
42
 
@@ -48,13 +53,28 @@ const addRender = (app: App) => {
48
53
  };
49
54
  };
50
55
 
51
- export const createApp = (document = window.document) => {
56
+ const setupDomEnvironment = (app: App, domEnvironment: DomEnvironment) => {
57
+ const { window, document } = domEnvironment
58
+ if (window) {
59
+ app.window = window
60
+ app.document = window.document
61
+ } else if (document) {
62
+ app.window = document.defaultView as Window
63
+ app.document = document
64
+ } else {
65
+ app.window = window
66
+ app.document = window?.document
67
+ }
68
+ }
69
+
70
+ export const createApp = (domEnvironment: DomEnvironment | undefined) => {
52
71
  const app = {} as App;
72
+ setupDomEnvironment(app, domEnvironment || {} as DomEnvironment)
53
73
 
54
74
  setupBus(app);
55
75
  setupState(app);
56
76
  connectBusToState(app);
57
- setupRenderKit(app, document);
77
+ setupRenderKit(app);
58
78
  setupHistory(app);
59
79
  setupNavigation(app);
60
80
  triggerRoute(app);
@@ -15,7 +15,7 @@ export const extractQueryParams = (queryString) => {
15
15
  }, {})
16
16
  }
17
17
 
18
- export const onLocationChange = (_payload, { publish, state }) => {
18
+ export const onLocationChange = (_payload, { publish, state, window }) => {
19
19
  const { host, pathname, search } = window.location
20
20
  const path = pathname
21
21
  const query = extractQueryParams(search)
@@ -31,7 +31,7 @@ export const onLocationChange = (_payload, { publish, state }) => {
31
31
  }
32
32
 
33
33
  export const setupHistory = (app) => {
34
- const { publish, subscribe, state } = app
34
+ const { publish, subscribe, state, window } = app
35
35
  createRouteState(state)
36
36
  window.addEventListener('popstate', () => publish(locationChangeEvent))
37
37
  subscribe(locationChangeEvent, onLocationChange)
@@ -4,17 +4,17 @@ import { locationChangeEvent } from './setupHistory'
4
4
  export const linkNavigationEvent = 'goToHref'
5
5
  export const programmaticNavigationEvent = 'navigate'
6
6
 
7
- export const navigate = (path, { publish }) => {
7
+ export const navigate = (path, { publish, window }) => {
8
8
  window.history.pushState(null, '', path)
9
9
  publish(locationChangeEvent)
10
10
  }
11
11
 
12
- export const onLinkClick = (domEvent, { publish }) => {
12
+ export const onLinkClick = (domEvent, { publish, window }) => {
13
13
  if (!domEvent || !domEvent.target) return
14
14
  domEvent.preventDefault()
15
15
 
16
16
  const href = findHref(domEvent.target)
17
- navigate(href, { publish })
17
+ navigate(href, { publish, window })
18
18
  }
19
19
 
20
20
  export const setupNavigation = (app) => {
@@ -45,12 +45,10 @@ export const setEventsOnElement = (
45
45
  };
46
46
 
47
47
  export const createNode = (type: string, document: Document) => {
48
- document = document || window.document;
49
48
  return document.createElement(type);
50
49
  };
51
50
 
52
51
  export const createTextNode = (value: string, document: Document) => {
53
- document = document || window.document;
54
52
  return document.createTextNode(value);
55
53
  };
56
54
 
@@ -5,7 +5,7 @@ export const isSvgTag = (tagType: string) => tagType === 'svg'
5
5
  export const isSvg = (element: SVGElement) => element.namespaceURI === namespace
6
6
 
7
7
  export const createSvgNode = (type: string, attributes: Attributes, renderKit: RenderKit) => {
8
- const document = renderKit && renderKit.document || window.document;
8
+ const { document } = renderKit
9
9
  const node = document.createElementNS(namespace, type)
10
10
 
11
11
  for (const key in attributes) {
package/src/types.ts CHANGED
@@ -4,6 +4,7 @@ export type DomEventPublisher = (eventName: string, domEvent: Event) => void;
4
4
 
5
5
  export type RenderKit = {
6
6
  document: Document;
7
+ window: Window;
7
8
  publish: DomEventPublisher;
8
9
  subscribe: BusSubscribe;
9
10
  state: State;
@@ -149,4 +150,11 @@ export type App = {
149
150
  state?: State
150
151
  renderKit: RenderKit;
151
152
  render: (template: Template, selector: string) => Template;
153
+ window: Window;
154
+ document: Document;
152
155
  };
156
+
157
+ export type DomEnvironment = {
158
+ document?: Document;
159
+ window?: Window;
160
+ }