| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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;
- constructor(target) {
- super();
-
- this.#target = target;
- }
- get target() {
- return this.#target;
- }
- get _name() {
- return "Product editor";
- }
- _build_form() {
- 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/*";
- }
- );
- }
- async #code_image() {
- if (this.#image.files.length === 0) {
- return null;
- }
- const file = this.#image.files.item(0);
- const buffer = await file.arrayBuffer();
- const list = new Uint8Array(buffer);
- let content = new String();
- list.forEach((code) => {
- content += String.fromCharCode(code);
- });
- return btoa(content);
- }
- 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.#code_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);
- }
- }
- }
|