add
This commit is contained in:
1 parent
bea0fff388
commit
4ac51969e2
37 files changed
+1270
-166
No files matched your search
@@ -0,0 +1,105 @@
|
||||
/* ============================================================
|
||||
หน้าเกี่ยวกับเรา — loader
|
||||
------------------------------------------------------------
|
||||
อ่านเนื้อหาจาก partials/about-data/ เรียงสลับกันไปเรื่อย ๆ:
|
||||
text1.json + pic1.<ext> , text2.json + pic2.<ext> , ...
|
||||
- textN.json: { "th": { "title": "...", "body": ["...", "..."] },
|
||||
"en": { "title": "...", "body": [...] } }
|
||||
(ไม่เขียน en → โหมด EN ใช้ th แทน)
|
||||
- picN รองรับ .jpg .jpeg .png .webp
|
||||
- หยุดเองเมื่อหา textN.json ถัดไปไม่เจอ
|
||||
============================================================ */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var DATA_DIR = './partials/about-data/';
|
||||
var IMG_EXTS = ['jpg', 'jpeg', 'png', 'webp'];
|
||||
var MAX_BLOCKS = 30;
|
||||
|
||||
function lang() {
|
||||
return (window.I18N && I18N.getLang()) || 'th';
|
||||
}
|
||||
|
||||
function pick(data, l) {
|
||||
if (!data) return null;
|
||||
if (l !== 'th' && data[l]) return data[l];
|
||||
return data.th || data[l] || data[Object.keys(data)[0]] || data;
|
||||
}
|
||||
|
||||
function fetchJSON(n) {
|
||||
return fetch(DATA_DIR + 'text' + n + '.json', { cache: 'no-store' })
|
||||
.then(function (r) { return r.ok ? r.json() : null; })
|
||||
.catch(function () { return null; });
|
||||
}
|
||||
|
||||
function findImage(n) {
|
||||
var i = 0;
|
||||
function tryNext() {
|
||||
if (i >= IMG_EXTS.length) return Promise.resolve(null);
|
||||
var url = DATA_DIR + 'pic' + n + '.' + IMG_EXTS[i++];
|
||||
return fetch(url, { method: 'HEAD', cache: 'no-store' })
|
||||
.then(function (r) { return r.ok ? url : tryNext(); })
|
||||
.catch(tryNext);
|
||||
}
|
||||
return tryNext();
|
||||
}
|
||||
|
||||
function renderBlock(host, data, imgUrl) {
|
||||
var c = pick(data, lang());
|
||||
if (!c) return;
|
||||
var art = document.createElement('article');
|
||||
art.className = 'mu-about-block';
|
||||
|
||||
var text = document.createElement('div');
|
||||
text.className = 'mu-about-text';
|
||||
var h2 = document.createElement('h2');
|
||||
h2.textContent = c.title || '';
|
||||
text.appendChild(h2);
|
||||
(c.body || c.paragraphs || []).forEach(function (p) {
|
||||
var el = document.createElement('p');
|
||||
el.textContent = p;
|
||||
text.appendChild(el);
|
||||
});
|
||||
art.appendChild(text);
|
||||
|
||||
if (imgUrl) {
|
||||
var media = document.createElement('div');
|
||||
media.className = 'mu-about-media';
|
||||
var img = document.createElement('img');
|
||||
img.src = imgUrl;
|
||||
img.alt = c.imgAlt || c.title || '';
|
||||
img.loading = 'lazy';
|
||||
img.decoding = 'async';
|
||||
media.appendChild(img);
|
||||
art.appendChild(media);
|
||||
}
|
||||
host.appendChild(art);
|
||||
}
|
||||
|
||||
function init() {
|
||||
var host = document.getElementById('about-blocks');
|
||||
if (!host) return;
|
||||
var cleared = false;
|
||||
|
||||
var n = 1;
|
||||
function next() {
|
||||
if (n > MAX_BLOCKS) return;
|
||||
var current = n++;
|
||||
fetchJSON(current).then(function (data) {
|
||||
if (!data) return; // ไม่มี textN ถัดไป → จบ
|
||||
if (!cleared) { host.innerHTML = ''; cleared = true; }
|
||||
findImage(current).then(function (imgUrl) {
|
||||
renderBlock(host, data, imgUrl);
|
||||
next();
|
||||
});
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user