180 lines
4.8 KiB
TypeScript
180 lines
4.8 KiB
TypeScript
|
|
/**
|
||
|
|
*/
|
||
|
|
async function do_rental
|
||
|
|
(
|
||
|
|
rental_model : lib_mvc.type_model<mod_model.rental.type_subject>,
|
||
|
|
target_dom : HTMLElement
|
||
|
|
) : Promise<{complex : lib_mvc.type_complex<mod_platform.rental.web.type_state>; element : HTMLElement;}>
|
||
|
|
{
|
||
|
|
const platform : lib_mvc.type_model<mod_platform.rental.web.type_state> = lib_mvc.model_make<mod_platform.rental.web.type_state>
|
||
|
|
(
|
||
|
|
mod_platform.rental.web.make(rental_model)
|
||
|
|
);
|
||
|
|
|
||
|
|
const rental_complex : lib_mvc.type_complex<mod_platform.rental.web.type_state> =
|
||
|
|
{
|
||
|
|
"model": platform,
|
||
|
|
"views":
|
||
|
|
[
|
||
|
|
mod_view.rental.console_.implementation_view(),
|
||
|
|
mod_view.rental.web.implementation_view(),
|
||
|
|
],
|
||
|
|
"controls":
|
||
|
|
[
|
||
|
|
mod_control.rental.implementation_control(),
|
||
|
|
],
|
||
|
|
};
|
||
|
|
await mod_platform.rental.web.setup(platform, target_dom);
|
||
|
|
await lib_mvc.complex_setup<mod_platform.rental.web.type_state>(rental_complex);
|
||
|
|
|
||
|
|
return Promise.resolve<{complex : lib_mvc.type_complex<mod_platform.rental.web.type_state>; element : HTMLElement;}>
|
||
|
|
(
|
||
|
|
{
|
||
|
|
"complex": rental_complex,
|
||
|
|
"element": platform.state.element_dom,
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
async function do_app
|
||
|
|
(
|
||
|
|
app_model : lib_mvc.type_model<mod_model.app.type_subject>,
|
||
|
|
target_dom : HTMLElement
|
||
|
|
) : Promise<{complex : lib_mvc.type_complex<mod_platform.app.web.type_state>; element : HTMLElement;}>
|
||
|
|
{
|
||
|
|
const platform : lib_mvc.type_model<mod_platform.app.web.type_state> = lib_mvc.model_make<mod_platform.app.web.type_state>
|
||
|
|
(
|
||
|
|
mod_platform.app.web.make(app_model)
|
||
|
|
);
|
||
|
|
const app_complex : lib_mvc.type_complex<mod_platform.app.web.type_state> =
|
||
|
|
{
|
||
|
|
"model": platform,
|
||
|
|
"views":
|
||
|
|
[
|
||
|
|
mod_view.app.console_.implementation_view(),
|
||
|
|
mod_view.app.web.implementation_view(),
|
||
|
|
],
|
||
|
|
"controls":
|
||
|
|
[
|
||
|
|
mod_control.app.implementation_control(),
|
||
|
|
],
|
||
|
|
};
|
||
|
|
await mod_platform.app.web.setup(platform, target_dom);
|
||
|
|
await lib_mvc.complex_setup<mod_platform.app.web.type_state>(app_complex);
|
||
|
|
|
||
|
|
return Promise.resolve<{complex : lib_mvc.type_complex<mod_platform.app.web.type_state>; element : HTMLElement;}>
|
||
|
|
(
|
||
|
|
{
|
||
|
|
"complex": app_complex,
|
||
|
|
"element": platform.state.element_dom,
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
*/
|
||
|
|
async function main
|
||
|
|
(
|
||
|
|
) : Promise<void>
|
||
|
|
{
|
||
|
|
// consts
|
||
|
|
const styles : Record<string, mod_view.app.web.type_style> =
|
||
|
|
{
|
||
|
|
"default": {"sheet_name": "style/default.css", "force_language": null},
|
||
|
|
"strogg": {"sheet_name": "style/strogg.css", "force_language": "en"},
|
||
|
|
};
|
||
|
|
|
||
|
|
// args
|
||
|
|
const url : URL = new URL(window.location.href);
|
||
|
|
let parameters : Record<string, string> = {};
|
||
|
|
url.searchParams.forEach((value, key) => {parameters[key] = value;});
|
||
|
|
const style_name : string = (
|
||
|
|
(! ("style" in parameters))
|
||
|
|
? "default"
|
||
|
|
: ((! (parameters["style"] in styles)) ? "default" : parameters["style"])
|
||
|
|
);
|
||
|
|
const language : (null | string) = (parameters["language"] ?? null);
|
||
|
|
|
||
|
|
// vars
|
||
|
|
const style : mod_view.app.web.type_style = styles[style_name];
|
||
|
|
|
||
|
|
// load conf
|
||
|
|
await lib_conf.load("conf.json");
|
||
|
|
|
||
|
|
// setup localization
|
||
|
|
await lib_loc.setup
|
||
|
|
(
|
||
|
|
(
|
||
|
|
[
|
||
|
|
style.force_language,
|
||
|
|
language,
|
||
|
|
navigator.language,
|
||
|
|
]
|
||
|
|
.filter(x => (x !== null))
|
||
|
|
),
|
||
|
|
{
|
||
|
|
"resolve_path": (language => lib_string.coin("localization/{{language}}.json", {"language": language})),
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// setup nextbike api
|
||
|
|
lib_nextbike.setup(lib_conf.get("baseurl"), lib_conf.get("apikey"), {"testmode": true});
|
||
|
|
|
||
|
|
// place stylesheet
|
||
|
|
{
|
||
|
|
let link_dom : HTMLElement = document.createElement("link");
|
||
|
|
link_dom.setAttribute("type", "text/css");
|
||
|
|
link_dom.setAttribute("rel", "stylesheet");
|
||
|
|
link_dom.setAttribute("href", style.sheet_name);
|
||
|
|
document.querySelector("head").appendChild(link_dom);
|
||
|
|
}
|
||
|
|
|
||
|
|
// exec
|
||
|
|
const loginkey : (null | string) = lib_storage.read("nextbike_loginkey");
|
||
|
|
|
||
|
|
let app_model : lib_mvc.type_model<mod_model.app.type_subject> = lib_mvc.model_make<mod_model.app.type_subject>(mod_model.app.inital(loginkey));
|
||
|
|
const app_deed = await do_app(app_model, document.querySelector("body"));
|
||
|
|
|
||
|
|
// sub:rentals
|
||
|
|
{
|
||
|
|
lib_mvc.model_listen<mod_model.app.type_subject, any>
|
||
|
|
(
|
||
|
|
app_model,
|
||
|
|
async (event) =>
|
||
|
|
{
|
||
|
|
switch (event.type)
|
||
|
|
{
|
||
|
|
default:
|
||
|
|
{
|
||
|
|
// do nothing
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "new_rental":
|
||
|
|
{
|
||
|
|
let rental_model : lib_mvc.type_model<mod_model.rental.type_subject> = event.data["rental_model"];
|
||
|
|
let rentals_dom : HTMLElement = app_deed.element.querySelector(".app_rentals");
|
||
|
|
const rental_deed = await do_rental(rental_model, rentals_dom);
|
||
|
|
return Promise.resolve<void>(undefined);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case "end_rental":
|
||
|
|
{
|
||
|
|
let rentals_dom : HTMLElement = app_deed.element.querySelector(".app_rentals");
|
||
|
|
let rental_dom : HTMLElement = document.querySelector
|
||
|
|
(
|
||
|
|
lib_string.coin(".rental[rel=\"{{rel}}\"]", {"rel": event.data["id"]})
|
||
|
|
);
|
||
|
|
rentals_dom.removeChild(rental_dom);
|
||
|
|
return Promise.resolve<void>(undefined);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|