@brandup/ui-app 1.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/README.md +116 -0
- package/dist/cjs/index.js +886 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/mjs/index.js +880 -0
- package/dist/mjs/index.js.map +1 -0
- package/dist/types.d.ts +226 -0
- package/package.json +50 -0
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# brandup-ui-app
|
|
2
|
+
|
|
3
|
+
[]()
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install NPM package [@brandup/ui-app](https://www.npmjs.com/package/@brandup/ui-app).
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm i @brandup/ui-app@latest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Configure and run application
|
|
14
|
+
|
|
15
|
+
Configure your application with middlewares and run.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
import { ApplicationBuilder } from "@brandup/ui-app";
|
|
19
|
+
import { PagesMiddleware } from "./middlewares/pages";
|
|
20
|
+
import "./styles.less";
|
|
21
|
+
|
|
22
|
+
// Customize application model
|
|
23
|
+
interface ExampleApplicationModel extends ApplicationModel {
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Customize application type
|
|
27
|
+
export class ExampleApplication extends Application<ExampleApplicationModel> {
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const builder = new ApplicationBuilder<ExampleApplicationModel>();
|
|
31
|
+
builder
|
|
32
|
+
.useApp(ExampleApplication)
|
|
33
|
+
.useMiddleware(() => new PagesMiddleware());
|
|
34
|
+
|
|
35
|
+
const appModel: ExampleApplicationModel = {};
|
|
36
|
+
const app = builder.build<ExampleApplicationModel>({ basePath: "/" }, appModel);
|
|
37
|
+
|
|
38
|
+
app.run({ ...optional context params })
|
|
39
|
+
.then(navContext => { })
|
|
40
|
+
.catch(reason => { });
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Default HTMLElement of application is `document.body`. Set custom element:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
const appElement = document.getElementById("app")
|
|
47
|
+
app.run({ ...optional context params }, appElement);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Middlewares
|
|
51
|
+
|
|
52
|
+
Inject to application lifecycle event methods. Middleware methods are called one after another in the order in which they were registered in the `ApplicationBuilder`.
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
export class PagesMiddleware implements Middleware {
|
|
56
|
+
readonly name: string = "pages"; // unique name of current middleware
|
|
57
|
+
|
|
58
|
+
start(context: StartContext<ExampleApplication>, next: MiddlewareNext) {
|
|
59
|
+
console.log("start");
|
|
60
|
+
|
|
61
|
+
// context.app - access to application members
|
|
62
|
+
// context.data - get or set context data
|
|
63
|
+
|
|
64
|
+
return next();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async loaded(context: StartContext<ExampleApplication>, next: MiddlewareNext) {
|
|
68
|
+
console.log("loaded");
|
|
69
|
+
|
|
70
|
+
return next();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async navigate(context: NavigateContext<ExampleApplication, PageNavigationData>, next: MiddlewareNext) {
|
|
74
|
+
if (context.replace)
|
|
75
|
+
location.replace(context.url);
|
|
76
|
+
else
|
|
77
|
+
location.assign(context.url);
|
|
78
|
+
|
|
79
|
+
return next();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async submit(context: SubmitContext<ExampleApplication>, next: MiddlewareNext) {
|
|
83
|
+
console.log("submit");
|
|
84
|
+
|
|
85
|
+
return next();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async stop(context: StopContext<ExampleApplication>, next: MiddlewareNext) {
|
|
89
|
+
console.log("stop");
|
|
90
|
+
|
|
91
|
+
return next();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Example SPA navigation middleware: [example/src/frontend/middlewares/pages.ts](/example/src/frontend/middlewares/pages.ts)
|
|
97
|
+
|
|
98
|
+
Retrivie middleware by unique name:
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
const middleware = app.middleware<PagesMiddleware>("pages");
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Async middleware execution
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
export class PagesMiddleware implements Middleware {
|
|
108
|
+
async navigate(context: NavigateContext, next: MiddlewareNext) {
|
|
109
|
+
// Exec before next middleware
|
|
110
|
+
|
|
111
|
+
await next();
|
|
112
|
+
|
|
113
|
+
// Exec after next middleware
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|