PageSourceSearch

https://introtcs.org/public/js/plugin-bookdown.js

js introtcs.org collected 2026-10-03 19:39:08 UTC 8,539 bytes, 246 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.