246 lines
8.3KB

  1. gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) {
  2. var gs = gitbook.storage;
  3. gitbook.events.bind("start", function(e, config) {
  4. // add the Edit button (edit on Github)
  5. var edit = config.edit;
  6. if (edit && edit.link) gitbook.toolbar.createButton({
  7. icon: 'fa fa-edit',
  8. label: edit.text || 'Edit',
  9. position: 'left',
  10. onClick: function(e) {
  11. e.preventDefault();
  12. window.open(edit.link);
  13. }
  14. });
  15. // add the History button (file history on Github)
  16. var history = config.history;
  17. if (history && history.link) gitbook.toolbar.createButton({
  18. icon: 'fa fa-history',
  19. label: history.text || 'History',
  20. position: 'left',
  21. onClick: function(e) {
  22. e.preventDefault();
  23. window.open(history.link);
  24. }
  25. });
  26. // add the Download button
  27. var down = config.download;
  28. var normalizeDownload = function() {
  29. if (!down || !(down instanceof Array) || down.length === 0) return;
  30. if (down[0] instanceof Array) return down;
  31. return $.map(down, function(file, i) {
  32. return [[file, file.replace(/.*[.]/g, '').toUpperCase()]];
  33. });
  34. };
  35. down = normalizeDownload(down);
  36. if (down) if (down.length === 1 && /[.]pdf$/.test(down[0][0])) {
  37. gitbook.toolbar.createButton({
  38. icon: 'fa fa-file-pdf-o',
  39. label: down[0][1],
  40. position: 'left',
  41. onClick: function(e) {
  42. e.preventDefault();
  43. window.open(down[0][0]);
  44. }
  45. });
  46. } else {
  47. gitbook.toolbar.createButton({
  48. icon: 'fa fa-download',
  49. label: 'Download',
  50. position: 'left',
  51. dropdown: $.map(down, function(item, i) {
  52. return {
  53. text: item[1],
  54. onClick: function(e) {
  55. e.preventDefault();
  56. window.open(item[0]);
  57. }
  58. };
  59. })
  60. });
  61. }
  62. // add the Information button
  63. var info = ['Keyboard shortcuts (<> indicates arrow keys):',
  64. '<left>/<right>: navigate to previous/next page',
  65. 's: Toggle sidebar'];
  66. if (config.search !== false) info.push('f: Toggle search input ' +
  67. '(use <up>/<down>/Enter in the search input to navigate through search matches; ' +
  68. 'press Esc to cancel search)');
  69. gitbook.toolbar.createButton({
  70. icon: 'fa fa-info',
  71. label: 'Information about the toolbar',
  72. position: 'left',
  73. onClick: function(e) {
  74. e.preventDefault();
  75. window.alert(info.join('\n\n'));
  76. }
  77. });
  78. // highlight the current section in TOC
  79. var href = window.location.pathname;
  80. href = href.substr(href.lastIndexOf('/') + 1);
  81. if (href === '') href = 'index.html';
  82. var li = $('a[href^="' + href + location.hash + '"]').parent('li.chapter').first();
  83. var summary = $('ul.summary'), chaps = summary.find('li.chapter');
  84. if (li.length === 0) li = chaps.first();
  85. li.addClass('active');
  86. chaps.on('click', function(e) {
  87. chaps.removeClass('active');
  88. $(this).addClass('active');
  89. gs.set('tocScrollTop', summary.scrollTop());
  90. });
  91. var toc = config.toc;
  92. // collapse TOC items that are not for the current chapter
  93. if (toc && toc.collapse) (function() {
  94. var type = toc.collapse;
  95. if (type === 'none') return;
  96. if (type !== 'section' && type !== 'subsection') return;
  97. // sections under chapters
  98. var toc_sub = summary.children('li[data-level]').children('ul');
  99. if (type === 'section') {
  100. toc_sub.hide()
  101. .parent().has(li).children('ul').show();
  102. } else {
  103. toc_sub.children('li').children('ul').hide()
  104. .parent().has(li).children('ul').show();
  105. }
  106. li.children('ul').show();
  107. var toc_sub2 = toc_sub.children('li');
  108. if (type === 'section') toc_sub2.children('ul').hide();
  109. summary.children('li[data-level]').find('a')
  110. .on('click.bookdown', function(e) {
  111. if (href === $(this).attr('href').replace(/#.*/, ''))
  112. $(this).parent('li').children('ul').toggle();
  113. });
  114. })();
  115. // add tooltips to the <a>'s that are truncated
  116. $('a').each(function(i, el) {
  117. if (el.offsetWidth >= el.scrollWidth) return;
  118. if (typeof el.title === 'undefined') return;
  119. el.title = el.text;
  120. });
  121. // restore TOC scroll position
  122. var pos = gs.get('tocScrollTop');
  123. if (typeof pos !== 'undefined') summary.scrollTop(pos);
  124. // highlight the TOC item that has same text as the heading in view as scrolling
  125. if (toc && toc.scroll_highlight !== false) (function() {
  126. // scroll the current TOC item into viewport
  127. var ht = $(window).height(), rect = li[0].getBoundingClientRect();
  128. if (rect.top >= ht || rect.top <= 0 || rect.bottom <= 0) {
  129. summary.scrollTop(li[0].offsetTop);
  130. }
  131. // current chapter TOC items
  132. var items = $('a[href^="' + href + '"]').parent('li.chapter'),
  133. m = items.length;
  134. if (m === 0) {
  135. items = summary.find('li.chapter');
  136. m = items.length;
  137. }
  138. if (m === 0) return;
  139. // all section titles on current page
  140. var hs = bookInner.find('.page-inner').find('h1,h2,h3'), n = hs.length,
  141. ts = hs.map(function(i, el) { return $(el).text(); });
  142. if (n === 0) return;
  143. var scrollHandler = function(e) {
  144. var ht = $(window).height();
  145. clearTimeout($.data(this, 'scrollTimer'));
  146. $.data(this, 'scrollTimer', setTimeout(function() {
  147. // find the first visible title in the viewport
  148. for (var i = 0; i < n; i++) {
  149. var rect = hs[i].getBoundingClientRect();
  150. if (rect.top >= 0 && rect.bottom <= ht) break;
  151. }
  152. if (i === n) return;
  153. items.removeClass('active');
  154. for (var j = 0; j < m; j++) {
  155. if (items.eq(j).children('a').first().text() === ts[i]) break;
  156. }
  157. if (j === m) j = 0; // highlight the chapter title
  158. // search bottom-up for a visible TOC item to highlight; if an item is
  159. // hidden, we check if its parent is visible, and so on
  160. while (j > 0 && items.eq(j).is(':hidden')) j--;
  161. items.eq(j).addClass('active');
  162. }, 250));
  163. };
  164. bookInner.on('scroll.bookdown', scrollHandler);
  165. bookBody.on('scroll.bookdown', scrollHandler);
  166. })();
  167. // do not refresh the page if the TOC item points to the current page
  168. $('a[href="' + href + '"]').parent('li.chapter').children('a')
  169. .on('click', function(e) {
  170. bookInner.scrollTop(0);
  171. bookBody.scrollTop(0);
  172. return false;
  173. });
  174. var toolbar = config.toolbar;
  175. if (!toolbar || toolbar.position !== 'static') {
  176. var bookHeader = $('.book-header');
  177. bookBody.addClass('fixed');
  178. bookHeader.addClass('fixed')
  179. .css('background-color', bookBody.css('background-color'))
  180. .on('click.bookdown', function(e) {
  181. // the theme may have changed after user clicks the theme button
  182. bookHeader.css('background-color', bookBody.css('background-color'));
  183. });
  184. }
  185. });
  186. gitbook.events.bind("page.change", function(e) {
  187. // store TOC scroll position
  188. var summary = $('ul.summary');
  189. gs.set('tocScrollTop', summary.scrollTop());
  190. });
  191. var bookBody = $('.book-body'), bookInner = bookBody.find('.body-inner');
  192. var chapterTitle = function() {
  193. return bookInner.find('.page-inner').find('h1,h2').first().text();
  194. };
  195. var saveScrollPos = function(e) {
  196. // save scroll position before page is reloaded
  197. gs.set('bodyScrollTop', {
  198. body: bookBody.scrollTop(),
  199. inner: bookInner.scrollTop(),
  200. focused: document.hasFocus(),
  201. title: chapterTitle()
  202. });
  203. };
  204. $(document).on('servr:reload', saveScrollPos);
  205. // check if the page is loaded in an iframe (e.g. the RStudio preview window)
  206. var inIFrame = function() {
  207. var inIframe = true;
  208. try { inIframe = window.self !== window.top; } catch (e) {}
  209. return inIframe;
  210. };
  211. if (inIFrame()) {
  212. $(window).on('blur unload', saveScrollPos);
  213. }
  214. $(function(e) {
  215. var pos = gs.get('bodyScrollTop');
  216. if (pos) {
  217. if (pos.title === chapterTitle()) {
  218. if (pos.body !== 0) bookBody.scrollTop(pos.body);
  219. if (pos.inner !== 0) bookInner.scrollTop(pos.inner);
  220. }
  221. }
  222. if ((pos && pos.focused) || !inIFrame()) bookInner.find('.page-wrapper').focus();
  223. // clear book body scroll position
  224. gs.remove('bodyScrollTop');
  225. });
  226. });