aboutsummaryrefslogtreecommitdiffstats
path: root/site/static/js/docs.js
blob: 13095259a77c4ed38ff97931d414281348d47070 (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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

// Platforms we have logos for and their formatted titles. Used for
// converting "mark" elements to logos
var platforms = {
    'android': 'Android',
    'ios': 'iOS',
    'windows': 'Windows',
    'ubuntu': 'Ubuntu',
    'blackberry': 'Blackberry',
    'fireos': 'FireOS',
    'firefoxos': 'FirefoxOS',
    'webos': 'webOS',
    'osx': 'OS X',
    'browser': 'Browser'
};

$(document).ready(function () {

    var HEADER_OFFSET  = 56; // in pixels
    var TOC_TOP_OFFSET = HEADER_OFFSET + 55;

    // if this page's ToC entry can be found, scroll the ToC to it
    var thisPageEntry = $(".this-page");
    if (thisPageEntry.length > 0) {
        $(".site-toc-container").scrollTop(thisPageEntry.first().offset().top - TOC_TOP_OFFSET);
    }

    function slugifyLikeGitHub(originalText) {

        var text = originalText;

        // convert to lowercase
        text = text.toLowerCase();

        // replace spaces with dashes
        text = text.replace(/ /g, '-');

        // remove unaccepted characters
        // NOTE:
        //      a better regex would have been /[^\d\s\w-_]/ug, but the 'u' flag
        //      (Unicode) is not supported in some browsers, and we support
        //      many languages that use Unicode characters
        text = text.replace(/[\[\]\(\)\;\:\=\+\?\!\.\,\{\}\\\/\>\<]/g, '');

        // trim remaining whitespace
        text = text.trim();

        return text;
    }

    function getIdForHeading(heading) {
        if (heading.id) {
            return heading.id;
        } else if (heading.name) {
            return heading.name;
        } else {
            return slugifyLikeGitHub(heading.innerText);
        }
    }

    function permalinkTo(id) {
        var anchor       = document.createElement("a");
        anchor.className = "header-link";
        anchor.href      = "#" + id;
        anchor.innerHTML = "<i class=\"glyphicon glyphicon-link\"></i>";
        return anchor;
    }

    function anchorFor(id) {
        var anchor       = document.createElement("a");
        anchor.className = "fragment-anchor";
        anchor.id        = id;
        return anchor;
    }

    // go through all headings in the content and add some links
    $('#page-toc-source h1, #page-toc-source h2, #page-toc-source h3, #page-toc-source h4, #page-toc-source h5, #page-toc-source h6')
    .each(function (i, heading) {

        headingId = getIdForHeading(heading);

        // add an anchor for linking to the heading
        // NOTE:
        //      we could have set the ID on the heading itself but we didn't
        //      because the <a> has some extra CSS that floats it above the
        //      heading and makes it easier to see when linked
        $(heading).before(anchorFor(headingId))

        // add a permalink to all but the first heading
        if (i > 0) {
            $(heading).append(permalinkTo(headingId));
        }
    });

    // Table of Contents
    $('#page-toc').toc({
        'selectors':         'h1,h2', // elements to use as headings
        'container':         '#page-toc-source', // element to find all selectors in
        'prefix':            '', // prefix for anchor tags and class names
        'onHighlight':       function(el) {}, // called when a new section is highlighted
        'highlightOnScroll': true, // add class to heading that is currently in focus
        'highlightOffset':   100, // offset to trigger the next headline
        'anchorName':        function(i, heading, prefix) { // custom function for anchor name
            return getIdForHeading(heading);
        },
        'headerText': function(i, heading, $heading) { // custom function building the header-item text
            return $heading.text();
        },
        'itemClass': function(i, heading, $heading, prefix) { // custom function for item class

            // don't assign any special classes to the toc entry itself
            return '';
        }
    });

    $('mark').each(function(index, element) {
        var platform = element.innerText.toLowerCase();
        platform = platform.replace(/ /g, '');
        if (platforms[platform]) {
            $(element).addClass('logo');
            $(element).addClass(platform);
            $(element).attr('title', platforms[platform]);
            element.innerText = '';
        }
    });
});