fhemweb.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  1. "use strict";
  2. var FW_version={};
  3. FW_version["fhemweb.js"] = "$Id: fhemweb.js 17478 2018-10-07 16:45:18Z rudolfkoenig $";
  4. var FW_serverGenerated;
  5. var FW_serverFirstMsg = (new Date()).getTime()/1000;
  6. var FW_serverLastMsg = FW_serverFirstMsg;
  7. var FW_isIE = (navigator.appVersion.indexOf("MSIE") > 0);
  8. var FW_isiOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/);
  9. var FW_scripts = {}, FW_links = {};
  10. var FW_docReady = false, FW_longpollType, FW_csrfToken, FW_csrfOk=true;
  11. var FW_root = "/fhem"; // root
  12. var FW_availableJs={};
  13. var FW_urlParams={};
  14. var embedLoadRetry = 100;
  15. var FW_os = "unknown";
  16. if(FW_isiOS) { FW_os = "iOS";
  17. } else if(navigator.userAgent.indexOf("Android") >= 0) { FW_os = "android";
  18. } else if(navigator.userAgent.indexOf("OS X") >= 0) { FW_os = "osx";
  19. } else if(navigator.userAgent.indexOf("Windows") >= 0) { FW_os = "windows";
  20. } else if(navigator.userAgent.indexOf("Linux") >= 0) { FW_os = "linux";
  21. }
  22. // createFn returns an HTML Element, which may contain
  23. // - setValueFn, which is called when data via longpoll arrives
  24. // - activateFn, which is called after the HTML element is part of the DOM.
  25. var FW_widgets = {
  26. select: { createFn:FW_createSelect },
  27. selectnumbers: { createFn:FW_createSelectNumbers, },
  28. slider: { createFn:FW_createSlider },
  29. time: { createFn:FW_createTime },
  30. noArg: { createFn:FW_createNoArg },
  31. multiple: { createFn:FW_createMultiple },
  32. "multiple-strict": { createFn:FW_createMultiple, second:true },
  33. textField: { createFn:FW_createTextField },
  34. textFieldNL: { createFn:FW_createTextField, second:true },
  35. "textField-long": { createFn:FW_createTextField, second:true },
  36. "textFieldNL-long":{ createFn:FW_createTextField, second:true }
  37. };
  38. window.onbeforeunload = function(e)
  39. {
  40. FW_leaving = 1;
  41. return undefined;
  42. }
  43. window.onerror = function(errMsg, url, lineno)
  44. {
  45. url = url.replace(/.*\//,'');
  46. if($("body").attr("data-confirmJSError") != 0)
  47. FW_okDialog(url+" line "+lineno+":<br>"+errMsg);
  48. }
  49. function
  50. FW_replaceWidgets(parent)
  51. {
  52. parent.find("div.fhemWidget").each(function() {
  53. var dev=$(this).attr("dev");
  54. var cmd=$(this).attr("cmd");
  55. var rd=$(this).attr("reading");
  56. var params = cmd.split(" ");
  57. var type=$(this).attr("type");
  58. if(type == undefined)
  59. type = "set";
  60. FW_replaceWidget(this, dev, $(this).attr("arg").split(","),
  61. $(this).attr("current"), rd, params[0], params.slice(1),
  62. function(arg) {
  63. FW_cmd(FW_root+"?cmd="+type+" "+dev+
  64. (params[0]=="state" ? "":" "+params[0])+" "+arg+"&XHR=1");
  65. });
  66. });
  67. }
  68. function
  69. FW_jqueryReadyFn()
  70. {
  71. if(FW_docReady) // loading fhemweb.js twice is hard to debug
  72. return;
  73. FW_docReady = true;
  74. FW_serverGenerated = $("body").attr("generated");
  75. FW_longpollType = $("body").attr("longpoll");
  76. var ajs = $("body").attr("data-availableJs");
  77. if(ajs) {
  78. ajs = ajs.split(",");
  79. for(var i1=0; i1<ajs.length; i1++)
  80. FW_availableJs[ajs[i1]] = 1;
  81. }
  82. if(FW_longpollType != "0")
  83. setTimeout("FW_longpoll()", 100);
  84. FW_csrfToken = $("body").attr('fwcsrf');
  85. $("a").each(function() { FW_replaceLink(this); })
  86. $("head script").each(function() {
  87. var sname = $(this).attr("src"),
  88. p = FW_scripts[sname];
  89. if(!p) {
  90. FW_scripts[sname] = { loaded:true };
  91. return;
  92. }
  93. FW_scripts[sname].loaded = true;
  94. if(p.callbacks && !p.called) {
  95. p.called = true; // Avoid endless loop
  96. for(var i1=0; i1< p.callbacks.length; i1++)
  97. if(p.callbacks[i1]) // pushing undefined callbacks on the stack is ok
  98. p.callbacks[i1]();
  99. delete(p.callbacks);
  100. }
  101. });
  102. $("head link").each(function() { FW_links[$(this).attr("href")] = 1 });
  103. $("div.makeSelect select").each(function() {
  104. FW_detailSelect(this);
  105. $(this).change(FW_detailSelect);
  106. });
  107. // Activate the widgets
  108. var r = $("head").attr("root");
  109. if(r)
  110. FW_root = r;
  111. FW_replaceWidgets($("html"));
  112. FW_confirmDelete();
  113. // Fix the td count by setting colspan on the last column
  114. $("table.block.wide").each(function(){ // table
  115. var id = $(this).attr("id");
  116. if(!id || id.indexOf("TYPE") != 0)
  117. return;
  118. var maxTd=0, tdCount=[], tbl = $(this);
  119. $(tbl).find("> tbody > tr").each(function(){ // count the td's
  120. var cnt = 0, row=this;
  121. $(row).find("> td").each(function(){
  122. var cs = $(this).attr("colspan");
  123. cnt += parseInt(cs ? cs : 1);
  124. });
  125. if(maxTd < cnt)
  126. maxTd = cnt;
  127. tdCount.push(cnt);
  128. });
  129. $(tbl).find("> tbody> tr").each(function(){ // set the colspan
  130. var tdc = tdCount.shift();
  131. $(this).find("> td:last").each(function(){
  132. var cs = $(this).attr("colspan");
  133. $(this).attr("colspan", maxTd-tdc+(cs ? parseInt(cs) : 1));
  134. });
  135. });
  136. });
  137. $("form input.get[type=submit]").click(function(e) { //"get" via XHR to dialog
  138. e.preventDefault();
  139. var cmd = "", el=this;
  140. $(el).parent().find("input,[name]").each(function() {
  141. cmd += (cmd?"&":"")+encodeURIComponent($(this).attr("name"))+
  142. "="+encodeURIComponent($(this).val());
  143. });
  144. FW_cmd(FW_root+"?"+cmd+"&XHR=1&addLinks=1", function(data) {
  145. if(!data.match(/^[\r\n]*$/)) // ignore empty answers
  146. FW_okDialog('<pre>'+data+'</pre>', el);
  147. });
  148. });
  149. $("#saveCheck")
  150. .css("cursor", "pointer")
  151. .click(function(){
  152. var parent = this;
  153. FW_cmd(FW_root+"?cmd=save ?&XHR=1", function(data) {
  154. FW_okDialog('<pre>'+data+'</pre>',parent);
  155. });
  156. });
  157. $("form").each(function(){ // main input special cases
  158. var input = $(this).find("input.maininput");
  159. if(!input.length)
  160. return;
  161. $(this).on("submit", function(e) {
  162. var val = $(input).val();
  163. if(val.match(/^\s*ver.*/)) { // version
  164. e.preventDefault();
  165. $(input).val("");
  166. return FW_showVersion(val);
  167. } else if(val.match(/^\s*shutdown/)) { // shutdown
  168. FW_cmd(FW_root+"?XHR=1&cmd="+val);
  169. $(input).val("");
  170. return false;
  171. } else if(val.match(/^\s*l\s/)) { // l dev
  172. var m = val.match(/^\s*l\s+(.*)/);
  173. location.href = FW_root+"?detail="+m[1];
  174. e.preventDefault();
  175. return false;
  176. } else if(val.match(/^\s*get\s+/)) { // get
  177. // make get use xhr instead of reload
  178. //return true;
  179. FW_cmd(FW_root+"?cmd="+encodeURIComponent(val)+"&XHR=1", function(data){
  180. if( !data.match( /^<html>.*<\/html>/ ) ) {
  181. data = data.replace( '<', '&lt;' );
  182. data = '<pre>'+data+'</pre>';
  183. }
  184. if( location.href.indexOf('?') === -1 )
  185. $('#content').html(data);
  186. else
  187. FW_okDialog(data);
  188. });
  189. e.preventDefault();
  190. $(input).val("");
  191. return false;
  192. }
  193. return true;
  194. });
  195. });
  196. $("div.devSpecHelp a").each(function(){ // Help on detail window
  197. var dev = FW_getLink(this).split("#").pop();
  198. $(this).unbind("click");
  199. $(this).attr("href", "#"); // Desktop: show underlined Text
  200. $(this).removeAttr("onclick");
  201. $(this).click(function(evt){
  202. if($("#devSpecHelp").length) {
  203. $("#devSpecHelp").remove();
  204. return;
  205. }
  206. FW_getHelp(dev, function(data){
  207. $("#content").append('<div id="devSpecHelp"></div>');
  208. $("#devSpecHelp").html(data);
  209. var off = $("#devSpecHelp").position().top-20;
  210. $('body, html').animate({scrollTop:off}, 500);
  211. });
  212. });
  213. });
  214. $("table.attributes tr div.dname") // Click on attribute fills input value
  215. .each(function(){
  216. $(this)
  217. .html('<a>'+$(this).html()+'</a>')
  218. .css({cursor:"pointer"})
  219. .click(function(){
  220. var attrName = $(this).text();
  221. var sel = "#sel_attr"+$(this).attr("data-name").replace(/\./g,'_');
  222. if($(sel+" option[value='"+attrName+"']").length == 0)
  223. $(sel).append('<option value="'+attrName+'">'+attrName+'</option>');
  224. $(sel).val(attrName);
  225. FW_detailSelect(sel, true);
  226. });
  227. });
  228. $("[name=icon-filter]").on("change keyup paste", function() {
  229. clearTimeout($.data(this, 'delayTimer'));
  230. var wait = setTimeout(FW_filterIcons, 300);
  231. $(this).data('delayTimer', wait);
  232. });
  233. $("pre.motd").each(function(){ // add links for passwort setting
  234. var txt = $(this).text();
  235. txt = txt.replace(/(configuring|define|attr) .*/g, function(x) {
  236. return "<a href='#'>"+x+"</a>";
  237. });
  238. $(this).html(txt);
  239. $(this).find("a").click(function(){
  240. var txt = $(this).text();
  241. var ma = txt.match(/configuring.*device (.*)/); // ??
  242. if(ma)
  243. location.href = FW_root+"?detail="+ma[1];
  244. FW_cmd(FW_root+"?cmd="+encodeURIComponent(txt)+"&XHR=1",
  245. function(data){
  246. if(txt.indexOf("attr") == 0) $("pre.motd").html("");
  247. if(txt.indexOf("define") == 0)
  248. location.href = FW_root+"?detail=allowed";
  249. });
  250. });
  251. });
  252. var sa = location.search.substring(1).split("&");
  253. for(var i = 0; i < sa.length; i++) {
  254. var kv = sa[i].split("=");
  255. FW_urlParams[kv[0]] = kv[1];
  256. }
  257. $("select[id^=sel_attr],select[id^=sel_set],select[id^=sel_get]")
  258. .change(function(){ // online help
  259. var val = $(this).val();
  260. var m = $(this).attr("name").match(/arg.(set|get|attr)(.*)/);
  261. if(!m)
  262. return;
  263. $("#devSpecHelp").remove();
  264. var sel = this;
  265. FW_getHelp(m[2], function(data) {
  266. var mm = data.match(new RegExp('<a[^"]*"'+val+'[^A-Za-z_0-9]'));
  267. if(mm == null) {
  268. data = "";
  269. } else {
  270. data = data.substr(mm.index);
  271. var o1 = data.indexOf('<a', 1);
  272. if(o1 > 0)
  273. data = data.substr(0,o1);
  274. }
  275. if(data) {
  276. $(sel).closest("div[cmd='"+m[1]+"']")
  277. .after('<div class="makeTable" id="devSpecHelp"></div>')
  278. $("#devSpecHelp").html(data);
  279. }
  280. });
  281. });
  282. FW_smallScreenCommands();
  283. FW_inlineModify();
  284. FW_rawDef();
  285. FW_treeMenu();
  286. $("body").attr("data-os", FW_os);
  287. // automatic reload for style change
  288. if(location.search.indexOf("cmd=style%20select") > 0) {
  289. $('a[href*="style set"],a[onclick*="style set"]').each(function(){
  290. var href = $(this).attr("href");
  291. if(!href && (href = $(this).attr("onclick")))
  292. href = href.substr(15,href.length-16);
  293. $(this).click(function(e){
  294. e.preventDefault();
  295. FW_cmd(href+"&XHR=1", function(data) { location.reload(true); });
  296. });
  297. });
  298. }
  299. }
  300. var FW_helpData;
  301. function
  302. FW_getHelp(dev, fn)
  303. {
  304. if(FW_helpData)
  305. return fn(FW_helpData);
  306. FW_cmd(FW_root+"?cmd=help "+dev+"&XHR=1", function(data) {
  307. FW_helpData = data;
  308. return fn(FW_helpData);
  309. });
  310. }
  311. function
  312. FW_showVersion(val)
  313. {
  314. FW_cmd(FW_root+"?cmd="+encodeURIComponent(val)+"&XHR=1", function(data){
  315. var list = Object.keys(FW_version);
  316. list.sort();
  317. for(var i1=0; i1<list.length; i1++) {
  318. var ma = /\$Id: ([^ ]*) (.*) \$/.exec(FW_version[list[i1]]);
  319. if(ma) {
  320. if(ma[1].length < 26)
  321. ma[1] = (ma[1]+" ").substr(0,26);
  322. data += "\n"+ma[1]+" "+ma[2];
  323. }
  324. }
  325. FW_okDialog('<pre>'+data+'</pre>');
  326. });
  327. return false;
  328. }
  329. function
  330. FW_filterIcons()
  331. {
  332. var icons = $('.dist[title]');
  333. icons.show();
  334. var filterText = $('[name=icon-filter]').val();
  335. if (filterText != '') {
  336. var re = RegExp(filterText,"i");
  337. icons.filter(function() {
  338. return !re.test(this.title);
  339. }).hide();
  340. }
  341. }
  342. function
  343. FW_confirmDelete()
  344. {
  345. var b = $("body");
  346. var cd = $(b).attr("data-confirmDelete");
  347. if(!cd || cd == 0)
  348. return;
  349. var wn = $(b).attr("data-webName");
  350. $("div#content").find("a").each(function(){
  351. var href = $(this).attr("href");
  352. if(!href)
  353. return;
  354. var ma = $(this).attr("href").match(/.*cmd[^=]*=(delete[^&]*).*$/);
  355. if(!ma || ma.length != 2)
  356. return;
  357. $(this).attr("href", "#");
  358. $(this).unbind("click");
  359. $(this).click(function(e){
  360. e.preventDefault();
  361. var div = $("<div id='FW_okDialog'>");
  362. $(div).html("Do you really want to "+ma[1]+"?<br><br>"+
  363. "<input type='checkbox' name='noconf'> Skip this dialog in the future");
  364. $("body").append(div);
  365. function
  366. doClose()
  367. {
  368. if($(div).find("input:checked").length)
  369. FW_cmd(FW_root+"?cmd=attr "+wn+" confirmDelete 0&XHR=1");
  370. $(this).dialog("close"); $(div).remove();
  371. }
  372. $(div).dialog({
  373. dialogClass:"no-close", modal:true, width:"auto", closeOnEscape:true,
  374. maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
  375. buttons: [
  376. {text:"Yes", click:function(){ location.href = ma[0]; doClose(); }},
  377. {text:"No", click:function(){ doClose(); }}]
  378. });
  379. });
  380. });
  381. }
  382. // Show the webCmd list in a dialog if: smallScreen & hiddenroom=detail & room
  383. function
  384. FW_smallScreenCommands()
  385. {
  386. if($("div#menu select").length == 0 || // not SmallScreen
  387. $("div#content").attr("room") == undefined || // not room Overview
  388. $("div#content div.col1 a").length > 0) // no hiddenroom=detail
  389. return;
  390. $("div#content div.col1").each(function(){
  391. var tr = $(this).closest("tr");
  392. if($(tr).find("> td").length <= 2)
  393. return;
  394. $(this).html("<a href='#'>"+$(this).html()+"</a>");
  395. $(this).find("a").click(function(){
  396. var t = $("<table></table>"), row=0;
  397. $(tr).find("> td").each(function(){
  398. $(t).append("<tr></tr>");
  399. if(row++ == 0) {
  400. $(t).find("tr:last").append($(this).find("a").html());
  401. } else {
  402. $(this).attr("data-orig", 1);
  403. this.orig=$(this).parent();
  404. $(t).find("tr:last").append($(this).detach());
  405. }
  406. });
  407. FW_okDialog(t, this, function(){
  408. $("#FW_okDialog [data-orig]").each(function(){
  409. $(this).detach().appendTo(this.orig);
  410. });
  411. });
  412. });
  413. });
  414. }
  415. if(window.jQuery) {
  416. $(document).ready(FW_jqueryReadyFn);
  417. } else {
  418. // FLOORPLAN compatibility
  419. loadScript("pgm2/jquery.min.js", function() {
  420. loadScript("pgm2/jquery-ui.min.js", function() {
  421. FW_jqueryReadyFn();
  422. }, true);
  423. }, true);
  424. }
  425. // FLOORPLAN compatibility
  426. function
  427. FW_delayedStart()
  428. {
  429. setTimeout("FW_longpoll()", 100);
  430. }
  431. function
  432. log(txt)
  433. {
  434. var d = new Date();
  435. var ms = ("000"+(d.getMilliseconds()%1000));
  436. ms = ms.substr(ms.length-3,3);
  437. txt = d.toTimeString().substring(0,8)+"."+ms+" "+txt;
  438. if(typeof window.console != "undefined")
  439. console.log(txt);
  440. }
  441. function
  442. addcsrf(arg)
  443. {
  444. if(typeof FW_csrfToken != "undefined") {
  445. arg = arg.replace(/&fwcsrf=[^&]*/,'');
  446. arg += '&fwcsrf='+encodeURIComponent(FW_csrfToken);
  447. }
  448. return arg;
  449. }
  450. function
  451. FW_csrfRefresh(callback)
  452. {
  453. log("FW_csrfRefresh, last was "+(FW_csrfOk ? "ok":"bad"));
  454. if(!FW_csrfOk) // avoid endless loop
  455. return;
  456. $.ajax({
  457. url:location.pathname+"?XHR=1",
  458. success: function(data, textStatus, request){
  459. FW_csrfToken = request.getResponseHeader('x-fhem-csrftoken');
  460. FW_csrfOk = false;
  461. if(callback)
  462. callback();
  463. }
  464. });
  465. }
  466. function
  467. FW_cmd(arg, callback)
  468. {
  469. if(arg.length < 120)
  470. log("FW_cmd:"+arg);
  471. else
  472. log("FW_cmd:"+arg.substr(0,120)+"...");
  473. $.ajax({
  474. url:addcsrf(arg)+'&fw_id='+$("body").attr('fw_id'),
  475. method:'POST',
  476. success: function(data, textStatus, req){
  477. FW_csrfOk = true;
  478. if(callback)
  479. callback(req.responseText);
  480. else if(req.responseText)
  481. FW_errmsg(req.responseText, 5000);
  482. },
  483. error:function(xhr, status, err) {
  484. if(xhr.status == 400 && typeof FW_csrfToken != "undefined") {
  485. FW_csrfToken = "";
  486. FW_csrfRefresh(function(){FW_cmd(arg, callback)});
  487. }
  488. }
  489. });
  490. }
  491. function
  492. FW_errmsg(txt, timeout)
  493. {
  494. log("ERRMSG:"+txt+"<");
  495. var errmsg = document.getElementById("errmsg");
  496. if(!errmsg) {
  497. if(txt == "")
  498. return;
  499. errmsg = document.createElement('div');
  500. errmsg.setAttribute("id","errmsg");
  501. document.body.appendChild(errmsg);
  502. }
  503. if(txt == "") {
  504. document.body.removeChild(errmsg);
  505. return;
  506. }
  507. errmsg.innerHTML = txt;
  508. if(timeout)
  509. setTimeout("FW_errmsg('')", timeout);
  510. }
  511. function
  512. FW_okDialog(txt, parent, removeFn)
  513. {
  514. var div = $("<div id='FW_okDialog'>");
  515. $(div).html(txt);
  516. $("body").append(div);
  517. var oldPos = $("body").scrollTop();
  518. $(div).dialog({
  519. dialogClass:"no-close", modal:true, width:"auto", closeOnEscape:true,
  520. maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
  521. buttons: [{text:"OK", click:function(){
  522. $(this).dialog("close");
  523. if(removeFn)
  524. removeFn();
  525. $(div).remove();
  526. }}]
  527. });
  528. FW_replaceWidgets(div);
  529. $(div).find("a").each(function(){FW_replaceLink(this);}); //Forum #33766
  530. if(parent)
  531. $(div).dialog( "option", "position", {
  532. my: "left top", at: "right bottom",
  533. of: parent, collision: "flipfit"
  534. });
  535. setTimeout(function(){$("body").scrollTop(oldPos);}, 1); // Not ideal.
  536. }
  537. function
  538. FW_menu(evt, el, arr, dis, fn, embedEl)
  539. {
  540. if(!embedEl)
  541. evt.stopPropagation();
  542. if($("#fwmenu").length) {
  543. delfwmenu();
  544. return;
  545. }
  546. var html = '<ul id="fwmenu">';
  547. for(var i=0; i<arr.length; i++) {
  548. html+='<li class="'+ ((dis && dis[i]) ? 'ui-state-disabled' : '')+'">'+
  549. '<a row="'+i+'" href="#">'+arr[i]+'</a></li>';
  550. }
  551. html += '</ul>';
  552. $("body").append(html);
  553. function
  554. delfwmenu()
  555. {
  556. $("ul#fwmenu").remove();
  557. $('html').unbind('click.fwmenu');
  558. }
  559. var wt = $(window).scrollTop();
  560. $("#fwmenu")
  561. .menu({
  562. select: function(e,ui) { // changes the scrollTop();
  563. e.stopPropagation();
  564. fn($(e.currentTarget).find("[row]").attr("row"));
  565. delfwmenu();
  566. setTimeout(function(){ $(window).scrollTop(wt) }, 1); // Bug in select?
  567. }
  568. });
  569. var off = $(el).offset();
  570. if(embedEl) {
  571. var embOff = $(embedEl).offset();
  572. off.top += embOff.top;
  573. off.left += embOff.left;
  574. }
  575. var dH = $("#fwmenu").height(), dW = $("#fwmenu").width(),
  576. wH = $(window).height(), wW = $(window).width();
  577. var ey = off.top+dH+20, ex = off.left+dW;
  578. if(ex>wW && ey>wH) { off.top -= dH; off.left -= (dW+16);
  579. } else if(ey > wH) { off.top -= dH; off.left += 20;
  580. } else if(ex > wW) { off.left -= (dW+16);
  581. } else { off.top += 20;
  582. }
  583. $("#fwmenu").css(off);
  584. $('html').bind('click.fwmenu', function() { delfwmenu(); });
  585. }
  586. function
  587. FW_getLink(el)
  588. {
  589. var attr = $(el).attr("href");
  590. if(!attr) {
  591. attr = $(el).attr("onclick"); // Tablet/smallScreen version
  592. if(!attr)
  593. return "";
  594. attr = attr.replace(/^location.href='/,'');
  595. attr = attr.replace(/'$/,'');
  596. }
  597. return attr;
  598. }
  599. function
  600. FW_replaceLink(el)
  601. {
  602. var attr = FW_getLink(el);
  603. if(!attr)
  604. return;
  605. var ma = attr.match(/^(.*\?)(cmd[^=]*=.*)$/);
  606. if(ma == null || ma.length == 0 || !ma[2].match(/=(save|set)/)) {
  607. ma = attr.match(new RegExp("^"+FW_root)); // Avoid "Connection lost" @iOS
  608. if(ma) {
  609. $(el).click(function(e) {
  610. // Open link in window/tab, Forum #39154
  611. if(e.shiftKey || e.ctrlKey || e.metaKey || e.button == 1)
  612. return;
  613. e.preventDefault();
  614. FW_leaving = 1;
  615. if($(el).attr("target") == "_blank") {
  616. window.open(attr, '_blank').focus();
  617. } else {
  618. location.href = attr;
  619. }
  620. });
  621. }
  622. return;
  623. }
  624. $(el).removeAttr("href");
  625. $(el).removeAttr("onclick");
  626. $(el).click(function() {
  627. FW_cmd(attr+"&XHR=1", function(txt){
  628. if(!txt)
  629. return;
  630. if(ma[2].match(/=set/)) // Forum #38875
  631. FW_okDialog('<pre>'+txt+'<pre>', el);
  632. else
  633. FW_errmsg(txt, 5000);
  634. });
  635. });
  636. $(el).css("cursor", "pointer");
  637. }
  638. function
  639. FW_htmlQuote(text)
  640. {
  641. return text.replace(/&/g, '&amp;') // Same as in 01_FHEMWEB
  642. .replace(/</g, '&lt;')
  643. .replace(/>/g, '&gt;');
  644. }
  645. function
  646. FW_inlineModify() // Do not generate a new HTML page upon pressing modify
  647. {
  648. var cm;
  649. if( typeof AddCodeMirror == 'function' ) {
  650. // init codemirror for FW_style edit textarea
  651. AddCodeMirror($('textarea[name="data"]'));
  652. }
  653. $('#DEFa').click(function(){
  654. var old = $('#edit').css('display');
  655. $('#edit').css('display', old=='none' ? 'block' : 'none');
  656. $('#disp').css('display', old=='none' ? 'none' : 'block');
  657. if( typeof AddCodeMirror == 'function' ) {
  658. var s=document.getElementById("edit").getElementsByTagName("textarea");
  659. AddCodeMirror(s[0], function(pcm) {cm = pcm;});
  660. }
  661. });
  662. $("div input.psc[type=submit]:not(.get)").click(function(e){
  663. e.preventDefault();
  664. var newDef = typeof cm !== 'undefined' ?
  665. cm.getValue() : $(this).closest("form").find("textarea").val();
  666. var cmd = $(this).attr("name")+"="+$(this).attr("value")+" "+newDef;
  667. var isDef = true, reloadIfOk = false;
  668. if(newDef == undefined || $(this).attr("value").indexOf("modify") != 0) {
  669. isDef = false;
  670. var div = $(this).closest("div.makeSelect");
  671. var devName = $(div).attr("dev"),
  672. cmd = $(div).attr("cmd");
  673. var sel = $(this).closest("form").find("select");
  674. var arg = $(sel).val();
  675. var ifid = (devName+"-"+arg).replace(/([^_a-z0-9])/gi,
  676. function(m){ return "\\"+m });
  677. if($(".dval[informid="+ifid+"]").length == 0) {
  678. if(cmd == "attr") {
  679. reloadIfOk = true;
  680. } else {
  681. $(this).unbind('click').click();// No element found to replace, reload
  682. return;
  683. }
  684. }
  685. newDef = $(this).closest("form").find("input:text").val();
  686. if(newDef == undefined)
  687. newDef = $(this).closest("form").find("[name^=val]").val();
  688. cmd = $(this).attr("name")+"="+cmd+" "+devName+" "+arg+" "+newDef;
  689. }
  690. FW_cmd(FW_root+"?"+encodeURIComponent(cmd)+"&XHR=1", function(resp){
  691. if(!resp && reloadIfOk)
  692. location.reload();
  693. if(resp) {
  694. resp = FW_htmlQuote(resp);
  695. if(resp.indexOf("\n") >= 0)
  696. resp = '<pre>'+resp+'</pre>';
  697. return FW_okDialog(resp);
  698. }
  699. if(isDef) {
  700. if(newDef.indexOf("\n") >= 0)
  701. newDef = '<pre>'+newDef+'</pre>';
  702. else
  703. newDef = FW_htmlQuote(newDef);
  704. $("div#disp").html(newDef).css("display", "");
  705. $("div#edit").css("display", "none");
  706. }
  707. });
  708. });
  709. }
  710. function
  711. FW_rawDef()
  712. {
  713. $("div.rawDef a").each(function(){ // Help on detail window
  714. var dev = FW_getLink(this).split(" ").pop().split("&")[0];
  715. $(this).unbind("click");
  716. $(this).attr("href", "#"); // Desktop: show underlined Text
  717. $(this).removeAttr("onclick");
  718. $(this).click(function(evt){
  719. if($("#rawDef").length) {
  720. $("#rawDef").remove();
  721. return;
  722. }
  723. var textAreaStyle = typeof AddCodeMirror == 'function'?'opacity:0':'';
  724. $("#content").append('<div id="rawDef">'+
  725. '<textarea id="td_rawDef" rows="25" cols="60" style="width:99%; '+
  726. textAreaStyle+'"/>'+
  727. '<button>Execute commands</button>'+
  728. ' Dump "Probably associated with" too <input type="checkbox">'+
  729. '<br><br></div>');
  730. var cmVar;
  731. function
  732. fillData(opt)
  733. {
  734. var s = $('#rawDef textarea');
  735. FW_cmd(FW_root+"?cmd=list "+opt+" "+dev+"&XHR=1", function(data) {
  736. var re = new RegExp("^define", "gm");
  737. data = data.replace(re, "defmod");
  738. s.val(data);
  739. var off = $("#rawDef").position().top-20;
  740. $('body, html').animate({scrollTop:off}, 500);
  741. $("#rawDef button").hide();
  742. var propertychange = function() {
  743. var nData = $("#rawDef textarea").val();
  744. if(nData != data)
  745. $("#rawDef button").show();
  746. else
  747. $("#rawDef button").hide();
  748. };
  749. s.bind('input propertychange', propertychange);
  750. if(cmVar) {
  751. cmVar.setValue(data);
  752. } else if(typeof AddCodeMirror == 'function') {
  753. AddCodeMirror(s, function(cm) {
  754. cmVar = cm;
  755. cm.on("change", function() {
  756. s.val(cm.getValue());
  757. propertychange();
  758. })
  759. });
  760. }
  761. });
  762. }
  763. fillData("-r");
  764. $("#rawDef input").click(function(){fillData(this.checked ?"-R":"-r")});
  765. $("#rawDef button").click(function(){
  766. var data = $("#rawDef textarea").val();
  767. var arr = data.split("\n"), str="", i1=-1;
  768. function
  769. doNext()
  770. {
  771. if(++i1 >= arr.length) {
  772. return FW_okDialog("Executed everything, no errors found.");
  773. }
  774. str += arr[i1];
  775. if(arr[i1].charAt(arr[i1].length-1) === "\\") {
  776. str += "\n";
  777. return doNext();
  778. }
  779. if(str != "") {
  780. str = str.replace(/\\\n/g, "\n")
  781. .replace(/;;/g, ";");
  782. FW_cmd(FW_root+"?cmd."+dev+"="+encodeURIComponent(str)+"&XHR=1",
  783. function(r){
  784. if(r)
  785. return FW_okDialog('<pre>'+r+'</pre>');
  786. str = "";
  787. doNext();
  788. });
  789. } else {
  790. doNext();
  791. }
  792. }
  793. doNext();
  794. });
  795. });
  796. });
  797. }
  798. var FW_arrowDown="", FW_arrowRight="";
  799. function
  800. FW_treeMenu()
  801. {
  802. var a = $("a").get(0);
  803. var col = 'rgb(39, 135, 38)';
  804. if(window.getComputedStyle && a)
  805. col = getComputedStyle(a,null).getPropertyValue('color');
  806. FW_arrowRight = 'data:image/svg+xml;utf8,<svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path fill="gray" d="M1171 960q0 13-10 23l-466 466q-10 10-23 10t-23-10l-50-50q-10-10-10-23t10-23l393-393-393-393q-10-10-10-23t10-23l50-50q10-10 23-10t23 10l466 466q10 10 10 23z"/></svg>'
  807. .replace('gray', col);
  808. FW_arrowDown =FW_arrowRight.replace('/>',' transform="rotate(90,896,896)"/>');
  809. var fnd;
  810. $("div#menu table.room").each(function(){ // one loop per Block
  811. var t = this, ma = {};
  812. $(t).find("td > div > a > span").each(function(e){
  813. var span = this, spanTxt = $(span).text().replace(/,/g,'');
  814. var ta = spanTxt.split("->");
  815. if(ta.length <= 1)
  816. return;
  817. fnd = true;
  818. var nxt="", lst="", tr=$(span).closest("tr");
  819. for(var i1=0; i1<ta.length-1; i1++) {
  820. nxt += "->"+ta[i1];
  821. if(!ma[nxt]) {
  822. $(tr).before("<tr class='menuTree closed level"+i1+"' "+
  823. "data-mTree='"+lst+"' data-nxt='"+nxt+"'>"+
  824. "<td><div><a href='#'>"+ta[i1]+"</a><div></div></div></td></tr>");
  825. }
  826. ma[nxt] = true;
  827. lst = nxt;
  828. }
  829. $(span).html(ta[ta.length-1]);
  830. $(tr).attr("data-mTree", nxt)
  831. .addClass("menuTree level"+(ta.length-1));
  832. });
  833. });
  834. if(fnd) {
  835. $("head").append(
  836. "<style>"+
  837. "tr.menuTree { cursor:pointer; }"+
  838. "tr.menuTree.level1 > td > div { margin-left:10px; }"+
  839. "tr.menuTree.level2 > td > div { margin-left:20px; }"+
  840. "tr.menuTree.level3 > td > div { margin-left:30px; }"+
  841. "tr.menuTree.open { font-weight: bold; }"+
  842. "tr.menuTree > td > div > div { "+
  843. "display:inline-block; width:1em; height:1em; float:right;"+
  844. "background-size: contain; background-repeat: no-repeat;"+
  845. "}"+
  846. "</style>");
  847. var t = $("div#menu table.room");
  848. $(t).find("tr[data-mTree]").not(".level0").hide();
  849. $(t).find("tr.menuTree").click(function(){treeClick(this)});
  850. $(t).find("tr.menuTree > td > div > div")
  851. .css("background-image", "url('"+FW_arrowRight+"')");
  852. var selRoom = $("div#content").attr("room");
  853. if(selRoom) {
  854. var ta = selRoom.split("->"), nxt="";
  855. for(var i1=0; i1<ta.length-1; i1++) {
  856. nxt += FW_escapeSelector("->"+ta[i1]);
  857. treeClick($(t).find("tr.menuTree[data-nxt="+nxt+"]"));
  858. }
  859. }
  860. }
  861. function
  862. treeClick(el)
  863. {
  864. var tgt = FW_escapeSelector($(el).attr("data-nxt"));
  865. if($(el).hasClass("closed")) {
  866. $(el).closest("table").find("tr[data-mTree="+tgt+"]").show();
  867. $(el).find("div>div").css("background-image", "url('"+FW_arrowDown+"')");
  868. } else {
  869. $(el).closest("table").find("tr[data-mTree^="+tgt+"]")
  870. .hide().addClass("closed");
  871. $(el).find("div>div").css("background-image", "url('"+FW_arrowRight+"')");
  872. }
  873. $(el).toggleClass("closed");
  874. $(el).toggleClass("open");
  875. };
  876. }
  877. function
  878. FW_escapeSelector(s)
  879. {
  880. if(typeof s != 'string')
  881. return s;
  882. return s.replace(/[ .#\[\]>]/g, function(r) { return '\\'+r });
  883. }
  884. /*************** LONGPOLL START **************/
  885. var FW_pollConn;
  886. var FW_longpollOffset = 0;
  887. var FW_leaving;
  888. var FW_lastDataTime=0;
  889. function
  890. FW_doUpdate(evt)
  891. {
  892. var errstr = "Connection lost, trying a reconnect every 5 seconds.";
  893. var input="";
  894. var retryTime = 5000;
  895. var now = new Date()/1000;
  896. function
  897. setValue(d) // is Callable from eval below
  898. {
  899. $("[informId='"+d[0]+"']").each(function(){
  900. if(this.setValueFn) { // change the select/etc value
  901. this.setValueFn(d[1].replace(/\n/g, '\u2424'));
  902. } else {
  903. if(d[2].match(/\n/) && !d[2].match(/<.*>/)) // format multiline
  904. d[2] = '<html><pre>'+d[2]+'</pre></html>';
  905. var ma = /^<html>([\s\S]*)<\/html>$/.exec(d[2]);
  906. if(!d[0].match("-")) // not a reading
  907. $(this).html(d[2]);
  908. else if(ma)
  909. $(this).html(ma[1]);
  910. else
  911. $(this).text(d[2]);
  912. if(d[0].match(/-ts$/)) // timestamps
  913. $(this).addClass('changed');
  914. $(this).find("a").each(function() { FW_replaceLink(this) });
  915. }
  916. });
  917. }
  918. // iOS closes HTTP after 60s idle, websocket after 240s idle
  919. if(now-FW_lastDataTime > 59) {
  920. errstr="";
  921. retryTime = 100;
  922. }
  923. FW_lastDataTime = now;
  924. // Websocket starts with Android 4.4, and IE10
  925. if(typeof WebSocket == "function" && evt && evt.target instanceof WebSocket) {
  926. if(evt.type == 'close' && !FW_leaving) {
  927. FW_errmsg(errstr, retryTime-100);
  928. FW_pollConn.close();
  929. FW_pollConn = undefined;
  930. setTimeout(FW_longpoll, retryTime);
  931. return;
  932. }
  933. input = evt.data;
  934. FW_longpollOffset = 0;
  935. } else if(FW_pollConn != undefined) {
  936. if(FW_pollConn.readyState == 4 && !FW_leaving) {
  937. if(FW_pollConn.status == "400") {
  938. location.reload();
  939. return;
  940. }
  941. FW_errmsg(errstr, retryTime-100);
  942. setTimeout(FW_longpoll, retryTime);
  943. return;
  944. }
  945. if(FW_pollConn.readyState != 3)
  946. return;
  947. input = FW_pollConn.responseText;
  948. }
  949. var devs = new Array();
  950. if(!input || input.length <= FW_longpollOffset)
  951. return;
  952. FW_serverLastMsg = (new Date()).getTime()/1000;
  953. for(;;) {
  954. var nOff = input.indexOf("\n", FW_longpollOffset);
  955. if(nOff < 0)
  956. break;
  957. var l = input.substr(FW_longpollOffset, nOff-FW_longpollOffset);
  958. FW_longpollOffset = nOff+1;
  959. log("Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l));
  960. if(!l.length)
  961. continue;
  962. if(l.indexOf("<")== 0) { // HTML returned by proxy, if FHEM behind is dead
  963. FW_closeConn();
  964. FW_errmsg(errstr, retryTime-100);
  965. setTimeout(FW_longpoll, retryTime);
  966. return;
  967. }
  968. var d = JSON.parse(l);
  969. if(d.length != 3)
  970. continue;
  971. if( d[0].match(/^#FHEMWEB:/) ) {
  972. eval(d[1]);
  973. } else {
  974. setValue(d);
  975. }
  976. // updateLine is deprecated, use setValueFn
  977. for(var w in FW_widgets)
  978. if(FW_widgets[w].updateLine && !FW_widgets[w].second)
  979. FW_widgets[w].updateLine(d);
  980. devs.push(d);
  981. }
  982. // used for SVG to avoid double-reloads
  983. for(var w in FW_widgets)
  984. if(FW_widgets[w].updateDevs && !FW_widgets[w].second)
  985. FW_widgets[w].updateDevs(devs);
  986. // reset the connection to avoid memory problems
  987. if(FW_longpollOffset > 1024*1024 && FW_longpollOffset==input.length)
  988. FW_longpoll();
  989. }
  990. function
  991. FW_closeConn()
  992. {
  993. FW_leaving = 1;
  994. if(!FW_pollConn)
  995. return;
  996. if(typeof FW_pollConn.close == "function")
  997. FW_pollConn.close();
  998. else if(typeof FW_pollConn.abort == "function")
  999. FW_pollConn.abort();
  1000. FW_pollConn = undefined;
  1001. }
  1002. function
  1003. FW_longpoll()
  1004. {
  1005. FW_closeConn();
  1006. FW_leaving = 0;
  1007. FW_longpollOffset = 0;
  1008. // Build the notify filter for the backend
  1009. var filter = $("body").attr("longpollfilter");
  1010. if(filter == null)
  1011. filter = "";
  1012. var retry;
  1013. if(filter == "") {
  1014. $("embed").each(function() { // wait for all embeds to be there
  1015. if(retry)
  1016. return;
  1017. var ed = FW_getSVG(this);
  1018. if(!retry && ed == undefined && filter != ".*" && --embedLoadRetry > 0) {
  1019. retry = 1;
  1020. setTimeout(FW_longpoll, 100);
  1021. return;
  1022. }
  1023. if(ed && $(ed).find("svg[flog]").attr("flog"))
  1024. filter=".*";
  1025. });
  1026. if(retry)
  1027. return;
  1028. }
  1029. if(filter == "") {
  1030. if(FW_urlParams.room) filter="room="+FW_urlParams.room;
  1031. if(FW_urlParams.detail) filter=FW_urlParams.detail;
  1032. }
  1033. if($("#floorplan").length>0) //floorplan special
  1034. filter += ";iconPath="+$("body").attr("name");
  1035. if(filter == "") {
  1036. var content = document.getElementById("content");
  1037. if(content) {
  1038. var room = content.getAttribute("room");
  1039. if(room)
  1040. filter="room="+room;
  1041. }
  1042. }
  1043. var iP = $("body").attr("iconPath");
  1044. if(iP != null)
  1045. filter = filter +";iconPath="+iP;
  1046. var since = "null";
  1047. if(FW_serverGenerated)
  1048. since = FW_serverLastMsg + (FW_serverGenerated-FW_serverFirstMsg);
  1049. var query = "?XHR=1"+
  1050. "&inform=type=status;filter="+filter+";since="+since+";fmt=JSON"+
  1051. '&fw_id='+$("body").attr('fw_id')+
  1052. "&timestamp="+new Date().getTime();
  1053. var loc = (""+location).replace(/\?.*/,"");
  1054. if(typeof WebSocket == "function" && FW_longpollType == "websocket") {
  1055. FW_pollConn = new WebSocket(loc.replace(/[#&?].*/,'')
  1056. .replace(/^http/i, "ws")+query);
  1057. FW_pollConn.onclose =
  1058. FW_pollConn.onerror =
  1059. FW_pollConn.onmessage = FW_doUpdate;
  1060. FW_pollConn.onopen = function(){FW_wsPing(FW_pollConn);};
  1061. } else {
  1062. FW_pollConn = new XMLHttpRequest();
  1063. FW_pollConn.open("GET", location.pathname+query, true);
  1064. if(FW_pollConn.overrideMimeType) // Win 8.1, #66004
  1065. FW_pollConn.overrideMimeType("application/json");
  1066. FW_pollConn.onreadystatechange = FW_doUpdate;
  1067. FW_pollConn.send(null);
  1068. }
  1069. log("Inform-channel opened ("+(FW_longpollType==1 ? "HTTP":FW_longpollType)+
  1070. ") with filter "+filter);
  1071. }
  1072. function
  1073. FW_wsPing(conn) // idle websockets are closed by the browser after 55sec
  1074. {
  1075. if(!conn || conn.readyState != conn.OPEN)
  1076. return;
  1077. conn.send("\n");
  1078. // setTimeout(function(){FW_wsPing(conn);}, 30000);
  1079. }
  1080. /*************** LONGPOLL END **************/
  1081. /*************** WIDGETS START **************/
  1082. /*************** "Double" select in detail window ****/
  1083. function
  1084. FW_detailSelect(selEl, mayMissing)
  1085. {
  1086. if(selEl.target)
  1087. selEl = selEl.target;
  1088. var selVal = $(selEl).val();
  1089. var div = $(selEl).closest("div.makeSelect");
  1090. if(!div.attr("list")) // hiddenRoom=input
  1091. return;
  1092. var arg,
  1093. listArr = $(div).attr("list").split(" "),
  1094. devName = $(div).attr("dev"),
  1095. cmd = $(div).attr("cmd");
  1096. var i1;
  1097. for(i1=0; i1<listArr.length; i1++) {
  1098. arg = listArr[i1];
  1099. if(arg.indexOf(selVal) == 0 &&
  1100. (arg.length == selVal.length || arg[selVal.length] == ':'))
  1101. break;
  1102. }
  1103. var vArr = [];
  1104. if(i1==listArr.length && !mayMissing)
  1105. return;
  1106. if(i1<listArr.length) {
  1107. if(arg.length > selVal.length)
  1108. vArr = arg.substr(selVal.length+1).split(",");
  1109. }
  1110. FW_replaceWidget($(selEl).next(), devName, vArr,undefined,selVal,
  1111. undefined, undefined, undefined,
  1112. function(newEl) {
  1113. if(cmd == "attr")
  1114. FW_queryValue('{AttrVal("'+devName+'","'+selVal+'","")}', newEl);
  1115. if(cmd == "set")
  1116. FW_queryValue('{ReadingsVal("'+devName+'","'+selVal+'","")}', newEl);
  1117. });
  1118. }
  1119. function
  1120. FW_callCreateFn(elName, devName, vArr, currVal, set, params, cmd, finishFn)
  1121. {
  1122. for(var wn in FW_widgets) {
  1123. if(FW_widgets[wn].createFn && !FW_widgets[wn].second) {
  1124. var newEl = FW_widgets[wn].createFn(elName, devName, vArr,
  1125. currVal, set, params, cmd);
  1126. if(newEl)
  1127. return finishFn(wn, newEl);
  1128. }
  1129. }
  1130. var v0 = vArr[0].split("-")[0];
  1131. if(v0.indexOf("uzsu") == 0)
  1132. v0 = "uzsu";
  1133. if(FW_availableJs[v0]) {
  1134. loadScript("pgm2/fhemweb_"+v0+".js", function() {
  1135. if(FW_widgets[vArr[0]].createFn)
  1136. var newEl = FW_widgets[vArr[0]].createFn(elName, devName, vArr,
  1137. currVal, set, params, cmd);
  1138. finishFn(vArr[0], newEl);
  1139. });
  1140. } else {
  1141. finishFn();
  1142. }
  1143. }
  1144. function
  1145. FW_replaceWidget(oldEl,devName,vArr,currVal,reading,set,params,cmd,readyFn)
  1146. {
  1147. var elName = $(oldEl).attr("name");
  1148. if(!elName)
  1149. elName = $(oldEl).find("[name]").attr("name");
  1150. if(vArr.length == 0) { // No parameters, input field
  1151. var newEl = FW_createTextField(elName, devName, ["textField"], currVal,
  1152. set, params, cmd);
  1153. finishFn("textField", newEl);
  1154. } else {
  1155. return FW_callCreateFn(elName, devName, vArr, currVal, set,
  1156. params, cmd, finishFn);
  1157. }
  1158. function
  1159. finishFn(wn, newEl)
  1160. {
  1161. if(!newEl) {
  1162. vArr.unshift("select");
  1163. newEl = FW_createSelect(elName,devName,vArr,currVal,set,params,cmd);
  1164. wn = "select";
  1165. }
  1166. if(!newEl) { // Simple link
  1167. newEl = $('<div class="col3"><a style="cursor: pointer;">'+
  1168. set+' '+params.join(' ')+ '</a></div>');
  1169. $(newEl).click(function(arg) { cmd(params[0]) });
  1170. $(oldEl).replaceWith(newEl);
  1171. if(readyFn)
  1172. return readyFn(newEl);
  1173. return;
  1174. }
  1175. $(newEl).addClass(wn+"_widget");
  1176. if( $(newEl).find("[informId]").length==0 && !$(newEl).attr("informId") ) {
  1177. if(reading) {
  1178. var a = $(oldEl).closest("form").find("input[type=submit][value=attr]");
  1179. $(newEl).attr("informId", devName+(a.length?"-a-":"-")+reading);
  1180. }
  1181. var addTitle = $("body").attr("data-addHtmlTitle");
  1182. if(reading != "state" && addTitle==1)
  1183. $(newEl).attr("title", reading);
  1184. }
  1185. $(oldEl).replaceWith(newEl);
  1186. if(newEl.activateFn) // CSS is not applied if newEl is not in the document
  1187. newEl.activateFn();
  1188. if(readyFn)
  1189. readyFn(newEl);
  1190. }
  1191. }
  1192. function
  1193. FW_queryValue(cmd, el)
  1194. {
  1195. log("FW_queryValue:"+cmd);
  1196. var query = location.pathname+"?cmd="+encodeURIComponent(cmd)+"&XHR=1";
  1197. query = addcsrf(query);
  1198. var qConn = new XMLHttpRequest();
  1199. qConn.onreadystatechange = function() {
  1200. if(qConn.readyState != 4)
  1201. return;
  1202. var qResp = qConn.responseText.replace(/\n$/, '');
  1203. qResp = qResp.replace(/\n/g, '\u2424');
  1204. if(el.setValueFn)
  1205. el.setValueFn(qResp);
  1206. qConn.abort();
  1207. }
  1208. qConn.open("GET", query, true);
  1209. qConn.send(null);
  1210. }
  1211. /*************** TEXTFIELD **************/
  1212. function
  1213. FW_createTextField(elName, devName, vArr, currVal, set, params, cmd)
  1214. {
  1215. if(vArr.length != 1 ||
  1216. (vArr[0] != "textField" &&
  1217. vArr[0] != "textFieldNL" &&
  1218. vArr[0] != "textField-long" &&
  1219. vArr[0] != "textFieldNL-long") ||
  1220. (params && params.length))
  1221. return undefined;
  1222. var is_long = (vArr[0].indexOf("long") > 0);
  1223. var newEl = $("<div style='display:inline-block'>").get(0);
  1224. if(set && set != "state" && vArr[0].indexOf("NL") < 0)
  1225. $(newEl).append(set+":");
  1226. $(newEl).append('<input type="text" size="30">');
  1227. var inp = $(newEl).find("input").get(0);
  1228. if(elName)
  1229. $(inp).attr('name', elName);
  1230. if(currVal != undefined)
  1231. $(inp).val(currVal);
  1232. function addBlur() { if(cmd) $(inp).blur(function() { cmd($(inp).val()) }); };
  1233. newEl.setValueFn = function(arg){ $(inp).val(arg) };
  1234. addBlur();
  1235. var myFunc = function(){
  1236. $(inp).unbind("blur");
  1237. $('body').append(
  1238. '<div id="editdlg" style="display:none">'+
  1239. '<textarea id="td_longText" rows="25" cols="60" style="width:99%"/>'+
  1240. '</div>');
  1241. var txt = $(inp).val();
  1242. txt = txt.replace(/\u2424/g, '\n');
  1243. $("#td_longText").val(txt);
  1244. var cm;
  1245. if(typeof AddCodeMirror == 'function') {
  1246. AddCodeMirror($("#td_longText"), function(pcm) {cm = pcm;});
  1247. }
  1248. $('#editdlg').dialog(
  1249. { modal:true, closeOnEscape:true, width:$(window).width()*3/4,
  1250. height:$(window).height()*3/4,
  1251. close:function(){ $('#editdlg').remove(); },
  1252. buttons:[
  1253. { text:"Cancel", click:function(){
  1254. $(this).dialog('close');
  1255. addBlur();
  1256. }},
  1257. { text:"OK", click:function(){
  1258. if(cm)
  1259. $("#td_longText").val(cm.getValue());
  1260. var res=$("#td_longText").val();
  1261. res = res.replace(/\n/g, '\u2424' );
  1262. $(this).dialog('close');
  1263. $(inp).val(res);
  1264. addBlur();
  1265. }}]
  1266. });
  1267. };
  1268. if(is_long)
  1269. $(newEl).click(myFunc);
  1270. return newEl;
  1271. }
  1272. /*************** select **************/
  1273. function
  1274. FW_createSelect(elName, devName, vArr, currVal, set, params, cmd)
  1275. {
  1276. if(vArr.length < 2 || vArr[0] != "select" || (params && params.length))
  1277. return undefined;
  1278. var newEl = document.createElement('select');
  1279. var vHash = {};
  1280. for(var j=1; j < vArr.length; j++) {
  1281. var o = document.createElement('option');
  1282. o.text = o.value = vArr[j].replace(/#/g," ");
  1283. vHash[vArr[j]] = 1;
  1284. newEl.options[j-1] = o;
  1285. }
  1286. if(currVal)
  1287. $(newEl).val(currVal);
  1288. if(elName)
  1289. $(newEl).attr('name', elName);
  1290. if(cmd)
  1291. $(newEl).change(function(arg) { cmd($(newEl).val()) });
  1292. newEl.setValueFn = function(arg) { if(vHash[arg]) $(newEl).val(arg); };
  1293. return newEl;
  1294. }
  1295. /*************** selectNumbers **************/
  1296. // Syntax: selectnumbers,<min value>,<step|step of exponent>,<max value>,<number of digits after decimal point>,lin|log10
  1297. function
  1298. FW_createSelectNumbers(elName, devName, vArr, currVal, set, params, cmd)
  1299. {
  1300. if(vArr.length < 6 || vArr[0] != "selectnumbers" || (params && params.length))
  1301. return undefined;
  1302. var min = parseFloat(vArr[1]);
  1303. var stp = parseFloat(vArr[2]);
  1304. var max = parseFloat(vArr[3]);
  1305. var dp = parseFloat(vArr[4]); // decimal points
  1306. var fun = vArr[5]; // function
  1307. if(currVal != undefined)
  1308. currVal = currVal.replace(/[^\d.\-]/g, "");
  1309. currVal = (currVal==undefined || currVal=="") ? min : parseFloat(currVal);
  1310. if(max==min)
  1311. return undefined;
  1312. if(!(fun == "lin" || fun == "log10"))
  1313. return undefined;
  1314. if(currVal < min)
  1315. currVal = min;
  1316. if(currVal > max)
  1317. currVal = max;
  1318. var newEl = document.createElement('select');
  1319. var vHash = {};
  1320. var k = 0;
  1321. var v = 0;
  1322. if (fun == "lin") {
  1323. for(var j=min; j <= max; j+=stp) {
  1324. var o = document.createElement('option');
  1325. o.text = o.value = j.toFixed(dp);
  1326. vHash[j.toString()] = 1;
  1327. newEl.options[k] = o;
  1328. k++;
  1329. }
  1330. } else if (fun == "log10") {
  1331. if(min <= 0 || max <= 0)
  1332. return undefined;
  1333. for(var j=Math.log10(min); j <= Math.log10(max)+stp; j+=stp) {
  1334. var o = document.createElement('option');
  1335. var w = Math.pow(10, j)
  1336. if (w > max)
  1337. w = max;
  1338. if (v == w.toFixed(dp))
  1339. continue;
  1340. v = w.toFixed(dp);
  1341. o.text = o.value = v;
  1342. vHash[v] = 1;
  1343. newEl.options[k] = o;
  1344. k++;
  1345. }
  1346. }
  1347. if(currVal)
  1348. $(newEl).val(currVal.toFixed(dp));
  1349. if(elName)
  1350. $(newEl).attr('name', elName);
  1351. if(cmd)
  1352. $(newEl).change(function(arg) { cmd($(newEl).val()) });
  1353. newEl.setValueFn = function(arg) { if(vHash[arg]) $(newEl).val(arg); };
  1354. return newEl;
  1355. }
  1356. /*************** noArg **************/
  1357. function
  1358. FW_createNoArg(elName, devName, vArr, currVal, set, params, cmd)
  1359. {
  1360. if(vArr.length != 1 || vArr[0] != "noArg" || (params && params.length))
  1361. return undefined;
  1362. var newEl = $('<div style="display:none">').get(0);
  1363. if(elName)
  1364. $(newEl).append('<input type="hidden" name="'+elName+ '" value="">');
  1365. return(newEl);
  1366. }
  1367. /*************** slider **************/
  1368. function
  1369. FW_createSlider(elName, devName, vArr, currVal, set, params, cmd)
  1370. {
  1371. // min, step, max, float
  1372. if(vArr.length < 4 || vArr.length > 5 || vArr[0] != "slider" ||
  1373. (params && params.length))
  1374. return undefined;
  1375. var min = parseFloat(vArr[1]);
  1376. var stp = parseFloat(vArr[2]);
  1377. var max = parseFloat(vArr[3]);
  1378. var flt = (vArr.length == 5 && vArr[4] == "1");
  1379. var dp = 0; // decimal points for float
  1380. if(flt) {
  1381. var s = ""+stp;
  1382. if(s.indexOf(".") >= 0)
  1383. dp = s.substr(s.indexOf(".")+1).length;
  1384. }
  1385. if(currVal != undefined)
  1386. currVal = currVal.replace(/[^\d.\-]/g, "");
  1387. currVal = (currVal==undefined || currVal=="") ? min : parseFloat(currVal);
  1388. if(max==min)
  1389. return undefined;
  1390. if(currVal < min || currVal > max)
  1391. currVal = min;
  1392. var newEl = $('<div style="display:inline-block" tabindex="0">').get(0);
  1393. var slider = $('<div class="slider" id="slider.'+devName+'">').get(0);
  1394. $(newEl).append(slider);
  1395. var sh = $('<div class="handle">'+currVal+'</div>').get(0);
  1396. $(slider).append(sh);
  1397. if(elName)
  1398. $(newEl).append('<input type="hidden" name="'+elName+
  1399. '" value="'+currVal+'">');
  1400. var lastX=-1, offX=0, maxX=0, val=currVal;
  1401. newEl.activateFn = function() {
  1402. if(currVal < min || currVal > max)
  1403. return;
  1404. if(!slider.offsetWidth)
  1405. return setTimeout(newEl.activateFn, 1);
  1406. maxX = slider.offsetWidth-sh.offsetWidth;
  1407. offX = (currVal-min)*maxX/(max-min);
  1408. var strVal = (flt ? currVal.toFixed(dp) : ""+parseInt(currVal));
  1409. sh.innerHTML = strVal;
  1410. sh.setAttribute('style', 'left:'+offX+'px;');
  1411. if(elName)
  1412. slider.nextSibling.setAttribute('value', strVal);
  1413. }
  1414. $(newEl).keydown(function(e){
  1415. if(e.keyCode == 37) currVal -= stp;
  1416. else if(e.keyCode == 39) currVal += stp;
  1417. else return;
  1418. if(currVal < min) currVal = min;
  1419. if(currVal > max) currVal = max;
  1420. offX = (currVal-min)*maxX/(max-min);
  1421. var strVal = (flt ? currVal.toFixed(dp) : ""+parseInt(currVal));
  1422. sh.innerHTML = strVal;
  1423. sh.setAttribute('style', 'left:'+offX+'px;');
  1424. if(cmd)
  1425. cmd(strVal);
  1426. if(elName)
  1427. slider.nextSibling.setAttribute('value', strVal);
  1428. });
  1429. function
  1430. touchFn(e, fn)
  1431. {
  1432. e.preventDefault(); // Prevents Safari from scrolling!
  1433. if(e.touches == null || e.touches.length == 0)
  1434. return;
  1435. e.clientX = e.touches[0].clientX;
  1436. fn(e);
  1437. }
  1438. function
  1439. mouseDown(e)
  1440. {
  1441. var oldFn1 = document.onmousemove, oldFn2 = document.onmouseup,
  1442. oldFn3 = document.ontouchmove, oldFn4 = document.ontouchend;
  1443. e.stopPropagation(); // Dashboard fix
  1444. lastX = e.clientX; // Does not work on IE8
  1445. function
  1446. mouseMove(e)
  1447. {
  1448. e.stopPropagation(); // Dashboard fix
  1449. if(maxX == 0) // Forum #35846
  1450. maxX = slider.offsetWidth-sh.offsetWidth;
  1451. var diff = e.clientX-lastX; lastX = e.clientX;
  1452. offX += diff;
  1453. if(offX < 0) offX = 0;
  1454. if(offX > maxX) offX = maxX;
  1455. val = offX/maxX * (max-min);
  1456. val = flt ? (Math.floor(val/stp)*stp+min).toFixed(dp) :
  1457. (Math.floor(Math.floor(val/stp)*stp)+min);
  1458. sh.innerHTML = val;
  1459. sh.setAttribute('style', 'left:'+offX+'px;');
  1460. }
  1461. document.onmousemove = mouseMove;
  1462. document.ontouchmove = function(e) { touchFn(e, mouseMove); }
  1463. document.onmouseup = document.ontouchend = function(e)
  1464. {
  1465. e.stopPropagation(); // Dashboard fix
  1466. document.onmousemove = oldFn1; document.onmouseup = oldFn2;
  1467. document.ontouchmove = oldFn3; document.ontouchend = oldFn4;
  1468. if(cmd)
  1469. cmd(val);
  1470. if(elName)
  1471. slider.nextSibling.setAttribute('value', val);
  1472. };
  1473. };
  1474. sh.onselectstart = function() { return false; }
  1475. sh.onmousedown = mouseDown;
  1476. sh.ontouchstart = function(e) { touchFn(e, mouseDown); }
  1477. newEl.setValueFn = function(arg) {
  1478. var res = arg.match(/[\d.\-]+/); // extract first number
  1479. currVal = (res ? parseFloat(res[0]) : min);
  1480. if(currVal < min || currVal > max)
  1481. currVal = min;
  1482. newEl.activateFn();
  1483. };
  1484. return newEl;
  1485. }
  1486. /*************** TIME **************/
  1487. function
  1488. FW_createTime(elName, devName, vArr, currVal, set, params, cmd)
  1489. {
  1490. if(vArr.length != 1 || vArr[0] != "time" || (params && params.length))
  1491. return undefined;
  1492. var open="-", closed="+";
  1493. var newEl = document.createElement('div');
  1494. $(newEl).append('<input type="text" size="5">');
  1495. $(newEl).append('<input type="button" value="'+closed+'">');
  1496. var inp = $(newEl).find("[type=text]");
  1497. var btn = $(newEl).find("[type=button]");
  1498. currVal = (currVal ? currVal : "12:00")
  1499. .replace(/[^\d]*(\d\d):(\d\d).*/g,"$1:$2");
  1500. $(inp).val(currVal)
  1501. if(elName)
  1502. $(inp).attr("name", elName);
  1503. var hh, mm; // the slider elements
  1504. newEl.setValueFn = function(arg) {
  1505. arg = arg.replace(/[^\d]*(\d\d):(\d\d).*/g,"$1:$2");
  1506. $(inp).val(arg);
  1507. var hhmm = arg.split(":");
  1508. if(hhmm.length == 2 && hh && mm) {
  1509. hh.setValueFn(hhmm[0]);
  1510. mm.setValueFn(hhmm[1]);
  1511. }
  1512. };
  1513. $(btn).click(function(){ // Open/Close the slider view
  1514. var v = $(inp).val();
  1515. if($(btn).val() == open) {
  1516. $(btn).val(closed);
  1517. $(newEl).find(".timeSlider").remove();
  1518. hh = mm = undefined;
  1519. if(cmd)
  1520. cmd(v);
  1521. return;
  1522. }
  1523. $(btn).val(open);
  1524. if(v.indexOf(":") < 0) {
  1525. v = "12:00";
  1526. $(inp).val(v);
  1527. }
  1528. var hhmm = v.split(":");
  1529. function
  1530. tSet(idx, arg)
  1531. {
  1532. if((""+arg).length < 2)
  1533. arg = '0'+arg;
  1534. hhmm[idx] = arg;
  1535. $(inp).val(hhmm.join(":"));
  1536. }
  1537. $(newEl).append('<div class="timeSlider">');
  1538. var ts = $(newEl).find(".timeSlider");
  1539. hh = FW_createSlider(undefined, devName+"HH", ["slider", 0, 1, 23],
  1540. hhmm[0], undefined, params, function(arg) { tSet(0, arg) });
  1541. mm = FW_createSlider(undefined, devName+"MM", ["slider", 0, 5, 55],
  1542. hhmm[1], undefined, params, function(arg) { tSet(1, arg) });
  1543. $(ts).append("<br>"); $(ts).append(hh); hh.activateFn();
  1544. $(ts).append("<br>"); $(ts).append(mm); mm.activateFn();
  1545. });
  1546. return newEl;
  1547. }
  1548. /*************** MULTIPLE **************/
  1549. function
  1550. FW_createMultiple(elName, devName, vArr, currVal, set, params, cmd)
  1551. {
  1552. if(vArr.length < 2 || (vArr[0]!="multiple" && vArr[0]!="multiple-strict") ||
  1553. (params && params.length))
  1554. return undefined;
  1555. var newEl = $('<input type="text" size="30" readonly>').get(0);
  1556. if(currVal)
  1557. $(newEl).val(currVal);
  1558. if(elName)
  1559. $(newEl).attr("name", elName);
  1560. newEl.setValueFn = function(arg){ $(newEl).val(arg) };
  1561. for(var i1=1; i1<vArr.length; i1++)
  1562. vArr[i1] = vArr[i1].replace(/#/g, " ");
  1563. $(newEl).focus(function(){
  1564. var sel = $(newEl).val().split(","), selObj={};
  1565. for(var i1=0; i1<sel.length; i1++)
  1566. selObj[sel[i1]] = 1;
  1567. var table = "";
  1568. for(var i1=1; i1<vArr.length; i1++) {
  1569. var v = vArr[i1];
  1570. table += '<tr>'+ // funny stuff for ios6 style, forum #23561
  1571. '<td><div class="checkbox">'+
  1572. '<input name="'+v+'" id="multiple_'+v+'" type="checkbox"'+
  1573. (selObj[v] ? " checked" : "")+'/>'+'</div></td>'+
  1574. '<td><label for="multiple_'+v+'">'+v+'</label></td></tr>';
  1575. delete(selObj[v]);
  1576. }
  1577. var selArr=[];
  1578. for(var i1 in selObj)
  1579. selArr.push(i1);
  1580. var strict = (vArr[0] == "multiple-strict");
  1581. $('body').append(
  1582. '<div id="multidlg" style="display:none">'+
  1583. '<table>'+table+'</table>'+(!strict ? '<input id="md_freeText" '+
  1584. 'value="'+selArr.join(',')+'"/>' : '')+
  1585. '</div>');
  1586. $('#multidlg').dialog(
  1587. { modal:true, closeOnEscape:false, maxHeight:$(window).height()*3/4,
  1588. buttons:[
  1589. { text:"Cancel", click:function(){ $('#multidlg').remove(); }},
  1590. { text:"OK", click:function(){
  1591. var res=[];
  1592. if($("#md_freeText").val())
  1593. res.push($("#md_freeText").val());
  1594. $("#multidlg table input").each(function(){
  1595. if($(this).prop("checked"))
  1596. res.push($(this).attr("name"));
  1597. });
  1598. $('#multidlg').remove();
  1599. $(newEl).val(res.join(","));
  1600. if(cmd)
  1601. cmd(res.join(","));
  1602. }}]});
  1603. });
  1604. return newEl;
  1605. }
  1606. /*************** WIDGETS END **************/
  1607. /*************** SCRIPT LOAD FUNCTIONS START **************/
  1608. function
  1609. loadScript(sname, callback, force)
  1610. {
  1611. var h = document.head || document.getElementsByTagName('head')[0];
  1612. sname = FW_root+"/"+sname;
  1613. if(FW_scripts[sname]) {
  1614. if(FW_scripts[sname].loaded) {
  1615. if(callback)
  1616. callback();
  1617. } else {
  1618. FW_scripts[sname].callbacks.push(callback);
  1619. }
  1620. return;
  1621. }
  1622. if(!FW_docReady && !force) {
  1623. FW_scripts[sname] = { callbacks:[ callback] };
  1624. return;
  1625. }
  1626. var script = document.createElement("script");
  1627. script.src = sname;
  1628. script.async = script.defer = false;
  1629. script.type = "text/javascript";
  1630. FW_scripts[sname] = { callbacks:[ callback] };
  1631. function
  1632. scriptLoaded()
  1633. {
  1634. var p = FW_scripts[sname];
  1635. p.loaded = true;
  1636. if(!p.called) {
  1637. p.called = true;
  1638. for(var i1=0; i1< p.callbacks.length; i1++)
  1639. if(p.callbacks[i1]) // pushing undefined callbacks on the stack is ok
  1640. p.callbacks[i1]();
  1641. }
  1642. delete(p.callbacks);
  1643. }
  1644. log("Loading script "+sname);
  1645. if(FW_isIE) {
  1646. script.onreadystatechange = function() {
  1647. if(script.readyState == 'loaded' || script.readyState == 'complete') {
  1648. script.onreadystatechange = null;
  1649. scriptLoaded();
  1650. }
  1651. }
  1652. } else {
  1653. if(FW_isiOS)
  1654. FW_closeConn();
  1655. script.onload = function(){
  1656. scriptLoaded();
  1657. }
  1658. }
  1659. h.appendChild(script);
  1660. }
  1661. function
  1662. loadLink(lname)
  1663. {
  1664. var h = document.head || document.getElementsByTagName('head')[0];
  1665. lname = FW_root+"/"+lname;
  1666. var arr = h.getElementsByTagName("link");
  1667. for(var i1=0; i1<arr.length; i1++)
  1668. if(lname == arr[i1].getAttribute("href"))
  1669. return;
  1670. var link = document.createElement("link");
  1671. link.href = lname;
  1672. link.rel = "stylesheet";
  1673. log("Loading link "+lname);
  1674. h.appendChild(link);
  1675. }
  1676. function
  1677. scriptAttribute(sname)
  1678. {
  1679. var attr="";
  1680. $("head script").each(function(){
  1681. var src = $(this).attr("src");
  1682. if(src && src.indexOf(sname) >= 0)
  1683. attr = $(this).attr("attr");
  1684. });
  1685. var ua={};
  1686. if(attr && attr != "") {
  1687. try {
  1688. ua=JSON.parse(attr);
  1689. } catch(e){
  1690. FW_errmsg(sname+" Parameter "+e,5000);
  1691. }
  1692. }
  1693. return ua;
  1694. }
  1695. /*************** SCRIPT LOAD FUNCTIONS END **************/
  1696. function
  1697. print_call_stack() {
  1698. var stack = new Error().stack;
  1699. console.log("PRINTING CALL STACK");
  1700. console.log( stack );
  1701. }
  1702. function
  1703. FW_getSVG(emb)
  1704. {
  1705. if(emb.contentDocument)
  1706. return emb.contentDocument;
  1707. if(typeof emb.getSVGDocument == "function") {
  1708. try {
  1709. return emb.getSVGDocument();
  1710. } catch(err) {
  1711. // dom not loaded -> fall through -> retry;
  1712. }
  1713. }
  1714. return undefined;
  1715. }
  1716. /*
  1717. =pod
  1718. =begin html
  1719. <li>noArg - show no input field.</li>
  1720. <li>time - show a JavaScript driven timepicker.<br>
  1721. Example: attr FS20dev widgetOverride on-till:time</li>
  1722. <li>textField - show an input field.<br>
  1723. Example: attr WEB widgetOverride room:textField</li>
  1724. <li>textFieldNL - show the input field and hide the label.</li>
  1725. <li>textField-long - show an input-field, but upon
  1726. clicking on the input field open a textArea (60x25).</li>
  1727. <li>textFieldNL-long - the behaviour is the same
  1728. as :textField-long, but no label is displayed.</li>
  1729. <li>slider,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;[,1] - show
  1730. a JavaScript driven slider. The optional ,1 at the end
  1731. avoids the rounding of floating-point numbers.</li>
  1732. <li>multiple,&lt;val1&gt;,&lt;val2&gt;,..." - present a
  1733. multiple-value-selector with an additional textfield. The result is
  1734. comman separated.</li>
  1735. <li>multiple-strict,&lt;val1&gt;,&lt;val2&gt;,... - like :multiple, but
  1736. without the textfield.</li>
  1737. <li>selectnumbers,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;,&lt;number of
  1738. digits after decimal point&gt;,lin|log10" - display a select widget
  1739. generated with values from min to max with step.<br>
  1740. lin generates a constantly increasing series. log10 generates an
  1741. exponentially increasing series to base 10, step is related to the
  1742. exponent, e.g. 0.0625.</li>
  1743. <li>select,&lt;val1&gt;,&lt;val2&gt;,... - show a dropdown with all values.
  1744. <b>NOTE</b>: this is also the fallback, if no modifier is found.</li>
  1745. =end html
  1746. =begin html_DE
  1747. <li>noArg - es wird kein weiteres Eingabefeld angezeigt.</li>
  1748. <li>time - zeigt ein Zeitauswahlmen&uuml;.
  1749. Beispiel: attr FS20dev widgetOverride on-till:time</li>
  1750. <li>textField - zeigt ein Eingabefeld.<br>
  1751. Beispiel: attr WEB widgetOverride room:textField</li>
  1752. <li>textField-long - ist wie textField, aber beim Click im Eingabefeld wird
  1753. ein Dialog mit einer HTML textarea (60x25) wird ge&ouml;ffnet.</li>
  1754. <li>slider,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;[,1] - zeigt einen
  1755. Schieberegler. Das optionale 1 (isFloat) vermeidet eine Rundung der
  1756. Fliesskommazahlen.</li>
  1757. <li>multiple,&lt;val1&gt;,&lt;val2&gt;,... - zeigt eine Mehrfachauswahl mit
  1758. einem zus&auml;tzlichen Eingabefeld. Das Ergebnis ist Komma
  1759. separiert.</li>
  1760. <li>multiple-strict,&lt;val1&gt;,&lt;val2&gt;,... - ist wie :multiple,
  1761. blo&szlig; ohne Eingabefeld.</li>
  1762. <li>selectnumbers,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;,&lt;number of
  1763. digits after decimal point&gt;,lin|log10" zeigt ein HTML-select mit einer
  1764. Zahlenreihe vom Wert min bis Wert max mit Schritten von step
  1765. angezeigt.<br>
  1766. Die Angabe lin erzeugt eine konstant ansteigende Reihe. Die Angabe
  1767. log10 erzeugt eine exponentiell ansteigende Reihe zur Basis 10,
  1768. step bezieht sich auf den Exponenten, z.B. 0.0625.</li>
  1769. <li>select,&lt;val1&gt;,&lt;val2&gt;,... - zeigt ein HTML select mit allen
  1770. Werten. <b>Achtung</b>: so ein Widget wird auch dann angezeigt, falls
  1771. kein passender Modifier gefunden wurde.</li>
  1772. =end html_DE
  1773. =cut
  1774. */