@dooboostore/dom-render
Version:
html view template engine
132 lines • 4.71 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { EventManager } from '../events/EventManager';
import { ConvertUtils } from '@dooboostore/core/convert/ConvertUtils';
import { Expression } from '@dooboostore/core/expression/Expression';
export class Router {
get searchParamObject() {
return ConvertUtils.toObject(this.getSearchParams());
}
get config() {
return this._config;
}
constructor(config) {
this.changeCallbacks = new Set();
this._config = config;
this._config.window.addEventListener('popstate', () => {
this.changeCallbacks.forEach(it => {
it(this.getRouteData());
});
});
// this.go({path:this.getUrl()});
}
getSearchFirstParamsObject(defaultValue) {
const a = ConvertUtils.toObject(this.getSearchParams(), { firstValue: true });
return Object.assign({}, defaultValue, a);
}
getHashSearchFirstParamsObject() {
return ConvertUtils.toObject(this.getHashSearchParams(), { firstValue: true });
}
addChangeCallback(callback) {
this.changeCallbacks.add(callback);
}
attach() {
return __awaiter(this, void 0, void 0, function* () {
const proxy = this.config.rootObject._DomRender_proxy;
if (proxy) {
const key = `___${EventManager.ROUTER_VARNAME}`;
yield proxy.render(key);
}
});
}
testRegexp(regexp) {
const b = RegExp(regexp).test(this.getPath());
return b;
}
test(urlExpression) {
if (this.getPathData(urlExpression)) {
return true;
}
else {
return false;
}
}
getRouteData(urlExpression) {
const newVar = {
path: this.getPath(),
url: this.getUrl(),
searchParams: this.getSearchParams()
};
const data = this.getData();
if (data) {
newVar.data = data;
}
if (urlExpression) {
const data = this.getPathData(urlExpression);
if (data) {
newVar.pathData = data;
}
}
newVar.router = this;
return Object.freeze(newVar);
}
pushState(data, title, path) {
this.config.window.history.pushState(data, title, path);
}
replaceState(data, title, path) {
this.config.window.history.replaceState(data, title, path);
}
dispatchPopStateEvent() {
this.config.window.dispatchEvent(new Event('popstate'));
}
reload() {
this.config.window.dispatchEvent(new Event('popstate'));
}
getData() {
return this.config.window.history.state;
}
getPathData(urlExpression, currentUrl = this.getPath()) {
return Expression.Path.pathNameData(currentUrl, urlExpression);
}
go(config) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof config === 'string') {
config = { path: config };
}
if (config === null || config === void 0 ? void 0 : config.replace) {
this.replace(config.path, config.data, config.title);
}
else {
this.push(config.path, config.data, config.title);
}
if (!this.config.disableAttach) {
yield this.attach();
}
if (!config.disabledPopEvent) {
this.dispatchPopStateEvent();
}
});
}
toUrl(data) {
var _a;
let targetPath;
if (typeof data === 'string') {
targetPath = data;
}
else {
const tpath = (_a = data.path) !== null && _a !== void 0 ? _a : this.getPath();
const s = data.searchParams ? ConvertUtils.toURLSearchParams(data.searchParams).toString() : '';
// data.searchParams
targetPath = `${tpath}${s.length > 0 ? '?' : ''}${s}`;
}
return targetPath;
}
}
//# sourceMappingURL=Router.js.map