From 27075885e95bd0efc062920685e04287feb4b556 Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Sun, 31 Oct 2021 14:31:29 +0100 Subject: Almost the end --- src/modules/Fridge.ts | 97 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 20 deletions(-) (limited to 'src/modules/Fridge.ts') diff --git a/src/modules/Fridge.ts b/src/modules/Fridge.ts index c12bc53..7137f6d 100644 --- a/src/modules/Fridge.ts +++ b/src/modules/Fridge.ts @@ -1,3 +1,4 @@ +import { setZi } from "@/consts"; import Chit, { ChitFromDb } from "Modules/Chit" import WYSiWYG from "./WYSiWYG"; @@ -5,50 +6,106 @@ export default class Fridge { #chits: Chit[] = []; //@ts-expect-error #wysiwyg: WYSiWYG; + #name: string; + #counter!: number; + #mileage!: number; constructor(wysiwyg: WYSiWYG) { const f = sessionStorage.fridge; - if (f === null) { + this.#name = sessionStorage.name; + if (f == null || this.#name == null || this.#name.length === 0) { location.href = "/" return; } + // sessionStorage.clear() - const cs = JSON.parse(f) as ChitFromDb[]; - for (const c of cs) { - this.#chits.push(new Chit(this, wysiwyg, c)); + const fdb = JSON.parse(f) as FridgeFromDb; + if (fdb.mileage != null && fdb.cs != null) { + this.mileage = fdb.mileage; + this.counter = fdb.cs.length; + for (const c of fdb.cs) { + this.#chits.push(new Chit(this, wysiwyg, c)); + } + if (fdb.cs.length > 0) + setZi(Math.max(...fdb.cs.map(c => c.zi)) + 1) + } else { + this.mileage = 0; + this.counter = 0; } + this.#wysiwyg = wysiwyg; } - setChitZIAuto() { + #createJson() { + const fdb: FridgeFromDb = {}; + fdb.mileage = this.#mileage; + fdb.cs = []; + for (const c of this.#chits) { + fdb.cs.push({ + top: c.top, + left: c.left, + txt: c.txt, + width: c.width, + height: c.height, + zi: parseInt(c.zi), + }) + } + return JSON.stringify({ name: this.#name, cs: fdb }); + } + async saveToDb() { + const json = this.#createJson(); + await fetch("http://localhost:8001/save-fridge.php", { + method: "POST", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: json, + }) + } + + resetZi(zi: number) { for (const c of this.#chits) - c.div.style.zIndex = "auto" + c.div.style.zIndex = (zi++).toString() + + return zi; } removeChit(ch: Chit) { + this.counter = this.#counter - 1; this.#chits = this.#chits.filter(c => c !== ch); + this.saveToDb() + } + makeTrans(except: Chit) { + for (const c of this.#chits.filter(c => c !== except)) { + c.makeTrans(); + } + } + unmakeTrans() { + for (const c of this.#chits) { + c.unmakeTrans(); + } } - // moveChit(coord: { top: number, left: number }) { - // for (const c of this.#chits) { - // if (c.isMoving) { - // c.move(coord) - // } - // } - // } - // stopChitMove() { - // for (const c of this.#chits) { - // c.isMoving = false; - // //@ts-expect-error - // c.mouseUpH({ preventDefault: () => null }) - // } - // } createChit() { + this.counter = this.#counter + 1; + this.mileage = this.#mileage + 1; this.#chits.push(new Chit(this, this.#wysiwyg)) + this.saveToDb() } get chits(): Chit[] { return this.#chits; } + + set mileage(m: number) { + this.#mileage = m; + (document.querySelector("#mileage") as HTMLSpanElement).innerHTML = m.toString() + } + set counter(c: number) { + this.#counter = c; + (document.querySelector("#counter") as HTMLSpanElement).innerHTML = c.toString() + } } +export type FridgeFromDb = { cs?: ChitFromDb[], mileage?: number }; -- cgit v1.3.1