Primer Commit. Este codigo fue escrito en el año 2022
This commit is contained in:
22
js/actualizar.js
Executable file
22
js/actualizar.js
Executable file
@@ -0,0 +1,22 @@
|
||||
import { carga, recarga } from "./lib/sys.js"
|
||||
var entradas
|
||||
|
||||
carga()
|
||||
.then(resp => {
|
||||
entradas = resp
|
||||
init()
|
||||
})
|
||||
|
||||
function init(){
|
||||
let titulo_b = document.createTextNode(entradas[0][0].sitio)
|
||||
titulo_blog.appendChild(titulo_b)
|
||||
|
||||
//descripcion del blog
|
||||
let descripcion_b = document.createTextNode(entradas[0][0].descripcion)
|
||||
descripcion_blog.appendChild(descripcion_b)
|
||||
}
|
||||
|
||||
actualizar_local.addEventListener("mousedown",e=>{
|
||||
recarga()
|
||||
window.location.href="/"
|
||||
})
|
||||
54
js/editar.js
Executable file
54
js/editar.js
Executable file
@@ -0,0 +1,54 @@
|
||||
import { actualizar, carga,datetime } from "./lib/sys.js"
|
||||
var md = window.markdownit()
|
||||
var entradas
|
||||
var indice
|
||||
|
||||
carga()
|
||||
.then(resp => {
|
||||
entradas = resp
|
||||
init()
|
||||
})
|
||||
|
||||
function init() {
|
||||
var editando = ""
|
||||
|
||||
let titulo_b = document.createTextNode(entradas[0][0].sitio)
|
||||
titulo_blog.appendChild(titulo_b)
|
||||
|
||||
let descripcion_b = document.createTextNode(entradas[0][0].descripcion)
|
||||
descripcion_blog.appendChild(descripcion_b)
|
||||
|
||||
entradas[1].map((resp, index) => {
|
||||
if (resp.id == window.location.search.substring(1).split("=")[1]) {
|
||||
indice = index
|
||||
editando = index
|
||||
titulo_post.value = resp.titulo
|
||||
contenido_post.value = resp.contenido
|
||||
etiquetas.value = resp.etiquetas
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
eliminar.addEventListener("mousedown", e => {
|
||||
entradas[1].splice(indice,1)
|
||||
actualizar(entradas)
|
||||
window.location.href="/"
|
||||
})
|
||||
|
||||
titulo_post.addEventListener("input", guardar_cambios)
|
||||
contenido_post.addEventListener("input", guardar_cambios)
|
||||
etiquetas.addEventListener("input", guardar_cambios)
|
||||
|
||||
function guardar_cambios(e) {
|
||||
entradas[1][indice].titulo = titulo_post.value
|
||||
entradas[1][indice].contenido = contenido_post.value
|
||||
entradas[1][indice].etiquetas = etiquetas.value
|
||||
entradas[1][indice].fecha_modificacion = datetime()
|
||||
|
||||
//console.log(entradas[1][indice].contenido)
|
||||
actualizar(entradas)
|
||||
}
|
||||
|
||||
//https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
|
||||
|
||||
//https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events
|
||||
18
js/exportar.js
Executable file
18
js/exportar.js
Executable file
@@ -0,0 +1,18 @@
|
||||
import { actualizar, carga,datetime } from "./lib/sys.js"
|
||||
var entradas
|
||||
|
||||
carga()
|
||||
.then(resp => {
|
||||
entradas = resp
|
||||
resultado.value=JSON.stringify(entradas)
|
||||
init()
|
||||
})
|
||||
|
||||
function init(){
|
||||
let titulo_b = document.createTextNode(entradas[0][0].sitio)
|
||||
titulo_blog.appendChild(titulo_b)
|
||||
|
||||
//descripcion del blog
|
||||
let descripcion_b = document.createTextNode(entradas[0][0].descripcion)
|
||||
descripcion_blog.appendChild(descripcion_b)
|
||||
}
|
||||
57
js/index.js
Executable file
57
js/index.js
Executable file
@@ -0,0 +1,57 @@
|
||||
import { carga } from "./lib/sys.js"
|
||||
var md = window.markdownit()
|
||||
var entradas
|
||||
|
||||
carga()
|
||||
.then(resp => {
|
||||
entradas = resp
|
||||
init()
|
||||
})
|
||||
|
||||
function init() {
|
||||
//Titulo del blog
|
||||
let titulo_b = document.createTextNode(entradas[0][0].sitio)
|
||||
titulo_blog.appendChild(titulo_b)
|
||||
|
||||
//descripcion del blog
|
||||
let descripcion_b = document.createTextNode(entradas[0][0].descripcion)
|
||||
descripcion_blog.appendChild(descripcion_b)
|
||||
|
||||
//cargar todas las entradas en orden invertido, tradicional para los blogs
|
||||
entradas[1].slice(0).reverse().map((resp, index) => {
|
||||
//construir los contenedores necesarios para las entradas
|
||||
let contenedor = document.createElement("div")
|
||||
let contenedor_post = document.createElement("div")
|
||||
let separador = document.createElement("hr")
|
||||
|
||||
//renderizar el contenido markdown en html
|
||||
contenedor_post.innerHTML = md.render(resp.contenido)
|
||||
|
||||
let meta = document.createElement("div")
|
||||
meta.innerHTML = `
|
||||
<div>
|
||||
<h3>`+ resp.titulo + `</h3>
|
||||
<a href="/editar?id=`+ resp.id + `"><small>Editar</small></a><br>
|
||||
<small><b>fecha de creación: </b>`+ resp.fecha_creacion + `</small><br>
|
||||
<small><b>fecha de modificacion: </b>`+ resp.fecha_modificacion + `</small><br>
|
||||
</div>
|
||||
<br>
|
||||
`
|
||||
contenedor.appendChild(meta)
|
||||
contenedor.appendChild(contenedor_post)
|
||||
contenedor.appendChild(separador)
|
||||
|
||||
post.appendChild(contenedor)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
|
||||
//https://developers.google.com/web/updates/2015/03/introduction-to-fetch si no fuera por esta pagina, nunca habria descubierto que habia que hacer un then al json() para obtener los datos
|
||||
//https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch ironicamente, aqui tambien esta la respuesta, pero no se nota tanto porque transforma la peticion a funcion asincrona para hacerle un await, por lo que el result.json() es parseado y regresado por el return
|
||||
//https://love2dev.com/blog/javascript-remove-from-array/ eliminar elementos de un array
|
||||
|
||||
//jsdoc -c jsdoc.conf js/index.js js/lib/sys.js README.md -d Documentacion
|
||||
//jsdoc -c jsdoc.conf -r ./js README.md -d Documentacion
|
||||
56
js/lib/sys.js
Executable file
56
js/lib/sys.js
Executable file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* # Carga Inicial
|
||||
*
|
||||
* Esta funcion permite determinar el estado de los datos en memoria, cargandolos desde el servidor en caso de que no exista o comprobando si se encuentran modificados en relacion con el archivo original
|
||||
*
|
||||
* Esta funcion devuelve un array que contiene los datos de la entrada
|
||||
*/
|
||||
export async function carga() {
|
||||
var respuesta
|
||||
var texto
|
||||
if (localStorage.getItem("entradas") == undefined) {//no hay nada guardado
|
||||
//agregar try and catch
|
||||
respuesta = await fetch("entradas.json")
|
||||
texto = await respuesta.text()
|
||||
localStorage.setItem("entradas", texto)
|
||||
return JSON.parse(texto)
|
||||
} else {
|
||||
return JSON.parse(localStorage.getItem("entradas"))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* # Recarga
|
||||
*
|
||||
* Esta funcion permite reemplazar la copia local por una copia obtenida del servidor.
|
||||
*/
|
||||
export async function recarga(){
|
||||
var respuesta
|
||||
var texto
|
||||
respuesta = await fetch("entradas.json")
|
||||
texto = await respuesta.text()
|
||||
localStorage.setItem("entradas", texto)
|
||||
}
|
||||
|
||||
/**
|
||||
* # Actualizar
|
||||
*
|
||||
* Actualiza los datos en en localStorage con una copia del array recibido por parametros
|
||||
*
|
||||
* @param {*} entradas El objeto completo de las entradas para combinar con el localstorage
|
||||
*/
|
||||
export function actualizar(entradas){
|
||||
localStorage.setItem("entradas",JSON.stringify(entradas))
|
||||
return "exito"
|
||||
}
|
||||
|
||||
/**
|
||||
* # Datetime
|
||||
*
|
||||
* Funcion que permite regresar una cadena de texto en formato de fecha y hora de mysql
|
||||
*/
|
||||
export function datetime(){
|
||||
var datetime = new Date()
|
||||
return datetime.getFullYear()+"-"+datetime.getMonth()+"-"+datetime.getDate()+" "+datetime.getHours()+":"+datetime.getMinutes()+":"+datetime.getSeconds()
|
||||
|
||||
}
|
||||
37
js/nueva_entrada.js
Executable file
37
js/nueva_entrada.js
Executable file
@@ -0,0 +1,37 @@
|
||||
import { carga,actualizar,datetime } from "./lib/sys.js"
|
||||
var md = window.markdownit()
|
||||
var entradas
|
||||
var indice
|
||||
|
||||
carga()
|
||||
.then(resp => {
|
||||
entradas = resp
|
||||
init()
|
||||
})
|
||||
|
||||
function init() {
|
||||
let titulo_b = document.createTextNode(entradas[0][0].sitio)
|
||||
titulo_blog.appendChild(titulo_b)
|
||||
|
||||
let descripcion_b = document.createTextNode(entradas[0][0].descripcion)
|
||||
descripcion_blog.appendChild(descripcion_b)
|
||||
}
|
||||
|
||||
guardar.addEventListener("mousedown",e=>{
|
||||
//crear nuevo objeto y hacer push al array de entradas[1], luego redirigir a la vista de edicion
|
||||
//insertar validaciones
|
||||
var data={
|
||||
"id":entradas[1][entradas[1].length-1].id+1,
|
||||
"titulo":titulo_post.value,
|
||||
"contenido":contenido_post.value,
|
||||
"etiquetas":etiquetas.value,
|
||||
"fecha_creacion": datetime(),
|
||||
"fecha_modificacion": datetime()
|
||||
}
|
||||
entradas[1].push(data)
|
||||
//console.log(entradas[1])
|
||||
actualizar(entradas)
|
||||
alert("Entrada creada con exito")
|
||||
//redirigir a la pagina de edicion con el id recientemente creado
|
||||
//window.location.href="editar?id="+entradas[1][entradas[1].length-1].id
|
||||
})
|
||||
Reference in New Issue
Block a user