1.0.29 • Published 9 months ago

@brandup/ui-app v1.0.29

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 months ago

@brandup/ui-app

Lightweight, fast and extensible application framework. Full async support. Weight after minification is less than 10KB!

Build Status

Installation

Install NPM package @brandup/ui-app.

npm i @brandup/ui-app

This package support TypeScript language.

Configure and run application

Configure your application with middlewares and run.

import { ApplicationBuilder } from "@brandup/ui-app";
import pages from "./middlewares/pages";
import "./styles.less";

// Customize application model
interface ExampleApplicationModel extends ApplicationModel {
}

// Customize application type
export class ExampleApplication extends Application<ExampleApplicationModel> {
}

const builder = new ApplicationBuilder<ExampleApplicationModel>();
builder
	.useApp(ExampleApplication)
	.useMiddleware(pages);

const appModel: ExampleApplicationModel = {};
const app = builder.build<ExampleApplicationModel>({ basePath: "/" }, appModel);

app.run({ /*optional context params*/ })
	.then(context: StartContext => { })
	.catch(reason => { });

Default HTMLElement of application is document.body. Set custom element:

const appElement = document.getElementById("app")
app.run({ /*optional context params*/ }, appElement);

Navigation

Example links for application navigation app.nav({ url: "url" }):

<a href="url" class="applink">text</a>
<button data-nav-url="url">text</button>

Replace current url app.nav({ url: "url", replace: true, scope: "replace scope" }):

<a href="url" class="applink" data-nav-replace>text</a>
<a href="url" class="applink" data-nav-replace data-nav-scope="replace scope">text</a>
<button data-nav-url="url" data-nav-replace>text</button>

The element's click event will start a chain of navigate method calls for all middleware.

During navigation and until it is completed, the loading class is added to the element that started the navigation.

Submit form

<form class="appform">
	<input type="text" name="value" />
</form>

The form's submit event will start a chain of submit method calls for all middleware. Class appform is required.

If the form method is GET, then navigation with the form data will start.

Middlewares

Inject to application lifecycle event methods. Middleware methods are called one after another in the order in which they were registered in the ApplicationBuilder.

class PagesMiddlewareImpl implements Middleware {
	name = "pages"; // unique name of this middleware

    start(context: StartContext<ExampleApplication>, next: MiddlewareNext) {
        console.log("start");

		// context.app - access to application members
		// context.data - get or set context data

		return next();
    }

    async loaded(context: StartContext<ExampleApplication>, next: MiddlewareNext) {
        console.log("loaded");

		return next();
    }

    async navigate(context: NavigateContext<ExampleApplication, PageNavigationData>, next: MiddlewareNext) {
        if (context.replace)
            location.replace(context.url);
        else
            location.assign(context.url);

		return next();
    }

    async submit(context: SubmitContext<ExampleApplication>, next: MiddlewareNext) {
        console.log("submit");

		return next();
    }

    async stop(context: StopContext<ExampleApplication>, next: MiddlewareNext) {
        console.log("stop");

		return next();
    }
}

/** External interface of middleware. */
export interface PagesMiddleware {
}

export default () => new PagesMiddlewareImpl();

Example SPA navigation middleware: example/src/frontend/middlewares/pages.ts

Access to middleware

Retrivie middleware by unique name:

const pages = app.middleware<PagesMiddleware>("pages");

Async execution

export class PagesMiddleware implements Middleware {
	async navigate(context: NavigateContext, next: MiddlewareNext) {
        // Exec before next middleware

		await next();

        // Exec after next middleware
    }
}

Redirect navigation

export class AuthMiddleware implements Middleware {
	async navigate(context: NavigateContext, next: MiddlewareNext) {
        await context.redirect({ url: "url" });
    }

	async submit(context: SubmitContext, next: MiddlewareNext) {
        await context.redirect({ url: "url" });
    }
}

The redirect method will always throw an exception with reason NAV_OVERIDE_ERROR constant and end the current navigation.

1.0.29

9 months ago

1.0.28

9 months ago

1.0.26

9 months ago

1.0.27

9 months ago

1.0.25

9 months ago

1.0.24

10 months ago

1.0.23

10 months ago

1.0.22

12 months ago

1.0.21

12 months ago

1.0.20

12 months ago

1.0.19

12 months ago

1.0.18

12 months ago

1.0.17

12 months ago

1.0.16

12 months ago

1.0.14

12 months ago

1.0.13

12 months ago

1.0.12

12 months ago

1.0.11

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago