| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | 
							- import { formscreen } from "./formscreen.js";
 
- import { edit_request } from "./edit_request.js";
 
- import { searcher } from "./searcher.js";
 
- import { edit_image_request } from "./edit_image_request.js";
 
- export class product_editor extends formscreen {
 
-     #target;
 
-     #name;
 
-     #description;
 
-     #author;
 
-     #barcode;
 
-     #stock_count;
 
-     #image;
 
-     #loaded_image_type;
 
-     #loaded_image;
 
-     #image_preview;
 
-     constructor(target) {
 
-         super();
 
-         
 
-         this.#target = target;
 
-     }
 
-     get target() {
 
-         return this.#target;
 
-     }
 
-     get _name() {
 
-         return "Product editor";
 
-     }
 
-     _build_form() {
 
-         this.#loaded_image = null;
 
-         this.#loaded_image_type = null;
 
-         this.#name = this._create_input(
 
-             "name", 
 
-             "Name:", 
 
-             "Sample...",
 
-             (input) => {
 
-                 input.value = this.#target.name;
 
-             }
 
-         );
 
-         this.#description = this._create_input(
 
-             "description",
 
-             "Description:",
 
-             "This is sample product...",
 
-             (input) => {
 
-                 input.value = this.#target.description;
 
-             }
 
-         );
 
-         this.#author = this._create_input(
 
-             "author",
 
-             "Author:",
 
-             "Jack Black",
 
-             (input) => {
 
-                 input.value = this.#target.author;
 
-             }
 
-         );
 
-         this.#barcode = this._create_input(
 
-             "barcode",
 
-             "Barcode (EAN):",
 
-             "123456789012...",
 
-             (input) => { 
 
-                 input.type = "number"; 
 
-                 input.value = this.#target.barcode
 
-             }
 
-         );
 
-         this.#stock_count = this._create_input(
 
-             "stock_count",
 
-             "Stock count:",
 
-             "10...",
 
-             (input) => { 
 
-                 input.type = "number"; 
 
-                 input.value = this.#target.stock_count
 
-             }
 
-         );
 
-         this._create_input(
 
-             "image",
 
-             "Change product image:",
 
-             "",
 
-             (input) => {
 
-                 this.#image = input;
 
-                 input.type = "file";
 
-                 input.accept = "image/*";
 
-                 input.addEventListener("change", () => {
 
-                     this.#load_image_from_file();
 
-                 });
 
-             }
 
-         );
 
-         this.#image_preview = document.createElement("img");
 
-         this.#image_preview.style.opacity = "1";
 
-         this.#image_preview.src = this.#target.image;
 
-         this._append_child(this.#image_preview);
 
-     }
 
-     
 
-     get #ready_image() {
 
-         return this.#loaded_image;
 
-     }
 
-     #update_image_preview() {
 
-         this.#image_preview.src = (
 
-             "data:" 
 
-             + this.#loaded_image_type 
 
-             + ";base64,"
 
-             + this.#loaded_image
 
-         );
 
-         this.#image_preview.style.opacity = "1";
 
-     }
 
-     #reset_image() {
 
-         this.#loaded_image = null;
 
-         this.#loaded_image_type = null;
 
-         this.#image_preview.style.opacity = "0";
 
-         this.#image_preview.src = "";
 
-     }
 
-     async #load_image_from_file() {
 
-         if (this.#image.files.length === 0) {
 
-             this.#reset_image();
 
-         }
 
-         const file = this.#image.files.item(0);
 
-         const buffer = await file.arrayBuffer();
 
-         let as_string = new String();
 
-         new Uint8Array(buffer).forEach(letter => {
 
-             as_string += String.fromCharCode(letter);
 
-         });
 
-         this.#loaded_image = btoa(as_string);
 
-         this.#loaded_image_type = file.type;
 
-         this.#update_image_preview();
 
-     }
 
-     async #submit() {
 
-         const copy = this.#target.copy();
 
-         copy.name = this.#name();
 
-         copy.description = this.#description();
 
-         copy.author = this.#author();
 
-         copy.barcode = this.#barcode();
 
-         copy.stock_count = this.#stock_count();
 
-         
 
-         const request = new edit_request(this.#target, copy);
 
-         const response = await request.connect();
 
-         if (!response.result) {
 
-             throw new Error(response.cause);
 
-         }
 
-         this.#target = copy;
 
-     }   
 
-     async #image_submit() {
 
-         const image = await this.#ready_image;
 
-         if (image === null) {
 
-             return;
 
-         }
 
-         const request = new edit_image_request(this.#target, image);
 
-         const response = await request.connect();
 
-         if (!response.result) {
 
-             throw new Error(response.cause);
 
-         }
 
-     }
 
-     async _process() {
 
-         try {
 
-             this._info = "Uploading...";
 
-             await this.#submit();
 
-             this._info = "Processing image...";
 
-             await this.#image_submit();
 
-             this._success = "Updated success!";
 
-             searcher.reload();
 
-             setTimeout(() => {
 
-                 this.hide();
 
-             }, 500);
 
-         } catch (error) {
 
-             this._error = new String(error);
 
-         }
 
-     }
 
- }   
 
 
  |