fhemdoc_modular.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. "use strict";
  2. // $Id: fhemdoc_modular.js 16786 2018-05-27 11:07:33Z 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. fd_mode = "FHEM";
  6. var fd_otherSrc = { "usb":"autocreate", "createlog":"autocreate" };
  7. function
  8. fd_status(txt)
  9. {
  10. var errmsg = $("#errmsg");
  11. if(!$(errmsg).length) {
  12. $('#menu').append('<a style="background-color:black; color:white; position:fixed; top:0px; left:40px; z-index: 10;" id="errmsg" href="#"></a>');
  13. errmsg = $("#errmsg");
  14. }
  15. if(txt == "")
  16. $(errmsg).remove();
  17. else
  18. $(errmsg).html(txt);
  19. }
  20. function
  21. fd_fC(fn, callback)
  22. {
  23. console.log("fd_fC:"+fn);
  24. if(fd_mode == "FHEM") {
  25. var p = location.pathname;
  26. var cmd = p.substr(0,p.indexOf('/doc'))+'?cmd='+fn+csrfToken+'&XHR=1';
  27. $.ajax({
  28. url:cmd, method:'POST', cache:false, success:callback,
  29. error:function(xhr, status, err) {
  30. if(xhr.status == 400 && csrfToken) {
  31. csrfToken = "";
  32. fd_csrfRefresh(function(){fd_fC(fn, callback)});
  33. } else {
  34. console.log("FAIL ERR:"+xhr.status+" STAT:"+status);
  35. }
  36. }
  37. });
  38. } else { // static
  39. $.ajax({
  40. url:fn, method:'GET',
  41. success:function(ret) {
  42. callback('<html>'+ret+'</html>');
  43. },
  44. error:function(xhr, status, err) {
  45. callback("");
  46. console.log("FAIL ERR:"+xhr.status+" STAT:"+status);
  47. fd_status("Cannot load "+fn);
  48. setTimeout(function(){ fd_status("") }, 5000);
  49. }
  50. });
  51. }
  52. }
  53. // Dynamically load the codumentation of one module.
  54. var inLoadOneDoc = false;
  55. function
  56. loadOneDoc(mname, lang)
  57. {
  58. var origLink = mname;
  59. if(inLoadOneDoc)
  60. return;
  61. function
  62. done(err, calc)
  63. {
  64. if(fd_progress) {
  65. fd_status(fd_progress+" / "+fd_allCnt);
  66. if(++fd_progress > fd_allCnt) {
  67. fd_progress = 0;
  68. setTimeout(calcOffsets,100); // Firefox returns wrong offsets
  69. fd_status("");
  70. }
  71. } else {
  72. if(calc)
  73. setTimeout(calcOffsets,100);
  74. inLoadOneDoc = true; // avoid the hashchange callback
  75. setTimeout(function(){ location.href = "#"+origLink; }, 100);
  76. // takes long if the complete doc is loaded
  77. setTimeout(function(){ inLoadOneDoc = false; }, 2000);
  78. }
  79. }
  80. if(fd_modLinks[mname])
  81. mname = fd_modLinks[mname];
  82. if(fd_loadedHash[mname] && fd_loadedHash[mname] == lang)
  83. return done(false, false);
  84. fd_fC(fd_mode=="FHEM" ? "help "+mname+" "+lang :
  85. "/cref"+(lang=="EN" ? "":"_"+lang)+"/"+mname+".cref",
  86. function(ret){
  87. if(ret.indexOf("<html>") != 0 || ret.indexOf("<html>No help found") == 0)
  88. return done(true, false);
  89. ret = ret.replace(/<\/?html>/g,'');
  90. ret = ret.replace(/Keine deutsche Hilfe gefunden!<br\/>/,'');
  91. ret = '<div id="FD_'+mname+'">'+ret+'</div>';
  92. ret = ret.replace(/target="_blank"/g, ''); // revert help URL rewrite
  93. ret = ret.replace(/href=".*?commandref.*?.html#/g, 'href="#');
  94. if(fd_loadedHash[mname])
  95. $("div#FD_"+mname).remove();
  96. if(!fd_loadedHash[mname])
  97. fd_loadedList.push(mname);
  98. fd_loadedHash[mname] = lang;
  99. fd_loadedList.sort();
  100. var idx=0;
  101. while(fd_loadedList[idx] != mname)
  102. idx++;
  103. var toIns = "perl";
  104. if(idx < fd_loadedList.length-1)
  105. toIns = fd_loadedList[idx+1];
  106. console.log("insert "+mname+" before "+toIns);
  107. $(ret).insertBefore("a[name="+toIns+"]");
  108. addAHooks("div#FD_"+mname);
  109. return done(false, true);
  110. });
  111. }
  112. // Add a hook for each <a> tag to load & scroll to the corresponding item
  113. function
  114. addAHooks(el)
  115. {
  116. $(el).find("a[href]").each(function(){
  117. var href = $(this).attr("href");
  118. if(!href || href.indexOf("#") != 0)
  119. return;
  120. href = href.substr(1);
  121. if(fd_modLinks[href] && !fd_loadedHash[href]) {
  122. $(this).click(function(){
  123. $("a[href=#"+href+"]").unbind('click');
  124. loadOneDoc(href, fd_lang);
  125. });
  126. }
  127. });
  128. }
  129. // remember the offset of all loaded elements, to be able to dynamically show
  130. // the correct "load <XXX> in other language" link
  131. function
  132. calcOffsets()
  133. {
  134. fd_offsets=[];
  135. for(var i1=0; i1<fd_loadedList.length; i1++) {
  136. var cr = $("a[name="+fd_loadedList[i1]+"]").offset();
  137. fd_offsets.push(cr ? cr.top : -1);
  138. }
  139. checkScroll();
  140. }
  141. // Show the correct otherLang, see calcOffsets
  142. function
  143. checkScroll()
  144. {
  145. if(!fd_scrolled) {
  146. setTimeout(checkScroll, 500);
  147. return;
  148. }
  149. fd_scrolled = 0;
  150. var viewTop=$(window).scrollTop(), viewBottom=viewTop+$(window).height();
  151. var idx=0;
  152. while(idx<fd_offsets.length) {
  153. if(fd_offsets[idx] >= viewTop && viewBottom > fd_offsets[idx]+30)
  154. break;
  155. idx++;
  156. }
  157. if(idx >= fd_offsets.length) {
  158. $("a#otherLang").hide();
  159. } else {
  160. var mname = fd_loadedList[idx];
  161. var l1 = fd_loadedHash[mname], l2 = (l1=="EN" ? "DE" : "EN");
  162. $("a#otherLang span.mod").html(mname);
  163. $("a#otherLang span[lang="+l1+"]").hide();
  164. $("a#otherLang span[lang="+l2+"]").show();
  165. $("a#otherLang").show();
  166. }
  167. }
  168. // Load the current entry in the other langueage
  169. function
  170. loadOtherLang()
  171. {
  172. var mname = $("a#otherLang span.mod").html();
  173. loadOneDoc(mname, fd_loadedHash[mname]=="EN" ? "DE" : "EN");
  174. }
  175. // get the current csrf from FHEMWEB
  176. function
  177. fd_csrfRefresh(callback)
  178. {
  179. if(fd_mode != "FHEM")
  180. return;
  181. console.log("fd_csrfRefresh");
  182. $.ajax({
  183. url:location.pathname.replace(/docs.*/,'')+"?XHR=1",
  184. success: function(data, textStatus, request){
  185. csrfToken = request.getResponseHeader('x-fhem-csrftoken');
  186. csrfToken = csrfToken ? ("&fwcsrf="+csrfToken) : "";
  187. if(callback)
  188. callback();
  189. }
  190. });
  191. }
  192. $(document).ready(function(){
  193. var p = location.pathname.split(/[_.]/);
  194. fd_lang = (p[1] == "modular" ? p[2] : p[1]);
  195. if(fd_lang == "html")
  196. fd_lang = "EN";
  197. if(location.host == "fhem.de" || location.host == "commandref.fhem.de")
  198. fd_mode = "static";
  199. $("div#modLinks").each(function(){
  200. var a1 = $(this).html().split(" ");
  201. for(var i1=0; i1<a1.length; i1++) {
  202. var a2 = a1[i1].split(/[:,]/);
  203. var mName = a2.shift();
  204. for(var i2=0; i2<a2.length; i2++)
  205. if(!fd_modLinks[a2[i2]])
  206. fd_modLinks[a2[i2]] = mName;
  207. }
  208. });
  209. $("a[name]").each(function(){ fd_loadedHash[$(this).attr("name")]=fd_lang; });
  210. $("table.summary td.modname a")
  211. .each(function(){
  212. var mod = $(this).html();
  213. fd_all[mod]=1;
  214. fd_modLinks[mod] = fd_modLinks[mod+"define"] = fd_modLinks[mod+"get"] =
  215. fd_modLinks[mod+"set"] = fd_modLinks[mod+"attribute"]= mod;
  216. })
  217. .click(function(e){
  218. e.preventDefault();
  219. loadOneDoc($(this).html(), fd_lang);
  220. });
  221. for(var i1 in fd_otherSrc)
  222. fd_modLinks[i1] = fd_otherSrc[i1];
  223. if(location.hash && location.hash.length > 1)
  224. loadOneDoc(location.hash.substr(1), fd_lang);
  225. $(window).bind('hashchange', function() {
  226. if(location.hash.length > 1)
  227. loadOneDoc(location.hash.substr(1), fd_lang);
  228. });
  229. $("a[name=loadAll]").show().click(function(e){
  230. e.preventDefault();
  231. $("a[name=loadAll]").hide();
  232. location.href = "#doctop";
  233. fd_allCnt = 0;
  234. for(var m in fd_all) fd_allCnt++
  235. fd_progress = 1;
  236. for(var mname in fd_all)
  237. loadOneDoc(mname, fd_lang);
  238. });
  239. $("a#otherLang").click(loadOtherLang);
  240. addAHooks("body");
  241. window.onscroll = function(){
  242. if(!fd_scrolled++)
  243. setTimeout(checkScroll, 500);
  244. };
  245. fd_csrfRefresh();
  246. });