"use strict"; var FW_serverGenerated; var FW_serverFirstMsg = (new Date()).getTime()/1000; var FW_serverLastMsg = FW_serverFirstMsg; var FW_isIE = (navigator.appVersion.indexOf("MSIE") > 0); var FW_isiOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/); var FW_scripts = {}, FW_links = {}; var FW_docReady = false; var FW_root = "/fhem"; // root // createFn returns an HTML Element, which may contain // - setValueFn, which is called when data via longpoll arrives // - activateFn, which is called after the HTML element is part of the DOM. var FW_widgets = { select: { createFn:FW_createSelect }, slider: { createFn:FW_createSlider }, time: { createFn:FW_createTime }, noArg: { createFn:FW_createNoArg }, multiple: { createFn:FW_createMultiple }, "multiple-strict": { createFn:FW_createMultiple }, textfield: { createFn:FW_createTextField }, "textfield-long": { createFn:FW_createTextField } }; window.onbeforeunload = function(e) { FW_leaving = 1; return undefined; } function FW_replaceWidgets(parent) { parent.find("div.fhemWidget").each(function() { var dev=$(this).attr("dev"); var cmd=$(this).attr("cmd"); var rd=$(this).attr("reading"); var params = cmd.split(" "); var type=$(this).attr("type"); if( type == undefined ) type = "set"; FW_replaceWidget(this, dev, $(this).attr("arg").split(","), $(this).attr("current"), rd, params[0], params.slice(1), function(arg) { FW_cmd(FW_root+"?cmd="+type+" "+dev+ (params[0]=="state" ? "":" "+params[0])+" "+arg+"&XHR=1"); }); }); } function FW_jqueryReadyFn() { FW_docReady = true; FW_serverGenerated = document.body.getAttribute("generated"); if(document.body.getAttribute("longpoll")) setTimeout("FW_longpoll()", 100); $("div.devSpecHelp a").each(function(){ // Help on detail window var dev = $(this).attr("href").split("#").pop(); $(this).attr("href", "#"); $(this).click(function(evt){ if($("#devSpecHelp").length) { $("#devSpecHelp").remove(); return; } $("#content").append('
'); FW_cmd(FW_root+"?cmd=help "+dev+"&XHR=1", function(data) { $("#devSpecHelp").html(data); var off = $("#devSpecHelp").position().top-20; $('body, html').animate({scrollTop:off}, 500); }); }); }); $("a").each(function() { FW_replaceLink(this); }) $("head script").each(function() { var sname = $(this).attr("src"), p = FW_scripts[sname]; if(!p) { FW_scripts[sname] = { loaded:true }; return; } FW_scripts[sname].loaded = true; if(p.callbacks && !p.called) { p.called = true; // Avoid endless loop for(var i1=0; i1< p.callbacks.length; i1++) if(p.callbacks[i1]) // pushing undefined callbacks on the stack is ok p.callbacks[i1](); delete(p.callbacks); } }); $("head link").each(function() { FW_links[$(this).attr("href")] = 1 }); $("div.makeSelect select").each(function() { FW_detailSelect(this); $(this).change(FW_detailSelect); }); // Activate the widgets var r = $("head").attr("root"); if(r) FW_root = r; FW_replaceWidgets($("html")); // Fix the td count by setting colspan on the last column $("table.block.wide").each(function(){ // table var id = $(this).attr("id"); if(!id || id.indexOf("TYPE") != 0) return; var maxTd=0, tdCount=[]; $(this).find("tr").each(function(){ // count the td's var cnt=0; $(this).find("td").each(function(){ cnt++; }); if(maxTd < cnt) maxTd = cnt; tdCount.push(cnt); }); $(this).find("tr").each(function(){ // set the colspan $(this).find("td").last().attr("colspan", maxTd-tdCount.shift()+1); }); }); // Replace the FORM-POST in detail-view by XHR /* Inactive, as Internals and Attributes arent auto updated. $("form input[type=submit]").click(function(e) { var cmd = ""; $(this).parent().find("[name]").each(function() { cmd += (cmd?"&":"")+$(this).attr("name")+"="+$(this).val(); }); if(cmd.indexOf("detail=") < 0) return; e.preventDefault(); FW_cmd(FW_root+"?"+cmd+"&XHR=1"); }); */ $("form input.get[type=submit]").click(function(e) { //"get" via XHR to dialog e.preventDefault(); var cmd = "", el=this; $(el).parent().find("input,[name]").each(function() { cmd += (cmd?"&":"")+$(this).attr("name")+"="+$(this).val(); }); FW_cmd(FW_root+"?"+cmd+"&XHR=1&addLinks=1", function(data) { if(!data.match(/^[\r\n]*$/)) // ignore empty answers FW_okDialog(''+data+'', el); }); }); $("#saveCheck") .css("cursor", "pointer") .click(function(){ var parent = this; FW_cmd(FW_root+"?cmd=save ?&XHR=1", function(data) { FW_okDialog('
'+data+'',parent); }); }); $("form").each(function(){ // shutdown handling var input = $(this).find("input.maininput"); if(!input.length) return; $(this).on("submit", function() { if($(input).val().match(/^\s*shutdown/)) { FW_cmd(FW_root+"?XHR=1&cmd="+$(input).val()); $(input).val(""); return false; } return true; }); }); } if(window.jQuery) { $(document).ready(FW_jqueryReadyFn); } else { // FLOORPLAN compatibility loadScript("pgm2/jquery.min.js", function() { loadScript("pgm2/jquery-ui.min.js", function() { FW_jqueryReadyFn(); }, true); }, true); } // FLOORPLAN compatibility function FW_delayedStart() { setTimeout("FW_longpoll()", 100); } function log(txt) { var d = new Date(); var ms = ("000"+(d.getMilliseconds()%1000)); ms = ms.substr(ms.length-3,3); txt = d.toTimeString().substring(0,8)+"."+ms+" "+txt; if(typeof window.console != "undefined") console.log(txt); } function addcsrf(arg) { var csrf = document.body.getAttribute('fwcsrf'); if(csrf && arg.indexOf('fwcsrf') < 0) arg += '&fwcsrf='+csrf; return arg; } function FW_cmd(arg, callback) { log("FW_cmd:"+arg); arg = addcsrf(arg); var req = new XMLHttpRequest(); req.open("POST", arg, true); req.send(null); req.onreadystatechange = function(){ if(req.readyState == 4 && req.responseText) { if(callback) callback(req.responseText); else FW_errmsg(req.responseText, 5000); } } } function FW_errmsg(txt, timeout) { log("ERRMSG:"+txt+"<"); var errmsg = document.getElementById("errmsg"); if(!errmsg) { if(txt == "") return; errmsg = document.createElement('div'); errmsg.setAttribute("id","errmsg"); document.body.appendChild(errmsg); } if(txt == "") { document.body.removeChild(errmsg); return; } errmsg.innerHTML = txt; if(timeout) setTimeout("FW_errmsg('')", timeout); } function FW_okDialog(txt, parent) { var div = $("