$(document).ready(()=>{ const baseUrl = `http://${window.location.hostname}`; const content = $('.content'); const urlString = window.location.href; const url = new URL(urlString); const sid = url.searchParams.get("sid"); $.getJSON( baseUrl + "/apis/getMessages.php", { sid: sid }).done( (session)=> { console.log(session); if(session.enabled){ $.each(session.messages, (index,item)=>{ if(item.timer != session.start){ const now = new Date().getTime(); const timerValue = item.timer.split(' '); const timerDate = timerValue[0].split('-'); const timerHours = timerValue[1].split(':'); const timer = new Date(timerDate[0], timerDate[1] - 1, timerDate[2], timerHours[0], timerHours[1], timerHours[2]).getTime(); const startValue = session.start.split(' '); const startDate = startValue[0].split('-'); const startHours = startValue[1].split(':'); const start = new Date(startDate[0], startDate[1] - 1, startDate[2], startHours[0], startHours[1], startHours[2]).getTime(); if(now > timer){ showCloud(item.title, [item.short_text, item.long_text], item.image, item.actions, session.messages); notify(true); } else { const delay = (timer - now); setTimeout( ()=>{ showCloud(item.title, [item.short_text, item.long_text], item.image, item.actions, session.messages); notify(true); },delay); } } }); } }); function showCloud(title, text, img, actions, messages){ const image = img ? `
${text[1]}
` : ''; const cloud = $(`
${title}
${text[0]}
${long_text} ${image}
`); cloud.find('.toggle').on('click', (e)=>{ const button = $(e.currentTarget); const text = button.siblings('.text-content'); if(text.is(':visible')){ button.text('More...'); text.slideUp(); }else{ button.text('Less...'); text.slideDown(); } }); content.append(cloud); if(actions.length){ const actionsContainer = $('
'); $.each(actions, (index, value)=>{ const button = $(``); button.on('click', (e)=>{ const goto = $(e.currentTarget); const messageId = goto.data('goto'); const fullMessage = messages.filter( (m)=> m.id == messageId); showCloud(fullMessage[0].title, [fullMessage[0].short_text, fullMessage[0].long_text], fullMessage[0].image, fullMessage[0].actions, messages); notify(false); }); actionsContainer.append(button); }); content.append(actionsContainer); } scrollBottom(); } function scrollBottom(){ $('html, body').stop().animate({ scrollTop: $('.cloud').last().offset().top }, 1000); } function notify(vibrate){ if(vibrate){ const canVibrate = "vibrate" in navigator || "mozVibrate" in navigator; if (canVibrate && !("vibrate" in navigator)){navigator.vibrate = navigator.mozVibrate;} navigator.vibrate(500); } $('#notify1')[0].play(); } });