5 constructor(socketLibrary, server) {
7 super(socketLibrary, server);
10 this.extractedEditor =
null;
11 this.materialsList = [];
12 this.materialNames = [];
13 this.materialCount = 0;
16 this.findMaterialByName = this.findMaterialByName.bind(
this);
17 this.populateForm = this.populateForm.bind(
this);
18 this.setupEventHandlers = this.setupEventHandlers.bind(
this);
19 this.setupXML = this.setupXML.bind(
this);
26 findMaterialByName(name) {
27 let foundMaterial =
null;
29 for (
const element of this.materialsList) {
30 let resultsArray = JSON.parse(element).results;
32 for (
const result of resultsArray) {
33 if (result.title === name) {
34 foundMaterial = result;
36 this.populateForm(result);
45 updateStatusInput(message, force =
false)
47 const inputDOM = document.getElementById(
'status_message');
48 if (inputDOM.value ==
'Status' || force)
49 inputDOM.value = message
51 inputDOM.value +=
'\n' + message
53 inputDOM.scrollTop = inputDOM.scrollHeight;
56 handleMaterialXDownLoad(data)
58 console.log(
'WEB: materialx downloaded event:', data);
59 this.materialCount = data.materialCount;
60 this.materialsList = data.materialsList;
61 this.materialNames = data.materialNames;
63 if (this.materialCount > 0) {
65 for (
const element of this.materialsList) {
66 listString += element +
'\n';
68 this.editor.setValue(listString);
71 const materialSelect = document.getElementById(
'materialSelect');
72 materialSelect.innerHTML =
'';
73 this.materialNames.forEach((name, index) => {
74 const option = document.createElement(
'option');
75 option.value = index + 1;
77 materialSelect.appendChild(option);
81 const firstMaterial = JSON.parse(this.materialsList[0]).results[0];
83 this.populateForm(firstMaterial);
88 handleMaterialXExtract(data)
90 console.log(
'WEB: materialx extracted event:', data.extractedData);
91 const extractedData = data.extractedData[0];
92 const title = extractedData.title;
93 console.log(
'Title:', title);
95 const dataObj = extractedData.data;
96 const imageDOM = document.getElementById(
'extracted_images');
97 imageDOM.innerHTML =
'';
99 const mtlxDOM = document.getElementById(
'extracted_mtlx');
100 this.extractedEditor.setValue(
'');
104 let save_extracted = document.getElementById(
'save_extracted').checked;
105 let zip = save_extracted ?
new JSZip() :
null;
106 for (
const key in dataObj) {
107 if (key.endsWith(
'.mtlx')) {
108 this.extractedEditor.setValue(dataObj[key]);
111 zip.file(key, dataObj[key]);
113 const base64String = dataObj[key];
114 const binary = atob(base64String);
115 const array =
new Uint8Array(binary.length);
116 for (let i = 0; i < binary.length; i++) {
117 array[i] = binary.charCodeAt(i);
119 const extension = key.split(
'.').pop();
120 const blob =
new Blob([array], { type: `image/${extension}` });
121 const url = URL.createObjectURL(blob);
124 const imageContainer = document.createElement(
'div');
125 imageContainer.style.display =
'inline-block';
126 imageContainer.style.margin =
'10px';
127 imageContainer.style.textAlign =
'center';
130 const img = document.createElement(
'img');
139 const label = document.createElement(
'div');
140 label.innerText = key;
141 label.style.fontSize =
'0.8rem';
145 imageContainer.appendChild(img);
146 imageContainer.appendChild(label);
149 imageDOM.appendChild(imageContainer);
154 const imgFile =
new File([blob], key, { type:
'image/${extension}' });
155 zip.file(key, imgFile);
163 zip.generateAsync({ type:
'blob' }).then(
function(content) {
165 const link = document.createElement(
'a');
166 link.href = URL.createObjectURL(content);
167 link.download = title +
'.zip';
170 .
catch(
function(error) {
171 console.error(
'Error creating zip file:', error);
177 const materialSelect = document.getElementById(
'materialSelect');
178 let selectedItem = materialSelect.options[materialSelect.selectedIndex].text;
179 console.log(
"WEB: Emitting extract_material event");
180 this.emit(
'extract_material', { expression: selectedItem });
183 downloadMaterials() {
184 console.log(
"WEB: Emitting download_materialx event");
185 this.emit(
'download_materialx', {});
188 setupEventHandlers() {
190 document.getElementById(
'clear_status').addEventListener(
'click', () => {
191 this.updateStatusInput(
'Status',
true);
195 document.getElementById(
'extract_material').addEventListener(
'click', () => {
196 this.extractMaterials();
200 document.getElementById(
'getMTLXButton').addEventListener(
'click', () => {
201 this.downloadMaterials();
206 materialx_status: (data) => { console.log(
'WEB: materialx status event:', data.message); this.updateStatusInput(data.message) },
207 materialx_downloaded: (data) => { this.handleMaterialXDownLoad(data) },
208 materialx_extracted: (data) => { this.handleMaterialXExtract(data) }
212 document.getElementById(
'materialSelect').addEventListener(
'change', () => {
213 const materialSelect = document.getElementById(
'materialSelect');
214 const selectedItem = materialSelect.options[materialSelect.selectedIndex].text;
215 this.findMaterialByName(selectedItem);
221 const materialXTextArea = document.getElementById(
'mtlxOutput');
222 this.editor = CodeMirror.fromTextArea(materialXTextArea, {
223 mode:
'application/json',
227 this.editor.setSize(
'auto',
'300px');
230 const materialXTextArea2 = document.getElementById(
'extracted_mtlx');
231 this.extractedEditor = CodeMirror.fromTextArea(materialXTextArea2, {
232 mode:
'application/xml',
236 this.extractedEditor.setSize(
'auto',
'300px');
240 document.getElementById(
'm_title').value = data.title ||
'';
241 document.getElementById(
'author').value = data.author ||
'';
242 const pdate = data.published_date ||
'';
244 const date =
new Date(pdate);
245 const offset = date.getTimezoneOffset();
246 const localDate =
new Date(date.getTime() - offset * 60 * 1000);
247 const formattedDate = localDate.toISOString().slice(0, 16);
248 document.getElementById(
'published_date').value = formattedDate;
250 document.getElementById(
'mtlx_filename').value = data.mtlx_filename ||
'';
251 document.getElementById(
'category').value = data.category ||
'';
252 document.getElementById(
'status').value = data.status ||
'Unpublished';
253 document.getElementById(
'tags').value = data.tags ? data.tags.join(
', ') :
'';
254 document.getElementById(
'packages').value = data.packages ? data.packages.join(
', ') :
'';
255 document.getElementById(
'renders').value = data.renders ? data.renders.join(
', ') :
'';
256 document.getElementById(
'description').value = data.description ||
'';