117 lines
3.6 KiB
JavaScript
117 lines
3.6 KiB
JavaScript
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
var tipagemRotas_exports = {};
|
|
__export(tipagemRotas_exports, {
|
|
TipagemRotas: () => TipagemRotas
|
|
});
|
|
module.exports = __toCommonJS(tipagemRotas_exports);
|
|
class TipagemRotas {
|
|
/** Ao criar novo obijeto de tipagem de rota é necessário passar o caminho parcial
|
|
** export const mCaminho = new TipagemRotas<{q:string}>("aplicacao","funcionalidade")
|
|
*/
|
|
constructor({
|
|
caminho,
|
|
acaoIr,
|
|
rotulo
|
|
}) {
|
|
this._partesCaminho = [];
|
|
this._acaoIr = acaoIr;
|
|
this._partesCaminho = (Array.isArray(caminho) ? caminho : [caminho]).filter(Boolean).map((a) => String(a)).flatMap((a) => a.split("/")).filter(Boolean);
|
|
this.rotulo = rotulo;
|
|
}
|
|
/** Retorna o caminho completo da rota
|
|
** console.log(mCaminho.caminho)
|
|
** "/caminho"
|
|
*/
|
|
get caminho() {
|
|
const ret = `/${this._partesCaminho.join("/")}`;
|
|
return ret;
|
|
}
|
|
/** Define o caminho completo da rota
|
|
** mCaminho.caminho = "/novoCaminho"
|
|
** console.log(mCaminho.caminho)
|
|
** "/novoCaminho"
|
|
** */
|
|
set caminho(caminhoParcial) {
|
|
this._partesCaminho = caminhoParcial.split("/").filter((parte) => parte);
|
|
}
|
|
/** Retorna o caminho completo da rota com a query
|
|
** console.log(mCaminho.resolve({q:"query"}))
|
|
** "http://localhost:3000/caminho?q=query"
|
|
*/
|
|
endereco(query, usarComoHash) {
|
|
const url = new URL(
|
|
typeof window !== "undefined" ? window.location.href : "http://localhost"
|
|
);
|
|
url.pathname = this.caminho;
|
|
url.search = "";
|
|
const queryKeys = Object.entries(query);
|
|
for (const [key, value] of queryKeys) {
|
|
url.searchParams.set(String(key), JSON.stringify(value));
|
|
}
|
|
url.hash = "";
|
|
if (usarComoHash) {
|
|
url.hash = `#${url.search}`;
|
|
url.search = "";
|
|
}
|
|
return url.href;
|
|
}
|
|
/** Vai para a url
|
|
** mCaminho.ir({q:"query"})
|
|
** window.location.href = "http://localhost:3000/caminho?q=query"
|
|
*/
|
|
ir(query) {
|
|
if (this._acaoIr) {
|
|
this._acaoIr(this.endereco(query));
|
|
} else {
|
|
if (typeof window != "undefined") {
|
|
window.location.href = this.endereco(query);
|
|
}
|
|
}
|
|
}
|
|
/** Retorna os parametros da url
|
|
** console.log(mCaminho.parametros())
|
|
** {q:"query"}
|
|
*/
|
|
parametros(urlEntrada) {
|
|
const url = urlEntrada ? new URL(urlEntrada) : new URL(
|
|
typeof window !== "undefined" ? window.location.href : "http://localhost"
|
|
);
|
|
const query = url.searchParams;
|
|
const queryObj = Object.fromEntries(query.entries());
|
|
const hash = url.hash;
|
|
if (hash) {
|
|
const hashObj = Object.fromEntries(
|
|
new URLSearchParams(hash.slice(1)).entries()
|
|
);
|
|
return { ...queryObj, ...hashObj };
|
|
}
|
|
for (const chave in queryObj) {
|
|
try {
|
|
queryObj[chave] = JSON.parse(queryObj[chave]);
|
|
} catch {
|
|
}
|
|
}
|
|
return queryObj;
|
|
}
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
TipagemRotas
|
|
});
|