fhemweb_weekprofile.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. FW_version["fhemweb_weekprofile.js"] = "$Id: fhemweb_weekprofile.js 16213 2018-02-18 13:10:19Z Risiko $";
  2. var language = 'de';
  3. //for tooltip
  4. $(document).ready(function(){
  5. $('[data-toggle="tooltip"]').tooltip();
  6. language = window.navigator.userLanguage || window.navigator.language;
  7. });
  8. var shortDays = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
  9. function FW_weekprofileInputDialog(title,inp,def,parent,callback)
  10. {
  11. var div = $("<div id='FW_weekprofileInputDiolog'>");
  12. var table = $("<table>");
  13. var content = [];
  14. for(var i=0; i<title.length; i++){
  15. var tr =
  16. content[i] = $('<input type="'+inp[i]+'">').get(0);
  17. if (def)
  18. content[i].value = def[i];
  19. var tr = $("<tr>");
  20. var td1 = $("<td>");
  21. $(td1).append(title[i]);
  22. if (inp[i] == 'hidden') {
  23. $(td1).attr("colspan","2");
  24. $(td1).attr("align","center");
  25. }
  26. var td2 = $("<td>");
  27. $(td2).append(content[i]);
  28. $(tr).append(td1);
  29. $(tr).append(td2);
  30. table.append(tr);
  31. }
  32. $(div).append(table);
  33. $("body").append(div);
  34. $(div).dialog({
  35. dialogClass:"no-close",modal:true, width:"auto", closeOnEscape:true,
  36. maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
  37. title: title,
  38. buttons: [{text:"OK", click:function(){
  39. $(this).dialog("close");
  40. $(div).remove();
  41. if(callback) {
  42. var backCont = [];
  43. for(var i=0; i<content.length; i++){
  44. backCont[i] = content[i].value;
  45. if (inp[i] == 'checkbox')
  46. backCont[i] = content[i].checked;
  47. }
  48. callback(backCont,1);
  49. }
  50. }},{text:"CANCEL", click:function(){
  51. $(this).dialog("close");
  52. $(div).remove();
  53. content.value = null;
  54. if(callback)
  55. callback(null,0);
  56. }}]
  57. });
  58. if(parent)
  59. $(div).dialog( "option", "position", {
  60. my: "left top", at: "right bottom",
  61. of: parent, collision: "flipfit"
  62. });
  63. }
  64. function FW_weekprofileMultiSelDialog(title, elementNames, elementLabels, selected,freeInp,parent,callback)
  65. {
  66. var table = "<table>";
  67. if (elementNames) {
  68. for(var i1=0; i1<elementNames.length; i1++){
  69. var n = elementNames[i1];
  70. var l = n;
  71. if (elementLabels && elementLabels.length == elementNames.length)
  72. l = elementLabels[i1];
  73. var sel = 0;
  74. if (selected && selected.indexOf(n)>=0)
  75. sel=1;
  76. table += '<tr><td><div class="checkbox"><input name="'+n+'" type="checkbox"';
  77. table += (sel ? " checked" : "")+'/><label for="'+n+'"><span></span></label></div></td>';
  78. table += '<td><label for="' +n+'">'+l+'</label></td></tr>';
  79. }
  80. }
  81. table += "</table>";
  82. var div = $("<div id='FW_weekprofileMultiSelDiolog'>");
  83. $(div).append(title);
  84. $(div).append(table);
  85. if (freeInp && freeInp == 1)
  86. $(div).append('<input id="FW_weekprofileMultiSelDiologFreeText" />');
  87. $("body").append(div);
  88. $(div).dialog({
  89. dialogClass:"no-close",modal:true, width:"auto", closeOnEscape:true,
  90. maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
  91. title: title,
  92. buttons: [{text:"OK", click:function(){
  93. var res=[];
  94. if($("#FW_weekprofileMultiSelDiologFreeText").val())
  95. res.push($("#FW_weekprofileMultiSelDiologFreeText").val());
  96. $("#FW_weekprofileMultiSelDiolog table input").each(function(){
  97. if($(this).prop("checked"))
  98. res.push($(this).attr("name"));
  99. });
  100. $(this).dialog("close");
  101. $(div).remove();
  102. if(callback)
  103. callback(res);
  104. }},{text:"CANCEL", click:function(){
  105. $(this).dialog("close");
  106. $(div).remove();
  107. if(callback)
  108. callback(null);
  109. }}]
  110. });
  111. if(parent)
  112. $(div).dialog( "option", "position", {
  113. my: "left top", at: "right bottom",
  114. of: parent, collision: "flipfit"
  115. });
  116. }
  117. function FW_GetTranslation(widget,translate)
  118. {
  119. if (widget.TRANSLATIONS == null)
  120. return translate;
  121. var translated = widget.TRANSLATIONS[translate];
  122. if (translated.length == 0)
  123. return translate;
  124. return translated;
  125. }
  126. function weekprofile_DoEditWeek(devName,newPage)
  127. {
  128. var widget = $('div[informid="'+devName+'"]').get(0);
  129. if (newPage == 1) {
  130. var csrfToken = $("body").attr('fwcsrf');
  131. var url = FW_root+'?cmd={weekprofile_editOnNewpage("'+widget.DEVICE+'","'+widget.CURTOPIC+':'+widget.CURPRF+'");;}';
  132. if (csrfToken)
  133. url = url + '&fwcsrf='+ csrfToken;
  134. window.location.assign(url);
  135. } else {
  136. widget.MODE = 'EDIT';
  137. $(widget.MENU.BASE).hide();
  138. widget.setValueFn("REUSEPRF");
  139. }
  140. }
  141. function FW_weekprofilePRF_chached(devName,select)
  142. {
  143. var widget = $('div[informid="'+devName+'"]').get(0)
  144. var prfName = select.options[select.selectedIndex].value;
  145. widget.CURPRF = prfName;
  146. widget.PROFILE = null;
  147. FW_queryValue('get '+devName+' profile_data '+widget.CURTOPIC+':'+widget.CURPRF, widget);
  148. }
  149. function FW_weekprofileTOPIC_chached(devName,select)
  150. {
  151. var widget = $('div[informid="'+devName+'"]').get(0)
  152. var topicName = select.options[select.selectedIndex].value;
  153. widget.CURTOPIC = topicName;
  154. widget.CURPRF = null;
  155. widget.PROFILE = null;
  156. FW_cmd(FW_root+'?cmd=get '+devName+' profile_names '+widget.CURTOPIC+'&XHR=1',function(data){FW_weekprofileGetValues(devName,"PROFILENAMES",data);});
  157. }
  158. function FW_weekprofileChacheTo(devName,topicName,profileName)
  159. {
  160. var widget = $('div[informid="'+devName+'"]').get(0)
  161. widget.CURTOPIC = topicName;
  162. widget.CURPRF = profileName;
  163. widget.PROFILE = null;
  164. FW_cmd(FW_root+'?cmd=get '+devName+' profile_names '+widget.CURTOPIC+'&XHR=1',function(data){FW_weekprofileGetValues(devName,"PROFILENAMES",data);});
  165. }
  166. function FW_weekprofileRestoreTopic(devName,bnt)
  167. {
  168. var widget = $('div[informid="'+devName+'"]').get(0)
  169. FW_weekprofileInputDialog(["<p>Restore topic: '"+widget.CURTOPIC+"'&nbsp;?</p>"],["hidden"],null,bnt,function(name,ok){
  170. if (ok == 1)
  171. FW_cmd(FW_root+'?cmd=set '+devName+' restore_topic '+widget.CURTOPIC+'&XHR=1',function(data){
  172. if (data != "")
  173. {
  174. console.log(devName+" error restore topic '" +data+"'");
  175. FW_errmsg(devName+" error restore topic '" +data+"'",5000);
  176. return;
  177. }
  178. });
  179. });
  180. }
  181. function FW_weekprofileSendToDev(devName,bnt)
  182. {
  183. var widget = $('div[informid="'+devName+'"]').get(0)
  184. var deviceLst = null;
  185. bnt.setValueFn = function(data) {
  186. try {
  187. deviceLst=JSON.parse(data);
  188. var devicesNames = [];
  189. var devicesAlias = [];
  190. for (var k=0; k < deviceLst.length; k++) {
  191. devicesNames.push(deviceLst[k]['NAME']);
  192. devicesAlias.push(deviceLst[k]['ALIAS']);
  193. }
  194. var selected = [];
  195. if (widget.MASTERDEV)
  196. selected.push(widget.MASTERDEV);
  197. FW_weekprofileMultiSelDialog("<span>Device(s):</span>",devicesNames,devicesAlias,selected,1,bnt,
  198. function(sndDevs) {
  199. if (!sndDevs || sndDevs.length==0)
  200. return;
  201. FW_cmd(FW_root+"?cmd=set "+widget.DEVICE+" send_to_device "+widget.CURTOPIC+':'+widget.CURPRF+" "+sndDevs.join(',')+"&XHR=1",function(arg) {FW_weekprofileSendCallback(widget.DEVICE,arg);});
  202. });
  203. } catch(e){
  204. console.log(devName+" error parsing json '" +data+"'");
  205. FW_errmsg(devName+" Parameter "+e,5000);
  206. return;
  207. }
  208. }
  209. FW_queryValue('get '+devName+' sndDevList', bnt);
  210. }
  211. function FW_weekprofileCopyPrf(devName,lnk)
  212. {
  213. var widget = $('div[informid="'+devName+'"]').get(0)
  214. var title = [];
  215. var inp = [];
  216. var def = [];
  217. var idx = 0;
  218. var topic = '';
  219. if (widget.USETOPICS==1) {
  220. topic=widget.CURTOPIC+":";
  221. }
  222. title[idx] = "<p>Create new entry from: "+topic+widget.CURPRF+"</p>";
  223. inp[idx] = "hidden";
  224. def[idx] = '';
  225. idx++;
  226. if (widget.USETOPICS==1) {
  227. title[idx] = "<p>Reference:</p>";
  228. inp[idx] = "checkbox";
  229. def[idx] = '';
  230. idx++;
  231. title[idx] = "<p>Topic:</p>";
  232. inp[idx] = "text";
  233. def[idx] = widget.CURTOPIC;
  234. idx++;
  235. }
  236. title[idx] = "<p>Name:</p>";
  237. inp[idx] = "text";
  238. def[idx] = '';
  239. FW_weekprofileInputDialog(title,inp,def,lnk,function(names,ok){
  240. if (ok < 1)
  241. return;
  242. var topic = widget.CURTOPIC;
  243. var name = names[names.length-1].trim();
  244. var ref = 0;
  245. if (widget.USETOPICS==1) {
  246. topic = names[names.length-2].trim();
  247. ref = names[names.length-3];
  248. }
  249. if (topic.length < 1 || name.length < 1)
  250. return;
  251. if (ref != 0)
  252. FW_cmd(FW_root+"?cmd=set "+widget.DEVICE+" reference_profile "+widget.CURTOPIC+':'+widget.CURPRF+" "+topic+':'+name+"&XHR=1",function(arg) {FW_weekprofileSendCallback(widget.DEVICE,arg);});
  253. else
  254. FW_cmd(FW_root+"?cmd=set "+widget.DEVICE+" copy_profile "+widget.CURTOPIC+':'+widget.CURPRF+" "+topic+':'+name+"&XHR=1",function(arg) {FW_weekprofileSendCallback(widget.DEVICE,arg);});
  255. });
  256. }
  257. function FW_weekprofileRemovePrf(devName,lnk)
  258. {
  259. var widget = $('div[informid="'+devName+'"]').get(0)
  260. FW_weekprofileInputDialog(["<p>Delete Profile: '"+widget.CURTOPIC+':'+widget.CURPRF+"'&nbsp;?</p>"],["hidden"],null,lnk,function(name,ok){
  261. if (ok < 1)
  262. return;
  263. FW_cmd(FW_root+"?cmd=set "+widget.DEVICE+" remove_profile "+widget.CURTOPIC+':'+widget.CURPRF+"&XHR=1",function(arg) {FW_weekprofileSendCallback(widget.DEVICE,arg);});
  264. });
  265. }
  266. function FW_weekprofileShow(widget)
  267. {
  268. $(widget.MENU.BASE).show();
  269. var editIcon = $(widget.MENU.BASE).find('a[name="'+widget.DEVICE+'.edit"]').get(0);
  270. $(editIcon).css("visibility", "visible"); //hide() remove the element
  271. $(widget.MENU.CONTENT).empty();
  272. var selMargin=0;
  273. var tdStyle="style=\"padding-left:0px;padding-right:0px\"";
  274. if (widget.USETOPICS == 1) {
  275. selMargin = 2;
  276. tdStyle = "style=\"padding:0px\"";
  277. }
  278. if (widget.PROFILENAMES) {
  279. var html='';
  280. html += '<table style="padding-bottom:0">';
  281. html += "<tr><td style=\"padding:0px\">";
  282. if (widget.USETOPICS == 1 && widget.TOPICNAMES) {
  283. html += "<select style=\"margin-bottom:"+selMargin+"px\" name=\"TOPICS\" onchange=\"FW_weekprofileTOPIC_chached('"+widget.DEVICE+"',this)\">";
  284. for (var k=0; k < widget.TOPICNAMES.length; k++)
  285. {
  286. var name = widget.TOPICNAMES[k].trim();
  287. var selected = (widget.CURTOPIC == name) ? "selected " : "";
  288. html += "<option "+selected+"value=\""+name+"\">"+name+"</option>";
  289. }
  290. html += "</select>";
  291. }
  292. html += "</td>";
  293. html += "<td rowspan=\"2\" "+tdStyle+">";
  294. html += "<button type=\"button\"onclick=\"FW_weekprofileCopyPrf('"+widget.DEVICE+"',this)\" data-toggle=\"tooltip\" title=\"copy profile\">+</button>";
  295. html += "&nbsp;"
  296. html += "<button type=\"button\" onclick=\"FW_weekprofileRemovePrf('"+widget.DEVICE+"',this)\" data-toggle=\"tooltip\" title=\"remove profile\">-</button>";
  297. html += "&nbsp;"
  298. if (widget.USETOPICS == 0) {
  299. html += "<button type=\"button\" onclick=\"FW_weekprofileSendToDev('"+widget.DEVICE+"',this)\" data-toggle=\"tooltip\" title=\"send to device\">--></button>";
  300. } else {
  301. html += "<button type=\"button\" onclick=\"FW_weekprofileRestoreTopic('"+widget.DEVICE+"',this)\" data-toggle=\"tooltip\" title=\"restore topic\">T</button>";
  302. }
  303. html += "</td></tr>";
  304. html += "<tr><td "+tdStyle+">";
  305. html += "<select style=\"margin-top:"+selMargin+"px\" name=\"PROFILES\" onchange=\"FW_weekprofilePRF_chached('"+widget.DEVICE+"',this)\">";
  306. for (var k=0; k < widget.PROFILENAMES.length; k++)
  307. {
  308. var name = widget.PROFILENAMES[k];
  309. var selected = (widget.CURPRF == name) ? "selected " : "";
  310. html += "<option "+selected+"value=\""+name+"\">"+name+"</option>";
  311. }
  312. html += "</select>";
  313. html += "</td></tr>";
  314. if (widget.PRFREF) {
  315. $(editIcon).css("visibility", "hidden");
  316. var names = widget.PRFREF.split(':');
  317. names[0] = names[0].trim();
  318. names[1] = names[1].trim();
  319. html += "<tr><td colspan=\"2\" align=\"left\" "+tdStyle+">";
  320. html += "&nbsp;"
  321. html += "<a href=\"javascript:void(0)\" onclick=\"FW_weekprofileChacheTo('"+widget.DEVICE+"','"+names[0]+"','"+names[1]+"')\">REF: "+names[0]+":"+names[1]+"</a>";
  322. html += "</td></tr>";
  323. }
  324. html += "</table>";
  325. $(widget.MENU.CONTENT).append(html);
  326. var select = $(widget.MENU.CONTENT).find('select[name="PROFILES"]').get(0);
  327. var prfName = select.options[select.selectedIndex].value;
  328. if (widget.CURPRF != prfName)
  329. FW_weekprofilePRF_chached(widget.DEVICE,select);
  330. }
  331. if (!widget.PROFILE) {
  332. return;
  333. }
  334. var table = widget.CONTENT;
  335. $(table).empty();
  336. for (var i = 0; i < shortDays.length; ++i) {
  337. $(table).append('<tr class="'+ ( (i+1)%2==0 ? 'even':'odd')+ '"><td>'+widget.WEEKDAYS[i]+'</td></tr>');
  338. var tr = $(table).find("tr").get(i);
  339. for (var k = 0; k < widget.PROFILE[shortDays[i]]['temp'].length; ++k) {
  340. var str = '';
  341. k>0 ? str = widget.PROFILE[shortDays[i]]['time'][k-1] : str = '00:00';
  342. str = str + '-' + widget.PROFILE[shortDays[i]]['time'][k];
  343. $(tr).append('<td>'+str+ '</td>');
  344. str = widget.PROFILE[shortDays[i]]['temp'][k]+' °C';
  345. $(tr).append('<td>'+str+ '</td>');
  346. }
  347. }
  348. }
  349. function FW_weekprofileEditTime_changed(inp)
  350. {
  351. if (inp == null) {return;}
  352. var times = inp.value.split(':');
  353. if (times.length == 0)
  354. return;
  355. var hour = parseInt(times[0]);
  356. var min = (times.length==2) ? parseInt(times[1]): 0;
  357. inp.value = ((hour<10)?("0"+hour):hour) +":"+ ((min<10)?("0"+min):min);
  358. //set new end time as new start time for the next interval
  359. var nexttr = inp.parentNode.parentNode.nextSibling;
  360. if (nexttr!=null){
  361. nexttr.firstChild.firstChild.innerHTML=inp.value;
  362. }
  363. }
  364. function FW_weekprofileEditRowStyle(table)
  365. {
  366. var alltr = $(table).find("tr");
  367. for (var i = 0; i < alltr.length; ++i){
  368. var delButton = $(alltr[i]).find('input[name="DEL"]');
  369. var addButton = $(alltr[i]).find('input[name="ADD"]');
  370. var inp = $(alltr[i]).find('input[name="ENDTIME"]');
  371. $(alltr[i]).attr('class',(i%2==0)? "odd":"even");
  372. delButton.attr('type',"button");
  373. addButton.attr('type',"button");
  374. inp.removeAttr('style');
  375. inp.removeAttr('readonly');
  376. FW_weekprofileEditTime_changed(inp.get(0));
  377. if (i==0){
  378. $(alltr[i]).find('span[name="STARTTIME"]').get(0).innerHTML = "00:00";
  379. if (alltr.length == 1){
  380. delButton.attr('type',"hidden");
  381. }
  382. }
  383. if (i==alltr.length-1){
  384. if (alltr.length > 1){
  385. addButton.attr('type',"hidden");
  386. }
  387. inp.attr('style',"border:none;background:transparent;box-shadow:none");
  388. inp.get(0).value = "24:00";
  389. inp.attr('readonly',true);
  390. }
  391. }
  392. }
  393. function FW_weekprofileEditAddInterval(tr)
  394. {
  395. var newtr = $(tr).clone(true);
  396. var alltr = $(tr).parent().children();
  397. for (var i = 0; i < alltr.length; ++i) {
  398. if ( $(alltr[i]).is($(tr))) {
  399. newtr.insertAfter($(alltr[i]));
  400. break;
  401. }
  402. }
  403. FW_weekprofileEditRowStyle($(tr).parent());
  404. var timSel = newtr.find('input[name="ENDTIME"]');
  405. if (alltr.length == 1)
  406. timSel = $(tr).find('input[name="ENDTIME"]');
  407. timSel.focus();
  408. timSel.select();
  409. }
  410. function FW_weekprofileEditDelInterval(tr)
  411. {
  412. var parent = $(tr).parent();
  413. $(tr).remove();
  414. FW_weekprofileEditRowStyle(parent)
  415. }
  416. function FW_weekprofileTransDay(devName,day,bnt)
  417. {
  418. var widget = $('div[informid="'+devName+'"]').get(0);
  419. var srcDay = $(widget.CONTENT).find("table[id*=\"weekprofile."+widget.DEVICE+"\"][data-day*=\""+shortDays[day]+"\"]");
  420. var dayNames = [];
  421. var dayAlias = [];
  422. for (var k=0; k < shortDays.length; k++) {
  423. if (k != day) {
  424. dayNames.push(shortDays[k]);
  425. dayAlias.push(widget.WEEKDAYS[k]);
  426. }
  427. }
  428. var selected = [];
  429. FW_weekprofileMultiSelDialog("<span>Days(s):</span>",dayNames,dayAlias,selected,0,bnt,
  430. function(selDays) {
  431. if (!selDays || selDays.length==0)
  432. return;
  433. for (var k=0; k < selDays.length; k++) {
  434. var destDay = $(widget.CONTENT).find("table[id*=\"weekprofile."+widget.DEVICE+"\"][data-day*=\""+selDays[k]+"\"]");
  435. destDay.empty();
  436. destDay.append(srcDay.clone().contents());
  437. }
  438. });
  439. }
  440. function FW_weekprofileTemp_chached(select)
  441. {
  442. var val = select.options[select.selectedIndex].value;
  443. $(select).find("option").removeAttr('selected');
  444. $(select).val(val);
  445. $(select.options[select.selectedIndex]).attr("selected","selected");
  446. }
  447. function FW_weekprofileEditDay(widget,day)
  448. {
  449. var div = $("<div>").get(0);
  450. var html= '';
  451. html += "<div style=\"padding:5px;\">";
  452. html += "<span style=\"margin-right:10px;margin-left:10px\">"+widget.WEEKDAYS[day]+"</span>";
  453. html += "<a href=\"javascript:void(0)\" onclick=\"FW_weekprofileTransDay('"+widget.DEVICE+"',"+day+",this)\" data-toggle=\"tooltip\" title=\"transfer day\">--></a>";
  454. html += "</div>";
  455. $(div).append(html);
  456. var table = $("<table>").get(0);
  457. $(table).attr('id',"weekprofile."+widget.DEVICE).attr("data-day", shortDays[day]);
  458. $(table).attr('class',"block wide weekprofile");
  459. html = '';
  460. var times = widget.PROFILE[shortDays[day]]['time'];
  461. var temps = widget.PROFILE[shortDays[day]]['temp'];
  462. for (var i = 0; i < times.length; ++i) {
  463. var startTime = (i>0) ? times[i-1] : "00:00";
  464. var endTime = (i<times.length-1) ? times[i] : "24:00";
  465. html += "<tr>";
  466. //from
  467. html += "<td><span name=\"STARTTIME\">"+startTime+"</span></td>";
  468. html += "<td>-</td>";
  469. //to
  470. html += "<td><input type=\"text\" name=\"ENDTIME\" size=\"5\" maxlength=\"5\" align=\"center\" value=\""+endTime+"\" onblur=\"FW_weekprofileEditTime_changed(this)\"/></td>";
  471. //temp
  472. var tempOn = widget.TEMP_ON;
  473. var tempOff = widget.TEMP_OFF;
  474. if (tempOn == null)
  475. tempOn = 30;
  476. if (tempOff == null)
  477. tempOff = 5;
  478. if (tempOff > tempOn)
  479. {
  480. var tmp = tempOn;
  481. tempOn = tempOff;
  482. tempOff = tmp;
  483. }
  484. html += "<td><select name=\"TEMP\" size=\"1\" onchange=\"FW_weekprofileTemp_chached(this)\">";
  485. for (var k=tempOff; k <= tempOn; k+=.5)
  486. {
  487. var selected = (k == temps[i]) ? "selected " : "";
  488. if (k == widget.TEMP_OFF)
  489. html += "<option "+selected+"value=\"off\">off</option>";
  490. else if (k == widget.TEMP_ON)
  491. html += "<option "+selected+"value=\"on\">on</option>";
  492. else
  493. html += "<option "+selected+"value=\""+k.toFixed(1)+"\">"+k.toFixed(1)+"</option>";
  494. }
  495. html += "</select></td>";
  496. //ADD-Button
  497. html += "<td><input type=\"button\" name=\"ADD\" value=\"+\" onclick=\"FW_weekprofileEditAddInterval(this.parentNode.parentNode)\"></td>";
  498. //DEL-Button
  499. html += "<td><input type=\"button\" name=\"DEL\" value=\"-\" onclick=\"FW_weekprofileEditDelInterval(this.parentNode.parentNode)\"></td>";
  500. html += "</tr>";
  501. }
  502. $(table).append(html);
  503. $(div).append(table);
  504. FW_weekprofileEditRowStyle(table);
  505. return div;
  506. }
  507. function FW_weekprofileEditWeek(widget)
  508. {
  509. $(widget.MENU.CONTENT).empty();
  510. var table = widget.CONTENT;
  511. var daysInRow = 2;
  512. if (widget.EDIT_DAYSINROW)
  513. daysInRow = widget.EDIT_DAYSINROW;
  514. $(table).append('<tr>');
  515. var tr = $(table).find("tr:last");
  516. for (var i = 0; i < shortDays.length; ++i) {
  517. tr.append('<td>');
  518. tr.find('td:last').append(FW_weekprofileEditDay(widget,i));
  519. if ((i+1)%daysInRow == 0){
  520. $('<tr>').insertAfter(tr);
  521. tr = $(table).find("tr:last");
  522. }
  523. }
  524. tr.append("<td><table><tr>");
  525. tr = tr.find("tr:last");
  526. tr.append("<td><input type=\"button\" value=\""+FW_GetTranslation(widget,'Speichern')+"\" onclick=\"FW_weekprofilePrepAndSendProf('"+widget.DEVICE+"')\">");
  527. tr.append("<td><input type=\"button\" value=\""+FW_GetTranslation(widget,'Abbrechen')+"\" onclick=\"FW_weekprofileEditAbort('"+widget.DEVICE+"')\">");
  528. }
  529. function FW_weekprofileSendCallback(devName, data)
  530. {
  531. var widget = $('div[informid="'+devName+'"]').get(0);
  532. if(!data.match(/^[\r\n]*$/)) // ignore empty answers
  533. FW_okDialog('<pre>'+data+'</pre>',widget);
  534. }
  535. function FW_weekprofilePrepAndSendProf(devName)
  536. {
  537. var widget = $('div[informid="'+devName+'"]').get(0);
  538. var tableDay = $(widget).find("table[id*=\"weekprofile."+devName+"\"]");
  539. if (tableDay.length == 0){
  540. FW_errmsg(widget.DEVICE+" internal error ",10000);
  541. return;
  542. }
  543. var prf=new Object();
  544. for (var i = 0; i < tableDay.length; ++i) {
  545. var timeEL = $(tableDay[i]).find('input[name="ENDTIME"]');
  546. var tempEL = $(tableDay[i]).find('select[name="TEMP"]');
  547. if (timeEL.length != tempEL.length){
  548. FW_errmsg(widget.DEVICE+" internal error ",10000);
  549. return;
  550. }
  551. var day = $(tableDay[i]).attr("data-day");
  552. prf[day] = new Object();
  553. prf[day]['time'] = new Array();
  554. prf[day]['temp'] = new Array();
  555. for (var k = 0; k < timeEL.length; ++k) {
  556. prf[day]['time'].push(timeEL[k].value);
  557. prf[day]['temp'].push(tempEL[k].value);
  558. }
  559. }
  560. try {
  561. var data=JSON.stringify(prf);
  562. FW_cmd(FW_root+"?cmd=set "+widget.DEVICE+" profile_data "+widget.CURTOPIC+':'+widget.CURPRF+" "+data+"&XHR=1",function(arg) {FW_weekprofileSendCallback(widget.DEVICE,arg);});
  563. } catch(e){
  564. FW_errmsg(devName+" Parameter "+e,5000);
  565. return;
  566. }
  567. for (var i = 0; i < shortDays.length; ++i) {
  568. var day = shortDays[i];
  569. if (prf[day] != null){
  570. widget.PROFILE[day] = prf[day];
  571. }
  572. }
  573. FW_weekprofileBack(widget);
  574. }
  575. function FW_weekprofileBack(widget)
  576. {
  577. if (widget.JMPBACK){
  578. var isInIframe = (window.location != window.parent.location) ? true : false;
  579. if (isInIframe) {
  580. parent.history.back();
  581. } else
  582. if (document.referrer) {
  583. window.location.assign(document.referrer)
  584. } else {
  585. window.history.back() //maybe problems with reload
  586. }
  587. }
  588. else {
  589. widget.MODE = "SHOW";
  590. widget.setValueFn("REUSEPRF");
  591. }
  592. }
  593. function FW_weekprofileEditAbort(devName)
  594. {
  595. var widget = $('div[informid="'+devName+'"]').get(0);
  596. FW_weekprofileBack(widget);
  597. }
  598. function FW_weekprofileGetProfileData(devName,data)
  599. {
  600. var widget = $('div[informid="'+devName+'"]').get(0);
  601. $(widget.CONTENT).empty();
  602. var reuse = (data == "REUSEPRF") ? 1 : 0;
  603. var prf={};
  604. try {
  605. (reuse) ? prf = widget.PROFILE : prf=JSON.parse(data);
  606. } catch(e){
  607. console.log(devName+" error parsing json '" +data+"'");
  608. FW_errmsg(devName+" Parameter "+e,5000);
  609. return;
  610. }
  611. widget.PROFILE = prf;
  612. widget.PRFREF = null;
  613. if (widget.MODE == 'SHOW')
  614. {
  615. if (reuse == 0 && widget.USETOPICS != 0) {
  616. //check if data is a reference
  617. FW_cmd(FW_root+'?cmd=get '+devName+' profile_references '+widget.CURTOPIC+':'+widget.CURPRF+'&XHR=1',function(data){
  618. if (data != 0) {
  619. var name = data.split(':');
  620. if (name.length == 2) {
  621. widget.PRFREF = data;
  622. } else {
  623. console.log(devName+" error get references '" +data+"'");
  624. }
  625. }
  626. FW_weekprofileShow(widget);
  627. });
  628. } else {
  629. FW_weekprofileShow(widget);
  630. }
  631. }
  632. else if (widget.MODE == 'EDIT')
  633. {
  634. FW_weekprofileEditWeek(widget);
  635. }
  636. else
  637. {
  638. FW_errmsg(devName+" unknown Mode",10000);
  639. }
  640. }
  641. function FW_weekprofileGetValues(devName,what,data)
  642. {
  643. data = data.trim();
  644. if(data.match(/^[\r\n]*$/)) {return;}
  645. var widget = $('div[informid="'+devName+'"]').get(0);
  646. if (what == "WEEKDAYS"){
  647. widget.WEEKDAYS = data.split(',');
  648. } else if (what == "PROFILENAMES") {
  649. widget.PROFILENAMES = data.split(',');
  650. if (widget.MODE != 'EDIT') {
  651. if (widget.CURPRF == null && widget.PROFILENAMES) {
  652. widget.CURPRF = widget.PROFILENAMES[0];
  653. }
  654. FW_queryValue('get '+devName+' profile_data '+widget.CURTOPIC+':'+widget.CURPRF, widget);
  655. } else {
  656. widget.setValueFn("REUSEPRF");
  657. }
  658. } else if (what == "TOPICNAMES") {
  659. widget.TOPICNAMES = data.split(',');
  660. var found = 0;
  661. for (var k = 0; k < widget.TOPICNAMES.length; ++k) {
  662. widget.TOPICNAMES[k] = widget.TOPICNAMES[k].trim();
  663. if (widget.CURTOPIC == widget.TOPICNAMES[k]) {
  664. found=1;
  665. }
  666. }
  667. if (found==0) {
  668. widget.CURTOPIC = widget.TOPICNAMES[0];
  669. }
  670. FW_weekprofileChacheTo(devName,widget.CURTOPIC,null);
  671. } else if (what == "TRANSLATE") {
  672. var arr = data.split(',');
  673. widget.TRANSLATIONS = new Array();
  674. for (var k = 0; k < arr.length; ++k) {
  675. var trans = arr[k].split(':');
  676. if (trans.length == 2)
  677. widget.TRANSLATIONS[trans[0].trim()] = trans[1].trim();
  678. }
  679. }
  680. }
  681. function
  682. FW_weekprofileCreate(elName, devName, vArr, currVal, set, params, cmd)
  683. {
  684. // called from FW_replaceWidget fhemweb.js
  685. if( 0 ) {
  686. console.log( "elName: "+elName );
  687. console.log( "devName: "+devName ); // attr dev
  688. console.log( "vArr: "+vArr ); // attr arg split ','
  689. console.log( "currVal: "+currVal ); // attr current
  690. console.log( "set: "+set ); // attr cmd split ' ' first entry
  691. console.log( "params: "+params ); // attr cmd list split ' ' without first entry
  692. console.log( "cmd: "+cmd ); // function for ToDo
  693. }
  694. if(!vArr.length || vArr[0] != "weekprofile")
  695. return undefined;
  696. var widget = $('div[informid="'+devName+'"]').get(0);
  697. var content = $('<table class="block wide weekprofile" id="weekprofile_content">').get(0);
  698. $(widget).append(content);
  699. widget.CONTENT = content;
  700. widget.HEADER = $('div[id="weekprofile.'+devName+'.header"]').get(0);
  701. widget.MENU = new Object();
  702. widget.MENU.BASE = $(widget.HEADER).find('div[id*="menu.base"]').get(0);
  703. var menuContent = '<td style="display:inline;padding:0px;margin:0px;"><div id="weekprofile.menu.content"></td>';
  704. $(widget.MENU.BASE.parentElement.parentElement).append(menuContent);
  705. widget.MENU.CONTENT = $(widget.HEADER).find('div[id*="menu.content"]').get(0);
  706. widget.JMPBACK = null;
  707. widget.MODE = 'SHOW';
  708. widget.USETOPICS = 0;
  709. widget.TEMP_ON = null;
  710. widget.TEMP_OFF = null;
  711. for (var i = 1; i < vArr.length; ++i) {
  712. var arg = vArr[i].split(':');
  713. switch (arg[0]) {
  714. case "MODE": widget.MODE = arg[1]; break;
  715. case "JMPBACK": widget.JMPBACK = arg[1]; break;
  716. case "MASTERDEV": widget.MASTERDEV = arg[1]; break;
  717. case "USETOPICS": widget.USETOPICS = arg[1]; break;
  718. case "DAYINROW": widget.EDIT_DAYSINROW = arg[1]; break;
  719. case "TEMP_ON": widget.TEMP_ON = parseFloat(arg[1]); break;
  720. case "TEMP_OFF": widget.TEMP_OFF = parseFloat(arg[1]);break;
  721. }
  722. }
  723. widget.DEVICE = devName;
  724. widget.WEEKDAYS = shortDays.slice();
  725. var current = currVal.split(':');
  726. widget.CURTOPIC = current[0];
  727. widget.CURPRF = current[1];
  728. widget.setValueFn = function(arg){FW_weekprofileGetProfileData(devName,arg);}
  729. widget.activateFn = function(arg){
  730. FW_queryValue('get '+devName+' profile_data '+widget.CURTOPIC+':'+widget.CURPRF, widget);
  731. FW_cmd(FW_root+'?cmd={AttrVal("'+devName+'","widgetWeekdays","")}&XHR=1',function(data){FW_weekprofileGetValues(devName,"WEEKDAYS",data);});
  732. FW_cmd(FW_root+'?cmd={AttrVal("'+devName+'","widgetTranslations","")}&XHR=1',function(data){FW_weekprofileGetValues(devName,"TRANSLATE",data);});
  733. if (widget.USETOPICS == 1) {
  734. FW_cmd(FW_root+'?cmd=get '+devName+' topic_names&XHR=1',function(data){FW_weekprofileGetValues(devName,"TOPICNAMES",data);});
  735. } else {
  736. FW_cmd(FW_root+'?cmd=get '+devName+' profile_names '+widget.CURTOPIC+'&XHR=1',function(data){FW_weekprofileGetValues(devName,"PROFILENAMES",data);});
  737. }
  738. };
  739. //inform profile_count changed
  740. var prfCnt = $('<div informid="'+devName+'-profile_count" style="display:none">').get(0);
  741. prfCnt.setValueFn = function(arg){
  742. if (widget.USETOPICS == 1) {
  743. FW_cmd(FW_root+'?cmd=get '+devName+' topic_names&XHR=1',function(data){FW_weekprofileGetValues(devName,"TOPICNAMES",data);});
  744. } else {
  745. FW_cmd(FW_root+'?cmd=get '+devName+' profile_names '+widget.CURTOPIC+'&XHR=1',function(data){FW_weekprofileGetValues(devName,"PROFILENAMES",data);});
  746. }
  747. }
  748. $(widget.HEADER).append(prfCnt);
  749. return widget;
  750. }
  751. FW_widgets['weekprofile'] = {
  752. createFn:FW_weekprofileCreate,
  753. };
  754. /*
  755. =pod
  756. =begin html
  757. =end html
  758. =begin html_DE
  759. =end html_DE
  760. =cut
  761. */