fhemdoc_modular.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. "use strict";
  2. // $Id: fhemdoc_modular.js 16015 2018-01-27 21:30:01Z rudolfkoenig $
  3. var fd_loadedHash={}, fd_loadedList=[], fd_all={}, fd_allCnt, fd_progress=0,
  4. fd_lang, fd_offsets=[], fd_scrolled=0, fd_modLinks={}, csrfToken="X";
  5. function
  6. fd_status(txt)
  7. {
  8. var errmsg = $("#errmsg");
  9. if(!$(errmsg).length) {
  10. $('#menuScrollArea').append('<div id="errmsg">');
  11. errmsg = $("#errmsg");
  12. }
  13. if(txt == "")
  14. $(errmsg).remove();
  15. else
  16. $(errmsg).html(txt);
  17. }
  18. function
  19. fd_fC(fn, callback)
  20. {
  21. console.log("fd_fC:"+fn);
  22. var p = location.pathname;
  23. var cmd = p.substr(0,p.indexOf('/doc'))+'?cmd='+fn+csrfToken+'&XHR=1';
  24. $.ajax({
  25. url:cmd, method:'POST', cache:false, success:callback,
  26. error:function(xhr, status, err) {
  27. if(xhr.status == 400 && csrfToken) {
  28. csrfToken = "";
  29. fd_csrfRefresh(function(){fd_fC(fn, callback)});
  30. } else {
  31. console.log("FAIL ERR:"+xhr.status+" STAT:"+status);
  32. }
  33. }
  34. });
  35. }
  36. function
  37. loadOneDoc(mname, lang)
  38. {
  39. var origLink = mname;
  40. function
  41. done(err, calc)
  42. {
  43. if(fd_progress) {
  44. fd_status(fd_progress+" / "+fd_allCnt);
  45. if(++fd_progress > fd_allCnt) {
  46. fd_progress = 0;
  47. setTimeout(calcOffsets,100); // Firefox returns wrong offsets
  48. fd_status("");
  49. }
  50. } else {
  51. if(calc)
  52. setTimeout(calcOffsets,100);
  53. if(!err)
  54. setTimeout(function(){location.href = "#"+origLink;}, 100);
  55. }
  56. }
  57. if(fd_modLinks[mname])
  58. mname = fd_modLinks[mname];
  59. if(fd_loadedHash[mname] && fd_loadedHash[mname] == lang)
  60. return done(false, false);
  61. fd_fC("help "+mname+" "+lang, function(ret){
  62. //console.log(mname+" "+lang+" => "+ret.length);
  63. if(ret.indexOf("<html>") != 0 || ret.indexOf("<html>No help found") == 0)
  64. return done(true, false);
  65. ret = ret.replace(/<\/?html>/g,'');
  66. ret = ret.replace(/Keine deutsche Hilfe gefunden!<br\/>/,'');
  67. ret = '<div id="FD_'+mname+'">'+ret+'</div>';
  68. ret = ret.replace(/target="_blank"/g, ''); // revert help URL rewrite
  69. ret = ret.replace(/href=".*?commandref.*?.html#/g, 'href="#');
  70. if(fd_loadedHash[mname])
  71. $("div#FD_"+mname).remove();
  72. if(!fd_loadedHash[mname])
  73. fd_loadedList.push(mname);
  74. fd_loadedHash[mname] = lang;
  75. fd_loadedList.sort();
  76. var idx=0;
  77. while(fd_loadedList[idx] != mname)
  78. idx++;
  79. var toIns = "perl";
  80. if(idx < fd_loadedList.length-1)
  81. toIns = fd_loadedList[idx+1];
  82. console.log("insert "+mname+" before "+toIns);
  83. $(ret).insertBefore("a[name="+toIns+"]");
  84. addAHooks("div#FD_"+mname);
  85. return done(false, true);
  86. });
  87. }
  88. function
  89. addAHooks(el)
  90. {
  91. $(el).find("a[href]").each(function(){
  92. var href = $(this).attr("href");
  93. if(!href || href.indexOf("#") != 0)
  94. return;
  95. href = href.substr(1);
  96. if(fd_modLinks[href] && !fd_loadedHash[href]) {
  97. $(this).click(function(){
  98. $("a[href=#"+href+"]").unbind('click');
  99. loadOneDoc(href, fd_lang);
  100. });
  101. }
  102. });
  103. }
  104. function
  105. calcOffsets()
  106. {
  107. fd_offsets=[];
  108. for(var i1=0; i1<fd_loadedList.length; i1++) {
  109. var cr = $("a[name="+fd_loadedList[i1]+"]").offset();
  110. fd_offsets.push(cr ? cr.top : -1);
  111. }
  112. checkScroll();
  113. }
  114. function
  115. checkScroll()
  116. {
  117. if(!fd_scrolled) {
  118. setTimeout(checkScroll, 500);
  119. return;
  120. }
  121. fd_scrolled = 0;
  122. var viewTop=$(window).scrollTop(), viewBottom=viewTop+$(window).height();
  123. var idx=0;
  124. while(idx<fd_offsets.length) {
  125. if(fd_offsets[idx] >= viewTop && viewBottom > fd_offsets[idx]+30)
  126. break;
  127. idx++;
  128. }
  129. if(idx >= fd_offsets.length) {
  130. $("a#otherLang").hide();
  131. } else {
  132. var mname = fd_loadedList[idx];
  133. var l1 = fd_loadedHash[mname], l2 = (l1=="EN" ? "DE" : "EN");
  134. $("a#otherLang span.mod").html(mname);
  135. $("a#otherLang span[lang="+l1+"]").hide();
  136. $("a#otherLang span[lang="+l2+"]").show();
  137. $("a#otherLang").show();
  138. }
  139. }
  140. function
  141. loadOtherLang()
  142. {
  143. var mname = $("a#otherLang span.mod").html();
  144. loadOneDoc(mname, fd_loadedHash[mname]=="EN" ? "DE" : "EN");
  145. }
  146. function
  147. fd_csrfRefresh(callback)
  148. {
  149. console.log("fd_csrfRefresh");
  150. $.ajax({
  151. url:location.pathname.replace(/docs.*/,'')+"?XHR=1",
  152. success: function(data, textStatus, request){
  153. csrfToken = request.getResponseHeader('x-fhem-csrftoken');
  154. csrfToken = csrfToken ? ("&fwcsrf="+csrfToken) : "";
  155. if(callback)
  156. callback();
  157. }
  158. });
  159. }
  160. $(document).ready(function(){
  161. var p = location.pathname;
  162. fd_lang = p.substring(p.indexOf("commandref")+11,p.indexOf(".html"));
  163. if(!fd_lang || fd_lang == '.')
  164. fd_lang = "EN";
  165. $("div#modLinks").each(function(){
  166. var a1 = $(this).html().split(" ");
  167. for(var i1=0; i1<a1.length; i1++) {
  168. var a2 = a1[i1].split(/[:,]/);
  169. var mName = a2.shift();
  170. for(var i2=0; i2<a2.length; i2++)
  171. if(!fd_modLinks[a2[i2]])
  172. fd_modLinks[a2[i2]] = mName;
  173. }
  174. });
  175. $("a[name]").each(function(){ fd_loadedHash[$(this).attr("name")]=fd_lang; });
  176. $("table.summary td.modname a")
  177. .each(function(){
  178. var mod = $(this).html();
  179. fd_all[mod]=1;
  180. fd_modLinks[mod] = fd_modLinks[mod+"define"] = fd_modLinks[mod+"get"] =
  181. fd_modLinks[mod+"set"] = fd_modLinks[mod+"attribute"]= mod;
  182. })
  183. .click(function(e){
  184. e.preventDefault();
  185. loadOneDoc($(this).html(), fd_lang);
  186. });
  187. if(location.hash)
  188. loadOneDoc(location.hash.substr(1), fd_lang);
  189. $(window).bind('hashchange', function() {
  190. loadOneDoc(location.hash.substr(1), fd_lang);
  191. });
  192. $("a[name=loadAll]").show().click(function(e){
  193. e.preventDefault();
  194. $("a[name=loadAll]").hide();
  195. location.href = "#doctop";
  196. fd_allCnt = 0;
  197. for(var m in fd_all) fd_allCnt++
  198. fd_progress = 1;
  199. for(var mname in fd_all)
  200. loadOneDoc(mname, fd_lang);
  201. });
  202. $("a#otherLang").click(loadOtherLang);
  203. addAHooks("body");
  204. window.onscroll = function(){
  205. if(!fd_scrolled++)
  206. setTimeout(checkScroll, 500);
  207. };
  208. fd_csrfRefresh();
  209. });