X-Git-Url: https://git.rvb.name/esp-clock.git/blobdiff_plain/018c87cf62ac447add9510fe2cafd21ef1e75040..refs/remotes/github/master:/data/web/script.js diff --git a/data/web/script.js b/data/web/script.js index c40f7e1..ee43439 100644 --- a/data/web/script.js +++ b/data/web/script.js @@ -1,6 +1,7 @@ var pages var parameters = {} var daynames = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'] +var msgT function toggleMenu(e) { active = (document.getElementById('menuLink').className.indexOf('active') !== -1) @@ -28,22 +29,22 @@ function encode(r){ } function getAnchor() { - return window.location.hash; + return window.location.hash.slice(1); } -function DrawHeader(project) { +function drawHeader(project) { var menu_header = document.getElementById('_ui_menu_header') menu_header.innerHTML = project.name document.title = project.name + '/' + project.version } -function ParseJsonQ(url, callback) { +function parseJsonQ(url, callback) { var req = new XMLHttpRequest(); req.onreadystatechange = function () { if (this.readyState != 4) return; if (this.status != 200 && this.status != 500 && this.status != 404) { - setTimeout(ParseJsonQ(url, callback),30000); + setTimeout(parseJsonQ(url, callback),30000); return; } var json = JSON.parse(this.responseText) @@ -55,7 +56,7 @@ function ParseJsonQ(url, callback) { } -function UpdateElement(id, value) { +function updateElement(id, value) { var element = document.getElementById("_ui_element_"+id) if (!element) return; var ui_class = element.dataset.ui_class; @@ -87,17 +88,17 @@ function UpdateElement(id, value) { } } -function UpdateValues(json) { +function updateValues(json) { for (var key in json) { var obj = document.getElementById("_ui_element_"+key) if (obj) { - UpdateElement(key, json[key]) + updateElement(key, json[key]) } parameters[key] = json[key] } var notification = document.getElementById('_ui_notification'); if (parameters['_changed']) { - notification.innerHTML = '' + notification.innerHTML = '' notification.removeAttribute('hidden') } else { notification.innerHTML = '' @@ -114,35 +115,39 @@ function sendUpdate(id) { case 'number': case 'range': if (input.checkValidity() && input.value != parameters[id]) { - ParseJsonQ('/config/set?name=' + id + '&value=' + encodeURIComponent(input.value), function(json) { - UpdateValues(json) + parseJsonQ('/config/set?name=' + id + '&value=' + encodeURIComponent(input.value), function(json) { + updateValues(json) }) } break case 'select': - ParseJsonQ('/config/set?name=' + id + '&value=' + encodeURIComponent(input.selectedOptions[0].value), function(json) { - UpdateValues(json) + parseJsonQ('/config/set?name=' + id + '&value=' + encodeURIComponent(input.selectedOptions[0].value), function(json) { + updateValues(json) }) break; case 'checkbox': - ParseJsonQ('/config/set?name=' + id + '&value=' + (input.checked?'true':'false'), function(json) { - UpdateValues(json) + parseJsonQ('/config/set?name=' + id + '&value=' + (input.checked?'true':'false'), function(json) { + updateValues(json) }) break; case 'week': - ParseJsonQ('/config/set?name=' + id + '&value=' + input.dataset.value, function(json) { - UpdateValues(json) + parseJsonQ('/config/set?name=' + id + '&value=' + input.dataset.value, function(json) { + updateValues(json) }) break; } } -function sendAction(name) { - ParseJsonQ('/action?name=' + name, function(json) { +function sendAction(name, params = {}) { + var url = '/action?name=' + name + for (var param in params) { + url += '&'+param+'='+encodeURIComponent(params[param]) + } + parseJsonQ(url, function(json) { if (json.result == 'FAILED') { alert(json.message) if (json.page) { - DrawPage(json.page) + drawPage(json.page) } } else { location.reload() @@ -150,7 +155,7 @@ function sendAction(name) { }) } -function ShowPwd(id) { +function showPwd(id) { var x = document.getElementById('_ui_element_' + id) if (x.type === "password") { x.type = "text"; @@ -159,20 +164,41 @@ function ShowPwd(id) { } } -function OpenSelect(id) { +function openSelect(id) { var selector = document.getElementById('_ui_elemmodal_'+id); selector.removeAttribute("hidden") } -function CloseSelect(id) { +function closeSelect(id) { var selector = document.getElementById('_ui_elemmodal_'+id); selector.hidden = true } -function SelectWiFi(id, ssid) { - CloseSelect(id); +function closeMsg() { + document.getElementById("_ui_message").hidden = true; +} + +function fadeMsg() { + var msg = document.getElementById('_ui_message'); + msg.classList.add("fadeout") + msgT = setTimeout(()=> { closeMsg() }, 5000); +} + +function openMsg(msgText) { + document.getElementById("_ui_message_text").innerText = msgText; + document.getElementById("_ui_message").classList.remove("fadeout"); + document.getElementById("_ui_message").removeAttribute('hidden'); + + if (msgT) { + window.clearTimeout(msgT); + } + msgT = setTimeout(()=> { fadeMsg(); }, 5000); +} + +function selectWiFi(id, ssid) { + closeSelect(id); var x = document.getElementById('_ui_element_' + id) - UpdateElement(x,ssid); + updateElement(x,ssid); sendUpdate(id) } @@ -188,13 +214,13 @@ function getWiFi(id) { return; } var json = JSON.parse(this.responseText) - var table = '' + var table = '
SSIDBSSIDRSSIКаналЗащита
' if (!json.length) { setTimeout(getWiFi(id),5000); } for (idx in json) { var encryption = json[idx].secure == 2? "TKIP" : json[idx].secure == 5? "WEP" : json[idx].secure == 4? "CCMP" : json[idx].secure == 7? "нет" : json[idx].secure == 8? "Автоматически" : "Не определено"; - table += '' + table += '' } table += '
SSIDBSSIDRSSIКаналЗащита
'+json[idx].ssid+''+json[idx].bssid+''+json[idx].rssi+''+json[idx].channel+''+encryption+'
'+json[idx].ssid+''+json[idx].bssid+''+json[idx].rssi+''+json[idx].channel+''+encryption+'
' @@ -203,10 +229,9 @@ function getWiFi(id) { req.open("GET", "/wifi/scan", true); req.send() - } -function ClickDay(id, day) { +function clickDay(id, day) { value = parameters[id].split('') day_value = value[day] day_value = (day_value=='0')?'1':'0' @@ -217,7 +242,35 @@ function ClickDay(id, day) { sendUpdate(id); } -function ElementHTML(element) { +function sendTime(id) { + var value = document.getElementById('_ui_element_' + id).value + if (value) { + var date = new Date(value) + var timestamp = Math.floor(date.getTime()/1000); + sendAction('time',{"timestamp":timestamp}) + } +} + +function downloadFile(url, name) { + const a = document.createElement('a') + a.href = url + a.download = name?name:url.split('/').pop() + document.body.appendChild(a) + a.click() + document.body.removeChild(a) +} + +function uploadConfig() { + var elem = document.getElementById('_config_file') + if (elem.value) { + var data = elem.files[0] + console.log(data) + fetch('/config/put', {method:'PUT',body:data}); + elem.value = null + } +} + +function elementHTML(element) { var value if (parameters[element.id] || !isNaN(parameters[element.id])) { value = parameters[element.id] @@ -230,13 +283,13 @@ function ElementHTML(element) { case 'hr': return '

' case 'button': - return '
' + return '
' case 'password': return '
' - + '' - + '
' + + '
' + + '
' case 'input': var pattern = "" if (element.pattern) { @@ -251,15 +304,15 @@ function ElementHTML(element) { pattern = ' pattern="' + encode(element.pattern) + '"' } return '
' - + '' - + '
' + + '
' + + '
' + '' + + '
' + + '
' case 'checkbox': return '
' return days + case 'timeset': + var now = new Date() + now.setMinutes(now.getMinutes() - now.getTimezoneOffset()) + value = now.toISOString().slice(0, -1); + return '
' + +'' + + '
->
' case 'text': return '

' + encode(value) + '

' case 'number': @@ -299,15 +359,22 @@ function ElementHTML(element) { + '' + case 'config': + return '
' + + '
' + + '' + + '' + + '' + + '
' case 'table': default: - return '
' 
-        + encode(element.label)+ '
' + encode(value) + '
' + return '
' + + encode(element.label)+ '' + encode(value) + '
' } } -function DrawPage(id) { +function drawPage(id) { var idx =0, i=0 for (const page of pages) { var menu_link = document.getElementById('_ui_pglink_' + page.id) @@ -324,25 +391,25 @@ function DrawPage(id) { var page_content = document.getElementById("_ui_page_content"); var content = '' for (const element of pages[idx].elements) { - content = content + ElementHTML(element) + '\n' + content = content + elementHTML(element) + '\n' } page_content.innerHTML = content window.location.hash = id } -function DrawNavigator(project, pages) { +function drawNavigator(project, pages) { var menu = document.getElementById('_ui_menu_list'); var list = '' for (const page of pages) { list = list + '' } menu.innerHTML = list } -function DrawContacts(contacts) { +function drawContacts(contacts) { if (!contacts) return; var contact_list = '

Контакты

' for (contact of contacts) { @@ -369,21 +436,21 @@ function DrawContacts(contacts) { footer.innerHTML = contact_list } -function DrawUI(ui) { - DrawHeader(ui.project) +function drawUI(ui) { + drawHeader(ui.project) pages = ui.pages - DrawNavigator(ui.project, pages) - DrawContacts(ui.project.contacts) + drawNavigator(ui.project, pages) + drawContacts(ui.project.contacts) var anchor = getAnchor() if (anchor) { - DrawPage(anchor) + drawPage(anchor) } else { - DrawPage(pages[0].id) + drawPage(pages[0].id) } } function GetUI() { - ParseJsonQ("/ui", DrawUI); + parseJsonQ("/ui", drawUI); } GetUI() @@ -391,31 +458,32 @@ GetUI() function initES() { if (!!window.EventSource) { var source = new EventSource('/events'); + openMsg('Соединение установлено') source.onerror = function(e) { if (source.readyState == 2) { + openMsg('Соединение прервано') setTimeout(initES, 5000); } }; - source.addEventListener('keepalive', function(e) { - UpdateValues(JSON.parse(e.data)); - }, false); - source.addEventListener('update', function(e) { - UpdateValues(JSON.parse(e.data)); + updateValues(JSON.parse(e.data)); + }, false); + source.addEventListener('message', function(e) { + openMsg(e.data); }, false); } } initES(); -function DrawConfig(cfg) { - UpdateValues(cfg) +function drawConfig(cfg) { + updateValues(cfg) } function GetCfg() { - ParseJsonQ("/config/get", DrawConfig); + parseJsonQ("/config/get", drawConfig); } GetCfg()