summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/glossary.js
blob: 3e80f64297db813d467e1287a8be83ac4479d52f (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
define([
    "jQuery",
    "lodash",
    "core/state"
], function($, _, state) {
    var index = null;

    // Use a specific idnex
    var useIndex = function(data) {
        index = data;
    };

    // Load complete index
    var loadIndex = function() {
        return $.getJSON(state.basePath+"/glossary_index.json")
        .then(useIndex);
    };

    // Get index and return a promise
    var getIndex = function() {
        var d = $.Deferred();

        if (index) {
            d.resolve(index);
        } else {
            loadIndex().done(function(){
                d.resolve(index);
            }).fail(d.reject);
        }

        return d.promise();
    }

    var init = function() {
        // Bind click on glossary item
        $(document).on("click", ".book-body .page-wrapper .page-inner .glossary-term", function(e) {
            e.preventDefault();

            location.href = state.basePath+"/GLOSSARY.html#"+$(e.currentTarget).data("glossary-term");
        });
    };

    var replaceTerm = function($el, term) {
        $el.find("p:contains('"+term.name+"')").each( function( i, element ) {
            element = $(element);

            var content = $(element).html();
            content = content.replace(term.name, '<span class="glossary-term" data-glossary-term="' + term.id + '" title="' + term.description + '">' + term.name + '</span>');
            element.html(content);
        });
    };

    var prepare = function() {
        getIndex()
        .done(function() {
            _.each(index, _.partial(replaceTerm, $(".book-body .page-wrapper .page-inner")));
        });
    };

    return {
        init: init,
        prepare: prepare
    };
});