fhemdoc_modular.js 5.2 KB

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