From 081ddc03aef7becc3b084a9079a2574fd3e3b49b Mon Sep 17 00:00:00 2001 From: Choom Date: Sat, 28 Jan 2023 00:52:56 -0300 Subject: userconf --- static/js/userconf.js | 116 ++++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 50 deletions(-) (limited to 'static/js/userconf.js') diff --git a/static/js/userconf.js b/static/js/userconf.js index 0118e88..399878e 100644 --- a/static/js/userconf.js +++ b/static/js/userconf.js @@ -1,35 +1,3 @@ -var request = new XMLHttpRequest(); - -function cargarEstilos() { - var api = '/cgi/api/styles'; - request.open("GET", api, true); - request.send(); -} - -function listarEstilos(id, estilos, def) { - var select = document.getElementById(id); - - for (var i=0; i < estilos.length; i++) { - var opt = document.createElement('option'); - opt.value = estilos[i]; - opt.textContent = estilos[i]; - if (i == def) { - opt.selected = true; - } - select.appendChild(opt); - } -} - -request.onreadystatechange = function() { - if (request.readyState == 4) { - var json = JSON.parse(request.responseText); - if (json.state == "success") { - listarEstilos('stylebbs', json.bbs_styles, json.bbs_styles_default); - listarEstilos('styleib', json.ib_styles, json.ib_styles_default); - } - } -}; - function cambiarModo(e) { var iframe = document.getElementById('examplebbs'); var estilo = document.getElementById('stylebbs').value; @@ -63,7 +31,7 @@ function mostrarEstilo(e) { loc += 'txt/'; } - css.href = loc + estilo.toLowerCase() + ".css"; + css.href = loc + estilo.trim().toLowerCase() + ".css"; var key = 'weabot_style_' + tipo; var estilo = document.getElementById(('style'+tipo)).value; @@ -74,32 +42,41 @@ function cargariframe(tipo) { var iframe = document.createElement('iframe'); iframe.id = 'example' + tipo; iframe.dataset.type = tipo; - iframe.addEventListener('load', cargarDatos); + iframe.addEventListener('load', cargarEstilo); iframe.src = '/static/example/' + tipo + '1.html'; var div = document.getElementById("desc" + tipo); div.appendChild(iframe); } -function cargarDatos(e) { +function cargarEstilo(e) { var tipo = this.dataset.type; var key = 'weabot_style_' + tipo; + if (localStorage.hasOwnProperty(key)) { - var estilo = localStorage.getItem(key); - document.getElementById(('style'+tipo)).value = estilo; + var estilo = localStorage.getItem(key).trim(); + + if (estilo != '') { + document.getElementById(('style'+tipo)).value = estilo; + } else { + localStorage.removeItem(key); + return; + } + mostrarEstilo(e); this.tabIndex = -1; } - this.removeEventListener('load', cargarDatos); + + this.removeEventListener('load', cargarEstilo); } function init(tipo) { - cargariframe(tipo); document.getElementById('style'+tipo).addEventListener('change', mostrarEstilo); + cargariframe(tipo); } function checkboxes() { var chk = document.getElementsByClassName("weabot-obj"); - for (var i=0; i < chk.length; i++) { + for (var i=0; i < chk.length; i++) { chk[i].addEventListener("change", saveInput); } } @@ -151,19 +128,19 @@ function nuevoFiltro(usar, patron, tipo) { var lista = document.getElementById("filterlist"); var fila = document.createElement("tr"); - var selected = ['', '', '', '']; - selected[tipo] = ' selected="selected"'; + var selected = ['', '', '', '']; + selected[tipo] = ' selected="selected"'; var html = '' : '>'); html += ''; html += ''; - html += ''; - fila.innerHTML = html; + + '' + + '' + + '' + + '' + + ''; + html += ''; + fila.innerHTML = html; fila.children[3].firstChild.addEventListener('click', quitarFiltro); lista.appendChild(fila); } @@ -201,7 +178,46 @@ function guardarFiltros() { } } +function importar() { + var url = location.href; + if (url.includes('#import=')) { + var data = location.href.split('#import=')[1]; + var conf = JSON.parse( decodeURIComponent(data) ); + for (var c in conf) { + localStorage.setItem(c, conf[c]); + } + } +} + +function exportar(e) { + var keys = Object.keys(localStorage); + var conf = {}; + for (var i=0; i < keys.length; i++) { + var value = localStorage.getItem(keys[i]); + if (value.trim() != '') { + conf[keys[i]] = value; + } + } + var data = encodeURIComponent( JSON.stringify(conf) ); + var box = document.getElementById('config-url'); + if (!box) { + var box = document.createElement('input'); + box.id = 'config-url'; + box.type = 'text'; + box.readOnly = true; + e.target.parentElement.appendChild(box); + var div = document.createElement('div'); + div.textContent = 'Copia y pega el enlace generado en un navegador para importar tu configuraciĆ³n.'; + e.target.parentElement.appendChild(div); + } + box.value = location.href.split('#')[0] + '#import=' + data; + box.focus(); + box.select(); +} + document.addEventListener("DOMContentLoaded", function() { + importar(); + if (localStorage.hasOwnProperty("weabot")) { weabot = JSON.parse(localStorage.getItem("weabot")); } else { @@ -215,11 +231,11 @@ document.addEventListener("DOMContentLoaded", function() { } setInputs(); - cargarEstilos(); checkboxes(); cargarFiltros(); document.getElementById('switchview').addEventListener('click', cambiarModo); + document.getElementById('export').addEventListener('click', exportar); init('bbs'); init('ib'); -- cgit v1.2.1-18-gbd029