fhemweb.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. "use strict";
  2. var version="$Id: fhemweb.js 13392 2017-02-11 18:32:13Z rudolfkoenig $";
  3. var FW_serverGenerated;
  4. var FW_serverFirstMsg = (new Date()).getTime()/1000;
  5. var FW_serverLastMsg = FW_serverFirstMsg;
  6. var FW_isIE = (navigator.appVersion.indexOf("MSIE") > 0);
  7. var FW_isiOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/);
  8. var FW_scripts = {}, FW_links = {};
  9. var FW_docReady = false, FW_longpollType;
  10. var FW_root = "/fhem"; // root
  11. var embedLoadRetry = 100;
  12. // createFn returns an HTML Element, which may contain
  13. // - setValueFn, which is called when data via longpoll arrives
  14. // - activateFn, which is called after the HTML element is part of the DOM.
  15. var FW_widgets = {
  16. select: { createFn:FW_createSelect },
  17. slider: { createFn:FW_createSlider },
  18. time: { createFn:FW_createTime },
  19. noArg: { createFn:FW_createNoArg },
  20. multiple: { createFn:FW_createMultiple },
  21. "multiple-strict": { createFn:FW_createMultiple },
  22. textfield: { createFn:FW_createTextField },
  23. "textfield-long": { createFn:FW_createTextField }
  24. };
  25. window.onbeforeunload = function(e)
  26. {
  27. FW_leaving = 1;
  28. return undefined;
  29. }
  30. window.onerror = function(errMsg, url, lineno)
  31. {
  32. url = url.replace(/.*\//,'');
  33. if($("body").attr("data-confirmJSError") != 0)
  34. FW_okDialog(url+" line "+lineno+":<br>"+errMsg);
  35. }
  36. function
  37. FW_replaceWidgets(parent)
  38. {
  39. parent.find("div.fhemWidget").each(function() {
  40. var dev=$(this).attr("dev");
  41. var cmd=$(this).attr("cmd");
  42. var rd=$(this).attr("reading");
  43. var params = cmd.split(" ");
  44. var type=$(this).attr("type");
  45. if( type == undefined ) type = "set";
  46. FW_replaceWidget(this, dev, $(this).attr("arg").split(","),
  47. $(this).attr("current"), rd, params[0], params.slice(1),
  48. function(arg) {
  49. FW_cmd(FW_root+"?cmd="+type+" "+dev+
  50. (params[0]=="state" ? "":" "+params[0])+" "+arg+"&XHR=1");
  51. });
  52. });
  53. }
  54. function
  55. FW_jqueryReadyFn()
  56. {
  57. if(FW_docReady) // loading fhemweb.js twice is hard to debug
  58. return;
  59. log(version);
  60. FW_docReady = true;
  61. FW_serverGenerated = $("body").attr("generated");
  62. FW_longpollType = $("body").attr("longpoll");
  63. if(FW_longpollType != "0")
  64. setTimeout("FW_longpoll()", 100);
  65. $("a").each(function() { FW_replaceLink(this); })
  66. $("head script").each(function() {
  67. var sname = $(this).attr("src"),
  68. p = FW_scripts[sname];
  69. if(!p) {
  70. FW_scripts[sname] = { loaded:true };
  71. return;
  72. }
  73. FW_scripts[sname].loaded = true;
  74. if(p.callbacks && !p.called) {
  75. p.called = true; // Avoid endless loop
  76. for(var i1=0; i1< p.callbacks.length; i1++)
  77. if(p.callbacks[i1]) // pushing undefined callbacks on the stack is ok
  78. p.callbacks[i1]();
  79. delete(p.callbacks);
  80. }
  81. });
  82. $("head link").each(function() { FW_links[$(this).attr("href")] = 1 });
  83. $("div.makeSelect select").each(function() {
  84. FW_detailSelect(this);
  85. $(this).change(FW_detailSelect);
  86. });
  87. // Activate the widgets
  88. var r = $("head").attr("root");
  89. if(r)
  90. FW_root = r;
  91. FW_replaceWidgets($("html"));
  92. FW_confirmDelete();
  93. // Fix the td count by setting colspan on the last column
  94. $("table.block.wide").each(function(){ // table
  95. var id = $(this).attr("id");
  96. if(!id || id.indexOf("TYPE") != 0)
  97. return;
  98. var maxTd=0, tdCount=[];
  99. $(this).find("tr").each(function(){ // count the td's
  100. var cnt=0;
  101. $(this).find("td").each(function(){ cnt++; });
  102. if(maxTd < cnt) maxTd = cnt;
  103. tdCount.push(cnt);
  104. });
  105. $(this).find("tr").each(function(){ // set the colspan
  106. $(this).find("td").last().attr("colspan", maxTd-tdCount.shift()+1);
  107. });
  108. });
  109. // Replace the FORM-POST in detail-view by XHR
  110. /* Inactive, as Internals and Attributes arent auto updated.
  111. $("form input[type=submit]").click(function(e) {
  112. var cmd = "";
  113. $(this).parent().find("[name]").each(function() {
  114. cmd += (cmd?"&":"")+$(this).attr("name")+"="+$(this).val();
  115. });
  116. if(cmd.indexOf("detail=") < 0)
  117. return;
  118. e.preventDefault();
  119. FW_cmd(FW_root+"?"+cmd+"&XHR=1");
  120. });
  121. */
  122. $("form input.get[type=submit]").click(function(e) { //"get" via XHR to dialog
  123. e.preventDefault();
  124. var cmd = "", el=this;
  125. $(el).parent().find("input,[name]").each(function() {
  126. cmd += (cmd?"&":"")+encodeURIComponent($(this).attr("name"))+
  127. "="+encodeURIComponent($(this).val());
  128. });
  129. FW_cmd(FW_root+"?"+cmd+"&XHR=1&addLinks=1", function(data) {
  130. if(!data.match(/^[\r\n]*$/)) // ignore empty answers
  131. FW_okDialog('<pre>'+data+'</pre>', el);
  132. });
  133. });
  134. $("#saveCheck")
  135. .css("cursor", "pointer")
  136. .click(function(){
  137. var parent = this;
  138. FW_cmd(FW_root+"?cmd=save ?&XHR=1", function(data) {
  139. FW_okDialog('<pre>'+data+'</pre>',parent);
  140. });
  141. });
  142. $("form").each(function(){ // shutdown handling
  143. var input = $(this).find("input.maininput");
  144. if(!input.length)
  145. return;
  146. $(this).on("submit", function(e) {
  147. var val = $(input).val();
  148. if(val.match(/^\s*shutdown/)) {
  149. FW_cmd(FW_root+"?XHR=1&cmd="+val);
  150. $(input).val("");
  151. return false;
  152. } else if(val.match(/^\s*get\s+/)) {
  153. // make get use xhr instead of reload
  154. //return true;
  155. FW_cmd(FW_root+"?cmd="+encodeURIComponent(val)+"&XHR=1", function(data){
  156. if( !data.match( /^<html>.*<\/html>/ ) ) {
  157. data = data.replace( '<', '&lt;' );
  158. data = '<pre>'+data+'</pre>';
  159. }
  160. if( location.href.indexOf('?') === -1 )
  161. $('#content').html(data);
  162. else
  163. FW_okDialog(data);
  164. });
  165. e.preventDefault();
  166. $(input).val("");
  167. return false;
  168. }
  169. return true;
  170. });
  171. });
  172. $("div.devSpecHelp a").each(function(){ // Help on detail window
  173. var dev = FW_getLink(this).split("#").pop();
  174. $(this).unbind("click");
  175. $(this).attr("href", "#"); // Desktop: show underlined Text
  176. $(this).removeAttr("onclick");
  177. $(this).click(function(evt){
  178. if($("#devSpecHelp").length) {
  179. $("#devSpecHelp").remove();
  180. return;
  181. }
  182. $("#content").append('<div id="devSpecHelp"></div>');
  183. FW_cmd(FW_root+"?cmd=help "+dev+"&XHR=1", function(data) {
  184. $("#devSpecHelp").html(data);
  185. var off = $("#devSpecHelp").position().top-20;
  186. $('body, html').animate({scrollTop:off}, 500);
  187. });
  188. });
  189. });
  190. $("table.attributes tr div.dname") // Click on attribute fills input value
  191. .each(function(){
  192. $(this)
  193. .html('<a>'+$(this).html()+'</a>')
  194. .css({cursor:"pointer"})
  195. .click(function(){
  196. var aname = "#sel_attr"+$(this).attr("data-name").replace(/\./g,'_');
  197. $(aname).val($(this).text());
  198. FW_detailSelect(aname);
  199. });
  200. });
  201. $("[name=icon-filter]").on("change keyup paste", function() {
  202. clearTimeout($.data(this, 'delayTimer'));
  203. var wait = setTimeout(FW_filterIcons, 300);
  204. $(this).data('delayTimer', wait);
  205. });
  206. FW_smallScreenCommands();
  207. FW_inlineModify();
  208. FW_rawDef();
  209. }
  210. function
  211. FW_filterIcons()
  212. {
  213. var icons = $('.dist[title]');
  214. icons.show();
  215. var filterText = $('[name=icon-filter]').val();
  216. if (filterText != '') {
  217. var re = RegExp(filterText,"i");
  218. icons.filter(function() {
  219. return !re.test(this.title);
  220. }).hide();
  221. }
  222. }
  223. function
  224. FW_confirmDelete()
  225. {
  226. var b = $("body");
  227. var cd = $(b).attr("data-confirmDelete");
  228. if(!cd || cd == 0)
  229. return;
  230. var wn = $(b).attr("data-webName");
  231. $("div#content").find("a").each(function(){
  232. var href = $(this).attr("href");
  233. if(!href)
  234. return;
  235. var ma = $(this).attr("href").match(/.*cmd[^=]*=(delete[^&]*).*$/);
  236. if(!ma || ma.length != 2)
  237. return;
  238. $(this).attr("href", "#");
  239. $(this).unbind("click");
  240. $(this).click(function(e){
  241. e.preventDefault();
  242. var div = $("<div id='FW_okDialog'>");
  243. $(div).html("Do you really want to "+ma[1]+"?<br><br>"+
  244. "<input type='checkbox' name='noconf'> Skip this dialog in the future");
  245. $("body").append(div);
  246. function
  247. doClose()
  248. {
  249. if($(div).find("input:checked").length)
  250. FW_cmd(FW_root+"?cmd=attr "+wn+" confirmDelete 0&XHR=1");
  251. $(this).dialog("close"); $(div).remove();
  252. }
  253. $(div).dialog({
  254. dialogClass:"no-close", modal:true, width:"auto", closeOnEscape:true,
  255. maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
  256. buttons: [
  257. {text:"Yes", click:function(){ location.href = ma[0]; doClose(); }},
  258. {text:"No", click:function(){ doClose(); }}]
  259. });
  260. });
  261. });
  262. }
  263. // Show the webCmd list in a dialog if: smallScreen & hiddenroom=detail & room
  264. function
  265. FW_smallScreenCommands()
  266. {
  267. if($("div#menu select").length == 0 || // not SmallScreen
  268. $("div#content").attr("room") == undefined || // not room Overview
  269. $("div#content div.col1 a").length > 0) // no hiddenroom=detail
  270. return;
  271. $("div#content div.col1").each(function(){
  272. var tr = $(this).closest("tr");
  273. if($(tr).find("> td").length <= 2)
  274. return;
  275. $(this).html("<a href='#'>"+$(this).html()+"</a>");
  276. $(this).find("a").click(function(){
  277. var t = $("<table></table>"), row=0;
  278. $(tr).find("> td").each(function(){
  279. $(t).append("<tr></tr>");
  280. if(row++ == 0) {
  281. $(t).find("tr:last").append($(this).find("a").html());
  282. } else {
  283. $(this).attr("data-orig", 1);
  284. this.orig=$(this).parent();
  285. $(t).find("tr:last").append($(this).detach());
  286. }
  287. });
  288. FW_okDialog(t, this, function(){
  289. $("#FW_okDialog [data-orig]").each(function(){
  290. $(this).detach().appendTo(this.orig);
  291. });
  292. });
  293. });
  294. });
  295. }
  296. if(window.jQuery) {
  297. $(document).ready(FW_jqueryReadyFn);
  298. } else {
  299. // FLOORPLAN compatibility
  300. loadScript("pgm2/jquery.min.js", function() {
  301. loadScript("pgm2/jquery-ui.min.js", function() {
  302. FW_jqueryReadyFn();
  303. }, true);
  304. }, true);
  305. }
  306. // FLOORPLAN compatibility
  307. function
  308. FW_delayedStart()
  309. {
  310. setTimeout("FW_longpoll()", 100);
  311. }
  312. function
  313. log(txt)
  314. {
  315. var d = new Date();
  316. var ms = ("000"+(d.getMilliseconds()%1000));
  317. ms = ms.substr(ms.length-3,3);
  318. txt = d.toTimeString().substring(0,8)+"."+ms+" "+txt;
  319. if(typeof window.console != "undefined")
  320. console.log(txt);
  321. }
  322. function
  323. addcsrf(arg)
  324. {
  325. var csrf = $("body").attr('fwcsrf');
  326. if(csrf && arg.indexOf('fwcsrf') < 0)
  327. arg += '&fwcsrf='+csrf;
  328. return arg;
  329. }
  330. function
  331. FW_cmd(arg, callback)
  332. {
  333. log("FW_cmd:"+arg);
  334. arg = addcsrf(arg);
  335. arg += '&fw_id='+$("body").attr('fw_id');
  336. var req = new XMLHttpRequest();
  337. req.open("POST", arg, true);
  338. req.send(null);
  339. req.onreadystatechange = function(){
  340. if(req.readyState == 4) {
  341. if(callback)
  342. callback(req.responseText);
  343. else if(req.responseText)
  344. FW_errmsg(req.responseText, 5000);
  345. }
  346. }
  347. }
  348. function
  349. FW_errmsg(txt, timeout)
  350. {
  351. log("ERRMSG:"+txt+"<");
  352. var errmsg = document.getElementById("errmsg");
  353. if(!errmsg) {
  354. if(txt == "")
  355. return;
  356. errmsg = document.createElement('div');
  357. errmsg.setAttribute("id","errmsg");
  358. document.body.appendChild(errmsg);
  359. }
  360. if(txt == "") {
  361. document.body.removeChild(errmsg);
  362. return;
  363. }
  364. errmsg.innerHTML = txt;
  365. if(timeout)
  366. setTimeout("FW_errmsg('')", timeout);
  367. }
  368. function
  369. FW_okDialog(txt, parent, removeFn)
  370. {
  371. var div = $("<div id='FW_okDialog'>");
  372. $(div).html(txt);
  373. $("body").append(div);
  374. var oldPos = $("body").scrollTop();
  375. $(div).dialog({
  376. dialogClass:"no-close", modal:true, width:"auto", closeOnEscape:true,
  377. maxWidth:$(window).width()*0.9, maxHeight:$(window).height()*0.9,
  378. buttons: [{text:"OK", click:function(){
  379. $(this).dialog("close");
  380. if(removeFn)
  381. removeFn();
  382. $(div).remove();
  383. }}]
  384. });
  385. FW_replaceWidgets(div);
  386. $(div).find("a").each(function(){FW_replaceLink(this);}); //Forum #33766
  387. if(parent)
  388. $(div).dialog( "option", "position", {
  389. my: "left top", at: "right bottom",
  390. of: parent, collision: "flipfit"
  391. });
  392. setTimeout(function(){$("body").scrollTop(oldPos);}, 1); // Not ideal.
  393. }
  394. function
  395. FW_menu(evt, el, arr, dis, fn, embedEl)
  396. {
  397. if(!embedEl)
  398. evt.stopPropagation();
  399. if($("#fwmenu").length) {
  400. delfwmenu();
  401. return;
  402. }
  403. var html = '<ul id="fwmenu">';
  404. for(var i=0; i<arr.length; i++) {
  405. html+='<li class="'+ ((dis && dis[i]) ? 'ui-state-disabled' : '')+'">'+
  406. '<a row="'+i+'" href="#">'+arr[i]+'</a></li>';
  407. }
  408. html += '</ul>';
  409. $("body").append(html);
  410. function
  411. delfwmenu()
  412. {
  413. $("ul#fwmenu").remove();
  414. $('html').unbind('click.fwmenu');
  415. }
  416. var wt = $(window).scrollTop();
  417. $("#fwmenu")
  418. .menu({
  419. select: function(e,ui) { // changes the scrollTop();
  420. e.stopPropagation();
  421. fn($(e.currentTarget).find("[row]").attr("row"));
  422. delfwmenu();
  423. setTimeout(function(){ $(window).scrollTop(wt) }, 1); // Bug in select?
  424. }
  425. });
  426. var off = $(el).offset();
  427. if(embedEl) {
  428. var embOff = $(embedEl).offset();
  429. off.top += embOff.top;
  430. off.left += embOff.left;
  431. }
  432. var dH = $("#fwmenu").height(), dW = $("#fwmenu").width(),
  433. wH = $(window).height(), wW = $(window).width();
  434. var ey = off.top+dH+20, ex = off.left+dW;
  435. if(ex>wW && ey>wH) { off.top -= dH; off.left -= (dW+16);
  436. } else if(ey > wH) { off.top -= dH; off.left += 20;
  437. } else if(ex > wW) { off.left -= (dW+16);
  438. } else { off.top += 20;
  439. }
  440. $("#fwmenu").css(off);
  441. $('html').bind('click.fwmenu', function() { delfwmenu(); });
  442. }
  443. function
  444. FW_getLink(el)
  445. {
  446. var attr = $(el).attr("href");
  447. if(!attr) {
  448. attr = $(el).attr("onclick"); // Tablet/smallScreen version
  449. if(!attr)
  450. return "";
  451. attr = attr.replace(/^location.href='/,'');
  452. attr = attr.replace(/'$/,'');
  453. }
  454. return attr;
  455. }
  456. function
  457. FW_replaceLink(el)
  458. {
  459. var attr = FW_getLink(el);
  460. if(!attr)
  461. return;
  462. var ma = attr.match(/^(.*\?)(cmd[^=]*=.*)$/);
  463. if(ma == null || ma.length == 0 || !ma[2].match(/=(save|set)/)) {
  464. ma = attr.match(new RegExp("^"+FW_root)); // Avoid "Connection lost" @iOS
  465. if(ma) {
  466. $(el).click(function(e) {
  467. // Open link in window/tab, Forum #39154
  468. if(e.shiftKey || e.ctrlKey || e.metaKey || e.button == 1)
  469. return;
  470. e.preventDefault();
  471. FW_leaving = 1;
  472. if($(el).attr("target") == "_blank") {
  473. window.open(attr, '_blank').focus();
  474. } else {
  475. location.href = attr;
  476. }
  477. });
  478. }
  479. return;
  480. }
  481. $(el).removeAttr("href");
  482. $(el).removeAttr("onclick");
  483. $(el).click(function() {
  484. FW_cmd(attr+"&XHR=1", function(txt){
  485. if(!txt)
  486. return;
  487. if(ma[2].match(/=set/)) // Forum #38875
  488. FW_okDialog('<pre>'+txt+'<pre>', el);
  489. else
  490. FW_errmsg(txt, 5000);
  491. });
  492. });
  493. $(el).css("cursor", "pointer");
  494. }
  495. function
  496. FW_inlineModify() // Do not generate a new HTML page upon pressing modify
  497. {
  498. var cm;
  499. if( typeof AddCodeMirror == 'function' ) {
  500. // init codemirror for FW_style edit textarea
  501. var s = $('textarea[name="data"]');
  502. if( s.length && !s[0].editor ) {
  503. s[0].editor = true; AddCodeMirror( s[0] );
  504. }
  505. }
  506. $('#DEFa').click(function(){
  507. var old = $('#edit').css('display');
  508. $('#edit').css('display', old=='none' ? 'block' : 'none');
  509. $('#disp').css('display', old=='none' ? 'none' : 'block');
  510. if( typeof AddCodeMirror == 'function' ) {
  511. var s=document.getElementById("edit").getElementsByTagName("textarea");
  512. if(!s[0].editor) {
  513. s[0].editor=true; AddCodeMirror(s[0], function(pcm) {cm = pcm;});
  514. }
  515. }
  516. });
  517. $("div input.psc[type=submit]:not(.get)").click(function(e){
  518. e.preventDefault();
  519. var newDef = typeof cm !== 'undefined' ?
  520. cm.getValue() : $(this).closest("form").find("textarea").val();
  521. var cmd = $(this).attr("name")+"="+$(this).attr("value")+" "+newDef;
  522. var isDef = true;
  523. if( newDef == undefined ) {
  524. isDef = false;
  525. var div = $(this).closest("div.makeSelect");
  526. var devName = $(div).attr("dev"),
  527. cmd = $(div).attr("cmd");
  528. var sel = $(this).closest("form").find("select");
  529. var arg = $(sel).val();
  530. var ifid = devName.replace(/\./g, '\\.');
  531. if($(".dval[informid="+ifid+"-"+arg+"]").length == 0) {
  532. console.log(this);
  533. $(this).unbind('click').click();// No element found to replace, reload
  534. return;
  535. }
  536. newDef = $(this).closest("form").find("input:text").val();
  537. if(newDef == undefined)
  538. newDef = $(this).closest("form").find("select:last").val();
  539. cmd = $(this).attr("name")+"="+cmd+" "+devName+" "+arg+" "+newDef;
  540. }
  541. FW_cmd(FW_root+"?"+encodeURIComponent(cmd)+"&XHR=1", function(resp){
  542. if(resp)
  543. return FW_okDialog(resp);
  544. newDef = newDef.replace(/&/g, '&amp;') // Same as in 01_FHEMWEB
  545. .replace(/</g, '&lt;')
  546. .replace(/>/g, '&gt;');
  547. if(isDef) {
  548. if(newDef.indexOf("\n") >= 0)
  549. newDef = '<pre>'+newDef+'</pre>';
  550. $("div#disp").html(newDef).css("display", "");
  551. $("div#edit").css("display", "none");
  552. }
  553. });
  554. });
  555. }
  556. function
  557. FW_rawDef()
  558. {
  559. $("div.rawDef a").each(function(){ // Help on detail window
  560. var dev = FW_getLink(this).split(" ").pop().split("&")[0];
  561. $(this).unbind("click");
  562. $(this).attr("href", "#"); // Desktop: show underlined Text
  563. $(this).removeAttr("onclick");
  564. $(this).click(function(evt){
  565. if($("#rawDef").length) {
  566. $("#rawDef").remove();
  567. return;
  568. }
  569. $("#content").append('<div id="rawDef">'+
  570. '<textarea id="td_longText" rows="25" cols="60" style="width:99%"/>'+
  571. '<button>Execute commands</button>'+
  572. ' Dump "Probably associated with" too <input type="checkbox">'+
  573. '</div></br>');
  574. function
  575. fillData(opt)
  576. {
  577. FW_cmd(FW_root+"?cmd=list "+opt+" "+dev+"&XHR=1", function(data) {
  578. var re = new RegExp("^define", "gm");
  579. data = data.replace(re, "defmod");
  580. $("#rawDef textarea").val(data);
  581. var off = $("#rawDef").position().top-20;
  582. $('body, html').animate({scrollTop:off}, 500);
  583. $("#rawDef button").hide();
  584. $('#rawDef textarea').bind('input propertychange', function() {
  585. var nData = $("#rawDef textarea").val();
  586. if(nData != data)
  587. $("#rawDef button").show();
  588. else
  589. $("#rawDef button").hide();
  590. });
  591. });
  592. }
  593. fillData("-r");
  594. $("#rawDef input").click(function(){fillData(this.checked ?"-R":"-r")});
  595. $("#rawDef button").click(function(){
  596. var data = $("#rawDef textarea").val();
  597. var arr = data.split("\n"), str="", i1=-1;
  598. function
  599. doNext()
  600. {
  601. if(++i1 >= arr.length) {
  602. return FW_okDialog("Executed everything, no errors found.");
  603. }
  604. str += arr[i1];
  605. if(arr[i1].charAt(arr[i1].length-1) === "\\") {
  606. str += "\n";
  607. return doNext();
  608. }
  609. if(str != "") {
  610. str = str.replace(/\\\n/g, "\n")
  611. .replace(/;;/g, ";");
  612. FW_cmd(FW_root+"?cmd."+dev+"="+encodeURIComponent(str)+"&XHR=1",
  613. function(r){
  614. if(r)
  615. return FW_okDialog('<pre>'+r+'</pre>');
  616. str = "";
  617. doNext();
  618. });
  619. } else {
  620. doNext();
  621. }
  622. }
  623. doNext();
  624. });
  625. });
  626. });
  627. }
  628. /*************** LONGPOLL START **************/
  629. var FW_pollConn;
  630. var FW_longpollOffset = 0;
  631. var FW_leaving;
  632. var FW_lastDataTime=0;
  633. function
  634. FW_doUpdate(evt)
  635. {
  636. var errstr = "Connection lost, trying a reconnect every 5 seconds.";
  637. var input="";
  638. var retryTime = 5000;
  639. var now = new Date()/1000;
  640. // iOS closes HTTP after 60s idle, websocket after 240s idle
  641. if(now-FW_lastDataTime > 59) {
  642. errstr="";
  643. retryTime = 100;
  644. }
  645. FW_lastDataTime = now;
  646. // Websocket starts with Android 4.4, and IE10
  647. if(typeof WebSocket == "function" && evt && evt.target instanceof WebSocket) {
  648. if(evt.type == 'close' && !FW_leaving) {
  649. FW_errmsg(errstr, retryTime-100);
  650. FW_pollConn.close();
  651. FW_pollConn = undefined;
  652. setTimeout(FW_longpoll, retryTime);
  653. return;
  654. }
  655. input = evt.data;
  656. FW_longpollOffset = 0;
  657. } else {
  658. if(FW_pollConn.readyState == 4 && !FW_leaving) {
  659. if(FW_pollConn.status == "401") {
  660. location.reload();
  661. return;
  662. }
  663. FW_errmsg(errstr, retryTime-100);
  664. setTimeout(FW_longpoll, retryTime);
  665. return;
  666. }
  667. if(FW_pollConn.readyState != 3)
  668. return;
  669. input = FW_pollConn.responseText;
  670. }
  671. var devs = new Array();
  672. if(!input || input.length <= FW_longpollOffset)
  673. return;
  674. FW_serverLastMsg = (new Date()).getTime()/1000;
  675. for(;;) {
  676. var nOff = input.indexOf("\n", FW_longpollOffset);
  677. if(nOff < 0)
  678. break;
  679. var l = input.substr(FW_longpollOffset, nOff-FW_longpollOffset);
  680. FW_longpollOffset = nOff+1;
  681. log("Rcvd: "+(l.length>132 ? l.substring(0,132)+"...("+l.length+")":l));
  682. if(!l.length)
  683. continue;
  684. if(l.indexOf("<")== 0) { // HTML returned by proxy, if FHEM behind is dead
  685. FW_closeConn();
  686. FW_errmsg(errstr, retryTime-100);
  687. setTimeout(FW_longpoll, retryTime);
  688. return;
  689. }
  690. var d = JSON.parse(l);
  691. if(d.length != 3)
  692. continue;
  693. if( d[0].match(/^#FHEMWEB:/) ) {
  694. eval(d[1]);
  695. } else {
  696. $("[informId='"+d[0]+"']").each(function(){
  697. if(this.setValueFn) { // change the select/etc value
  698. this.setValueFn(d[1].replace(/\n/g, '\u2424'));
  699. } else {
  700. if(d[2].match(/\n/))
  701. d[2] = '<html><pre>'+d[2]+'</pre></html>';
  702. var ma = /^<html>([\s\S]*)<\/html>$/.exec(d[2]);
  703. if(!d[0].match("-")) // not a reading
  704. $(this).html(d[2]);
  705. else if(ma)
  706. $(this).html(ma[1]);
  707. else
  708. $(this).text(d[2]);
  709. if(d[0].match(/-ts$/)) // timestamps
  710. $(this).addClass('changed');
  711. $(this).find("a").each(function() { FW_replaceLink(this) });
  712. }
  713. });
  714. }
  715. for(var w in FW_widgets)
  716. if(FW_widgets[w].updateLine) // updateLine is deprecated, use setValueFn
  717. FW_widgets[w].updateLine(d);
  718. devs.push(d);
  719. }
  720. for(var w in FW_widgets)
  721. if(FW_widgets[w].updateDevs) // used for SVG to avoid double-reloads
  722. FW_widgets[w].updateDevs(devs);
  723. // reset the connection to avoid memory problems
  724. if(FW_longpollOffset > 1024*1024 && FW_longpollOffset==input.length)
  725. FW_longpoll();
  726. }
  727. function
  728. FW_closeConn()
  729. {
  730. FW_leaving = 1;
  731. if(!FW_pollConn)
  732. return;
  733. if(typeof FW_pollConn.close == "function")
  734. FW_pollConn.close();
  735. else if(typeof FW_pollConn.abort == "function")
  736. FW_pollConn.abort();
  737. FW_pollConn = undefined;
  738. }
  739. function
  740. FW_longpoll()
  741. {
  742. FW_closeConn();
  743. FW_leaving = 0;
  744. FW_longpollOffset = 0;
  745. // Build the notify filter for the backend
  746. var filter = $("body").attr("longpollfilter");
  747. if(filter == null)
  748. filter = "";
  749. var retry;
  750. if(filter == "") {
  751. $("embed").each(function() {
  752. if(FW_getSVG(this) == undefined && !retry &&
  753. filter != ".*" && --embedLoadRetry > 0) {
  754. retry = 1;
  755. setTimeout(FW_longpoll, 100);
  756. return;
  757. }
  758. if($(FW_getSVG(this)).find("svg[flog]").attr("flog"))
  759. filter=".*";
  760. });
  761. if(retry)
  762. return;
  763. }
  764. if(filter == "") {
  765. var sa = location.search.substring(1).split("&");
  766. for(var i = 0; i < sa.length; i++) {
  767. if(sa[i].substring(0,5) == "room=")
  768. filter=sa[i];
  769. if(sa[i].substring(0,7) == "detail=")
  770. filter=sa[i].substring(7);
  771. }
  772. }
  773. if($("#floorplan").length>0) //floorplan special
  774. filter += ";iconPath="+$("body").attr("name");
  775. if(filter == "") {
  776. var content = document.getElementById("content");
  777. if(content) {
  778. var room = content.getAttribute("room");
  779. if(room)
  780. filter="room="+room;
  781. }
  782. }
  783. var iP = $("body").attr("iconPath");
  784. if(iP != null)
  785. filter = filter +";iconPath="+iP;
  786. var since = "null";
  787. if(FW_serverGenerated)
  788. since = FW_serverLastMsg + (FW_serverGenerated-FW_serverFirstMsg);
  789. var query = "?XHR=1"+
  790. "&inform=type=status;filter="+filter+";since="+since+";fmt=JSON"+
  791. '&fw_id='+$("body").attr('fw_id')+
  792. "&timestamp="+new Date().getTime();
  793. var loc = (""+location).replace(/\?.*/,"");
  794. if(typeof WebSocket == "function" && FW_longpollType == "websocket") {
  795. FW_pollConn = new WebSocket((loc+query).replace(/^http/i, "ws"));
  796. FW_pollConn.onclose =
  797. FW_pollConn.onerror =
  798. FW_pollConn.onmessage = FW_doUpdate;
  799. } else {
  800. FW_pollConn = new XMLHttpRequest();
  801. FW_pollConn.open("GET", location.pathname+query, true);
  802. if(FW_pollConn.overrideMimeType) // Win 8.1, #66004
  803. FW_pollConn.overrideMimeType("application/json");
  804. FW_pollConn.onreadystatechange = FW_doUpdate;
  805. FW_pollConn.send(null);
  806. }
  807. log("Inform-channel opened ("+(FW_longpollType==1 ? "HTTP":FW_longpollType)+
  808. ") with filter "+filter);
  809. }
  810. /*************** LONGPOLL END **************/
  811. /*************** WIDGETS START **************/
  812. /*************** "Double" select in detail window ****/
  813. function
  814. FW_detailSelect(selEl)
  815. {
  816. if(selEl.target)
  817. selEl = selEl.target;
  818. var selVal = $(selEl).val();
  819. var div = $(selEl).closest("div.makeSelect");
  820. var arg,
  821. listArr = $(div).attr("list").split(" "),
  822. devName = $(div).attr("dev"),
  823. cmd = $(div).attr("cmd");
  824. for(var i1=0; i1<listArr.length; i1++) {
  825. arg = listArr[i1];
  826. if(arg.indexOf(selVal) == 0 &&
  827. (arg.length == selVal.length || arg[selVal.length] == ':'))
  828. break;
  829. }
  830. if(!arg)
  831. return;
  832. var vArr = [];
  833. if(arg.length > selVal.length)
  834. vArr = arg.substr(selVal.length+1).split(",");
  835. var newEl = FW_replaceWidget($(selEl).next(), devName, vArr,undefined,selVal);
  836. if(cmd == "attr")
  837. FW_queryValue('{AttrVal("'+devName+'","'+selVal+'","")}', newEl);
  838. if(cmd == "set")
  839. FW_queryValue('{ReadingsVal("'+devName+'","'+selVal+'","")}', newEl);
  840. }
  841. function
  842. FW_replaceWidget(oldEl, devName, vArr, currVal, reading, set, params, cmd)
  843. {
  844. var newEl, wn;
  845. var elName = $(oldEl).attr("name");
  846. if(!elName)
  847. elName = $(oldEl).find("[name]").attr("name");
  848. if(vArr.length == 0) { // No parameters, input field
  849. newEl = FW_createTextField(elName, devName, ["textField"], currVal,
  850. set, params, cmd);
  851. wn = "textField";
  852. } else {
  853. for(wn in FW_widgets) {
  854. if(FW_widgets[wn].createFn) {
  855. newEl = FW_widgets[wn].createFn(elName, devName, vArr, currVal,
  856. set, params, cmd);
  857. if(newEl)
  858. break;
  859. }
  860. }
  861. if(!newEl) { // Select as fallback
  862. vArr.unshift("select");
  863. newEl = FW_createSelect(elName, devName, vArr, currVal, set, params, cmd);
  864. wn = "select";
  865. }
  866. }
  867. if(!newEl) { // Simple link
  868. newEl = $('<div class="col3"><a style="cursor: pointer;">'+
  869. set+' '+params.join(' ')+ '</a></div>');
  870. $(newEl).click(function(arg) { cmd(params[0]) });
  871. $(oldEl).replaceWith(newEl);
  872. return newEl;
  873. }
  874. $(newEl).addClass(wn+"_widget");
  875. if( $(newEl).find("[informId]").length == 0 && !$(newEl).attr("informId") ) {
  876. if(reading)
  877. $(newEl).attr("informId", devName+"-"+reading);
  878. }
  879. $(oldEl).replaceWith(newEl);
  880. if(newEl.activateFn) // CSS is not applied if newEl is not in the document
  881. newEl.activateFn();
  882. return newEl;
  883. }
  884. function
  885. FW_queryValue(cmd, el)
  886. {
  887. log("FW_queryValue:"+cmd);
  888. var query = location.pathname+"?cmd="+cmd+"&XHR=1";
  889. query = addcsrf(query);
  890. var qConn = new XMLHttpRequest();
  891. qConn.onreadystatechange = function() {
  892. if(qConn.readyState != 4)
  893. return;
  894. var qResp = qConn.responseText.replace(/\n$/, '');
  895. qResp = qResp.replace(/\n/g, '\u2424');
  896. if(el.setValueFn)
  897. el.setValueFn(qResp);
  898. qConn.abort();
  899. }
  900. qConn.open("GET", query, true);
  901. qConn.send(null);
  902. }
  903. /*************** TEXTFIELD **************/
  904. function
  905. FW_createTextField(elName, devName, vArr, currVal, set, params, cmd)
  906. {
  907. if(vArr.length != 1 ||
  908. (vArr[0] != "textField" &&
  909. vArr[0] != "textFieldNL" &&
  910. vArr[0] != "textField-long" &&
  911. vArr[0] != "textFieldNL-long") ||
  912. (params && params.length))
  913. return undefined;
  914. var is_long = (vArr[0].indexOf("long") > 0);
  915. var newEl = $("<div style='display:inline-block'>").get(0);
  916. if(set && set != "state" && vArr[0].indexOf("NL") < 0)
  917. $(newEl).append(set+":");
  918. $(newEl).append('<input type="text" size="30">');
  919. var inp = $(newEl).find("input").get(0);
  920. if(elName)
  921. $(inp).attr('name', elName);
  922. if(currVal != undefined)
  923. $(inp).val(currVal);
  924. function addBlur() { if(cmd) $(inp).blur(function() { cmd($(inp).val()) }); };
  925. newEl.setValueFn = function(arg){ $(inp).val(arg) };
  926. addBlur();
  927. var myFunc = function(){
  928. $(inp).unbind("blur");
  929. $('body').append(
  930. '<div id="editdlg" style="display:none">'+
  931. '<textarea id="td_longText" rows="25" cols="60" style="width:99%"/>'+
  932. '</div>');
  933. var txt = $(inp).val();
  934. txt = txt.replace(/\u2424/g, '\n');
  935. $("#td_longText").val(txt);
  936. var cm;
  937. if(typeof AddCodeMirror == 'function' && !$("#td_longText").get(0).editor) {
  938. $("#td_longText").get(0).editor = true;
  939. AddCodeMirror($("#td_longText").get(0), function(pcm) {cm = pcm;});
  940. }
  941. $('#editdlg').dialog(
  942. { modal:true, closeOnEscape:true, width:$(window).width()*3/4,
  943. maxHeight:$(window).height()*3/4,
  944. close:function(){ $('#editdlg').remove(); },
  945. buttons:[
  946. { text:"Cancel", click:function(){
  947. $(this).dialog('close');
  948. addBlur();
  949. }},
  950. { text:"OK", click:function(){
  951. if(cm)
  952. $("#td_longText").val(cm.getValue());
  953. var res=$("#td_longText").val();
  954. res = res.replace(/\n/g, '\u2424' );
  955. $(this).dialog('close');
  956. $(inp).val(res);
  957. addBlur();
  958. }}]
  959. });
  960. };
  961. if( is_long )
  962. $(newEl).click(myFunc);
  963. return newEl;
  964. }
  965. /*************** select **************/
  966. function
  967. FW_createSelect(elName, devName, vArr, currVal, set, params, cmd)
  968. {
  969. if(vArr.length < 2 || vArr[0] != "select" || (params && params.length))
  970. return undefined;
  971. var newEl = document.createElement('select');
  972. var vHash = {};
  973. for(var j=1; j < vArr.length; j++) {
  974. var o = document.createElement('option');
  975. o.text = o.value = vArr[j].replace(/#/g," ");
  976. vHash[vArr[j]] = 1;
  977. newEl.options[j-1] = o;
  978. }
  979. if(currVal)
  980. $(newEl).val(currVal);
  981. if(elName)
  982. $(newEl).attr('name', elName);
  983. if(cmd)
  984. $(newEl).change(function(arg) { cmd($(newEl).val()) });
  985. newEl.setValueFn = function(arg) { if(vHash[arg]) $(newEl).val(arg); };
  986. return newEl;
  987. }
  988. /*************** noArg **************/
  989. function
  990. FW_createNoArg(elName, devName, vArr, currVal, set, params, cmd)
  991. {
  992. if(vArr.length != 1 || vArr[0] != "noArg" || (params && params.length))
  993. return undefined;
  994. var newEl = $('<div style="display:none">').get(0);
  995. if(elName)
  996. $(newEl).append('<input type="hidden" name="'+elName+ '" value="">');
  997. return(newEl);
  998. }
  999. /*************** slider **************/
  1000. function
  1001. FW_createSlider(elName, devName, vArr, currVal, set, params, cmd)
  1002. {
  1003. // min, step, max, float
  1004. if(vArr.length < 4 || vArr.length > 5 || vArr[0] != "slider" ||
  1005. (params && params.length))
  1006. return undefined;
  1007. var min = parseFloat(vArr[1]);
  1008. var stp = parseFloat(vArr[2]);
  1009. var max = parseFloat(vArr[3]);
  1010. var flt = (vArr.length == 5 && vArr[4] == "1");
  1011. var dp = 0; // decimal points for float
  1012. if(flt) {
  1013. var s = ""+stp;
  1014. if(s.indexOf(".") >= 0)
  1015. dp = s.substr(s.indexOf(".")+1).length;
  1016. }
  1017. if(currVal != undefined)
  1018. currVal = currVal.replace(/[^\d.\-]/g, "");
  1019. currVal = (currVal==undefined || currVal=="") ? min : parseFloat(currVal);
  1020. if(max==min)
  1021. return undefined;
  1022. if(currVal < min || currVal > max)
  1023. currVal = min;
  1024. var newEl = $('<div style="display:inline-block" tabindex="0">').get(0);
  1025. var slider = $('<div class="slider" id="slider.'+devName+'">').get(0);
  1026. $(newEl).append(slider);
  1027. var sh = $('<div class="handle">'+currVal+'</div>').get(0);
  1028. $(slider).append(sh);
  1029. if(elName)
  1030. $(newEl).append('<input type="hidden" name="'+elName+
  1031. '" value="'+currVal+'">');
  1032. var lastX=-1, offX=0, maxX=0, val=currVal;
  1033. newEl.activateFn = function() {
  1034. if(currVal < min || currVal > max)
  1035. return;
  1036. maxX = slider.offsetWidth-sh.offsetWidth;
  1037. offX = (currVal-min)*maxX/(max-min);
  1038. var strVal = (flt ? currVal.toFixed(dp) : ""+parseInt(currVal));
  1039. sh.innerHTML = strVal
  1040. sh.setAttribute('style', 'left:'+offX+'px;');
  1041. if(elName)
  1042. slider.nextSibling.setAttribute('value', strVal);
  1043. }
  1044. $(newEl).keydown(function(e){
  1045. if(e.keyCode == 37) currVal -= stp;
  1046. else if(e.keyCode == 39) currVal += stp;
  1047. else return;
  1048. if(currVal < min) currVal = min;
  1049. if(currVal > max) currVal = max;
  1050. offX = (currVal-min)*maxX/(max-min);
  1051. var strVal = (flt ? currVal.toFixed(dp) : ""+parseInt(currVal));
  1052. sh.innerHTML = strVal;
  1053. sh.setAttribute('style', 'left:'+offX+'px;');
  1054. if(cmd)
  1055. cmd(strVal);
  1056. if(elName)
  1057. slider.nextSibling.setAttribute('value', strVal);
  1058. });
  1059. function
  1060. touchFn(e, fn)
  1061. {
  1062. e.preventDefault(); // Prevents Safari from scrolling!
  1063. if(e.touches == null || e.touches.length == 0)
  1064. return;
  1065. e.clientX = e.touches[0].clientX;
  1066. fn(e);
  1067. }
  1068. function
  1069. mouseDown(e)
  1070. {
  1071. var oldFn1 = document.onmousemove, oldFn2 = document.onmouseup,
  1072. oldFn3 = document.ontouchmove, oldFn4 = document.ontouchend;
  1073. e.stopPropagation(); // Dashboard fix
  1074. lastX = e.clientX; // Does not work on IE8
  1075. function
  1076. mouseMove(e)
  1077. {
  1078. e.stopPropagation(); // Dashboard fix
  1079. if(maxX == 0) // Forum #35846
  1080. maxX = slider.offsetWidth-sh.offsetWidth;
  1081. var diff = e.clientX-lastX; lastX = e.clientX;
  1082. offX += diff;
  1083. if(offX < 0) offX = 0;
  1084. if(offX > maxX) offX = maxX;
  1085. val = offX/maxX * (max-min);
  1086. val = flt ? (Math.floor(val/stp)*stp+min).toFixed(dp) :
  1087. (Math.floor(Math.floor(val/stp)*stp)+min);
  1088. sh.innerHTML = val;
  1089. sh.setAttribute('style', 'left:'+offX+'px;');
  1090. }
  1091. document.onmousemove = mouseMove;
  1092. document.ontouchmove = function(e) { touchFn(e, mouseMove); }
  1093. document.onmouseup = document.ontouchend = function(e)
  1094. {
  1095. e.stopPropagation(); // Dashboard fix
  1096. document.onmousemove = oldFn1; document.onmouseup = oldFn2;
  1097. document.ontouchmove = oldFn3; document.ontouchend = oldFn4;
  1098. if(cmd)
  1099. cmd(val);
  1100. if(elName)
  1101. slider.nextSibling.setAttribute('value', val);
  1102. };
  1103. };
  1104. sh.onselectstart = function() { return false; }
  1105. sh.onmousedown = mouseDown;
  1106. sh.ontouchstart = function(e) { touchFn(e, mouseDown); }
  1107. newEl.setValueFn = function(arg) {
  1108. var res = arg.match(/[\d.\-]+/); // extract first number
  1109. currVal = (res ? parseFloat(res[0]) : min);
  1110. if(currVal < min || currVal > max)
  1111. currVal = min;
  1112. newEl.activateFn();
  1113. };
  1114. return newEl;
  1115. }
  1116. /*************** TIME **************/
  1117. function
  1118. FW_createTime(elName, devName, vArr, currVal, set, params, cmd)
  1119. {
  1120. if(vArr.length != 1 || vArr[0] != "time" || (params && params.length))
  1121. return undefined;
  1122. var open="-", closed="+";
  1123. var newEl = document.createElement('div');
  1124. $(newEl).append('<input type="text" size="5">');
  1125. $(newEl).append('<input type="button" value="'+closed+'">');
  1126. var inp = $(newEl).find("[type=text]");
  1127. var btn = $(newEl).find("[type=button]");
  1128. currVal = (currVal ? currVal : "12:00")
  1129. .replace(/[^\d]*(\d\d):(\d\d).*/g,"$1:$2");
  1130. $(inp).val(currVal)
  1131. if(elName)
  1132. $(inp).attr("name", elName);
  1133. var hh, mm; // the slider elements
  1134. newEl.setValueFn = function(arg) {
  1135. arg = arg.replace(/[^\d]*(\d\d):(\d\d).*/g,"$1:$2");
  1136. $(inp).val(arg);
  1137. var hhmm = arg.split(":");
  1138. if(hhmm.length == 2 && hh && mm) {
  1139. hh.setValueFn(hhmm[0]);
  1140. mm.setValueFn(hhmm[1]);
  1141. }
  1142. };
  1143. $(btn).click(function(){ // Open/Close the slider view
  1144. var v = $(inp).val();
  1145. if($(btn).val() == open) {
  1146. $(btn).val(closed);
  1147. $(newEl).find(".timeSlider").remove();
  1148. hh = mm = undefined;
  1149. if(cmd)
  1150. cmd(v);
  1151. return;
  1152. }
  1153. $(btn).val(open);
  1154. if(v.indexOf(":") < 0) {
  1155. v = "12:00";
  1156. $(inp).val(v);
  1157. }
  1158. var hhmm = v.split(":");
  1159. function
  1160. tSet(idx, arg)
  1161. {
  1162. if((""+arg).length < 2)
  1163. arg = '0'+arg;
  1164. hhmm[idx] = arg;
  1165. $(inp).val(hhmm.join(":"));
  1166. }
  1167. $(newEl).append('<div class="timeSlider">');
  1168. var ts = $(newEl).find(".timeSlider");
  1169. hh = FW_createSlider(undefined, devName+"HH", ["slider", 0, 1, 23],
  1170. hhmm[0], undefined, params, function(arg) { tSet(0, arg) });
  1171. mm = FW_createSlider(undefined, devName+"MM", ["slider", 0, 5, 55],
  1172. hhmm[1], undefined, params, function(arg) { tSet(1, arg) });
  1173. $(ts).append("<br>"); $(ts).append(hh); hh.activateFn();
  1174. $(ts).append("<br>"); $(ts).append(mm); mm.activateFn();
  1175. });
  1176. return newEl;
  1177. }
  1178. /*************** MULTIPLE **************/
  1179. function
  1180. FW_createMultiple(elName, devName, vArr, currVal, set, params, cmd)
  1181. {
  1182. if(vArr.length < 2 || (vArr[0]!="multiple" && vArr[0]!="multiple-strict") ||
  1183. (params && params.length))
  1184. return undefined;
  1185. var newEl = $('<input type="text" size="30" readonly>').get(0);
  1186. if(currVal)
  1187. $(newEl).val(currVal);
  1188. if(elName)
  1189. $(newEl).attr("name", elName);
  1190. newEl.setValueFn = function(arg){ $(newEl).val(arg) };
  1191. for(var i1=1; i1<vArr.length; i1++)
  1192. vArr[i1] = vArr[i1].replace(/#/g, " ");
  1193. $(newEl).focus(function(){
  1194. var sel = $(newEl).val().split(","), selObj={};
  1195. for(var i1=0; i1<sel.length; i1++)
  1196. selObj[sel[i1]] = 1;
  1197. var table = "";
  1198. for(var i1=1; i1<vArr.length; i1++) {
  1199. var v = vArr[i1];
  1200. table += '<tr>'+ // funny stuff for ios6 style, forum #23561
  1201. '<td><div class="checkbox"><input name="'+v+'" type="checkbox"'+
  1202. (selObj[v] ? " checked" : "")+'/>'+
  1203. '<label for="'+v+'"><span></span></label></div></td>'+
  1204. '<td><label for="' +v+'">'+v+'</label></td></tr>';
  1205. delete(selObj[v]);
  1206. }
  1207. var selArr=[];
  1208. for(var i1 in selObj)
  1209. selArr.push(i1);
  1210. var strict = (vArr[0] == "multiple-strict");
  1211. $('body').append(
  1212. '<div id="multidlg" style="display:none">'+
  1213. '<table>'+table+'</table>'+(!strict ? '<input id="md_freeText" '+
  1214. 'value="'+selArr.join(',')+'"/>' : '')+
  1215. '</div>');
  1216. $('#multidlg').dialog(
  1217. { modal:true, closeOnEscape:false, maxHeight:$(window).height()*3/4,
  1218. buttons:[
  1219. { text:"Cancel", click:function(){ $('#multidlg').remove(); }},
  1220. { text:"OK", click:function(){
  1221. var res=[];
  1222. if($("#md_freeText").val())
  1223. res.push($("#md_freeText").val());
  1224. $("#multidlg table input").each(function(){
  1225. if($(this).prop("checked"))
  1226. res.push($(this).attr("name"));
  1227. });
  1228. $('#multidlg').remove();
  1229. $(newEl).val(res.join(","));
  1230. if(cmd)
  1231. cmd(res.join(","));
  1232. }}]});
  1233. });
  1234. return newEl;
  1235. }
  1236. /*************** WIDGETS END **************/
  1237. /*************** SCRIPT LOAD FUNCTIONS START **************/
  1238. function
  1239. loadScript(sname, callback, force)
  1240. {
  1241. var h = document.head || document.getElementsByTagName('head')[0];
  1242. sname = FW_root+"/"+sname;
  1243. if(FW_scripts[sname]) {
  1244. if(FW_scripts[sname].loaded) {
  1245. if(callback)
  1246. callback();
  1247. } else {
  1248. FW_scripts[sname].callbacks.push(callback);
  1249. }
  1250. return;
  1251. }
  1252. if(!FW_docReady && !force) {
  1253. FW_scripts[sname] = { callbacks:[ callback] };
  1254. return;
  1255. }
  1256. var script = document.createElement("script");
  1257. script.src = sname;
  1258. script.async = script.defer = false;
  1259. script.type = "text/javascript";
  1260. FW_scripts[sname] = { callbacks:[ callback] };
  1261. function
  1262. scriptLoaded()
  1263. {
  1264. var p = FW_scripts[sname];
  1265. p.loaded = true;
  1266. if(!p.called) {
  1267. p.called = true;
  1268. for(var i1=0; i1< p.callbacks.length; i1++)
  1269. if(p.callbacks[i1]) // pushing undefined callbacks on the stack is ok
  1270. p.callbacks[i1]();
  1271. }
  1272. delete(p.callbacks);
  1273. }
  1274. log("Loading script "+sname);
  1275. if(FW_isIE) {
  1276. script.onreadystatechange = function() {
  1277. if(script.readyState == 'loaded' || script.readyState == 'complete') {
  1278. script.onreadystatechange = null;
  1279. scriptLoaded();
  1280. }
  1281. }
  1282. } else {
  1283. if(FW_isiOS) {
  1284. FW_leaving = 1;
  1285. if(FW_pollConn)
  1286. FW_pollConn.abort();
  1287. }
  1288. script.onload = function(){
  1289. scriptLoaded();
  1290. // if(FW_isiOS) // Fixed in the maintime/not needed with 10.2
  1291. // FW_longpoll();
  1292. }
  1293. }
  1294. h.appendChild(script);
  1295. }
  1296. function
  1297. loadLink(lname)
  1298. {
  1299. var h = document.head || document.getElementsByTagName('head')[0];
  1300. lname = FW_root+"/"+lname;
  1301. var arr = h.getElementsByTagName("link");
  1302. for(var i1=0; i1<arr.length; i1++)
  1303. if(lname == arr[i1].getAttribute("href"))
  1304. return;
  1305. var link = document.createElement("link");
  1306. link.href = lname;
  1307. link.rel = "stylesheet";
  1308. log("Loading link "+lname);
  1309. h.appendChild(link);
  1310. }
  1311. function
  1312. scriptAttribute(sname)
  1313. {
  1314. var attr="";
  1315. $("head script").each(function(){
  1316. var src = $(this).attr("src");
  1317. if(src && src.indexOf(sname) >= 0)
  1318. attr = $(this).attr("attr");
  1319. });
  1320. var ua={};
  1321. if(attr && attr != "") {
  1322. try {
  1323. ua=JSON.parse(attr);
  1324. } catch(e){
  1325. FW_errmsg(sname+" Parameter "+e,5000);
  1326. }
  1327. }
  1328. return ua;
  1329. }
  1330. /*************** SCRIPT LOAD FUNCTIONS END **************/
  1331. function
  1332. print_call_stack() {
  1333. var stack = new Error().stack;
  1334. console.log("PRINTING CALL STACK");
  1335. console.log( stack );
  1336. }
  1337. function
  1338. FW_getSVG(emb)
  1339. {
  1340. if(emb.contentDocument)
  1341. return emb.contentDocument;
  1342. if(typeof emb.getSVGDocument == "function")
  1343. return emb.getSVGDocument();
  1344. return undefined;
  1345. }