f18.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. "use strict";
  2. FW_version["f18.js"] = "$Id: f18.js 17705 2018-11-08 10:30:28Z rudolfkoenig $";
  3. // TODO: hierMenu+Pin,SVGcolors,floorplan
  4. // Known bugs: AbsSize is wrong for ColorSlider
  5. var f18_attr={}, f18_sd, f18_icon={}, f18_room, f18_grid=20, f18_margin=10;
  6. var f18_small = (screen.width < 480 || screen.height < 480);
  7. $(window).resize(f18_resize);
  8. $(document).ready(function(){
  9. f18_room = $("div#content").attr("room");
  10. f18_sd = $("body").attr("data-styleData");
  11. if(f18_sd) {
  12. eval("f18_sd="+f18_sd);
  13. if(!f18_sd)
  14. f18_sd = {};
  15. f18_attr = f18_sd.f18;
  16. if(f18_attr)
  17. delete(f18_attr.cols); // fix the past
  18. } else {
  19. f18_sd = {};
  20. }
  21. if(!f18_sd.f18) {
  22. f18_attr = { "Pinned.menu":"true", "hidePin":"true" };
  23. f18_resetCol();
  24. f18_sd.f18 = f18_attr;
  25. }
  26. if(typeof f18_attr.savePinChanges == "undefined")
  27. f18_attr.savePinChanges = true;
  28. f18_setCss('init');
  29. var icon = FW_root+"/images/default/fhemicon_ios.png";
  30. $('head').append(
  31. '<meta name="viewport" content="initial-scale=1.0,user-scalable=1">'+
  32. '<meta name= "mobile-web-app-capable" content="yes">'+
  33. '<meta name="apple-mobile-web-app-capable" content="yes">'+
  34. '<link rel="apple-touch-icon" href="'+icon+'">');
  35. if('ontouchstart' in window)
  36. $("body").addClass('touch');
  37. if(f18_small) {
  38. $("body").addClass('small');
  39. f18_attr["Pinned.menu"] = false;
  40. }
  41. var f18_aCol = "rgb(39, 135, 39)";
  42. if($("a").length)
  43. f18_aCol = getComputedStyle($("a").get(0),null).getPropertyValue('color');
  44. f18_loadIcons();
  45. f18_loadTouch();
  46. for(var i in f18_icon)
  47. f18_icon[i] = f18_icon[i].replace('gray', f18_aCol);
  48. f18_icon.pinOut = f18_icon.pinIn
  49. .replace('/>',' transform="rotate(90,896,896)"/>');
  50. // Needed for moving this label
  51. var szc = $("[data-name=svgZoomControl]");
  52. if($(szc).length)
  53. $(szc).before("<div class='SVGplot'></div>");
  54. $(".SVGlabel[data-name]").each(function(){
  55. $(this).attr("data-name", "Room_"+f18_room+"_"+$(this).attr("data-name"));
  56. });
  57. f18_menu();
  58. f18_tables();
  59. f18_svgSetCols();
  60. if(typeof svgCallback != "undefined")
  61. svgCallback.f18 = f18_svgSetCols;
  62. $("[data-name]").each(function(){ f18_setPos(this) });
  63. f18_setWrapColumns();
  64. f18_setFixedInput();
  65. });
  66. function
  67. f18_menu()
  68. {
  69. $("#menu").toggleClass("hidden", f18_small || !f18_getAttr("Pinned.menu"));
  70. if($("#menuScrollArea #menuBtn").length)
  71. return fixMenu();
  72. $("<div id='menuBtn'></div>").prependTo("div#menuScrollArea")
  73. .css( {"background-image":"url('"+f18_icon.bars+"')", "cursor":"pointer" })
  74. .click(function(){ $("#menu").toggleClass("hidden") });
  75. $("div#menu").prepend("<div></div>");
  76. f18_addPin("div#menu > div:first", "menu", true, fixMenu, f18_small);
  77. setTimeout(function(){ $("#menu,#content,#logo,#hdr").addClass("animated"); },
  78. 10);
  79. function
  80. fixMenu()
  81. {
  82. $("#menuScrollArea #logo").css("display",
  83. f18_getAttr("hideLogo") ? "none" : "block");
  84. if(f18_getAttr("Pinned.menu")) {
  85. $("body").addClass("pinnedMenu");
  86. $("#menu").removeClass("hidden");
  87. $("#content").css("left",
  88. (parseInt($("div#menu").width())+2*f18_margin)+"px");
  89. } else {
  90. $("body").removeClass("pinnedMenu");
  91. $("#content").css("left", f18_margin);
  92. }
  93. f18_resize();
  94. }
  95. }
  96. function
  97. f18_tables()
  98. {
  99. // one-column roomoverview & readingsGroup
  100. $("div#content > table > tbody > tr > td > .devType:not(:first)")
  101. .css("margin-top", "20px");
  102. // multi-column roomoverview
  103. $("table.column tbody tr:not(:first-child) .devType")
  104. .css("margin-top", "20px");
  105. $("#content .devType").each(function(){
  106. var el = this, grp = $(el).text();
  107. f18_addPin(el, "Room."+FW_urlParams.room+".grp."+grp, true,
  108. function(isFixed){
  109. var ntr = $(el).closest("tr").next("tr");
  110. isFixed ? $(ntr).show() : $(ntr).hide();
  111. });
  112. });
  113. if(FW_urlParams.detail) {
  114. $("div.makeTable > span").each(function(){
  115. var el = this, grp = $(el).text();
  116. var nel = $("<div>"+grp+"</div>");
  117. $(el).replaceWith(nel);
  118. f18_addPin(nel, "detail."+grp, true,
  119. function(isFixed){
  120. var ntr = $(nel).next("table");
  121. isFixed ? $(ntr).show() : $(ntr).hide();
  122. });
  123. });
  124. }
  125. if(FW_urlParams.cmd == "style%20list" ||
  126. FW_urlParams.cmd == "style%20select")
  127. $("div.fileList").each(function(){ f18_addPinToStyleDiv(this) });
  128. if(FW_urlParams.cmd == "style%20select")
  129. f18_special();
  130. else if(f18_getAttr("showDragger"))
  131. $("[data-name]").each(function(){ f18_addDragger(this) });
  132. }
  133. function
  134. f18_special()
  135. {
  136. var row, room='all', appendTo;
  137. var attr = function(attrName, inRoom)
  138. {
  139. if(inRoom && room != "all") {
  140. var val = f18_attr["Room."+room+"."+attrName];
  141. if(val != undefined)
  142. return val;
  143. }
  144. return f18_attr[attrName];
  145. };
  146. var setAttr = function(attrName, attrVal, inRoom)
  147. {
  148. if(inRoom && room != "all")
  149. attrName = "Room."+room+"."+attrName;
  150. f18_setAttr(attrName, attrVal);
  151. };
  152. var addRow = function(name, desc, val)
  153. {
  154. $(appendTo)
  155. .append("<tr class='ar_"+name+" "+(++row%2 ? "even":"odd")+"'>"+
  156. "<td "+(val ? "" : "colspan='2'")+">"+
  157. "<div class='col1'>"+desc+"</div></td>"+
  158. (val ? "<td><div class='col2'>"+val+"</div></div></td>" : '')+
  159. "</tr>");
  160. };
  161. var addHider = function(name, inRoom, desc, fn)
  162. {
  163. addRow(name, desc, "<input type='checkbox'>");
  164. $(appendTo+" tr.ar_"+name+" input")
  165. .prop("checked", attr(name, inRoom))
  166. .click(function(){
  167. var c = $(this).is(":checked");
  168. setAttr(name, c, inRoom);
  169. if(fn)
  170. fn(c);
  171. });
  172. };
  173. var addColorChooser = function(name, desc)
  174. {
  175. addRow(name, desc, "<div class='cp'></div>");
  176. FW_replaceWidget(appendTo+" tr.ar_"+name+" div.col2 div.cp", name,
  177. ["colorpicker","RGB"], attr("cols."+name, true), name, "rgb", undefined,
  178. function(value) {
  179. setAttr("cols."+name, value, true);
  180. f18_setCss(name);
  181. });
  182. };
  183. // call drawspecial after got the roomlist...
  184. var f18_drawSpecial = function()
  185. {
  186. var roomHash={};
  187. var cleanRoom = function(){
  188. for(var k in f18_attr) {
  189. var m = k.match(/^room\.([^.]*)\..*/);
  190. if(m && !roomHash[m[1]])
  191. delete f18_attr[k];
  192. }
  193. };
  194. row = 0;
  195. $("div#content tr.f18").remove();
  196. $("div#content > table").append("<tr id='f18rs' class='f18'></tr>");
  197. $("tr#f18rs").append("<div class='fileList f18colors'>f18 special</div>");
  198. $("tr#f18rs").append("<table id='f18ts' class='block wide'></table>");
  199. appendTo = "table#f18ts";
  200. addHider("rightMenu", false, "MenuBtn right<br>on small screen",f18_resize);
  201. addHider("savePinChanges", false, "Save pin changes");
  202. addHider("showDragger", false, "Dragging active", function(c){
  203. if(c) {
  204. if($(".ui-draggable").length) {
  205. $(".ui-draggable").draggable("enable");
  206. $(".dragMove,.dragSize,.dragReset").show();
  207. } else {
  208. $("div.fileList").each(function(){ f18_addDragger(this) });
  209. }
  210. } else {
  211. $(".dragMove,.dragSize,.dragReset").hide();
  212. $(".ui-draggable").draggable("disable");
  213. }
  214. });
  215. addHider("snapToGrid", false, "Snap to grid", function(c){
  216. $(".ui-draggable").draggable("option", "grid",
  217. c ? [f18_grid,f18_grid] : [1,1]);
  218. });
  219. addRow("editStyle", "<a href='#'>Additional CSS</a>");
  220. $(appendTo+" tr.ar_editStyle a").click(function(){
  221. $('body').append(
  222. '<div id="editdlg" style="display:none">'+
  223. '<textarea id="f18_cssEd" rows="25" cols="60" style="width:99%"/>'+
  224. '</div>');
  225. $("#f18_cssEd").val($("head #fhemweb_css").html());
  226. $('#editdlg').dialog(
  227. { modal:true, closeOnEscape:true, width:$(window).width()*3/4,
  228. height:$(window).height()*3/4, title:$(this).text(),
  229. close:function(){ $('#editdlg').remove(); },
  230. buttons:[
  231. { text: "Cancel",click:function(){$(this).dialog('close')}},
  232. { text: "OK", click:function(){
  233. if(!$("head #fhemweb_css"))
  234. $("head").append("<style id='fhemweb_css'>\n</style>");
  235. var txt = $("#f18_cssEd").val();
  236. $("head #fhemweb_css").html(txt);
  237. var wn = $("body").attr("data-webName");
  238. FW_cmd(FW_root+"?cmd=attr "+wn+" Css "+
  239. encodeURIComponent(txt.replace(/;/g,";;"))+"&XHR=1");
  240. $(this).dialog('close');
  241. }}]
  242. });
  243. });
  244. $("div#content > table").append("<tr id='f18rr' class='f18'></tr>");
  245. $("tr#f18rr").append("<div class='fileList f18colors'>"+
  246. "f18: Room specific</div>");
  247. $("tr#f18rr").append("<table id='f18tr' class='block wide'></table>");
  248. appendTo = "table#f18tr";
  249. addRow("room", "Target <select><option>all</option></select>");
  250. FW_cmd(FW_root+"?cmd=jsonlist2 .* room&XHR=1", function(data) {
  251. var d;
  252. try { d=JSON.parse(data); } catch(e){ log(data); return FW_okDialog(e); }
  253. for(var i1=0; i1<d.Results.length; i1++) {
  254. var rname = d.Results[i1].Attributes.room;
  255. if(!rname || rname == "hidden")
  256. continue;
  257. var rl = rname.split(",")
  258. for(var i2=0; i2<rl.length; i2++)
  259. roomHash[rl[i2]] = true;
  260. }
  261. cleanRoom();
  262. var rArr = Object.keys(roomHash); rArr.sort();
  263. $(appendTo+" tr.ar_room select")
  264. .html("<option>all</option><option>"+
  265. rArr.join("</option><option>")+
  266. "</option>")
  267. .change(function(e){
  268. room = $(e.target).val();
  269. f18_drawSpecial();
  270. });
  271. $("tr.ar_room select").val(room);
  272. });
  273. addRow("reset", "Preset colors: "+
  274. "<a href='#'>default</a> "+
  275. "<a href='#'>light</a> "+
  276. "<a href='#'>dark</a> "+
  277. (room=='all' ? '': "<a href='#'>like:all</a>"));
  278. $(appendTo+" tr.ar_reset a").click(function(){
  279. var txt = $(this).text();
  280. if(txt == "like:all") {
  281. delete(roomHash[room]);
  282. cleanRoom();
  283. } else {
  284. f18_resetCol(txt, room);
  285. if(room == "all")
  286. f18_setCss('preset');
  287. }
  288. f18_setAttr();
  289. f18_drawSpecial();
  290. });
  291. addColorChooser("bg", "Background");
  292. addColorChooser("fg", "Foreground");
  293. addColorChooser("link", "Link");
  294. addColorChooser("evenrow", "Even row");
  295. addColorChooser("oddrow", "Odd row");
  296. addColorChooser("header", "Header row");
  297. addColorChooser("menu", "Menu");
  298. addColorChooser("sel", "Menu:Selected");
  299. addColorChooser("inpBack", "Input bg");
  300. $("table.f18colors input").attr("size", 8);
  301. var bgImg = attr("bgImg", true);
  302. addRow("bgImg", "<a href='#'>Background: <span>"+
  303. (bgImg ? bgImg : "none")+"</span></a>");
  304. $(appendTo+" tr.ar_bgImg a").click(function(){
  305. FW_cmd(FW_root+'?cmd='+
  306. '{join("\\n",FW_fileList("$FW_icondir/background/.*.(jpg|png)"))}&XHR=1',
  307. function(data) {
  308. if(data)
  309. data += "none";
  310. var imgList = data.split(/\n/);
  311. FW_okDialog("List of files in www/images/background:<br><ul>"+
  312. "<a href='#'>"+imgList.join("</a><br><a href='#'>")+'</a></ul>');
  313. $("#FW_okDialog a").click(function(){
  314. var txt = $(this).text();
  315. setAttr("bgImg", txt == 'none' ? undefined : txt, true);
  316. $(appendTo+" tr.ar_bgImg span").html(txt);
  317. f18_setCss("bgImg");
  318. });
  319. });
  320. });
  321. addHider("hideLogo", true, "Hide logo", f18_menu);
  322. addHider("hideInput", true, "Hide input", f18_menu);
  323. addHider("hidePin", true, "Hide pin", function(c){
  324. $("div.pinHeader div.pin").css("display", c ? "none":"block");
  325. });
  326. addHider("fixedInput", false, "Fixed input and menu", f18_setFixedInput);
  327. addHider("wrapcolumns",false,"Wrap columns<br>on small screen",
  328. f18_setWrapColumns);
  329. $("div.f18colors").css("margin-top", "20px");
  330. $("tr.f18 div.fileList").each(function(e){ f18_addPinToStyleDiv(this) });
  331. if(f18_getAttr("showDragger"))
  332. $("div.fileList").each(function(){ f18_addDragger(this) });
  333. $("[data-name]").each(function(){ f18_setPos(this) });
  334. f18_setWrapColumns();
  335. };
  336. loadScript("pgm2/fhemweb_colorpicker.js", f18_drawSpecial);
  337. }
  338. function
  339. f18_setFixedInput()
  340. {
  341. // togleClass is true for undefined
  342. $("body").toggleClass("fixedInput", f18_getAttr("fixedInput") ? true:false);
  343. f18_resize();
  344. }
  345. function
  346. f18_setWrapColumns()
  347. {
  348. $("table.block").toggleClass("wrapcolumns", f18_getAttr("wrapcolumns"));
  349. }
  350. function
  351. f18_addPinToStyleDiv(el)
  352. {
  353. var grp = $(el).text();
  354. f18_addPin(el, "style.list."+grp, true,
  355. function(isFixed){
  356. var ntr = $(el).next("table");
  357. isFixed ? $(ntr).show() : $(ntr).hide();
  358. });
  359. }
  360. function
  361. f18_resize()
  362. {
  363. var w=$(window).width();
  364. log("f18.js resize W:"+w+" S:"+screen.width);
  365. var hl = f18_getAttr("hideLogo"),
  366. hi = f18_getAttr("hideInput"),
  367. pm = f18_getAttr("Pinned.menu"),
  368. rm = (f18_getAttr("rightMenu") && f18_small);
  369. var left = 0;
  370. left += hl ? 0 : 40;
  371. left += pm ? 0 : 44;
  372. var lleft = (pm ? 10 : 52);
  373. $("input.maininput").css({ width:(w-left-(FW_isiOS ? 36 : 24))+'px',
  374. "margin-left":(rm ? "0px" : "10px"),
  375. display: hi ? "none":"block"});
  376. $("#menu,#content").css("top", (hi && pm && hl) ? "10px" : "50px");
  377. $("#hdr").css({ left:(rm ? 10 : left)+'px' });
  378. $("#menuBtn").toggle(!pm || f18_small);
  379. $("#menuBtn").css({ left:(rm ? "auto":"10px"), right:(rm ? "10px":"auto") });
  380. $("#logo") .css({ left:(rm ? "auto":lleft ), right:(rm ? "48px":"auto") });
  381. if(FW_isiOS)
  382. $("#logo,#menuBtn").css({ top:'12px'});
  383. }
  384. function
  385. f18_addPin(el, name, defVal, fn, hidePin)
  386. {
  387. var init = f18_getAttr("Pinned."+name);
  388. if(init == undefined)
  389. init = defVal;
  390. $("<div class='pin'></div>")
  391. .appendTo(el)
  392. .css("background-image", "url('"+
  393. (init ? f18_icon.pinIn : f18_icon.pinOut)+"')");
  394. var f18_name = name.replace(/[^A-Z0-9]/ig,'_');
  395. $(el)
  396. .addClass("col_header pinHeader "+f18_name)
  397. .attr("data-name", f18_name);
  398. el = $(el).find("div.pin");
  399. $(el)
  400. .addClass(init ? "pinIn" : "")
  401. .css("cursor", "pointer")
  402. .css("display", (f18_getAttr("hidePin") || hidePin) ? "none" : "block")
  403. .click(function(){
  404. var nextVal = !$(el).hasClass("pinIn");
  405. $(el).toggleClass("pinIn");
  406. $(el).css("background-image","url('"+
  407. (nextVal ? f18_icon.pinIn : f18_icon.pinOut)+"')")
  408. f18_setAttr("Pinned."+name, nextVal);
  409. fn(nextVal);
  410. });
  411. fn(init);
  412. }
  413. // el is the drag-handle, return the corresponding SVG/table etc
  414. function
  415. f18_compEl(el)
  416. {
  417. return $(el).hasClass("fileList") ? $(el).next("table") :
  418. $(el).hasClass("SVGlabel") ? $(el).prev(".SVGplot") :
  419. $(el).closest("tr").next().find(">td>table").first();
  420. }
  421. function
  422. f18_addDragger(el)
  423. {
  424. if(f18_small || FW_urlParams.detail)
  425. return;
  426. if($(el).find(".dragger").length)
  427. return;
  428. var comp = f18_compEl(el);
  429. if($(comp).length == 0)
  430. return;
  431. f18_convertToAbs();
  432. var ep = $(el).position();
  433. var cp = $(comp).position();
  434. var pl = parseInt($(el).css("padding-left").replace("px",""));
  435. var grid = [1,1];
  436. if(f18_getAttr("snapToGrid"))
  437. grid = [f18_grid, f18_grid];
  438. function
  439. save()
  440. {
  441. var nep = $(el).position();
  442. f18_setAttr("Pos."+$(el).attr("data-name"), {
  443. left:nep.left, top:nep.top,
  444. width:$(comp).width(), height:$(comp).height(),
  445. oTop:cp.top-ep.top, oLeft:cp.left-ep.left
  446. });
  447. }
  448. /////////////////////////////////////
  449. // Position
  450. $("<div class='dragger dragMove'></div>")
  451. .appendTo(el)
  452. .css({"cursor":"pointer",
  453. "background-image":"url('"+f18_icon.arrows+"')"})
  454. $(el).draggable({
  455. drag:function(evt,ui){
  456. $(comp).css({ left:ui.position.left+cp.left-ep.left,
  457. top: ui.position.top +cp.top -ep.top });
  458. },
  459. stop:save, grid:grid
  460. });
  461. /////////////////////////////////////
  462. // Size
  463. var off = 20;
  464. if(!$(el).hasClass("SVGlabel")) {
  465. $("<div class='dragSize'></div>")
  466. .appendTo(el)
  467. .css({ cursor:"pointer", "background-image":"url('"+f18_icon.arrows+"')",
  468. position:"absolute", width:"16px", height:"16px",
  469. top:$(comp).height()+2, left:$(comp).width()-off, "z-index":1 })
  470. .draggable({
  471. drag:function(evt,ui){
  472. $(el).css( { width:ui.position.left+off });
  473. $(comp).css({ width:ui.position.left+off,
  474. height:ui.position.top });
  475. },
  476. stop:save, grid:grid
  477. });
  478. }
  479. /////////////////////////////////////
  480. // Reset _all_ elements on this page
  481. $("<div class='dragger dragReset'></div>")
  482. .appendTo(el)
  483. .css({"cursor":"pointer",
  484. "background-image":"url('"+f18_icon.ban+"')"})
  485. .click(function(){
  486. function
  487. delStyle(e)
  488. {
  489. var style = $(e).attr("style");
  490. $(e).attr("style", style.replace(/position:.*;/,"")); // hack
  491. }
  492. $("[data-name]").each(function(){
  493. var el = this;
  494. var name = $(el).attr("data-name");
  495. if(!f18_getAttr("Pos."+name))
  496. return;
  497. delete(f18_attr["Pos."+$(el).attr("data-name")]);
  498. delStyle(el);
  499. delStyle(f18_compEl(el));
  500. $(el).draggable('disable');
  501. $(el).find(".dragMove,.dragSize,.dragReset").hide();
  502. });
  503. f18_setAttr();
  504. });
  505. }
  506. function
  507. f18_applyGrid(pos)
  508. {
  509. if(!f18_getAttr("snapToGrid"))
  510. return;
  511. pos.left = Math.floor((pos.left + f18_grid-1)/f18_grid)*f18_grid;
  512. pos.top = Math.floor((pos.top + f18_grid-1)/f18_grid)*f18_grid;
  513. pos.width = Math.floor((pos.width + f18_grid-1)/f18_grid)*f18_grid;
  514. pos.height = Math.floor((pos.height+ f18_grid-1)/f18_grid)*f18_grid;
  515. }
  516. //////////////////////////
  517. // We use absolute positioning for all elements, if a user positioned
  518. // an item, relative (the default one) else.
  519. function
  520. f18_convertToAbs()
  521. {
  522. // Need two loops, else the sizes/positions are wrong
  523. var sz = {};
  524. $("[data-name]").each(function(){
  525. var el = this;
  526. var name = $(el).attr("data-name");
  527. if(f18_getAttr("Pos."+name))
  528. return;
  529. var comp = f18_compEl(el);
  530. if($(comp).length == 0)
  531. return;
  532. sz[name] = { ep:$(el).position(), cp:$(comp).position(),
  533. w:$(comp).width(), h:$(comp).height() };
  534. });
  535. var needSave=false;
  536. $("[data-name]").each(function(){
  537. var el = this;
  538. var name = $(el).attr("data-name");
  539. if(!name || !sz[name])
  540. return;
  541. needSave = true;
  542. var comp = f18_compEl(el);
  543. var ep=sz[name].ep, cp=sz[name].cp, w=sz[name].w, h=sz[name].h;
  544. var pos = {
  545. left:ep.left, top:ep.top, width:w, height:h,
  546. oTop:cp.top-ep.top, oLeft:cp.left-ep.left
  547. };
  548. f18_doSetPos(el, comp, pos);
  549. f18_setAttr("Pos."+name, pos, true);
  550. });
  551. if(needSave)
  552. f18_setAttr();
  553. }
  554. function
  555. f18_setPos(el)
  556. {
  557. if(f18_small || FW_urlParams.detail)
  558. return;
  559. var comp = f18_compEl(el);
  560. if($(comp).length == 0)
  561. return;
  562. var name = $(el).attr("data-name");
  563. var pos = f18_getAttr("Pos."+name);
  564. if(!pos || !pos.width)
  565. return;
  566. f18_doSetPos(el, comp, pos);
  567. // correct position
  568. var ds = $(el).find(".dragSize");
  569. if($(ds).length)
  570. $(ds).css({ top:pos.height+2, left:pos.width-20 });
  571. }
  572. function
  573. f18_doSetPos(el, comp, pos)
  574. {
  575. f18_applyGrid(pos);
  576. $(el).css({ position:"absolute", left:pos.left, top:pos.top });
  577. if(!$(el).hasClass("SVGlabel")) {
  578. var padding = parseInt($(el).css("padding-left").replace("px",""));
  579. $(el).css({ width:pos.width-padding });
  580. }
  581. $(comp).css({ position:"absolute",
  582. left:pos.left+pos.oLeft, top:pos.top+pos.oTop,
  583. width:pos.width, height:pos.height });
  584. }
  585. function
  586. f18_getAttr(attrName)
  587. {
  588. if(f18_room != undefined) {
  589. var val = f18_attr["Room."+f18_room+"."+attrName];
  590. if(val != undefined)
  591. return val
  592. }
  593. return f18_attr[attrName];
  594. }
  595. function
  596. f18_setAttr(name, value, dontSave)
  597. {
  598. if(name)
  599. f18_attr[name]=value;
  600. if(name && value == undefined)
  601. delete f18_attr[name];
  602. if(name && name.indexOf("Pinned.") == 0 && !f18_attr.savePinChanges)
  603. return;
  604. if(dontSave)
  605. return;
  606. var wn = $("body").attr("data-webName");
  607. FW_cmd(FW_root+"?cmd=attr "+wn+" styleData "+
  608. encodeURIComponent(JSON.stringify(f18_sd, undefined, 1))+"&XHR=1");
  609. }
  610. function
  611. f18_resetCol(name, room)
  612. {
  613. var cols = {
  614. "default":{ bg: "FFFFE7", fg: "000000", link: "278727",
  615. evenrow:"F8F8E0", oddrow:"F0F0D8", header: "E0E0C8",
  616. menu: "D7FFFF", sel: "A0FFFF", inpBack:"FFFFFF" },
  617. light: { bg: "F8F8F8", fg: "465666", link: "4C9ED9",
  618. evenrow:"E8E8E8", oddrow:"F0F0F0", header: "DDDDDD",
  619. menu: "EEEEEE", sel: "CAC8CF", inpBack:"FFFFFF" },
  620. dark: { bg: "444444", fg: "CCCCCC", link: "FF9900",
  621. evenrow:"333333", oddrow:"111111", header: "222222",
  622. menu: "111111", sel: "333333", inpBack:"444444" }
  623. };
  624. var col = (name ? cols[name] : cols["default"]);
  625. var prefix = (room && room != 'all' ? "Room."+room+".cols." : "cols.");
  626. for(var c in col)
  627. f18_attr[prefix+c] = col[c];
  628. }
  629. // Put all the colors into a head style tag, send background changes to FHEM
  630. function
  631. f18_setCss(why)
  632. {
  633. var style = "";
  634. function col(n) { return f18_getAttr("cols."+n, true) };
  635. function bg(c) { return "{ background:#"+c+"; fill:#"+c+"; }\n" }
  636. function fg(c) { return "{ color:#"+c+"; }\n" }
  637. style += ".col_fg, body, input, textarea "+fg(col("fg"));
  638. style += ".col_bg, textarea, input, option "+bg(col("bg"));
  639. style += ".col_link,a:not(.changed),.handle,.fhemlog,input[type=submit],"+
  640. "select,div.ui-widget-content a "+
  641. "{color:#"+col("link")+"!important; stroke:#"+col("link")+";}\n";
  642. style += "svg:not([fill]):not(.jssvg) { fill:#"+col("link")+"; }\n";
  643. style += ".col_evenrow, table.block,div.block "+bg(col("evenrow"));
  644. style += ".col_oddrow,table.block tr.odd,table.block tr.sel "+
  645. bg(col("oddrow"));
  646. style += ".col_header "+bg(col("header"));
  647. style += ".col_menu, table.room "+bg(col("menu"));
  648. style += ".col_sel, table.room tr.sel "+bg(col("sel"));
  649. style += ".col_inpBack, input "+bg(col("inpBack"));
  650. if(col("bg") == "FFFFE7") // default
  651. style += "div.pinHeader.menu {background:#"+col("sel")+";}\n";
  652. style += "div.ui-dialog-titlebar "+bg(col("header"));
  653. style += "div.ui-widget-content "+bg(col("bg"));
  654. style += "div.ui-widget-content, .ui-button-text "+fg(col("fg")+"!important");
  655. style += "div.ui-dialog { border:1px solid #"+col("link")+"; }";
  656. style += "button.ui-button { background:#"+col("oddrow")+"!important; "+
  657. "border:1px solid #"+col("link")+"!important; }\n";
  658. if(typeof DashboardDraggable != "undefined") {
  659. var db = "#dashboard ";
  660. style += db+".dashboard_widgetheader "+bg(col("header"));
  661. style += db+".dashboard_tabnav "+bg(col("menu")+"!important");
  662. style += db+".ui-widget-header .ui-state-default "+bg(col("menu"));
  663. style += db+".ui-widget-header .ui-state-active "+bg(col("sel"));
  664. style += db+".ui-widget-header "+fg(col("fg")+"!important;");
  665. style += db+".ui-widget-header li { border:none!important; }";
  666. style += db+".ui-widget-content a "+fg(col("link")+"!important" );
  667. }
  668. var bgImg = f18_getAttr("bgImg", true);
  669. if(bgImg) {
  670. style += 'body { background-image: url('+FW_root+
  671. '/images/background/'+bgImg+');}';
  672. } else {
  673. style += "body "+bg(col("bg"));
  674. }
  675. $("head style#f18_css").remove();
  676. style = "<style id='f18_css'>"+style+"</style>";
  677. if($("head style#fhemweb_css").length)
  678. $("head style#fhemweb_css").before(style);
  679. else
  680. $("head").append(style);
  681. $("head meta[name=theme-color]").remove();
  682. $("head").append('<meta name="theme-color" content="#'+col("bg")+'">');
  683. // Recolor the menu arrows. CSS does not apply to such SVGs :(
  684. if(why=='init' || why=='preset' || why=='link') {
  685. var a = $("a").get(0);
  686. if(window.getComputedStyle && a) {
  687. var col = getComputedStyle(a,null).getPropertyValue('color');
  688. FW_arrowRight = FW_arrowRight.replace(/rgb[^)]*\)/,col);
  689. FW_arrowDown = FW_arrowDown.replace(/rgb[^)]*\)/,col);
  690. $("div#menu table.room tr.menuTree > td > div > div")
  691. .css("background-image", "url('"+FW_arrowRight+"')");
  692. $("div#menu table.room tr.menuTree.open > td > div > div")
  693. .css("background-image", "url('"+FW_arrowDown+"')");
  694. }
  695. }
  696. }
  697. // SVG color tuning
  698. function
  699. f18_svgSetCols(svg)
  700. {
  701. function col(n) { return f18_getAttr("cols."+n, true) };
  702. if(!svg || !svg.getAttribute("data-origin"))
  703. return;
  704. var style = $(svg).find("> style").first();
  705. var sTxt = $(style).text();
  706. sTxt = sTxt.replace(/font-family:Times/, "fill:#"+col("fg"));
  707. $(style).text(sTxt);
  708. function
  709. addCol(c, d)
  710. {
  711. var r="";
  712. for(var i1=0; i1<6; i1+=2) {
  713. var n = parseInt(c.substr(i1,2), 16);
  714. n += d;
  715. if(n>255) n = 255;
  716. if(n< 0) n = 0;
  717. n = n.toString(16);
  718. if(n.length < 2)
  719. n = "0"+n;
  720. r += n;
  721. }
  722. return r;
  723. }
  724. // SVG background gradient: .css does not work in Firefox, has to use .attr
  725. var stA = $(svg).find("> defs > #gr_bg").children();
  726. var so = "; stop-opacity:1;";
  727. $(stA[0]).attr("style", "stop-color:#"+addCol(col("bg"),10)+so);
  728. $(stA[1]).attr("style", "stop-color:#"+addCol(col("bg"),-10)+so);
  729. }
  730. function
  731. f18_loadIcons()
  732. {
  733. // font-awesome
  734. var f18_svgPrefix='data:image/svg+xml;utf8,<svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path fill="gray" ';
  735. f18_icon.pinIn=f18_svgPrefix+'d="M896 1088q66 0 128-15v655q0 26-19 45t-45 19h-128q-26 0-45-19t-19-45v-655q62 15 128 15zm0-1088q212 0 362 150t150 362-150 362-362 150-362-150-150-362 150-362 362-150zm0 224q14 0 23-9t9-23-9-23-23-9q-146 0-249 103t-103 249q0 14 9 23t23 9 23-9 9-23q0-119 84.5-203.5t203.5-84.5z"/></svg>';
  736. f18_icon.bars=f18_svgPrefix+'d="M1664 1344v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45zm0-512v128q0 26-19 45t-45 19h-1408q-26 0-45-19t-19-45v-128q0-26 19-45t45-19h1408q26 0 45 19t19 45z"/></svg>';
  737. f18_icon.arrows=f18_svgPrefix+'d="M1792 896q0 26-19 45l-256 256q-19 19-45 19t-45-19-19-45v-128h-384v384h128q26 0 45 19t19 45-19 45l-256 256q-19 19-45 19t-45-19l-256-256q-19-19-19-45t19-45 45-19h128v-384h-384v128q0 26-19 45t-45 19-45-19l-256-256q-19-19-19-45t19-45l256-256q19-19 45-19t45 19 19 45v128h384v-384h-128q-26 0-45-19t-19-45 19-45l256-256q19-19 45-19t45 19l256 256q19 19 19 45t-19 45-45 19h-128v384h384v-128q0-26 19-45t45-19 45 19l256 256q19 19 19 45z"/></svg>';
  738. f18_icon.ban=f18_svgPrefix+'d="M1440 893q0-161-87-295l-754 753q137 89 297 89 111 0 211.5-43.5t173.5-116.5 116-174.5 43-212.5zm-999 299l755-754q-135-91-300-91-148 0-273 73t-198 199-73 274q0 162 89 299zm1223-299q0 157-61 300t-163.5 246-245 164-298.5 61-298.5-61-245-164-163.5-246-61-300 61-299.5 163.5-245.5 245-164 298.5-61 298.5 61 245 164 163.5 245.5 61 299.5z"/></svg>';
  739. }
  740. function
  741. f18_loadTouch()
  742. {
  743. /*!
  744. * jQuery UI Touch Punch 0.2.3
  745. *
  746. * Copyright 2011-2014, Dave Furfero
  747. * Dual licensed under the MIT or GPL Version 2 licenses.
  748. *
  749. * Depends:
  750. * jquery.ui.widget.js
  751. * jquery.ui.mouse.js
  752. */
  753. !function(a){function f(a,b){if(!(a.originalEvent.touches.length>1)){a.preventDefault();var c=a.originalEvent.changedTouches[0],d=document.createEvent("MouseEvents");d.initMouseEvent(b,!0,!0,window,1,c.screenX,c.screenY,c.clientX,c.clientY,!1,!1,!1,!1,0,null),a.target.dispatchEvent(d)}}if(a.support.touch="ontouchend"in document,a.support.touch){var e,b=a.ui.mouse.prototype,c=b._mouseInit,d=b._mouseDestroy;b._touchStart=function(a){var b=this;!e&&b._mouseCapture(a.originalEvent.changedTouches[0])&&(e=!0,b._touchMoved=!1,f(a,"mouseover"),f(a,"mousemove"),f(a,"mousedown"))},b._touchMove=function(a){e&&(this._touchMoved=!0,f(a,"mousemove"))},b._touchEnd=function(a){e&&(f(a,"mouseup"),f(a,"mouseout"),this._touchMoved||f(a,"click"),e=!1)},b._mouseInit=function(){var b=this;b.element.bind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),c.call(b)},b._mouseDestroy=function(){var b=this;b.element.unbind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),d.call(b)}}}(jQuery);
  754. }