arc-ux 0.0.1
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/LICENSE +24 -0
- package/README.md +2 -0
- package/package.json +35 -0
- package/src/ArcUX.js +165 -0
- package/src/Form.js +53 -0
- package/src/FrameworkComponents/App.jsx +104 -0
- package/src/FrameworkComponents/ModalLayer.jsx +38 -0
- package/src/Html.js +40 -0
- package/src/withArcUX.js +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "arc-ux",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A purely functional router for independently evaluating a path against a list of routes",
|
|
5
|
+
"main": "src/ArcUX.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
|
|
9
|
+
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watchAll"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/anyuzer/arc-ux.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"react",
|
|
17
|
+
"ux",
|
|
18
|
+
"arc"
|
|
19
|
+
],
|
|
20
|
+
"author": "Ian Reid",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/anyuzer/arc-ux/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/anyuzer/arc-ux#readme",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"jest": "^30.2.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"arc-lib": "^9.2.0",
|
|
31
|
+
"react": "^19.2.4",
|
|
32
|
+
"react-dom": "^19.2.4",
|
|
33
|
+
"styled-components": "^6.3.10"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/ArcUX.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import {ArcRouter, ArcEvents, Log, is} from "arc-lib";
|
|
2
|
+
import {createRoot} from "react-dom/client";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import Html from "./Html.js";
|
|
5
|
+
import {ThemeProvider} from "styled-components";
|
|
6
|
+
import App from "./FrameworkComponents/App.jsx";
|
|
7
|
+
import Form from "./Form.js";
|
|
8
|
+
|
|
9
|
+
class ArcUX {
|
|
10
|
+
#routeMap = {};
|
|
11
|
+
#RouteRenderer;
|
|
12
|
+
#initialRoute;
|
|
13
|
+
|
|
14
|
+
#Html;
|
|
15
|
+
|
|
16
|
+
#handlers = {};
|
|
17
|
+
|
|
18
|
+
#themes = {};
|
|
19
|
+
#currentTheme;
|
|
20
|
+
#apis = {};
|
|
21
|
+
#environment = 'production';
|
|
22
|
+
#rootPath = '/cloud';
|
|
23
|
+
#keyVal = {};
|
|
24
|
+
|
|
25
|
+
#forms = {};
|
|
26
|
+
|
|
27
|
+
constructor() {
|
|
28
|
+
this.#RouteRenderer = new ArcRouter(this.#routeMap);
|
|
29
|
+
this.#Html = new Html;
|
|
30
|
+
|
|
31
|
+
Object.getOwnPropertyNames(Object.getPrototypeOf(this)).forEach(n => {
|
|
32
|
+
if(n === "constructor") { return; }
|
|
33
|
+
this[n] = this[n].bind(this)
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
//Turn our Singleton into an event emitter
|
|
37
|
+
ArcEvents.mixin(this);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//Forms
|
|
41
|
+
getForm(_form) {
|
|
42
|
+
if(!this.#forms[_form]) {
|
|
43
|
+
this.#forms[_form] = new Form;
|
|
44
|
+
}
|
|
45
|
+
return this.#forms[_form];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//Simplest shared data state ever
|
|
49
|
+
setKeyVal(_key, _value, _suppressEmit=false) {
|
|
50
|
+
this.#keyVal[_key] = _value;
|
|
51
|
+
if(!_suppressEmit) {
|
|
52
|
+
Log.dRed(`Emitting: ${_key}`, [_value]);
|
|
53
|
+
this.emit(_key, [_value]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getKeyVal(_key) {
|
|
58
|
+
return this.#keyVal[_key];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
setResolverVariables(_environment, _rootPath) {
|
|
62
|
+
this.#environment = _environment;
|
|
63
|
+
this.#rootPath = _rootPath;
|
|
64
|
+
|
|
65
|
+
this.#Html.setEnvironment(_environment);
|
|
66
|
+
this.#Html.setPathToClientBundle(`${this.#rootPath}/assets/${this.#environment}.client.js`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
addAPI(_name, _API) {
|
|
70
|
+
this.#apis[_name] = _API;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
API(_name){
|
|
74
|
+
return this.#apis[_name];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
addTheme(_theme, _setCurrent=false) {
|
|
78
|
+
this.#themes[_theme.name] = _theme;
|
|
79
|
+
if(_setCurrent || !this.#currentTheme) {
|
|
80
|
+
this.#currentTheme = _theme.name;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getAvailableThemes() {
|
|
85
|
+
return Object.keys(this.#themes);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
getCurrentTheme() {
|
|
89
|
+
return this.#themes[this.#currentTheme];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
addHandler(_handler, _Component) {
|
|
93
|
+
this.#handlers[_handler] = _Component;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getHandler(_handler) {
|
|
97
|
+
return this.#handlers[_handler];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
bindRoute(_route, _Component, _Shell=null) {
|
|
101
|
+
this.#routeMap[_route] = { Component: _Component, Shell: _Shell };
|
|
102
|
+
this.#RouteRenderer.setMap(this.#routeMap);
|
|
103
|
+
Log.dPink(`Bind Route?`, this.#routeMap);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
renderRoute(_route) {
|
|
107
|
+
const routeData = this.#RouteRenderer.travel(_route)
|
|
108
|
+
Log.dRed('RouteData?', routeData);
|
|
109
|
+
Log.dYellow('Router?', this.#RouteRenderer);
|
|
110
|
+
if (routeData.match) {
|
|
111
|
+
return routeData.match;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
Log.dGreen('NotFound?', this.#handlers);
|
|
115
|
+
|
|
116
|
+
if(this.getHandler('NotFound')){
|
|
117
|
+
return this.getHandler('NotFound');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
resolveClient(){
|
|
122
|
+
const rootTheme = this.getCurrentTheme();
|
|
123
|
+
const root = createRoot(document.getElementById('app'));
|
|
124
|
+
root.render(
|
|
125
|
+
<ThemeProvider theme={rootTheme || {name:undefined}}>
|
|
126
|
+
{rootTheme.globalStyle ? <rootTheme.globalStyle /> : null}
|
|
127
|
+
{rootTheme.svgDefinitions ? <rootTheme.svgDefinitions /> : null}
|
|
128
|
+
<App $initialRoute={this.#initialRoute} />
|
|
129
|
+
</ThemeProvider>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
htmlAddToHead(_headElement){
|
|
134
|
+
if(is(_headElement) === 'array'){
|
|
135
|
+
_headElement.forEach(_element => this.#Html.addHeadElement(_element));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
this.#Html.addHeadElement(_headElement)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
loadPage(_route, _suppressEmit=false) {
|
|
142
|
+
this.setKeyVal('route', _route, _suppressEmit);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
renderModal(_Modal, _props) {
|
|
146
|
+
this.emit('modal', [_Modal, _props || {}]);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
closeModal() {
|
|
150
|
+
this.emit('modal:close', []);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async serveHtml(_ctx, _next, _routeData) {
|
|
154
|
+
try{
|
|
155
|
+
_ctx.response.status = 200;
|
|
156
|
+
_ctx.response.body = this.#Html.getString();
|
|
157
|
+
} catch (e) {
|
|
158
|
+
Log.catch(e);
|
|
159
|
+
_ctx.response.status = 500;
|
|
160
|
+
_ctx.response.body = {message: e.message};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export default new ArcUX;
|
package/src/Form.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {ArcEvents} from "arc-lib";
|
|
2
|
+
|
|
3
|
+
class Form {
|
|
4
|
+
#form = {};
|
|
5
|
+
#modifiers = {};
|
|
6
|
+
#validChecks = {};
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
ArcEvents.mixin(this);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
removeField(_name) {
|
|
13
|
+
delete this.#form[_name];
|
|
14
|
+
delete this.#modifiers[_name];
|
|
15
|
+
delete this.#validChecks[_name];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
initField(_name, _value, _modifier, _isValid=true) {
|
|
19
|
+
this.#form[_name] = _value;
|
|
20
|
+
this.#modifiers[_name] = _modifier;
|
|
21
|
+
this.#validChecks[_name] = _isValid;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getField(_name) {
|
|
25
|
+
return this.#form[_name];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
updateField(_name, _value, _isValid) {
|
|
29
|
+
this.#form[_name] = _value;
|
|
30
|
+
this.#validChecks[_name] = _isValid;
|
|
31
|
+
this.emit('change', [this]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
modifyField(_name, _value) {
|
|
35
|
+
this.#modifiers[_name](_value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getData(){
|
|
39
|
+
return this.#form;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
isValid() {
|
|
43
|
+
let isValid = true;
|
|
44
|
+
Object.keys(this.#validChecks).forEach((_key) => {
|
|
45
|
+
if(!this.#validChecks[_key]){
|
|
46
|
+
isValid = false;
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
return isValid;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default Form;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import withArcUX from "../withArcUX.js";
|
|
3
|
+
import ModalLayer from "./ModalLayer.jsx";
|
|
4
|
+
import { Log } from "arc-lib";
|
|
5
|
+
|
|
6
|
+
class App extends React.Component {
|
|
7
|
+
#routeListener;
|
|
8
|
+
#modalListener;
|
|
9
|
+
#modalCloseListener;
|
|
10
|
+
constructor(props) {
|
|
11
|
+
super(props);
|
|
12
|
+
this.state = {
|
|
13
|
+
loading: true,
|
|
14
|
+
target: null,
|
|
15
|
+
shell: null,
|
|
16
|
+
loadedRoute: props.ArcUX.getKeyVal('route'),
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this.#routeListener = props.ArcUX.on('route', this.loadRoute.bind(this))
|
|
20
|
+
this.#modalListener = props.ArcUX.on('modal', this.renderModal.bind(this))
|
|
21
|
+
this.#modalCloseListener = props.ArcUX.on('modal:close', this.closeModal.bind(this))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async componentDidMount() {
|
|
25
|
+
const initialRoute = this.props.ArcUX.getKeyVal('route');
|
|
26
|
+
|
|
27
|
+
Log.dGreen(`Our initial route is ${initialRoute}`);
|
|
28
|
+
const match = this.props.ArcUX.renderRoute(initialRoute);
|
|
29
|
+
if(match){
|
|
30
|
+
this.setState({
|
|
31
|
+
loading: false,
|
|
32
|
+
target: match.Component,
|
|
33
|
+
shell: match.Shell
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async componentWillUnmount() {
|
|
39
|
+
if(this.#routeListener){
|
|
40
|
+
this.props.ArcUX.clear(this.#routeListener);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
render() {
|
|
45
|
+
const AppLoader = this.props.ArcUX.getHandler('AppLoader');
|
|
46
|
+
const Shell = this.state.shell;
|
|
47
|
+
|
|
48
|
+
if(AppLoader && this.state.loading){
|
|
49
|
+
return Shell ? <Shell><AppLoader {...this.props} /></Shell> : <AppLoader {...this.props} />;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const RoutedComponent = this.state.target;
|
|
53
|
+
const Modal = this.state.modal ? this.state.modal.modal : null;
|
|
54
|
+
if(Shell){
|
|
55
|
+
return (
|
|
56
|
+
<React.Fragment>
|
|
57
|
+
<Shell><RoutedComponent {...this.props} /></Shell>
|
|
58
|
+
{Modal ? <ModalLayer><Modal {...this.state.modal.props} /></ModalLayer> : null}
|
|
59
|
+
</React.Fragment>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<React.Fragment>
|
|
65
|
+
<RoutedComponent {...this.props} />
|
|
66
|
+
{Modal ? <ModalLayer><Modal {...this.state.modal.props} /></ModalLayer> : null}
|
|
67
|
+
</React.Fragment>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
loadRoute(_newRoute) {
|
|
72
|
+
Log.dRed('New route called?', [_newRoute])
|
|
73
|
+
const match = this.props.ArcUX.renderRoute(_newRoute);
|
|
74
|
+
if(!match){
|
|
75
|
+
Log.dRed('Not Found handler missing');
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const fullHistoryString = decodeURI(_newRoute);
|
|
79
|
+
window.history.pushState({ code: fullHistoryString }, "", fullHistoryString);
|
|
80
|
+
this.setState({
|
|
81
|
+
loading: false,
|
|
82
|
+
target: match.Component,
|
|
83
|
+
shell: match.Shell,
|
|
84
|
+
loadedRoute: _newRoute
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
renderModal(_Modal, _props) {
|
|
89
|
+
this.setState({
|
|
90
|
+
modal: {
|
|
91
|
+
modal: _Modal,
|
|
92
|
+
props: _props ||{}
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
closeModal() {
|
|
98
|
+
this.setState({
|
|
99
|
+
modal: null
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export default withArcUX(App);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import React, { Fragment } from 'react';
|
|
2
|
+
import withArcUX from "../withArcUX.js";
|
|
3
|
+
|
|
4
|
+
class ModalLayer extends React.Component {
|
|
5
|
+
render() {
|
|
6
|
+
return (
|
|
7
|
+
<div style={{
|
|
8
|
+
position: 'fixed',
|
|
9
|
+
zIndex: 800,
|
|
10
|
+
top: 0,
|
|
11
|
+
right: 0,
|
|
12
|
+
bottom: 0,
|
|
13
|
+
left: 0,
|
|
14
|
+
justifyContent: 'center',
|
|
15
|
+
alignItems: 'center',
|
|
16
|
+
display: "flex",
|
|
17
|
+
alignContent: "stretch"
|
|
18
|
+
}}>
|
|
19
|
+
<div
|
|
20
|
+
onClick={() => { this.props.ArcUX.emit('modal:close', []); }}
|
|
21
|
+
style={{
|
|
22
|
+
position: 'absolute',
|
|
23
|
+
top: 0,
|
|
24
|
+
right: 0,
|
|
25
|
+
bottom: 0,
|
|
26
|
+
left: 0,
|
|
27
|
+
backgroundColor: 'black',
|
|
28
|
+
cursor: 'pointer',
|
|
29
|
+
opacity: 0.7
|
|
30
|
+
}}
|
|
31
|
+
/>
|
|
32
|
+
{this.props.children}
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default withArcUX(ModalLayer);
|
package/src/Html.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
class Html {
|
|
2
|
+
#headElements = [];
|
|
3
|
+
#pathToClientBundle = '';
|
|
4
|
+
#environment = 'production';
|
|
5
|
+
|
|
6
|
+
addHeadElement(_element) {
|
|
7
|
+
this.#headElements.push(_element);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
setPathToClientBundle(_pathToClientBundle) {
|
|
11
|
+
this.#pathToClientBundle = _pathToClientBundle;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
setEnvironment(_environment) {
|
|
15
|
+
this.#environment = _environment;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getString() {
|
|
19
|
+
return (
|
|
20
|
+
`<!DOCTYPE html>
|
|
21
|
+
<head>
|
|
22
|
+
${this.#headElements.join('\r\n')}
|
|
23
|
+
<script>
|
|
24
|
+
window.app = ${JSON.stringify({
|
|
25
|
+
environment: this.#environment
|
|
26
|
+
})};
|
|
27
|
+
</script>
|
|
28
|
+
</head>
|
|
29
|
+
<body style="margin:0">
|
|
30
|
+
<!-- Our app container -->
|
|
31
|
+
<div id="app" style="display:flex;flex-direction: column;min-height: calc(100vh)"></div>
|
|
32
|
+
|
|
33
|
+
<!-- Our isomorphic bundle -->
|
|
34
|
+
<script id="appBundle" src="${this.#pathToClientBundle}"></script>
|
|
35
|
+
</body>
|
|
36
|
+
</html>`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default Html;
|
package/src/withArcUX.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ArcUX from "./ArcUX.js";
|
|
3
|
+
|
|
4
|
+
const withArcUX = (_WrappedComponent) => {
|
|
5
|
+
return function ArcUXComponent(props) {
|
|
6
|
+
return <_WrappedComponent {...props} theme={ArcUX.getCurrentTheme()} ArcUX={ArcUX} />
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default withArcUX;
|