const MszMessagesThread = function(thread) { if(!(thread instanceof Element)) throw 'thread must be an element'; const messages = Array.from(thread.querySelectorAll('.js-messages-message')).map(elem => new MszMessagesThreadMessage(elem)); const message = messages.find(msg => msg.isFull()); return { getMessage: () => message, getMessages: () => messages, }; }; const MszMessagesThreadMessage = function(message) { if(!(message instanceof Element)) throw 'message must be an element'; const msgId = message.dataset.msgId; const type = message.dataset.msgType; const url = message.dataset.msgUrl; if(type === 'snip') { message.addEventListener('click', ev => { if(typeof url !== 'string') return; let target = ev.target; while(target !== message) { if(target instanceof HTMLAnchorElement) return; target = target.parentNode; } ev.preventDefault(); location.assign(url); }); } else if(type === 'full') { message.scrollIntoView({ behavior: 'smooth', block: 'start', }); } const isRead = () => message.dataset.msgRead === 'read'; const setRead = state => { message.dataset.msgRead = state ? 'read' : 'unread'; }; const isSent = () => message.dataset.msgSent === 'sent'; const setSent = state => { message.dataset.msgRead = state ? 'sent' : 'draft'; }; const isDeleted = () => message.dataset.msgDeleted === 'yes'; const setDeleted = state => { if(state) { message.dataset.msgDeleted = 'yes'; message.classList.add('messages-message-deleted'); } else { message.dataset.msgDeleted = 'no'; message.classList.remove('messages-message-deleted'); } }; return { getId: () => msgId, getType: () => type, isFull: () => type === 'full', isSnippet: () => type === 'snip', isRead: isRead, setRead: setRead, isSent: isSent, setSent: setSent, isDeleted: isDeleted, setDeleted: setDeleted, }; };