fhemweb.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. "use strict";
  2. var FW_version={};
  3. FW_version["fhemweb.js"] = "$Id: fhemweb.js 17645 2018-10-30 10:27:44Z 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]]) {
  1136. log("ERROR: fhemweb_"+vArr[v0]+".js does not fill FW_widgets");
  1137. return;
  1138. }
  1139. if(FW_widgets[vArr[0]].createFn)
  1140. var newEl = FW_widgets[vArr[0]].createFn(elName, devName, vArr,
  1141. currVal, set, params, cmd);
  1142. finishFn(vArr[0], newEl);
  1143. });
  1144. } else {
  1145. finishFn();
  1146. }
  1147. }
  1148. function
  1149. FW_replaceWidget(oldEl,devName,vArr,currVal,reading,set,params,cmd,readyFn)
  1150. {
  1151. var elName = $(oldEl).attr("name");
  1152. if(!elName)
  1153. elName = $(oldEl).find("[name]").attr("name");
  1154. if(vArr.length == 0) { // No parameters, input field
  1155. var newEl = FW_createTextField(elName, devName, ["textField"], currVal,
  1156. set, params, cmd);
  1157. finishFn("textField", newEl);
  1158. } else {
  1159. return FW_callCreateFn(elName, devName, vArr, currVal, set,
  1160. params, cmd, finishFn);
  1161. }
  1162. function
  1163. finishFn(wn, newEl)
  1164. {
  1165. if(!newEl) {
  1166. vArr.unshift("select");
  1167. newEl = FW_createSelect(elName,devName,vArr,currVal,set,params,cmd);
  1168. wn = "select";
  1169. }
  1170. if(!newEl) { // Simple link
  1171. newEl = $('<div class="col3"><a style="cursor: pointer;">'+
  1172. set+' '+params.join(' ')+ '</a></div>');
  1173. $(newEl).click(function(arg) { cmd(params[0]) });
  1174. $(oldEl).replaceWith(newEl);
  1175. if(readyFn)
  1176. return readyFn(newEl);
  1177. return;
  1178. }
  1179. $(newEl).addClass(wn+"_widget");
  1180. if( $(newEl).find("[informId]").length==0 && !$(newEl).attr("informId") ) {
  1181. if(reading) {
  1182. var a = $(oldEl).closest("form").find("input[type=submit][value=attr]");
  1183. $(newEl).attr("informId", devName+(a.length?"-a-":"-")+reading);
  1184. }
  1185. var addTitle = $("body").attr("data-addHtmlTitle");
  1186. if(reading != "state" && addTitle==1)
  1187. $(newEl).attr("title", reading);
  1188. }
  1189. $(oldEl).replaceWith(newEl);
  1190. if(newEl.activateFn) // CSS is not applied if newEl is not in the document
  1191. newEl.activateFn();
  1192. if(readyFn)
  1193. readyFn(newEl);
  1194. }
  1195. }
  1196. function
  1197. FW_queryValue(cmd, el)
  1198. {
  1199. log("FW_queryValue:"+cmd);
  1200. var query = location.pathname+"?cmd="+encodeURIComponent(cmd)+"&XHR=1";
  1201. query = addcsrf(query);
  1202. var qConn = new XMLHttpRequest();
  1203. qConn.onreadystatechange = function() {
  1204. if(qConn.readyState != 4)
  1205. return;
  1206. var qResp = qConn.responseText.replace(/\n$/, '');
  1207. qResp = qResp.replace(/\n/g, '\u2424');
  1208. if(el.setValueFn)
  1209. el.setValueFn(qResp);
  1210. qConn.abort();
  1211. }
  1212. qConn.open("GET", query, true);
  1213. qConn.send(null);
  1214. }
  1215. /*************** TEXTFIELD **************/
  1216. function
  1217. FW_createTextField(elName, devName, vArr, currVal, set, params, cmd)
  1218. {
  1219. if(vArr.length != 1 ||
  1220. (vArr[0] != "textField" &&
  1221. vArr[0] != "textFieldNL" &&
  1222. vArr[0] != "textField-long" &&
  1223. vArr[0] != "textFieldNL-long") ||
  1224. (params && params.length))
  1225. return undefined;
  1226. var is_long = (vArr[0].indexOf("long") > 0);
  1227. var newEl = $("<div style='display:inline-block'>").get(0);
  1228. if(set && set != "state" && vArr[0].indexOf("NL") < 0)
  1229. $(newEl).append(set+":");
  1230. $(newEl).append('<input type="text" size="30">');
  1231. var inp = $(newEl).find("input").get(0);
  1232. if(elName)
  1233. $(inp).attr('name', elName);
  1234. if(currVal != undefined)
  1235. $(inp).val(currVal);
  1236. function addBlur() { if(cmd) $(inp).blur(function() { cmd($(inp).val()) }); };
  1237. newEl.setValueFn = function(arg){ $(inp).val(arg) };
  1238. addBlur();
  1239. var myFunc = function(){
  1240. $(inp).unbind("blur");
  1241. $('body').append(
  1242. '<div id="editdlg" style="display:none">'+
  1243. '<textarea id="td_longText" rows="25" cols="60" style="width:99%"/>'+
  1244. '</div>');
  1245. var txt = $(inp).val();
  1246. txt = txt.replace(/\u2424/g, '\n');
  1247. $("#td_longText").val(txt);
  1248. var cm;
  1249. if(typeof AddCodeMirror == 'function') {
  1250. AddCodeMirror($("#td_longText"), function(pcm) {cm = pcm;});
  1251. }
  1252. $('#editdlg').dialog(
  1253. { modal:true, closeOnEscape:true, width:$(window).width()*3/4,
  1254. height:$(window).height()*3/4,
  1255. close:function(){ $('#editdlg').remove(); },
  1256. buttons:[
  1257. { text:"Cancel", click:function(){
  1258. $(this).dialog('close');
  1259. addBlur();
  1260. }},
  1261. { text:"OK", click:function(){
  1262. if(cm)
  1263. $("#td_longText").val(cm.getValue());
  1264. var res=$("#td_longText").val();
  1265. res = res.replace(/\n/g, '\u2424' );
  1266. $(this).dialog('close');
  1267. $(inp).val(res);
  1268. addBlur();
  1269. }}]
  1270. });
  1271. };
  1272. if(is_long)
  1273. $(newEl).click(myFunc);
  1274. return newEl;
  1275. }
  1276. /*************** select **************/
  1277. function
  1278. FW_createSelect(elName, devName, vArr, currVal, set, params, cmd)
  1279. {
  1280. if(vArr.length < 2 || vArr[0] != "select" || (params && params.length))
  1281. return undefined;
  1282. var newEl = document.createElement('select');
  1283. var vHash = {};
  1284. for(var j=1; j < vArr.length; j++) {
  1285. var o = document.createElement('option');
  1286. o.text = o.value = vArr[j].replace(/#/g," ");
  1287. vHash[vArr[j]] = 1;
  1288. newEl.options[j-1] = o;
  1289. }
  1290. if(currVal)
  1291. $(newEl).val(currVal);
  1292. if(elName)
  1293. $(newEl).attr('name', elName);
  1294. if(cmd)
  1295. $(newEl).change(function(arg) { cmd($(newEl).val()) });
  1296. newEl.setValueFn = function(arg) { if(vHash[arg]) $(newEl).val(arg); };
  1297. return newEl;
  1298. }
  1299. /*************** selectNumbers **************/
  1300. // Syntax: selectnumbers,<min value>,<step|step of exponent>,<max value>,<number of digits after decimal point>,lin|log10
  1301. function
  1302. FW_createSelectNumbers(elName, devName, vArr, currVal, set, params, cmd)
  1303. {
  1304. if(vArr.length < 6 || vArr[0] != "selectnumbers" || (params && params.length))
  1305. return undefined;
  1306. var min = parseFloat(vArr[1]);
  1307. var stp = parseFloat(vArr[2]);
  1308. var max = parseFloat(vArr[3]);
  1309. var dp = parseFloat(vArr[4]); // decimal points
  1310. var fun = vArr[5]; // function
  1311. if(currVal != undefined)
  1312. currVal = currVal.replace(/[^\d.\-]/g, "");
  1313. currVal = (currVal==undefined || currVal=="") ? min : parseFloat(currVal);
  1314. if(max==min)
  1315. return undefined;
  1316. if(!(fun == "lin" || fun == "log10"))
  1317. return undefined;
  1318. if(currVal < min)
  1319. currVal = min;
  1320. if(currVal > max)
  1321. currVal = max;
  1322. var newEl = document.createElement('select');
  1323. var vHash = {};
  1324. var k = 0;
  1325. var v = 0;
  1326. if (fun == "lin") {
  1327. for(var j=min; j <= max; j+=stp) {
  1328. var o = document.createElement('option');
  1329. o.text = o.value = j.toFixed(dp);
  1330. vHash[j.toString()] = 1;
  1331. newEl.options[k] = o;
  1332. k++;
  1333. }
  1334. } else if (fun == "log10") {
  1335. if(min <= 0 || max <= 0)
  1336. return undefined;
  1337. for(var j=Math.log10(min); j <= Math.log10(max)+stp; j+=stp) {
  1338. var o = document.createElement('option');
  1339. var w = Math.pow(10, j)
  1340. if (w > max)
  1341. w = max;
  1342. if (v == w.toFixed(dp))
  1343. continue;
  1344. v = w.toFixed(dp);
  1345. o.text = o.value = v;
  1346. vHash[v] = 1;
  1347. newEl.options[k] = o;
  1348. k++;
  1349. }
  1350. }
  1351. if(currVal)
  1352. $(newEl).val(currVal.toFixed(dp));
  1353. if(elName)
  1354. $(newEl).attr('name', elName);
  1355. if(cmd)
  1356. $(newEl).change(function(arg) { cmd($(newEl).val()) });
  1357. newEl.setValueFn = function(arg) { if(vHash[arg]) $(newEl).val(arg); };
  1358. return newEl;
  1359. }
  1360. /*************** noArg **************/
  1361. function
  1362. FW_createNoArg(elName, devName, vArr, currVal, set, params, cmd)
  1363. {
  1364. if(vArr.length != 1 || vArr[0] != "noArg" || (params && params.length))
  1365. return undefined;
  1366. var newEl = $('<div style="display:none">').get(0);
  1367. if(elName)
  1368. $(newEl).append('<input type="hidden" name="'+elName+ '" value="">');
  1369. return(newEl);
  1370. }
  1371. /*************** slider **************/
  1372. function
  1373. FW_createSlider(elName, devName, vArr, currVal, set, params, cmd)
  1374. {
  1375. // min, step, max, float
  1376. if(vArr.length < 4 || vArr.length > 5 || vArr[0] != "slider" ||
  1377. (params && params.length))
  1378. return undefined;
  1379. var min = parseFloat(vArr[1]);
  1380. var stp = parseFloat(vArr[2]);
  1381. var max = parseFloat(vArr[3]);
  1382. var flt = (vArr.length == 5 && vArr[4] == "1");
  1383. var dp = 0; // decimal points for float
  1384. if(flt) {
  1385. var s = ""+stp;
  1386. if(s.indexOf(".") >= 0)
  1387. dp = s.substr(s.indexOf(".")+1).length;
  1388. }
  1389. if(currVal != undefined)
  1390. currVal = currVal.replace(/[^\d.\-]/g, "");
  1391. currVal = (currVal==undefined || currVal=="") ? min : parseFloat(currVal);
  1392. if(max==min)
  1393. return undefined;
  1394. if(currVal < min || currVal > max)
  1395. currVal = min;
  1396. var newEl = $('<div style="display:inline-block" tabindex="0">').get(0);
  1397. var slider = $('<div class="slider" id="slider.'+devName+'">').get(0);
  1398. $(newEl).append(slider);
  1399. var sh = $('<div class="handle">'+currVal+'</div>').get(0);
  1400. $(slider).append(sh);
  1401. if(elName)
  1402. $(newEl).append('<input type="hidden" name="'+elName+
  1403. '" value="'+currVal+'">');
  1404. var lastX=-1, offX=0, maxX=0, val=currVal;
  1405. newEl.activateFn = function() {
  1406. if(currVal < min || currVal > max)
  1407. return;
  1408. if(!slider.offsetWidth)
  1409. return setTimeout(newEl.activateFn, 1);
  1410. maxX = slider.offsetWidth-sh.offsetWidth;
  1411. offX = (currVal-min)*maxX/(max-min);
  1412. var strVal = (flt ? currVal.toFixed(dp) : ""+parseInt(currVal));
  1413. sh.innerHTML = strVal;
  1414. sh.setAttribute('style', 'left:'+offX+'px;');
  1415. if(elName)
  1416. slider.nextSibling.setAttribute('value', strVal);
  1417. }
  1418. $(newEl).keydown(function(e){
  1419. if(e.keyCode == 37) currVal -= stp;
  1420. else if(e.keyCode == 39) currVal += stp;
  1421. else return;
  1422. if(currVal < min) currVal = min;
  1423. if(currVal > max) currVal = max;
  1424. offX = (currVal-min)*maxX/(max-min);
  1425. var strVal = (flt ? currVal.toFixed(dp) : ""+parseInt(currVal));
  1426. sh.innerHTML = strVal;
  1427. sh.setAttribute('style', 'left:'+offX+'px;');
  1428. if(cmd)
  1429. cmd(strVal);
  1430. if(elName)
  1431. slider.nextSibling.setAttribute('value', strVal);
  1432. });
  1433. function
  1434. touchFn(e, fn)
  1435. {
  1436. e.preventDefault(); // Prevents Safari from scrolling!
  1437. if(e.touches == null || e.touches.length == 0)
  1438. return;
  1439. e.clientX = e.touches[0].clientX;
  1440. fn(e);
  1441. }
  1442. function
  1443. mouseDown(e)
  1444. {
  1445. var oldFn1 = document.onmousemove, oldFn2 = document.onmouseup,
  1446. oldFn3 = document.ontouchmove, oldFn4 = document.ontouchend;
  1447. e.stopPropagation(); // Dashboard fix
  1448. lastX = e.clientX; // Does not work on IE8
  1449. function
  1450. mouseMove(e)
  1451. {
  1452. e.stopPropagation(); // Dashboard fix
  1453. if(maxX == 0) // Forum #35846
  1454. maxX = slider.offsetWidth-sh.offsetWidth;
  1455. var diff = e.clientX-lastX; lastX = e.clientX;
  1456. offX += diff;
  1457. if(offX < 0) offX = 0;
  1458. if(offX > maxX) offX = maxX;
  1459. val = offX/maxX * (max-min);
  1460. val = flt ? (Math.floor(val/stp)*stp+min).toFixed(dp) :
  1461. (Math.floor(Math.floor(val/stp)*stp)+min);
  1462. sh.innerHTML = val;
  1463. sh.setAttribute('style', 'left:'+offX+'px;');
  1464. }
  1465. document.onmousemove = mouseMove;
  1466. document.ontouchmove = function(e) { touchFn(e, mouseMove); }
  1467. document.onmouseup = document.ontouchend = function(e)
  1468. {
  1469. e.stopPropagation(); // Dashboard fix
  1470. document.onmousemove = oldFn1; document.onmouseup = oldFn2;
  1471. document.ontouchmove = oldFn3; document.ontouchend = oldFn4;
  1472. if(cmd)
  1473. cmd(val);
  1474. if(elName)
  1475. slider.nextSibling.setAttribute('value', val);
  1476. };
  1477. };
  1478. sh.onselectstart = function() { return false; }
  1479. sh.onmousedown = mouseDown;
  1480. sh.ontouchstart = function(e) { touchFn(e, mouseDown); }
  1481. newEl.setValueFn = function(arg) {
  1482. var res = arg.match(/[\d.\-]+/); // extract first number
  1483. currVal = (res ? parseFloat(res[0]) : min);
  1484. if(currVal < min || currVal > max)
  1485. currVal = min;
  1486. newEl.activateFn();
  1487. };
  1488. return newEl;
  1489. }
  1490. /*************** TIME **************/
  1491. function
  1492. FW_createTime(elName, devName, vArr, currVal, set, params, cmd)
  1493. {
  1494. if(vArr.length != 1 || vArr[0] != "time" || (params && params.length))
  1495. return undefined;
  1496. var open="-", closed="+";
  1497. var newEl = document.createElement('div');
  1498. $(newEl).append('<input type="text" size="5">');
  1499. $(newEl).append('<input type="button" value="'+closed+'">');
  1500. var inp = $(newEl).find("[type=text]");
  1501. var btn = $(newEl).find("[type=button]");
  1502. currVal = (currVal ? currVal : "12:00")
  1503. .replace(/[^\d]*(\d\d):(\d\d).*/g,"$1:$2");
  1504. $(inp).val(currVal)
  1505. if(elName)
  1506. $(inp).attr("name", elName);
  1507. var hh, mm; // the slider elements
  1508. newEl.setValueFn = function(arg) {
  1509. arg = arg.replace(/[^\d]*(\d\d):(\d\d).*/g,"$1:$2");
  1510. $(inp).val(arg);
  1511. var hhmm = arg.split(":");
  1512. if(hhmm.length == 2 && hh && mm) {
  1513. hh.setValueFn(hhmm[0]);
  1514. mm.setValueFn(hhmm[1]);
  1515. }
  1516. };
  1517. $(btn).click(function(){ // Open/Close the slider view
  1518. var v = $(inp).val();
  1519. if($(btn).val() == open) {
  1520. $(btn).val(closed);
  1521. $(newEl).find(".timeSlider").remove();
  1522. hh = mm = undefined;
  1523. if(cmd)
  1524. cmd(v);
  1525. return;
  1526. }
  1527. $(btn).val(open);
  1528. if(v.indexOf(":") < 0) {
  1529. v = "12:00";
  1530. $(inp).val(v);
  1531. }
  1532. var hhmm = v.split(":");
  1533. function
  1534. tSet(idx, arg)
  1535. {
  1536. if((""+arg).length < 2)
  1537. arg = '0'+arg;
  1538. hhmm[idx] = arg;
  1539. $(inp).val(hhmm.join(":"));
  1540. }
  1541. $(newEl).append('<div class="timeSlider">');
  1542. var ts = $(newEl).find(".timeSlider");
  1543. hh = FW_createSlider(undefined, devName+"HH", ["slider", 0, 1, 23],
  1544. hhmm[0], undefined, params, function(arg) { tSet(0, arg) });
  1545. mm = FW_createSlider(undefined, devName+"MM", ["slider", 0, 5, 55],
  1546. hhmm[1], undefined, params, function(arg) { tSet(1, arg) });
  1547. $(ts).append("<br>"); $(ts).append(hh); hh.activateFn();
  1548. $(ts).append("<br>"); $(ts).append(mm); mm.activateFn();
  1549. });
  1550. return newEl;
  1551. }
  1552. /*************** MULTIPLE **************/
  1553. function
  1554. FW_createMultiple(elName, devName, vArr, currVal, set, params, cmd)
  1555. {
  1556. if(vArr.length < 2 || (vArr[0]!="multiple" && vArr[0]!="multiple-strict") ||
  1557. (params && params.length))
  1558. return undefined;
  1559. var newEl = $('<input type="text" size="30" readonly>').get(0);
  1560. if(currVal)
  1561. $(newEl).val(currVal);
  1562. if(elName)
  1563. $(newEl).attr("name", elName);
  1564. newEl.setValueFn = function(arg){ $(newEl).val(arg) };
  1565. for(var i1=1; i1<vArr.length; i1++)
  1566. vArr[i1] = vArr[i1].replace(/#/g, " ");
  1567. $(newEl).focus(function(){
  1568. var sel = $(newEl).val().split(","), selObj={};
  1569. for(var i1=0; i1<sel.length; i1++)
  1570. selObj[sel[i1]] = 1;
  1571. var table = "";
  1572. for(var i1=1; i1<vArr.length; i1++) {
  1573. var v = vArr[i1];
  1574. table += '<tr>'+ // funny stuff for ios6 style, forum #23561
  1575. '<td><div class="checkbox">'+
  1576. '<input name="'+v+'" id="multiple_'+v+'" type="checkbox"'+
  1577. (selObj[v] ? " checked" : "")+'/>'+'</div></td>'+
  1578. '<td><label for="multiple_'+v+'">'+v+'</label></td></tr>';
  1579. delete(selObj[v]);
  1580. }
  1581. var selArr=[];
  1582. for(var i1 in selObj)
  1583. selArr.push(i1);
  1584. var strict = (vArr[0] == "multiple-strict");
  1585. $('body').append(
  1586. '<div id="multidlg" style="display:none">'+
  1587. '<table>'+table+'</table>'+(!strict ? '<input id="md_freeText" '+
  1588. 'value="'+selArr.join(',')+'"/>' : '')+
  1589. '</div>');
  1590. $('#multidlg').dialog(
  1591. { modal:true, closeOnEscape:false, maxHeight:$(window).height()*3/4,
  1592. buttons:[
  1593. { text:"Cancel", click:function(){ $('#multidlg').remove(); }},
  1594. { text:"OK", click:function(){
  1595. var res=[];
  1596. if($("#md_freeText").val())
  1597. res.push($("#md_freeText").val());
  1598. $("#multidlg table input").each(function(){
  1599. if($(this).prop("checked"))
  1600. res.push($(this).attr("name"));
  1601. });
  1602. $('#multidlg').remove();
  1603. $(newEl).val(res.join(","));
  1604. if(cmd)
  1605. cmd(res.join(","));
  1606. }}]});
  1607. });
  1608. return newEl;
  1609. }
  1610. /*************** WIDGETS END **************/
  1611. /*************** SCRIPT LOAD FUNCTIONS START **************/
  1612. function
  1613. loadScript(sname, callback, force)
  1614. {
  1615. var h = document.head || document.getElementsByTagName('head')[0];
  1616. sname = FW_root+"/"+sname;
  1617. if(FW_scripts[sname]) {
  1618. if(FW_scripts[sname].loaded) {
  1619. if(callback)
  1620. callback();
  1621. } else {
  1622. FW_scripts[sname].callbacks.push(callback);
  1623. }
  1624. return;
  1625. }
  1626. if(!FW_docReady && !force) {
  1627. FW_scripts[sname] = { callbacks:[ callback] };
  1628. return;
  1629. }
  1630. var script = document.createElement("script");
  1631. script.src = sname;
  1632. script.async = script.defer = false;
  1633. script.type = "text/javascript";
  1634. FW_scripts[sname] = { callbacks:[ callback] };
  1635. function
  1636. scriptLoaded()
  1637. {
  1638. var p = FW_scripts[sname];
  1639. p.loaded = true;
  1640. if(!p.called) {
  1641. p.called = true;
  1642. for(var i1=0; i1< p.callbacks.length; i1++)
  1643. if(p.callbacks[i1]) // pushing undefined callbacks on the stack is ok
  1644. p.callbacks[i1]();
  1645. }
  1646. delete(p.callbacks);
  1647. }
  1648. log("Loading script "+sname);
  1649. if(FW_isIE) {
  1650. script.onreadystatechange = function() {
  1651. if(script.readyState == 'loaded' || script.readyState == 'complete') {
  1652. script.onreadystatechange = null;
  1653. scriptLoaded();
  1654. }
  1655. }
  1656. } else {
  1657. if(FW_isiOS)
  1658. FW_closeConn();
  1659. script.onload = function(){
  1660. scriptLoaded();
  1661. }
  1662. }
  1663. h.appendChild(script);
  1664. }
  1665. function
  1666. loadLink(lname)
  1667. {
  1668. var h = document.head || document.getElementsByTagName('head')[0];
  1669. lname = FW_root+"/"+lname;
  1670. var arr = h.getElementsByTagName("link");
  1671. for(var i1=0; i1<arr.length; i1++)
  1672. if(lname == arr[i1].getAttribute("href"))
  1673. return;
  1674. var link = document.createElement("link");
  1675. link.href = lname;
  1676. link.rel = "stylesheet";
  1677. log("Loading link "+lname);
  1678. h.appendChild(link);
  1679. }
  1680. function
  1681. scriptAttribute(sname)
  1682. {
  1683. var attr="";
  1684. $("head script").each(function(){
  1685. var src = $(this).attr("src");
  1686. if(src && src.indexOf(sname) >= 0)
  1687. attr = $(this).attr("attr");
  1688. });
  1689. var ua={};
  1690. if(attr && attr != "") {
  1691. try {
  1692. ua=JSON.parse(attr);
  1693. } catch(e){
  1694. FW_errmsg(sname+" Parameter "+e,5000);
  1695. }
  1696. }
  1697. return ua;
  1698. }
  1699. /*************** SCRIPT LOAD FUNCTIONS END **************/
  1700. function
  1701. print_call_stack() {
  1702. var stack = new Error().stack;
  1703. console.log("PRINTING CALL STACK");
  1704. console.log( stack );
  1705. }
  1706. function
  1707. FW_getSVG(emb)
  1708. {
  1709. if(emb.contentDocument)
  1710. return emb.contentDocument;
  1711. if(typeof emb.getSVGDocument == "function") {
  1712. try {
  1713. return emb.getSVGDocument();
  1714. } catch(err) {
  1715. // dom not loaded -> fall through -> retry;
  1716. }
  1717. }
  1718. return undefined;
  1719. }
  1720. /*
  1721. =pod
  1722. =begin html
  1723. <li>noArg - show no input field.</li>
  1724. <li>time - show a JavaScript driven timepicker.<br>
  1725. Example: attr FS20dev widgetOverride on-till:time</li>
  1726. <li>textField - show an input field.<br>
  1727. Example: attr WEB widgetOverride room:textField</li>
  1728. <li>textFieldNL - show the input field and hide the label.</li>
  1729. <li>textField-long - show an input-field, but upon
  1730. clicking on the input field open a textArea (60x25).</li>
  1731. <li>textFieldNL-long - the behaviour is the same
  1732. as :textField-long, but no label is displayed.</li>
  1733. <li>slider,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;[,1] - show
  1734. a JavaScript driven slider. The optional ,1 at the end
  1735. avoids the rounding of floating-point numbers.</li>
  1736. <li>multiple,&lt;val1&gt;,&lt;val2&gt;,..." - present a
  1737. multiple-value-selector with an additional textfield. The result is
  1738. comman separated.</li>
  1739. <li>multiple-strict,&lt;val1&gt;,&lt;val2&gt;,... - like :multiple, but
  1740. without the textfield.</li>
  1741. <li>selectnumbers,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;,&lt;number of
  1742. digits after decimal point&gt;,lin|log10" - display a select widget
  1743. generated with values from min to max with step.<br>
  1744. lin generates a constantly increasing series. log10 generates an
  1745. exponentially increasing series to base 10, step is related to the
  1746. exponent, e.g. 0.0625.</li>
  1747. <li>select,&lt;val1&gt;,&lt;val2&gt;,... - show a dropdown with all values.
  1748. <b>NOTE</b>: this is also the fallback, if no modifier is found.</li>
  1749. =end html
  1750. =begin html_DE
  1751. <li>noArg - es wird kein weiteres Eingabefeld angezeigt.</li>
  1752. <li>time - zeigt ein Zeitauswahlmen&uuml;.
  1753. Beispiel: attr FS20dev widgetOverride on-till:time</li>
  1754. <li>textField - zeigt ein Eingabefeld.<br>
  1755. Beispiel: attr WEB widgetOverride room:textField</li>
  1756. <li>textField-long - ist wie textField, aber beim Click im Eingabefeld wird
  1757. ein Dialog mit einer HTML textarea (60x25) wird ge&ouml;ffnet.</li>
  1758. <li>slider,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;[,1] - zeigt einen
  1759. Schieberegler. Das optionale 1 (isFloat) vermeidet eine Rundung der
  1760. Fliesskommazahlen.</li>
  1761. <li>multiple,&lt;val1&gt;,&lt;val2&gt;,... - zeigt eine Mehrfachauswahl mit
  1762. einem zus&auml;tzlichen Eingabefeld. Das Ergebnis ist Komma
  1763. separiert.</li>
  1764. <li>multiple-strict,&lt;val1&gt;,&lt;val2&gt;,... - ist wie :multiple,
  1765. blo&szlig; ohne Eingabefeld.</li>
  1766. <li>selectnumbers,&lt;min&gt;,&lt;step&gt;,&lt;max&gt;,&lt;number of
  1767. digits after decimal point&gt;,lin|log10" zeigt ein HTML-select mit einer
  1768. Zahlenreihe vom Wert min bis Wert max mit Schritten von step
  1769. angezeigt.<br>
  1770. Die Angabe lin erzeugt eine konstant ansteigende Reihe. Die Angabe
  1771. log10 erzeugt eine exponentiell ansteigende Reihe zur Basis 10,
  1772. step bezieht sich auf den Exponenten, z.B. 0.0625.</li>
  1773. <li>select,&lt;val1&gt;,&lt;val2&gt;,... - zeigt ein HTML select mit allen
  1774. Werten. <b>Achtung</b>: so ein Widget wird auch dann angezeigt, falls
  1775. kein passender Modifier gefunden wurde.</li>
  1776. =end html_DE
  1777. =cut
  1778. */