Commit 627ab87f authored by edwin's avatar edwin

refactor createDOMString, change fetch url and update filter logic again

parent 2a517d0e
......@@ -2,7 +2,7 @@
const baseurl = "https://digitallibrary-external.fastretailing.com/";
const fetchNumber = 30;
const sleepTime = 3000;
const sleepTime = 2500;
let allowDebugMessage = true;
// adding the lightbox (control panel) to the body
......@@ -93,7 +93,7 @@
// fetch product URL
let currentms = new Date().getTime();
let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&et=hk&c=DEFAULT`, `${SKU} product image`);
let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&c=DEFAULT`, `${SKU} product image`);
let DOMString = createDOMString(SKU, assetlist);
container.insertAdjacentHTML('beforeend', DOMString);
clientSideFilter();
......@@ -140,33 +140,10 @@
}
function createDOMString(SKU, assetlist){
return assetlist.map(asset => {
// determine whether it is product file
let classes = "";
let tagid = asset.tags.map(x => x.id);
if (!(asset.file_extension === "jpg" || asset.file_extension === "gif") ||
asset.filename.includes(`${SKU}_I0`) ||
(asset.filename.includes("goods_") && !asset.filename.includes(SKU)) ||
tagid.includes("assettype/Poster") ||
asset.filename.includes("GU_")
) {
classes = "is-unrelated";
} else if (
asset.filename.includes(`goods_${SKU}_sub`) ||
asset.filename.includes(`_${SKU}_chip`) ||
(asset.filename.match(new RegExp(`goods_[0-9]{2}_${SKU}$`)) && asset.models.length === 0 && asset.products.length === 1)
) {
classes = "is-product";
} else if( tagid.find(x => x.includes("Model") || x.includes("Appeal POP")) ) {
classes = "is-model";
} else{
classes = "is-unrelated";
}
let multilist = filtering(assetlist, SKU);
let createString = (asset, SKU, classes) => {
return `
<div class="asset-item ${classes}">
<div><img src="/api/asset/${asset.id}/thumbnail/medium?r=1" loading="lazy"/></div>
......@@ -179,7 +156,13 @@
<span class="asset-date" hidden>${asset.updatedDate}</span>
</div>
`;
}).join('');
};
let productString = multilist["product"].map(x => createString(x, SKU, "is-product")).join("");
let modelString = multilist["model"].map(x => createString(x, SKU, "is-model")).join("");
let unrelatedString = multilist["unrelated"].map(x => createString(x, SKU, "is-unrelated")).join("");
return productString + modelString + unrelatedString;
}
// perform operation similar to old extension
......@@ -200,7 +183,7 @@
// first link
let currentms = new Date().getTime();
let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&et=hk&c=DEFAULT`, `${SKU} product image`);
let assetlist = await fetchSKU(`${baseurl}api/asset?time=${currentms}&t=main_product%2FGU%2F${SKU}&t=datacategory%2Fecasset&r=north&z=${fetchNumber}&c=DEFAULT`, `${SKU} product image`);
let downloadlist = filtering(assetlist, SKU);
await downloadList(downloadlist["product"], `${SKU}`);
await sleep(sleepTime);
......@@ -294,7 +277,6 @@
}
// filter asset to different array
// only used on Download All SKUs
// usage:
// let downloadlist = filtering(assetlist, SKU);
......@@ -305,26 +287,28 @@
let tagid = item.tags.map(x => x.id);
if (item.filename.includes(`goods_${SKU}_sub`) ||
item.filename.includes(`_${SKU}_chip`) ||
(item.filename.match(new RegExp(`goods_[0-9]{2}_${SKU}$`)) && item.models.length === 0 && item.products.length === 1) // file name include SKU and no model
if ( item.filename.match(new RegExp(`^goods_${SKU}_sub`)) ||
item.filename.includes(`_${SKU}_chip`) ||
(item.filename.match(new RegExp(`^goods_[0-9]{2}_${SKU}$`)) && item.models.length === 0 && item.products.length === 1) // file name include SKU and no model
) {
arrlist["product"].push(item);
} else if (item.filename.includes(`${SKU}_I0`) ||
(item.filename.includes("goods") && !item.filename.includes(SKU)) ||
(item.filename.includes("goods") && ( !item.filename.includes(SKU) || item.filename[0] !== 'h' )) ||
tagid.includes("assettype/Poster") ||
item.filename.includes("GU_") ||
item.filename.match(new RegExp(`^[0-9]{6}_.{8}_[0-9]{2}_.{4}`))
) {
// not adding to model
arrlist["unrelated"].push(item);
}else if ( tagid.find(x => x.includes("Model") || x.includes("Appeal POP")) ) {
arrlist["model"].push(item);
}else{
arrlist["unrelated"].push(item);
}
return arrlist;
}, { "product": [], "model": [] }); // initial value
}, { "product": [], "model": [], "unrelated": [] }); // initial value
return multilist;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment