product_editor.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { formscreen } from "./formscreen.js";
  2. import { edit_request } from "./edit_request.js";
  3. import { searcher } from "./searcher.js";
  4. import { edit_image_request } from "./edit_image_request.js";
  5. export class product_editor extends formscreen {
  6. #target;
  7. #name;
  8. #description;
  9. #author;
  10. #barcode;
  11. #stock_count;
  12. #image;
  13. #loaded_image_type;
  14. #loaded_image;
  15. #image_preview;
  16. constructor(target) {
  17. super();
  18. this.#target = target;
  19. }
  20. get target() {
  21. return this.#target;
  22. }
  23. get _name() {
  24. return _("product-editor");
  25. }
  26. _build_form() {
  27. this.#loaded_image = null;
  28. this.#loaded_image_type = null;
  29. this.#name = this._create_input(
  30. "name",
  31. _("name-prompt"),
  32. _("name-sample"),
  33. (input) => {
  34. input.value = this.#target.name;
  35. }
  36. );
  37. this.#description = this._create_input(
  38. "description",
  39. _("description-prompt"),
  40. _("description-sample"),
  41. (input) => {
  42. input.value = this.#target.description;
  43. }
  44. );
  45. this.#author = this._create_input(
  46. "author",
  47. _("author-prompt"),
  48. _("author-sample"),
  49. (input) => {
  50. input.value = this.#target.author;
  51. }
  52. );
  53. this.#barcode = this._create_input(
  54. "barcode",
  55. _("barcode-prompt"),
  56. _("barcode-sample"),
  57. (input) => {
  58. input.type = "number";
  59. input.value = this.#target.barcode
  60. }
  61. );
  62. this.#stock_count = this._create_input(
  63. "stock_count",
  64. _("stock-count-prompt"),
  65. _("stock-count-sample"),
  66. (input) => {
  67. input.type = "number";
  68. input.value = this.#target.stock_count
  69. }
  70. );
  71. this._create_input(
  72. "image",
  73. _("change-product-image"),
  74. "",
  75. (input) => {
  76. this.#image = input;
  77. input.type = "file";
  78. input.accept = "image/*";
  79. input.addEventListener("change", () => {
  80. this.#load_image_from_file();
  81. });
  82. }
  83. );
  84. this.#image_preview = document.createElement("img");
  85. this.#image_preview.style.opacity = "1";
  86. this.#image_preview.src = this.#target.image;
  87. this._append_child(this.#image_preview);
  88. }
  89. get #ready_image() {
  90. return this.#loaded_image;
  91. }
  92. #update_image_preview() {
  93. this.#image_preview.src = (
  94. "data:"
  95. + this.#loaded_image_type
  96. + ";base64,"
  97. + this.#loaded_image
  98. );
  99. this.#image_preview.style.opacity = "1";
  100. }
  101. #reset_image() {
  102. this.#loaded_image = null;
  103. this.#loaded_image_type = null;
  104. this.#image_preview.style.opacity = "0";
  105. this.#image_preview.src = "";
  106. }
  107. async #load_image_from_file() {
  108. if (this.#image.files.length === 0) {
  109. this.#reset_image();
  110. }
  111. const file = this.#image.files.item(0);
  112. const buffer = await file.arrayBuffer();
  113. let as_string = new String();
  114. new Uint8Array(buffer).forEach(letter => {
  115. as_string += String.fromCharCode(letter);
  116. });
  117. this.#loaded_image = btoa(as_string);
  118. this.#loaded_image_type = file.type;
  119. this.#update_image_preview();
  120. }
  121. async #submit() {
  122. const copy = this.#target.copy();
  123. copy.name = this.#name();
  124. copy.description = this.#description();
  125. copy.author = this.#author();
  126. copy.barcode = this.#barcode();
  127. copy.stock_count = this.#stock_count();
  128. const request = new edit_request(this.#target, copy);
  129. const response = await request.connect();
  130. if (!response.result) {
  131. throw new Error(response.cause);
  132. }
  133. this.#target = copy;
  134. }
  135. async #image_submit() {
  136. const image = await this.#ready_image;
  137. if (image === null) {
  138. return;
  139. }
  140. const request = new edit_image_request(this.#target, image);
  141. const response = await request.connect();
  142. if (!response.result) {
  143. throw new Error(response.cause);
  144. }
  145. }
  146. async _process() {
  147. try {
  148. this._info = _("updating-product-data");
  149. await this.#submit();
  150. this._info = _("processing-image");
  151. await this.#image_submit();
  152. this._success = _("uploaded-successfull");
  153. searcher.reload();
  154. setTimeout(() => {
  155. this.hide();
  156. }, 500);
  157. } catch (error) {
  158. this._error = new String(error);
  159. }
  160. }
  161. }