summaryrefslogtreecommitdiff
path: root/mkdocs-material/src/assets/javascripts/application.js
blob: d1ffbec30ec5b39ce047feb48276d784ea3395df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * Copyright (c) 2016 Martin Donath <martin.donath@squidfunk.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/* Hey, there's your missing semicolon, lunr.js! */
;

/* Truncate a string after the given number of characters */
String.prototype.truncate = function(n) {
  if (this.length > n) {
    while (this[n] != ' ' && --n > 0);
    return this.substring(0, n) + '&hellip;';
  }
  return this;
}

/* Wrap an HTMLElement around each element in an HTMLElement array */
HTMLElement.prototype.wrap = function (elms) {
  if (!elms.length) elms = [elms];
  for (var i = elms.length - 1; i >= 0; i--) {
    var child = (i > 0) ? this.cloneNode(true) : this;
    var el = elms[i];

    /* Cache current parent and sibling */
    var parent  = el.parentNode,
        sibling = el.nextSibling;

    /* Wrap the element and remove it from its current parent */
    child.appendChild(el);
    if (sibling) {
      parent.insertBefore(child, sibling);
    } else {
      parent.appendChild(child);
    }
  }
}

/* ----------------------------------------------------------------------------
 * Application logic
 * ------------------------------------------------------------------------- */

/* Initialize application upon DOM ready */
document.addEventListener('DOMContentLoaded', function() {
  'use strict';

  /* Test for iOS */
  Modernizr.addTest('ios', function() {
    return !!navigator.userAgent.match(/(iPad|iPhone|iPod)/g);
  });

  /* Test for web application context */
  Modernizr.addTest('standalone', function() {
    return !!navigator.standalone;
  });

  /* Attack FastClick to mitigate 300ms delay on touch devices */
  FastClick.attach(document.body);

  /* Grab relevant elements from the DOM */
  var toggle  = document.getElementById('toggle-search'),
      reset   = document.getElementById('reset-search'),
      drawer  = document.querySelector('.drawer'),
      anchors = document.querySelectorAll('.anchor'),
      search  = document.querySelector('.search .field'),
      query   = document.querySelector('.query'),
      meta    = document.querySelector('.results .meta');

/* ----------------------------------------------------------------------------
 * Initialize drawer
 * ------------------------------------------------------------------------- */

  /* Automatically close drawer when anchors are clicked */
  Array.prototype.forEach.call(anchors, function(item) {
    item.querySelector('a').addEventListener('click', function() {
      document.getElementById('toggle-drawer').checked = false;
      document.body.classList.remove('toggle-drawer');
    });
  });

  /* Align drawer to window offset */
  var pageYOffsetLast = window.pageYOffset;
  var align = function() {
    var boundary = window.pageYOffset + window.innerHeight;
    var clipping = Math.max(0, window.innerHeight - drawer.offsetHeight);

    /* Ensure alignment with footer if at end of document */
    if (boundary > document.body.clientHeight - (96 - clipping)) {
      if (drawer.style.position != 'absolute') {
        drawer.style.position = 'absolute';
        drawer.style.top      = null;
        drawer.style.bottom   = 0;
      }

    /* Pin drawer to top, if window is higher than drawer */
    } else if (drawer.offsetHeight < window.innerHeight) {
      if (drawer.style.position != 'fixed') {
        drawer.style.position = 'fixed';
        drawer.style.top = 0;
        drawer.style.bottom = null;
      }

    /* If the drawer is not pinned, check if we need to pin it */
    } else if (drawer.style.position != 'fixed') {

      /* Pin drawer to bottom of window */
      if (boundary > drawer.offsetTop + drawer.offsetHeight) {
        drawer.style.position = 'fixed';
        drawer.style.top = null;
        drawer.style.bottom = -96 + 'px';

      /* Pin drawer to top of window */
      } else if (window.pageYOffset < drawer.offsetTop) {
        drawer.style.position = 'fixed';
        drawer.style.top = 0;
        drawer.style.bottom = null;
      }

    /* If the drawer is pinned, check if we have to unpin it */
    } else {
      if (window.pageYOffset > pageYOffsetLast) {
        if (drawer.style.top) {
          drawer.style.position = 'absolute';
          drawer.style.top = Math.max(0, pageYOffsetLast) + 'px';
          drawer.style.bottom = null;
        }
      } else if (drawer.style.bottom) {
        drawer.style.position = 'absolute';
        drawer.style.top = (boundary - drawer.offsetHeight) + 'px';
        drawer.style.bottom = null;
      }
    }

    /* Update last offset (mitigiate negative offsets in Safari) */
    pageYOffsetLast = Math.max(0, window.pageYOffset);
  }

  /* Check for media query events */
  var check = function() {
    var main = document.querySelector('.main');
    window.removeEventListener('scroll', align);

    /* Reset drawer position when entering collapsed mode */
    if (matchMedia("only screen and (max-width: 959px)").matches) {
      drawer.style.position = null;
      drawer.style.top      = null;
      drawer.style.bottom   = null;

    /* Check if the scroll handler needs to be registered */
    } else if (drawer.offsetHeight + 96 < main.offsetHeight) {
      window.addEventListener('scroll', align);
      align();
    }
  }

  /* Register resize handler and fire once */
  if (!Modernizr.ios) {
    window.addEventListener('resize', check);
    check();
  }

/* ----------------------------------------------------------------------------
 * Initialize search index
 * ------------------------------------------------------------------------- */

  /* Initialize index */
  var initialize = function() {
    pegasus(base_url + '/mkdocs/search_index.json').then(

      /* Request successful, we got the index */
      function(data, xhr) {

        /* Create index */
        var index = lunr(function() {
          this.field('title', { boost: 10 });
          this.field('text');
          this.ref('location');
        });

        /* Index articles */
        var articles = {};
        data.docs.map(function(article) {
          article.location = base_url + article.location;
          articles[article.location] = article;
          index.add(article);
        });

        /* Register keyhandler to execute search on key up */
        query.addEventListener('keyup', function() {
          var container = document.querySelector('.results .list');
          while (container.firstChild)
            container.removeChild(container.firstChild);

          /* Abort, if the query is empty */
          var bar = document.querySelector('.bar.search');
          if (!query.value.length) {
            while (meta.firstChild)
              meta.removeChild(meta.firstChild);

            /* Restore state */
            bar.classList.remove('non-empty');
            return;
          }

          /* Show reset button */
          bar.classList.add('non-empty');

          /* Execute search */
          var results = index.search(query.value);
          results.map(function(result) {
            var article = articles[result.ref];

            /* Create article container */
            var teaser = document.createElement('article');
            teaser.classList.add('result');

            /* Create title element */
            var title = document.createElement('h1');
            title.innerHTML = article.title;
            teaser.appendChild(title);

            // /* Create text element */
            // var text = document.createElement('p');
            // text.innerHTML = article.text.truncate(140);
            // teaser.appendChild(text);

            /* Create a link referring to the article */
            var link = document.createElement('a');
            link.href = article.location;
            link.appendChild(teaser);

            /* Create url element */
            var url = document.createElement('span');
            url.innerHTML = link.href.split('#')[0];
            teaser.appendChild(url);

            /* Close search and jump to anchor when on same page */
            var parts = link.href.split('#');
            if (parts[0] == document.location.href.split('#')[0]) {
              link.addEventListener('click', function(e) {
                document.body.classList.remove('toggle-search');
                document.body.classList.remove('locked');
                toggle.checked = false;

                /* Don't catch anchors if the search doesn't cover the page */
                if (matchMedia('only screen and (min-width: 960px)').matches)
                  return;

                /* Prevent default to intercept scroll-to behaviour and
                   stop propagation, as this interferes with the link-lock in
                   the web application context, which opens all internal links
                   inside the same context */
                e.preventDefault();
                e.stopPropagation();

                /* Scroll to chapter, if given */
                if (parts.length != 1) {
                  var chapter = document.getElementById(parts[1]);
                  if (chapter) {

                    /* Scroll to chapter, but wait for 100ms to prevent flashes
                       on iOS. A short timeout seems to do the trick */
                    setTimeout(function() {
                      chapter.scrollIntoView && chapter.scrollIntoView() ||
                        window.scrollTo(0, chapter.offsetTop);
                    }, 100);
                  }
                }
              });
            }

            /* Add article to search results */
            container.appendChild(link);
          });

          /* Show number of search results */
          var number = document.createElement('strong');
          number.innerHTML = results.length + ' search result'
            + (results.length != 1 ? 's' : '');

          /* Update number */
          while (meta.firstChild)
            meta.removeChild(meta.firstChild);
          meta.appendChild(number);
        });
      },

      /* Handle error */
      function(data, xhr) {
        console.error(data, xhr.status);
      }
    );

    /* Remove listener, as we only have to initialize once */
    toggle.removeEventListener('click', initialize);
  };

  /* Initialize on first click */
  toggle.addEventListener('click', initialize);

/* ----------------------------------------------------------------------------
 * Initialize search modal
 * ------------------------------------------------------------------------- */

  /* Intercept click on search mode toggle */
  var offset = 0;
  toggle.addEventListener('click', function(e) {
    var list = document.body.classList;
    var lock = !matchMedia('only screen and (min-width: 960px)').matches;

    /* Exiting search mode */
    if (list.contains('locked')) {
      list.remove('locked');

      /* Scroll to former position, but wait for 100ms to prevent flashes
         on iOS. A short timeout seems to do the trick */
      if (lock)
        setTimeout(function() {
          window.scrollTo(0, offset);
        }, 100);

    /* Entering search mode */
    } else {
      offset = window.scrollY;

      /* First timeout: scroll to top after transition, to omit flickering */
      if (lock)
        setTimeout(function(){
          window.scrollTo(0, 0);
        }, 400);

      /* Second timeout: Lock body after finishing transition and scrolling to
         top and focus input field. Sadly, the focus event is not dispatched
         on iOS Safari and there's nothing we can do about it. */
      setTimeout(function() {

        /* This additional check is necessary to handle fast subsequent clicks
           on the toggle and the timeout to lock the body must be cancelled */
        if (this.checked) {
          if (lock)
            list.add('locked');
          setTimeout(function() {
            query.focus();
          }, 200);
        }
      }.bind(this), 450);
    }
  });

  /* Dispatch input focus on touch of search section */
  search.addEventListener('touchstart', function() {
    query.focus();
  });

  /* Exit search mode when pressing ESC */
  window.addEventListener('keyup', function(e) {
    var code = e.keyCode || e.which;
    if (code == 27) {
      query.blur();

      /* Exit locked state */
      document.body.classList.remove('toggle-search');
      document.body.classList.remove('locked');
      toggle.checked = false;
    }
  });

  /* Delete search results upon click on "x" */
  var empty = document.getElementById('reset-search');
  empty.addEventListener('click', function() {
    var container = document.querySelector('.results .list');
    while (container.firstChild)
      container.removeChild(container.firstChild);

    /* Hide search button */
    var bar = document.querySelector('.bar.search');
    bar.classList.remove('non-empty');

    /* Reset number of search results */
    meta.innerHTML = '';

    /* Empty search input */
    query.value = '';
    query.focus();
  });

/* ----------------------------------------------------------------------------
 * Initialize scroll spy
 * ------------------------------------------------------------------------- */

  /* Retrieve vertical offset of article chapters */
  var chapters = document.querySelectorAll('h2');
  chapters = Array.prototype.map.call(chapters, function(item) {
    return item.offsetTop;
  });

  /* Update currently active chapter, if the new chapter is two thirds
     into the viewport - account for iOS web application context */
  var visible = null;
  document.addEventListener('scroll', function() {
    var offset = window.scrollY + (window.innerHeight / 3),
        active = chapters.length - 1;
    for (var c = 0; c < active; c++)
      if (offset < chapters[c + 1])
        active = c;

    /* Update anchors, if a new chapter became visible */
    if (active != visible) {
      visible = active;
      Array.prototype.forEach.call(anchors, function(item, index) {
        var link = item.querySelector('a');
        if (index != visible || link.classList.add('current'))
          link.classList.remove('current');
      });
    }
  });

/* ----------------------------------------------------------------------------
 * Fix syntax highlighting
 * ------------------------------------------------------------------------- */

  /* Fix highlighting for function calls */
  var functions = document.querySelectorAll('.n + .p');
  Array.prototype.forEach.call(functions, function(item) {
    var text = item.innerText || item.textContent;
    if (text && text[0] == '(')
      item.previousSibling.classList.add('f');
  });

/* ----------------------------------------------------------------------------
 * Progressive structure enhancement
 * ------------------------------------------------------------------------- */

  /* Wrap all data tables */
  var tables = document.querySelectorAll('table');
  Array.prototype.forEach.call(tables, function(item) {
    var wrapper = document.createElement('div');
    wrapper.classList.add('data');
    wrapper.wrap(item);
  });

/* ----------------------------------------------------------------------------
 * Fix overflow scrolling on iOS
 * ------------------------------------------------------------------------- */

  /* Force 1px scroll offset to trigger overflow scrolling */
  if (Modernizr.ios) {
    var scrollable = document.querySelectorAll(
      '.scrollable, .standalone .article');
    Array.prototype.forEach.call(scrollable, function(item) {
      item.addEventListener('touchstart', function() {
        var top = this.scrollTop;

        /* We're at the top of the container */
        if (top == 0) {
          this.scrollTop = 1;

        /* We're at the bottom of the container */
        } else if (top + this.offsetHeight == this.scrollHeight) {
          this.scrollTop = top - 1;
        }
      });
    });
  }

  /* Prevent scrolling on project, overlay and header */
  var prevented = document.querySelectorAll('.project, .overlay, .header');
  Array.prototype.forEach.call(prevented, function(item) {
    item.addEventListener('touchmove', function(e) {
      e.preventDefault();
    });
  });

/* ----------------------------------------------------------------------------
 * Fallback for browsers that don't support :checked
 * ------------------------------------------------------------------------- */

  /* Set representative class on body for active toggle */
  var toggles = document.querySelectorAll('.toggle');
  Array.prototype.forEach.call(toggles, function(item) {
    item.addEventListener('click', function() {
      document.body.classList.toggle(this.id);
    });
  });

/* ----------------------------------------------------------------------------
 * Initialize GitHub star button
 * ------------------------------------------------------------------------- */

  /* Get Stars for current repository */
  if (repo_id) {
    pegasus('https://api.github.com/repos/' + repo_id).then(

      /* Request successful, we got the stars */
      function(data, xhr) {
        var count = data.stargazers_count;
        if (count > 10000)
          count = (count / 1000).toFixed(0) + 'k';
        else if (count > 1000)
          count = (count / 1000).toFixed(1) + 'k';

        /* Set number of stars */
        var stars = document.querySelector('.repo-stars .count');
        stars.innerHTML = count;
      },

      /* Handle error */
      function(data, xhr) {
        console.error(data, xhr.status);
      }
    );
  }
});