main.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Miscellaneous js functions for WebHelp
  3. * Kasun Gajasinghe, http://kasunbg.blogspot.com
  4. * David Cramer, http://www.thingbag.net
  5. *
  6. */
  7. //Turn ON and OFF the animations for Show/Hide Sidebar. Extend this to other anime as well if any.
  8. var noAnimations=false;
  9. $(document).ready(function() {
  10. // When you click on a link to an anchor, scroll down
  11. // 105 px to cope with the fact that the banner
  12. // hides the top 95px or so of the page.
  13. // This code deals with the problem when
  14. // you click on a link within a page.
  15. $('a[href*=#]').click(function() {
  16. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
  17. && location.hostname == this.hostname) {
  18. var $target = $(this.hash);
  19. $target = $target.length && $target
  20. || $('[name=' + this.hash.slice(1) +']');
  21. if (!(this.hash == "#searchDiv" || this.hash == "#treeDiv" || this.hash == "") && $target.length) {
  22. var targetOffset = $target.offset().top - 120;
  23. $('html,body')
  24. .animate({scrollTop: targetOffset}, 200);
  25. return false;
  26. }
  27. }
  28. });
  29. // $("#showHideHighlight").button(); //add jquery button styling to 'Go' button
  30. //Generate tabs in nav-pane with JQuery
  31. $(function() {
  32. $("#tabs").tabs({
  33. cookie: {
  34. expires: 2 // store cookie for 2 days.
  35. }
  36. });
  37. });
  38. //Generate the tree
  39. $("#ulTreeDiv").attr("style", "");
  40. $("#tree").treeview({
  41. collapsed: true,
  42. animated: "medium",
  43. control: "#sidetreecontrol",
  44. persist: "cookie"
  45. });
  46. //after toc fully styled, display it. Until loading, a 'loading' image will be displayed
  47. $("#tocLoading").attr("style", "display:none;");
  48. // $("#ulTreeDiv").attr("style","display:block;");
  49. //.searchButton is the css class applied to 'Go' button
  50. $(function() {
  51. $("button", ".searchButton").button();
  52. $("button", ".searchButton").click(function() {
  53. return false;
  54. });
  55. });
  56. //'ui-tabs-1' is the cookie name which is used for the persistence of the tabs.(Content/Search tab)
  57. if ($.cookie('ui-tabs-1') === '1') { //search tab is active
  58. if ($.cookie('textToSearch') != undefined && $.cookie('textToSearch').length > 0) {
  59. document.getElementById('textToSearch').value = $.cookie('textToSearch');
  60. Verifie('searchForm');
  61. searchHighlight($.cookie('textToSearch'));
  62. $("#showHideHighlight").css("display", "block");
  63. }
  64. }
  65. syncToc(); //Synchronize the toc tree with the content pane, when loading the page.
  66. //$("#doSearch").button(); //add jquery button styling to 'Go' button
  67. // When you click on a link to an anchor, scroll down
  68. // 120 px to cope with the fact that the banner
  69. // hides the top 95px or so of the page.
  70. // This code deals with the problem when
  71. // you click on a link from another page.
  72. var hash = window.location.hash;
  73. if(hash){
  74. var targetOffset = $(hash).offset().top - 120;
  75. $('html,body').animate({scrollTop: targetOffset}, 200);
  76. return false;
  77. }
  78. });
  79. /**
  80. * If an user moved to another page by clicking on a toc link, and then clicked on #searchDiv,
  81. * search should be performed if the cookie textToSearch is not empty.
  82. */
  83. function doSearch() {
  84. //'ui-tabs-1' is the cookie name which is used for the persistence of the tabs.(Content/Search tab)
  85. if ($.cookie('textToSearch') != undefined && $.cookie('textToSearch').length > 0) {
  86. document.getElementById('textToSearch').value = $.cookie('textToSearch');
  87. Verifie('searchForm');
  88. }
  89. }
  90. /**
  91. * Synchronize with the tableOfContents
  92. */
  93. function syncToc() {
  94. var a = document.getElementById("webhelp-currentid");
  95. if (a != undefined) {
  96. //Expanding the child sections of the selected node.
  97. var nodeClass = a.getAttribute("class");
  98. if (nodeClass != null && !nodeClass.match(/collapsable/)) {
  99. a.setAttribute("class", "collapsable");
  100. //remove display:none; css style from <ul> block in the selected node.
  101. var ulNode = a.getElementsByTagName("ul")[0];
  102. if (ulNode != undefined) {
  103. if (ulNode.hasAttribute("style")) {
  104. ulNode.setAttribute("style", "display: block; background-color: #D8D8D8 !important;");
  105. } else {
  106. var ulStyle = document.createAttribute("style");
  107. ulStyle.nodeValue = "display: block; background-color: #D8D8D8 !important;";
  108. ulNode.setAttributeNode(ulStyle);
  109. } }
  110. //adjust tree's + sign to -
  111. var divNode = a.getElementsByTagName("div")[0];
  112. if (divNode != undefined) {
  113. if (divNode.hasAttribute("class")) {
  114. divNode.setAttribute("class", "hitarea collapsable-hitarea");
  115. } else {
  116. var divClass = document.createAttribute("class");
  117. divClass.nodeValue = "hitarea collapsable-hitarea";
  118. divNode.setAttributeNode(divClass);
  119. } }
  120. //set persistence cookie when a node is auto expanded
  121. // setCookieForExpandedNode("webhelp-currentid");
  122. }
  123. var b = a.getElementsByTagName("a")[0];
  124. if (b != undefined) {
  125. //Setting the background for selected node.
  126. var style = a.getAttribute("style", 2);
  127. if (style != null && !style.match(/background-color: Background;/)) {
  128. a.setAttribute("style", "background-color: #D8D8D8; " + style);
  129. b.setAttribute("style", "color: black;");
  130. } else if (style != null) {
  131. a.setAttribute("style", "background-color: #D8D8D8; " + style);
  132. b.setAttribute("style", "color: black;");
  133. } else {
  134. a.setAttribute("style", "background-color: #D8D8D8; ");
  135. b.setAttribute("style", "color: black;");
  136. }
  137. }
  138. //shows the node related to current content.
  139. //goes a recursive call from current node to ancestor nodes, displaying all of them.
  140. while (a.parentNode && a.parentNode.nodeName) {
  141. var parentNode = a.parentNode;
  142. var nodeName = parentNode.nodeName;
  143. if (nodeName.toLowerCase() == "ul") {
  144. parentNode.setAttribute("style", "display: block;");
  145. } else if (nodeName.toLocaleLowerCase() == "li") {
  146. parentNode.setAttribute("class", "collapsable");
  147. parentNode.firstChild.setAttribute("class", "hitarea collapsable-hitarea ");
  148. }
  149. a = parentNode;
  150. } } }
  151. /*
  152. function setCookieForExpandedNode(nodeName) {
  153. var tocDiv = document.getElementById("tree"); //get table of contents Div
  154. var divs = tocDiv.getElementsByTagName("div");
  155. var matchedDivNumber;
  156. var i;
  157. for (i = 0; i < divs.length; i++) { //1101001
  158. var div = divs[i];
  159. var liNode = div.parentNode;
  160. }
  161. //create a new cookie if a treeview does not exist
  162. if ($.cookie(treeCookieId) == null || $.cookie(treeCookieId) == "") {
  163. var branches = $("#tree").find("li");//.prepareBranches(treesettings);
  164. var data = [];
  165. branches.each(function(i, e) {
  166. data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0;
  167. });
  168. $.cookie(treeCookieId, data.join(""));
  169. }
  170. if (i < divs.length) {
  171. var treeviewCookie = $.cookie(treeCookieId);
  172. var tvCookie1 = treeviewCookie.substring(0, i);
  173. var tvCookie2 = treeviewCookie.substring(i + 1);
  174. var newTVCookie = tvCookie1 + "1" + tvCookie2;
  175. $.cookie(treeCookieId, newTVCookie);
  176. }
  177. } */
  178. /**
  179. * Code for Show/Hide TOC
  180. *
  181. */
  182. function showHideToc() {
  183. var showHideButton = $("#showHideButton");
  184. var leftNavigation = $("#sidebar"); //hide the parent div of leftnavigation, ie sidebar
  185. var content = $("#content");
  186. var animeTime=75
  187. if (showHideButton != undefined && showHideButton.hasClass("pointLeft")) {
  188. //Hide TOC
  189. showHideButton.removeClass('pointLeft').addClass('pointRight');
  190. if(noAnimations) {
  191. leftNavigation.css("display", "none");
  192. content.css("margin", "125px 0 0 0");
  193. } else {
  194. leftNavigation.hide(animeTime);
  195. content.animate( { "margin-left": 0 }, animeTime);
  196. }
  197. showHideButton.attr("title", "Show Sidebar");
  198. } else {
  199. //Show the TOC
  200. showHideButton.removeClass('pointRight').addClass('pointLeft');
  201. if(noAnimations) {
  202. content.css("margin", "125px 0 0 280px");
  203. leftNavigation.css("display", "block");
  204. } else {
  205. content.animate( { "margin-left": '280px' }, animeTime);
  206. leftNavigation.show(animeTime);
  207. }
  208. showHideButton.attr("title", "Hide Sidebar");
  209. }
  210. }
  211. /**
  212. * Code for search highlighting
  213. */
  214. var highlightOn = true;
  215. function searchHighlight(searchText) {
  216. highlightOn = true;
  217. if (searchText != undefined) {
  218. var wList;
  219. var sList = new Array(); //stem list
  220. //Highlight the search terms
  221. searchText = searchText.toLowerCase().replace(/<\//g, "_st_").replace(/\$_/g, "_di_").replace(/\.|%2C|%3B|%21|%3A|@|\/|\*/g, " ").replace(/(%20)+/g, " ").replace(/_st_/g, "</").replace(/_di_/g, "%24_")
  222. searchText = searchText.replace(/ +/g, " ");
  223. searchText = searchText.replace(/ $/, "").replace(/^ /, "");
  224. wList = searchText.split(" ");
  225. $("#content").highlight(wList); //Highlight the search input
  226. if (typeof stemmer != "undefined") {
  227. //Highlight the stems
  228. for (var i = 0; i < wList.length; i++) {
  229. var stemW = stemmer(wList[i]);
  230. sList.push(stemW);
  231. }
  232. } else {
  233. sList = wList;
  234. }
  235. $("#content").highlight(sList); //Highlight the search input's all stems
  236. }
  237. }
  238. function searchUnhighlight() {
  239. highlightOn = false;
  240. //unhighlight the search input's all stems
  241. $("#content").unhighlight();
  242. $("#content").unhighlight();
  243. }
  244. function toggleHighlight() {
  245. if (highlightOn) {
  246. searchUnhighlight();
  247. } else {
  248. searchHighlight($.cookie('textToSearch'));
  249. }
  250. }