check page: add share button (parity with Next) + shared share row styles
- ui/share.js: one handler for all pages (id=share-btn / id=share-status), short link = origin+path+hash, navigator.share + clipboard, LINE link, re-binds after router swaps the view (MutationObserver), skips buttons that a page already bound itself - checkSchedule.html: share row inside .mu-container (same position as Next) - _shared.css: .mu-share-* moved here from home.html, values copied from Next (ghost button, radius 8, 44px, tokens) so light/dark match - home.init.js marks its own binding so the shared handler does not double-bind - index.html + sw PRECACHE: share.js · VERSION 2026.09.27.124
This commit is contained in:
1 parent
1adc2a6cd0
commit
b6ccb66ce2
8 files changed
+189
-39
No files matched your search
+88
@@ -0,0 +1,88 @@
|
||||
/* ============================================================
|
||||
share.js — ปุ่ม "แชร์หน้านี้" ใช้ร่วมทุกหน้า (mock)
|
||||
------------------------------------------------------------
|
||||
พอร์ตจาก web-at-transit (lib/share.ts + ปุ่มในทุก view)
|
||||
- ปุ่มต้องมี id="share-btn" · ข้อความสถานะ id="share-status"
|
||||
- ลิงก์สั้น = origin + path + hash ของหน้าปัจจุบัน (ไม่ฝังชื่อเว็บ)
|
||||
- มี navigator.share → เปิดแผงแชร์ของระบบ · พร้อมคัดลอกลงคลิปบอร์ด
|
||||
- ไม่มี → คัดลอกอย่างเดียว
|
||||
- ทำงานซ้ำได้หลัง router เปลี่ยนหน้า (ข้ามปุ่มที่ผูกแล้ว)
|
||||
============================================================ */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
function t(key, fallback) {
|
||||
var v = (window.I18N && I18N.t) ? I18N.t(key) : key;
|
||||
return (!v || v === key) ? fallback : v;
|
||||
}
|
||||
|
||||
function shortUrl() {
|
||||
return location.origin + location.pathname + location.hash;
|
||||
}
|
||||
|
||||
function esc(s) {
|
||||
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
|
||||
return ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c];
|
||||
});
|
||||
}
|
||||
|
||||
function bind() {
|
||||
var btn = document.getElementById('share-btn');
|
||||
var status = document.getElementById('share-status');
|
||||
if (!btn || btn.dataset.bound === '1') return;
|
||||
btn.dataset.bound = '1';
|
||||
|
||||
function paint(msg) {
|
||||
if (!status) return;
|
||||
var url = shortUrl();
|
||||
status.innerHTML = esc(msg) +
|
||||
' <code class="mu-share-url">' + esc(url) + '</code> ' +
|
||||
'<a class="mu-share-line" href="https://social-plugins.line.me/lineit/share?url=' +
|
||||
encodeURIComponent(url) + '" target="_blank" rel="noopener">' +
|
||||
esc(t('share.line', 'แชร์ผ่าน LINE')) + '</a>';
|
||||
}
|
||||
|
||||
btn.addEventListener('click', function () {
|
||||
var url = shortUrl();
|
||||
|
||||
if (navigator.share) {
|
||||
navigator.share({ title: document.title, url: url }).catch(function () {});
|
||||
}
|
||||
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
navigator.clipboard.writeText(url).then(function () {
|
||||
paint(t('share.copied', 'คัดลอกลิงก์แล้ว'));
|
||||
}).catch(function () {
|
||||
paint(t('share.short', 'ลิงก์สั้น'));
|
||||
});
|
||||
} else {
|
||||
paint(t('share.short', 'ลิงก์สั้น'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
window.AtShareInit = bind;
|
||||
bind();
|
||||
|
||||
/* router inject partial ใหม่ทุกครั้งที่เปลี่ยนหน้า → ผูกปุ่มให้เอง */
|
||||
(function watch() {
|
||||
var host = document.getElementById('view');
|
||||
if (!host || !window.MutationObserver) return;
|
||||
var queued = 0;
|
||||
new MutationObserver(function (list) {
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
if (list[i].addedNodes && list[i].addedNodes.length) {
|
||||
if (queued) return;
|
||||
queued = 1;
|
||||
setTimeout(function () { queued = 0; bind(); }, 60);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}).observe(host, { childList: true, subtree: true });
|
||||
})();
|
||||
window.addEventListener('langchange', function () {
|
||||
var btn = document.getElementById('share-btn');
|
||||
if (btn) delete btn.dataset.bound;
|
||||
bind();
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user