1020 lines
32 KiB
JavaScript
1020 lines
32 KiB
JavaScript
(function (ventana, documento) {
|
|
"use strict";
|
|
|
|
var eventosRegistrados = false;
|
|
var espacioSvg = "http://www.w3.org/2000/svg";
|
|
var promesaGoogleMaps = null;
|
|
|
|
function texto(elemento, valor) {
|
|
if (!elemento) {
|
|
return;
|
|
}
|
|
|
|
elemento.textContent = valor || "";
|
|
elemento.style.display = valor ? "" : "none";
|
|
}
|
|
|
|
function extension(nombreArchivo) {
|
|
var nombre = (nombreArchivo || "").toLowerCase();
|
|
var indice = nombre.lastIndexOf(".");
|
|
return indice >= 0 ? nombre.substring(indice + 1) : "";
|
|
}
|
|
|
|
function detectarTipo(nombreArchivo, tipoContenido) {
|
|
var tipo = (tipoContenido || "").toLowerCase();
|
|
var ext = extension(nombreArchivo);
|
|
|
|
if (tipo === "application/pdf" || ext === "pdf") {
|
|
return "pdf";
|
|
}
|
|
|
|
if (tipo.indexOf("image/") === 0 ||
|
|
["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"].indexOf(ext) >= 0) {
|
|
return "imagen";
|
|
}
|
|
|
|
if (ext === "gis" || ext === "geojson") {
|
|
return "gis";
|
|
}
|
|
|
|
if (["txt", "log", "csv", "xml", "json"].indexOf(ext) >= 0 ||
|
|
tipo.indexOf("text/") === 0 ||
|
|
tipo.indexOf("application/json") === 0 ||
|
|
tipo.indexOf("application/xml") === 0 ||
|
|
tipo.indexOf("text/xml") === 0) {
|
|
return "texto";
|
|
}
|
|
|
|
if (["doc", "docx", "docm", "dot", "dotx", "dotm", "rtf", "xls", "xlsx", "xlsm", "xlsb", "xlt", "xltx", "xltm", "ppt", "pptx", "pptm", "pps", "ppsx", "ppsm", "pot", "potx", "potm", "odt", "ods", "odp"].indexOf(ext) >= 0 ||
|
|
tipo.indexOf("officedocument") >= 0 ||
|
|
tipo.indexOf("msword") >= 0 ||
|
|
tipo.indexOf("ms-excel") >= 0 ||
|
|
tipo.indexOf("ms-powerpoint") >= 0 ||
|
|
tipo.indexOf("opendocument") >= 0) {
|
|
return "office";
|
|
}
|
|
|
|
return "no-compatible";
|
|
}
|
|
|
|
function emitir(control, nombreEvento, detalle) {
|
|
var evento;
|
|
|
|
if (typeof ventana.CustomEvent === "function") {
|
|
evento = new CustomEvent(nombreEvento, {
|
|
bubbles: true,
|
|
detail: detalle || {}
|
|
});
|
|
} else {
|
|
evento = documento.createEvent("CustomEvent");
|
|
evento.initCustomEvent(nombreEvento, true, false, detalle || {});
|
|
}
|
|
|
|
control.dispatchEvent(evento);
|
|
}
|
|
|
|
function abortarSolicitud(control) {
|
|
if (control && control._sigVisualizadorSolicitud) {
|
|
try {
|
|
control._sigVisualizadorSolicitud.abort();
|
|
} catch (error) {
|
|
// La solicitud pudo haber terminado antes de cancelarse.
|
|
}
|
|
control._sigVisualizadorSolicitud = null;
|
|
}
|
|
}
|
|
|
|
function liberarObjectUrl(control) {
|
|
if (control && control._sigVisualizadorObjectUrl) {
|
|
try {
|
|
ventana.URL.revokeObjectURL(control._sigVisualizadorObjectUrl);
|
|
} catch (error) {
|
|
// La limpieza del object URL no debe interrumpir el visor.
|
|
}
|
|
control._sigVisualizadorObjectUrl = null;
|
|
}
|
|
}
|
|
|
|
function liberarMapaGis(control) {
|
|
if (!control || !control._sigVisualizadorMapaGis) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (ventana.google && ventana.google.maps && ventana.google.maps.event) {
|
|
if (control._sigVisualizadorMapaGis.data) {
|
|
ventana.google.maps.event.clearInstanceListeners(
|
|
control._sigVisualizadorMapaGis.data);
|
|
}
|
|
|
|
ventana.google.maps.event.clearInstanceListeners(
|
|
control._sigVisualizadorMapaGis);
|
|
}
|
|
} catch (error) {
|
|
// La limpieza del mapa no debe interrumpir el visor.
|
|
}
|
|
|
|
control._sigVisualizadorMapaGis = null;
|
|
}
|
|
|
|
function limpiarVista(control) {
|
|
var pdf;
|
|
var imagen;
|
|
var textoPlano;
|
|
var gis;
|
|
var mensaje;
|
|
|
|
if (!control) {
|
|
return;
|
|
}
|
|
|
|
abortarSolicitud(control);
|
|
liberarObjectUrl(control);
|
|
liberarMapaGis(control);
|
|
|
|
pdf = control.querySelector("[data-sig-visualizador-pdf]");
|
|
imagen = control.querySelector("[data-sig-visualizador-imagen]");
|
|
textoPlano = control.querySelector("[data-sig-visualizador-texto]");
|
|
gis = control.querySelector("[data-sig-visualizador-gis]");
|
|
mensaje = control.querySelector("[data-sig-visualizador-mensaje]");
|
|
|
|
if (pdf) {
|
|
pdf.removeAttribute("src");
|
|
pdf.style.display = "none";
|
|
}
|
|
|
|
if (imagen) {
|
|
imagen.removeAttribute("src");
|
|
imagen.style.display = "none";
|
|
}
|
|
|
|
if (textoPlano) {
|
|
textoPlano.textContent = "";
|
|
textoPlano.style.display = "none";
|
|
}
|
|
|
|
if (gis) {
|
|
while (gis.firstChild) {
|
|
gis.removeChild(gis.firstChild);
|
|
}
|
|
gis.classList.remove("sig-visualizador-archivo-gis-google");
|
|
gis.style.display = "none";
|
|
}
|
|
|
|
if (mensaje) {
|
|
mensaje.style.display = "none";
|
|
}
|
|
}
|
|
|
|
function mostrarMensaje(control, icono, mensaje) {
|
|
var contenedor = control.querySelector("[data-sig-visualizador-mensaje]");
|
|
var iconoElemento = contenedor ?
|
|
contenedor.querySelector(".sig-visualizador-archivo-mensaje-icono") : null;
|
|
var textoElemento = control.querySelector("[data-sig-visualizador-mensaje-texto]");
|
|
|
|
if (!contenedor) {
|
|
return;
|
|
}
|
|
|
|
if (iconoElemento) {
|
|
iconoElemento.className = "sig-visualizador-archivo-mensaje-icono fa " + icono;
|
|
}
|
|
|
|
if (textoElemento) {
|
|
textoElemento.textContent = mensaje;
|
|
}
|
|
|
|
contenedor.style.display = "flex";
|
|
}
|
|
|
|
function aplicarAcciones(control, datos) {
|
|
var descargar = control.querySelector("[data-sig-visualizador-descargar]");
|
|
var expandir = control.querySelector("[data-sig-visualizador-expandir]");
|
|
var permitirDescarga = control.getAttribute("data-sig-visualizador-permitir-descarga") !== "0";
|
|
var permitirExpandir = control.getAttribute("data-sig-visualizador-permitir-expandir") !== "0";
|
|
|
|
if (descargar) {
|
|
descargar.style.display = permitirDescarga && datos.urlDescarga ? "" : "none";
|
|
if (datos.urlDescarga) {
|
|
descargar.setAttribute("href", datos.urlDescarga);
|
|
} else {
|
|
descargar.removeAttribute("href");
|
|
}
|
|
}
|
|
|
|
if (expandir) {
|
|
expandir.style.display = permitirExpandir ? "" : "none";
|
|
}
|
|
}
|
|
|
|
function crearSolicitud(control, url, tipoRespuesta, alCompletar, alError) {
|
|
var solicitud = new XMLHttpRequest();
|
|
control._sigVisualizadorSolicitud = solicitud;
|
|
|
|
solicitud.open("GET", url, true);
|
|
solicitud.withCredentials = true;
|
|
|
|
if (tipoRespuesta) {
|
|
solicitud.responseType = tipoRespuesta;
|
|
}
|
|
|
|
solicitud.onreadystatechange = function () {
|
|
if (solicitud.readyState !== 4) {
|
|
return;
|
|
}
|
|
|
|
if (control._sigVisualizadorSolicitud === solicitud) {
|
|
control._sigVisualizadorSolicitud = null;
|
|
}
|
|
|
|
if (solicitud.status >= 200 && solicitud.status < 300) {
|
|
alCompletar(solicitud);
|
|
return;
|
|
}
|
|
|
|
alError(solicitud);
|
|
};
|
|
|
|
solicitud.onerror = function () {
|
|
if (control._sigVisualizadorSolicitud === solicitud) {
|
|
control._sigVisualizadorSolicitud = null;
|
|
}
|
|
alError(solicitud);
|
|
};
|
|
|
|
solicitud.send();
|
|
return solicitud;
|
|
}
|
|
|
|
function mostrarPdf(control, url) {
|
|
var pdf = control.querySelector("[data-sig-visualizador-pdf]");
|
|
if (!pdf) {
|
|
return;
|
|
}
|
|
|
|
pdf.setAttribute("src", url);
|
|
pdf.style.display = "block";
|
|
}
|
|
|
|
function mostrarImagen(control, datos) {
|
|
var imagen = control.querySelector("[data-sig-visualizador-imagen]");
|
|
if (!imagen) {
|
|
return;
|
|
}
|
|
|
|
imagen.setAttribute("src", datos.url);
|
|
imagen.setAttribute(
|
|
"alt",
|
|
datos.titulo || datos.nombreArchivo || "Vista previa del archivo");
|
|
imagen.style.display = "block";
|
|
}
|
|
|
|
function mensajeErrorSolicitud(solicitud, predeterminado) {
|
|
var encabezado;
|
|
|
|
if (!solicitud || typeof solicitud.getResponseHeader !== "function") {
|
|
return predeterminado;
|
|
}
|
|
|
|
encabezado = solicitud.getResponseHeader("X-SIG-Visualizador-Mensaje");
|
|
if (!encabezado) {
|
|
return predeterminado;
|
|
}
|
|
|
|
try {
|
|
return decodeURIComponent(encabezado.replace(/\+/g, " "));
|
|
} catch (error) {
|
|
return predeterminado;
|
|
}
|
|
}
|
|
|
|
function cargarOffice(control, datos) {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-spinner fa-spin",
|
|
"Generando vista previa del archivo de Office...");
|
|
|
|
crearSolicitud(
|
|
control,
|
|
datos.url,
|
|
"blob",
|
|
function (solicitud) {
|
|
var tipo = (solicitud.getResponseHeader("Content-Type") || "").toLowerCase();
|
|
var blob = solicitud.response;
|
|
|
|
if (!blob ||
|
|
(tipo.indexOf("application/pdf") < 0 &&
|
|
tipo.indexOf("text/html") < 0)) {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-file-o",
|
|
"No fue posible generar la vista previa de este archivo de Office. Puede descargar el archivo original.");
|
|
return;
|
|
}
|
|
|
|
limpiarVista(control);
|
|
control._sigVisualizadorObjectUrl = ventana.URL.createObjectURL(blob);
|
|
mostrarPdf(control, control._sigVisualizadorObjectUrl);
|
|
},
|
|
function (solicitud) {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-file-o",
|
|
mensajeErrorSolicitud(
|
|
solicitud,
|
|
"No fue posible generar la vista previa de este archivo de Office. Puede descargar el archivo original."));
|
|
});
|
|
}
|
|
|
|
function normalizarTextoVistaPrevia(nombreArchivo, contenido) {
|
|
var ext = extension(nombreArchivo);
|
|
var valor = String(contenido || "").replace(/^\uFEFF/, "");
|
|
|
|
if (ext === "json") {
|
|
try {
|
|
return JSON.stringify(JSON.parse(valor), null, 2);
|
|
} catch (error) {
|
|
return valor;
|
|
}
|
|
}
|
|
|
|
return valor;
|
|
}
|
|
|
|
function cargarTexto(control, datos) {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-spinner fa-spin",
|
|
"Cargando vista previa del archivo...");
|
|
|
|
crearSolicitud(
|
|
control,
|
|
datos.url,
|
|
"text",
|
|
function (solicitud) {
|
|
var pre = control.querySelector("[data-sig-visualizador-texto]");
|
|
if (!pre) {
|
|
return;
|
|
}
|
|
|
|
limpiarVista(control);
|
|
pre.textContent = normalizarTextoVistaPrevia(
|
|
datos.nombreArchivo,
|
|
solicitud.responseText);
|
|
pre.style.display = "block";
|
|
},
|
|
function () {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-file-text-o",
|
|
"No fue posible cargar la vista previa del archivo de texto. Puede descargar el archivo.");
|
|
});
|
|
}
|
|
|
|
function esNumero(valor) {
|
|
return typeof valor === "number" && isFinite(valor);
|
|
}
|
|
|
|
function recorrerCoordenadas(valor, callback) {
|
|
var indice;
|
|
|
|
if (!Array.isArray(valor)) {
|
|
return;
|
|
}
|
|
|
|
if (valor.length >= 2 && esNumero(valor[0]) && esNumero(valor[1])) {
|
|
callback(valor[0], valor[1]);
|
|
return;
|
|
}
|
|
|
|
for (indice = 0; indice < valor.length; indice++) {
|
|
recorrerCoordenadas(valor[indice], callback);
|
|
}
|
|
}
|
|
|
|
function obtenerNombreFeature(feature, indice) {
|
|
var propiedades = feature && feature.properties ? feature.properties : {};
|
|
return propiedades.Nombre ||
|
|
propiedades.nombre ||
|
|
propiedades.Numero ||
|
|
propiedades.numero ||
|
|
propiedades.Lote ||
|
|
propiedades.lote ||
|
|
(feature && feature.id !== undefined && feature.id !== null
|
|
? String(feature.id)
|
|
: "Polígono " + (indice + 1));
|
|
}
|
|
|
|
function validarGeoJson(valor) {
|
|
var features;
|
|
var validas = [];
|
|
|
|
if (!valor || valor.type !== "FeatureCollection" || !Array.isArray(valor.features)) {
|
|
throw new Error("El archivo GIS no contiene un GeoJSON FeatureCollection válido.");
|
|
}
|
|
|
|
features = valor.features;
|
|
features.forEach(function (feature) {
|
|
var geometria = feature && feature.geometry;
|
|
if (!geometria) {
|
|
return;
|
|
}
|
|
|
|
if (geometria.type === "Polygon" || geometria.type === "MultiPolygon") {
|
|
validas.push(feature);
|
|
}
|
|
});
|
|
|
|
if (!validas.length) {
|
|
throw new Error("El archivo GIS no contiene geometrías Polygon o MultiPolygon para visualizar.");
|
|
}
|
|
|
|
return validas;
|
|
}
|
|
|
|
function crearProyeccion(features, ancho, alto, margen) {
|
|
var minX = Infinity;
|
|
var minY = Infinity;
|
|
var maxX = -Infinity;
|
|
var maxY = -Infinity;
|
|
|
|
features.forEach(function (feature) {
|
|
recorrerCoordenadas(feature.geometry.coordinates, function (x, y) {
|
|
minX = Math.min(minX, x);
|
|
minY = Math.min(minY, y);
|
|
maxX = Math.max(maxX, x);
|
|
maxY = Math.max(maxY, y);
|
|
});
|
|
});
|
|
|
|
if (!isFinite(minX) || !isFinite(minY) || !isFinite(maxX) || !isFinite(maxY)) {
|
|
throw new Error("No fue posible obtener coordenadas válidas del archivo GIS.");
|
|
}
|
|
|
|
var rangoX = Math.max(maxX - minX, 0.0000001);
|
|
var rangoY = Math.max(maxY - minY, 0.0000001);
|
|
var escala = Math.min(
|
|
(ancho - (margen * 2)) / rangoX,
|
|
(alto - (margen * 2)) / rangoY);
|
|
var anchoDibujo = rangoX * escala;
|
|
var altoDibujo = rangoY * escala;
|
|
var offsetX = (ancho - anchoDibujo) / 2;
|
|
var offsetY = (alto - altoDibujo) / 2;
|
|
|
|
return function (x, y) {
|
|
return {
|
|
x: offsetX + ((x - minX) * escala),
|
|
y: alto - offsetY - ((y - minY) * escala)
|
|
};
|
|
};
|
|
}
|
|
|
|
function anilloAPath(anillo, proyectar) {
|
|
var partes = [];
|
|
|
|
(anillo || []).forEach(function (coordenada, indice) {
|
|
if (!Array.isArray(coordenada) || coordenada.length < 2 ||
|
|
!esNumero(coordenada[0]) || !esNumero(coordenada[1])) {
|
|
return;
|
|
}
|
|
|
|
var punto = proyectar(coordenada[0], coordenada[1]);
|
|
partes.push((indice === 0 ? "M" : "L") + punto.x.toFixed(2) + " " + punto.y.toFixed(2));
|
|
});
|
|
|
|
if (partes.length) {
|
|
partes.push("Z");
|
|
}
|
|
|
|
return partes.join(" ");
|
|
}
|
|
|
|
function geometriaAPath(geometria, proyectar) {
|
|
var partes = [];
|
|
var poligonos;
|
|
|
|
if (geometria.type === "Polygon") {
|
|
poligonos = [geometria.coordinates];
|
|
} else if (geometria.type === "MultiPolygon") {
|
|
poligonos = geometria.coordinates;
|
|
} else {
|
|
return "";
|
|
}
|
|
|
|
poligonos.forEach(function (poligono) {
|
|
(poligono || []).forEach(function (anillo) {
|
|
var path = anilloAPath(anillo, proyectar);
|
|
if (path) {
|
|
partes.push(path);
|
|
}
|
|
});
|
|
});
|
|
|
|
return partes.join(" ");
|
|
}
|
|
|
|
function centroFeature(feature, proyectar) {
|
|
var sumaX = 0;
|
|
var sumaY = 0;
|
|
var total = 0;
|
|
|
|
recorrerCoordenadas(feature.geometry.coordinates, function (x, y) {
|
|
var punto = proyectar(x, y);
|
|
sumaX += punto.x;
|
|
sumaY += punto.y;
|
|
total++;
|
|
});
|
|
|
|
return total > 0
|
|
? { x: sumaX / total, y: sumaY / total }
|
|
: null;
|
|
}
|
|
|
|
function renderizarGisSvg(control, valor) {
|
|
var contenedor = control.querySelector("[data-sig-visualizador-gis]");
|
|
var features = validarGeoJson(valor);
|
|
var ancho = 1000;
|
|
var alto = 650;
|
|
var margen = 35;
|
|
var proyectar = crearProyeccion(features, ancho, alto, margen);
|
|
var svg;
|
|
|
|
if (!contenedor) {
|
|
return;
|
|
}
|
|
|
|
limpiarVista(control);
|
|
|
|
svg = documento.createElementNS(espacioSvg, "svg");
|
|
svg.setAttribute("viewBox", "0 0 " + ancho + " " + alto);
|
|
svg.setAttribute("role", "img");
|
|
svg.setAttribute("aria-label", "Vista previa del archivo GIS");
|
|
svg.setAttribute("class", "sig-visualizador-archivo-gis-svg");
|
|
|
|
features.forEach(function (feature, indice) {
|
|
var pathTexto = geometriaAPath(feature.geometry, proyectar);
|
|
var path;
|
|
var centro;
|
|
var etiqueta;
|
|
|
|
if (!pathTexto) {
|
|
return;
|
|
}
|
|
|
|
path = documento.createElementNS(espacioSvg, "path");
|
|
path.setAttribute("d", pathTexto);
|
|
path.setAttribute("class", "sig-visualizador-archivo-gis-poligono");
|
|
path.setAttribute("fill-rule", "evenodd");
|
|
|
|
etiqueta = documento.createElementNS(espacioSvg, "title");
|
|
etiqueta.textContent = obtenerNombreFeature(feature, indice);
|
|
path.appendChild(etiqueta);
|
|
svg.appendChild(path);
|
|
|
|
centro = centroFeature(feature, proyectar);
|
|
if (centro && features.length <= 150) {
|
|
etiqueta = documento.createElementNS(espacioSvg, "text");
|
|
etiqueta.setAttribute("x", centro.x.toFixed(2));
|
|
etiqueta.setAttribute("y", centro.y.toFixed(2));
|
|
etiqueta.setAttribute("class", "sig-visualizador-archivo-gis-etiqueta");
|
|
etiqueta.textContent = obtenerNombreFeature(feature, indice);
|
|
svg.appendChild(etiqueta);
|
|
}
|
|
});
|
|
|
|
contenedor.appendChild(svg);
|
|
contenedor.style.display = "flex";
|
|
}
|
|
|
|
function googleMapsDisponible() {
|
|
return !!(
|
|
ventana.google &&
|
|
ventana.google.maps &&
|
|
ventana.google.maps.Map &&
|
|
ventana.google.maps.Data);
|
|
}
|
|
|
|
function googleMapsHabilitado(control) {
|
|
return control &&
|
|
control.getAttribute("data-sig-google-maps-habilitado") === "1" &&
|
|
!!control.getAttribute("data-sig-google-maps-api-key");
|
|
}
|
|
|
|
function cargarGoogleMaps(control) {
|
|
var apiKey;
|
|
|
|
if (googleMapsDisponible()) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
if (!googleMapsHabilitado(control)) {
|
|
return Promise.reject(new Error("Google Maps no está configurado."));
|
|
}
|
|
|
|
if (promesaGoogleMaps) {
|
|
return promesaGoogleMaps;
|
|
}
|
|
|
|
apiKey = control.getAttribute("data-sig-google-maps-api-key");
|
|
|
|
promesaGoogleMaps = new Promise(function (resolver, rechazar) {
|
|
var script = documento.createElement("script");
|
|
var completado = false;
|
|
|
|
function terminar(correcto) {
|
|
if (completado) {
|
|
return;
|
|
}
|
|
|
|
completado = true;
|
|
|
|
if (correcto && googleMapsDisponible()) {
|
|
resolver();
|
|
return;
|
|
}
|
|
|
|
promesaGoogleMaps = null;
|
|
rechazar(new Error("No fue posible cargar Google Maps."));
|
|
}
|
|
|
|
script.type = "text/javascript";
|
|
script.async = true;
|
|
script.defer = true;
|
|
script.setAttribute("data-sig-google-maps-visualizador", "1");
|
|
script.src =
|
|
"https://maps.googleapis.com/maps/api/js" +
|
|
"?key=" + encodeURIComponent(apiKey) +
|
|
"&v=weekly";
|
|
|
|
script.onload = function () {
|
|
terminar(true);
|
|
};
|
|
|
|
script.onerror = function () {
|
|
terminar(false);
|
|
};
|
|
|
|
documento.head.appendChild(script);
|
|
});
|
|
|
|
return promesaGoogleMaps;
|
|
}
|
|
|
|
function ajustarLimitesMapa(mapa) {
|
|
var limites = new ventana.google.maps.LatLngBounds();
|
|
var tieneCoordenadas = false;
|
|
|
|
mapa.data.forEach(function (feature) {
|
|
var geometria = feature.getGeometry();
|
|
|
|
if (!geometria || typeof geometria.forEachLatLng !== "function") {
|
|
return;
|
|
}
|
|
|
|
geometria.forEachLatLng(function (coordenada) {
|
|
limites.extend(coordenada);
|
|
tieneCoordenadas = true;
|
|
});
|
|
});
|
|
|
|
if (!tieneCoordenadas) {
|
|
return;
|
|
}
|
|
|
|
mapa.fitBounds(limites);
|
|
|
|
ventana.google.maps.event.addListenerOnce(
|
|
mapa,
|
|
"idle",
|
|
function () {
|
|
if (mapa.getZoom() > 20) {
|
|
mapa.setZoom(20);
|
|
}
|
|
});
|
|
}
|
|
|
|
function propiedadesFeatureGoogle(feature) {
|
|
var propiedades = {};
|
|
|
|
if (!feature || typeof feature.forEachProperty !== "function") {
|
|
return propiedades;
|
|
}
|
|
|
|
feature.forEachProperty(function (valor, nombre) {
|
|
propiedades[nombre] = valor;
|
|
});
|
|
|
|
return propiedades;
|
|
}
|
|
|
|
function registrarClickGis(control, mapa) {
|
|
mapa.data.addListener("click", function (evento) {
|
|
var detalle = {
|
|
propiedades: propiedadesFeatureGoogle(evento.feature),
|
|
latitud: evento.latLng ? evento.latLng.lat() : null,
|
|
longitud: evento.latLng ? evento.latLng.lng() : null
|
|
};
|
|
|
|
emitir(control, "sigvisualizadorgisfeatureclick", detalle);
|
|
});
|
|
}
|
|
|
|
function renderizarGisGoogleMaps(control, valor) {
|
|
var contenedor = control.querySelector("[data-sig-visualizador-gis]");
|
|
var features = validarGeoJson(valor);
|
|
var mapa;
|
|
var geoJson;
|
|
|
|
if (!contenedor) {
|
|
return;
|
|
}
|
|
|
|
limpiarVista(control);
|
|
contenedor.style.display = "block";
|
|
contenedor.classList.add("sig-visualizador-archivo-gis-google");
|
|
|
|
mapa = new ventana.google.maps.Map(contenedor, {
|
|
center: { lat: 29.0729673, lng: -110.9559192 },
|
|
zoom: 12,
|
|
mapTypeId: "roadmap",
|
|
mapTypeControl: true,
|
|
streetViewControl: false,
|
|
fullscreenControl: false
|
|
});
|
|
|
|
control._sigVisualizadorMapaGis = mapa;
|
|
|
|
geoJson = {
|
|
type: "FeatureCollection",
|
|
features: features
|
|
};
|
|
|
|
mapa.data.addGeoJson(geoJson);
|
|
mapa.data.setStyle({
|
|
strokeWeight: 2,
|
|
fillOpacity: 0.24
|
|
});
|
|
|
|
ajustarLimitesMapa(mapa);
|
|
registrarClickGis(control, mapa);
|
|
}
|
|
|
|
function renderizarGis(control, valor) {
|
|
if (!googleMapsHabilitado(control) && !googleMapsDisponible()) {
|
|
renderizarGisSvg(control, valor);
|
|
return;
|
|
}
|
|
|
|
cargarGoogleMaps(control)
|
|
.then(function () {
|
|
try {
|
|
renderizarGisGoogleMaps(control, valor);
|
|
} catch (error) {
|
|
renderizarGisSvg(control, valor);
|
|
}
|
|
})
|
|
.catch(function () {
|
|
renderizarGisSvg(control, valor);
|
|
});
|
|
}
|
|
|
|
function cargarGis(control, datos) {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-spinner fa-spin",
|
|
"Cargando vista previa GIS...");
|
|
|
|
crearSolicitud(
|
|
control,
|
|
datos.url,
|
|
"text",
|
|
function (solicitud) {
|
|
try {
|
|
var contenido = String(solicitud.responseText || "").replace(/^\uFEFF/, "");
|
|
renderizarGis(control, JSON.parse(contenido));
|
|
} catch (error) {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-map-o",
|
|
error && error.message
|
|
? error.message
|
|
: "No fue posible interpretar el archivo GIS. Puede descargar el archivo.");
|
|
}
|
|
},
|
|
function () {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-map-o",
|
|
"No fue posible cargar el archivo GIS. Puede descargar el archivo.");
|
|
});
|
|
}
|
|
|
|
function mostrar(control, datos) {
|
|
var tipo;
|
|
|
|
if (!control || !datos || !datos.url) {
|
|
return;
|
|
}
|
|
|
|
control.classList.add("sig-visualizador-archivo-activo");
|
|
control.classList.remove("sig-visualizador-archivo-expandido");
|
|
|
|
texto(control.querySelector("[data-sig-visualizador-titulo]"), datos.titulo);
|
|
texto(control.querySelector("[data-sig-visualizador-descripcion]"), datos.descripcion);
|
|
texto(control.querySelector("[data-sig-visualizador-nombre]"), datos.nombreArchivo);
|
|
|
|
aplicarAcciones(control, datos);
|
|
limpiarVista(control);
|
|
|
|
tipo = detectarTipo(datos.nombreArchivo, datos.tipoContenido);
|
|
|
|
if (tipo === "pdf") {
|
|
mostrarPdf(control, datos.url);
|
|
} else if (tipo === "imagen") {
|
|
mostrarImagen(control, datos);
|
|
} else if (tipo === "office") {
|
|
cargarOffice(control, datos);
|
|
} else if (tipo === "gis") {
|
|
cargarGis(control, datos);
|
|
} else if (tipo === "texto") {
|
|
cargarTexto(control, datos);
|
|
} else {
|
|
mostrarMensaje(
|
|
control,
|
|
"fa-file-o",
|
|
"Archivo no soportado para visualización. Puede descargarlo.");
|
|
}
|
|
|
|
emitir(control, "sigvisualizadorabierto", {
|
|
tipo: tipo,
|
|
nombreArchivo: datos.nombreArchivo || ""
|
|
});
|
|
}
|
|
|
|
function cerrar(control) {
|
|
if (!control) {
|
|
return;
|
|
}
|
|
|
|
limpiarVista(control);
|
|
control.classList.remove("sig-visualizador-archivo-activo");
|
|
control.classList.remove("sig-visualizador-archivo-expandido");
|
|
|
|
emitir(control, "sigvisualizadorcerrado", {});
|
|
}
|
|
|
|
function alternarExpandido(control) {
|
|
var expandido;
|
|
var boton;
|
|
var icono;
|
|
|
|
if (!control) {
|
|
return;
|
|
}
|
|
|
|
expandido = !control.classList.contains("sig-visualizador-archivo-expandido");
|
|
control.classList.toggle("sig-visualizador-archivo-expandido", expandido);
|
|
|
|
boton = control.querySelector("[data-sig-visualizador-expandir]");
|
|
icono = boton ? boton.querySelector("i") : null;
|
|
|
|
if (boton) {
|
|
boton.setAttribute("title", expandido ? "Restaurar visor" : "Expandir visor");
|
|
boton.setAttribute("aria-label", expandido ? "Restaurar visor" : "Expandir visor");
|
|
}
|
|
|
|
if (icono) {
|
|
icono.className = "fa " + (expandido ? "fa-compress" : "fa-expand");
|
|
}
|
|
|
|
if (control._sigVisualizadorMapaGis &&
|
|
ventana.google &&
|
|
ventana.google.maps &&
|
|
ventana.google.maps.event) {
|
|
ventana.setTimeout(function () {
|
|
ventana.google.maps.event.trigger(
|
|
control._sigVisualizadorMapaGis,
|
|
"resize");
|
|
ajustarLimitesMapa(control._sigVisualizadorMapaGis);
|
|
}, 0);
|
|
}
|
|
|
|
emitir(control, "sigvisualizadorexpandido", {
|
|
expandido: expandido
|
|
});
|
|
}
|
|
|
|
function obtenerDatosDisparador(disparador) {
|
|
return {
|
|
titulo: disparador.getAttribute("data-sig-visualizador-titulo") || "",
|
|
descripcion: disparador.getAttribute("data-sig-visualizador-descripcion") || "",
|
|
nombreArchivo: disparador.getAttribute("data-sig-visualizador-nombre") || "",
|
|
tipoContenido: disparador.getAttribute("data-sig-visualizador-tipo-contenido") || "",
|
|
url: disparador.getAttribute("data-sig-visualizador-url") || disparador.getAttribute("href") || "",
|
|
urlDescarga: disparador.getAttribute("data-sig-visualizador-url-descarga") || disparador.getAttribute("href") || ""
|
|
};
|
|
}
|
|
|
|
function encontrarVisualizador(disparador) {
|
|
var destino = disparador.getAttribute("data-sig-visualizador-destino");
|
|
var contexto;
|
|
|
|
if (destino) {
|
|
return documento.getElementById(destino);
|
|
}
|
|
|
|
contexto = disparador.closest("[data-sig-visualizador-contexto]");
|
|
return contexto ? contexto.querySelector("[data-sig-visualizador-archivo]") : null;
|
|
}
|
|
|
|
function inicializarControl(control) {
|
|
var activoInicial;
|
|
|
|
if (!control || control.getAttribute("data-sig-visualizador-inicializado") === "1") {
|
|
return;
|
|
}
|
|
|
|
control.setAttribute("data-sig-visualizador-inicializado", "1");
|
|
activoInicial = control.getAttribute("data-sig-visualizador-activo-inicial") === "1";
|
|
|
|
if (activoInicial) {
|
|
mostrar(control, {
|
|
titulo: control.getAttribute("data-sig-visualizador-titulo-inicial") || "",
|
|
descripcion: control.getAttribute("data-sig-visualizador-descripcion-inicial") || "",
|
|
nombreArchivo: control.getAttribute("data-sig-visualizador-nombre-inicial") || "",
|
|
tipoContenido: control.getAttribute("data-sig-visualizador-tipo-contenido-inicial") || "",
|
|
url: control.getAttribute("data-sig-visualizador-url-inicial") || "",
|
|
urlDescarga: control.getAttribute("data-sig-visualizador-url-descarga-inicial") || ""
|
|
});
|
|
}
|
|
}
|
|
|
|
function inicializar(raiz) {
|
|
var controles = (raiz || documento).querySelectorAll(
|
|
"[data-sig-visualizador-archivo]:not([data-sig-visualizador-inicializado='1'])"
|
|
);
|
|
|
|
Array.prototype.forEach.call(controles, inicializarControl);
|
|
}
|
|
|
|
function registrarEventos() {
|
|
if (eventosRegistrados) {
|
|
return;
|
|
}
|
|
|
|
eventosRegistrados = true;
|
|
|
|
documento.addEventListener("click", function (evento) {
|
|
var disparador = evento.target.closest("[data-sig-visualizador-abrir]");
|
|
var cerrarBoton = evento.target.closest("[data-sig-visualizador-cerrar]");
|
|
var expandirBoton = evento.target.closest("[data-sig-visualizador-expandir]");
|
|
var control;
|
|
|
|
if (disparador) {
|
|
control = encontrarVisualizador(disparador);
|
|
if (!control) {
|
|
return;
|
|
}
|
|
|
|
evento.preventDefault();
|
|
mostrar(control, obtenerDatosDisparador(disparador));
|
|
return;
|
|
}
|
|
|
|
if (cerrarBoton) {
|
|
evento.preventDefault();
|
|
cerrar(cerrarBoton.closest("[data-sig-visualizador-archivo]"));
|
|
return;
|
|
}
|
|
|
|
if (expandirBoton) {
|
|
evento.preventDefault();
|
|
alternarExpandido(expandirBoton.closest("[data-sig-visualizador-archivo]"));
|
|
}
|
|
});
|
|
}
|
|
|
|
ventana.SIGVisualizadorArchivo = {
|
|
inicializar: inicializar,
|
|
mostrar: mostrar,
|
|
cerrar: cerrar,
|
|
alternarExpandido: alternarExpandido
|
|
};
|
|
|
|
registrarEventos();
|
|
|
|
if (documento.readyState === "loading") {
|
|
documento.addEventListener("DOMContentLoaded", function () {
|
|
inicializar(documento);
|
|
});
|
|
} else {
|
|
inicializar(documento);
|
|
}
|
|
|
|
if (ventana.Sys && ventana.Sys.Application) {
|
|
ventana.Sys.Application.add_load(function () {
|
|
inicializar(documento);
|
|
});
|
|
}
|
|
}(window, document));
|