Internet-programming/dist/assets/lines-d024f035.js
2024-01-10 21:47:43 +04:00

2 lines
3.6 KiB
JavaScript

import{a as g,b as h,g as v,c as w,u as y,d as E}from"./lines-rest-api-38f971a9.js";function F(){const t=document.querySelectorAll("form.needs-validation");for(let e=0;e<t.length;e+=1){const o=t[e];o.addEventListener("submit",n=>{o.checkValidity()||(n.preventDefault(),n.stopPropagation()),o.classList.add("was-validated")})}}const a={button:document.getElementById("items-add"),table:document.querySelector("#items-table tbody"),form:document.getElementById("items-form"),lineId:document.getElementById("items-line-id"),genre:document.getElementById("item"),title:document.getElementById("title"),cast:document.getElementById("cast"),description:document.getElementById("description"),image:document.getElementById("image"),video:document.getElementById("video"),imagePreview:document.getElementById("image-preview")},m="https://via.placeholder.com/200";function u(t,e="",o=!1){const n=document.createElement("option");return n.value=e||"",n.selected=o,n.text=t,n}function f(t,e){const o=document.createElement("i");o.classList.add("fa-solid",t);const n=document.createElement("a");n.href="#",n.appendChild(o),n.onclick=l=>{l.preventDefault(),l.stopPropagation(),e()};const i=document.createElement("td");return i.appendChild(n),i}function d(t){const e=document.createElement("td");return e.textContent=t,e}function b(t,e,o,n,i){const l=document.createElement("th");l.scope="row",l.textContent=e+1;const c=document.createElement("tr");return c.id=`line-${t.id}`,c.appendChild(l),c.appendChild(d(t.items.name)),c.appendChild(d(t.title)),c.appendChild(d(t.cast)),c.appendChild(d(t.description)),c.appendChild(f("fa-pen-to-square",n)),c.appendChild(f("fa-trash",i)),c}async function C(){const t=await h();a.genre.appendChild(u("Выберите значение","",!0)),t.forEach(e=>{a.genre.appendChild(u(e.name,e.id))})}async function s(){if(console.info("Try to load data"),!a.table)return;const t=await v();a.table.innerHTML="",t.forEach((e,o)=>{a.table.appendChild(b(e,o,()=>location.assign(`Input.html?id=${e.id}`),()=>location.assign(`Input.html?id=${e.id}`),()=>P(e.id)))})}async function I(t,e,o,n,i,l){console.info("Try to add item");const c=await w(t,e,o,n,i,l);console.info("Added"),console.info(c),s()}async function L(t,e,o,n,i,l,c){console.info("Try to update item");const r=await y(t,e,o,n,i,l,c);console.info("Updated"),console.info(r),s()}async function P(t){if(!confirm("Do you really want to remove this item?")){console.info("Canceled");return}console.info("Try to remove item");const e=await E(t);console.info(e),s()}async function p(t){const e=new FileReader;return new Promise((o,n)=>{e.onloadend=()=>{const i=e.result;o(i)},e.onerror=()=>{n(new Error("oops, something went wrong with the file reader."))},e.readAsDataURL(t)})}async function B(){const t=a.image.files[0],e=await p(t);console.info("base64 ",e),a.imagePreview.src=e}function A(){console.info("linesForm"),s()}async function k(){console.info("linesPageForm"),C();const t=()=>location.assign("/admin.html");a.image.addEventListener("change",()=>B());const o=new URLSearchParams(location.search).get("id");if(o)try{const n=await g(o);a.genre.value=n.itemsId,a.title.value=n.title,a.cast.value=n.cast,a.description.value=n.description,a.imagePreview.src=n.image?n.image:m}catch{t()}a.form.addEventListener("submit",async n=>{if(console.info("Form onSubmit"),n.preventDefault(),n.stopPropagation(),!a.form.checkValidity())return;let i="imageTag",l="videoTag";if(a.imagePreview.src!==m){const r=await(await fetch(a.imagePreview.src)).blob();i=await p(r)}o?await L(o,a.genre.value,a.title.value,a.cast.value,a.description.value,i,l):await I(a.genre.value,a.title.value,a.cast.value,a.description.value,i,l),t()})}export{A as a,k as l,F as v};