Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
var currentlyloaded = [];
2
 
3
/**
4
 * Loads the data for the clicked navigation item.
5
 *
6
 * @param  {Object} clickednode The jquery object for the clicked node.
7
 */
8
function handleClick(clickednode) {
9
    var contextcrumb = '';
10
    var parentnodes = clickednode.parents('li');
11
    for (var i = parentnodes.length; i >= 0; i--) {
12
        var treenodes = window.$(parentnodes[i]);
13
        if (treenodes.hasClass('item')) {
14
            if (contextcrumb == '') {
15
                contextcrumb = treenodes[0].innerText;
16
            } else {
17
                contextcrumb = contextcrumb + ' | ' + treenodes[0].innerText;
18
            }
19
        } else if (treenodes.hasClass('menu-item')) {
20
            if (contextcrumb == '') {
21
                contextcrumb = treenodes[0].firstChild.textContent;
22
            } else {
23
                contextcrumb = contextcrumb + ' | ' + treenodes[0].firstChild.textContent;
24
            }
25
        }
26
    }
27
    var datafile = clickednode.attr('data-var');
28
    loadContent(datafile, function() {
29
        addFileDataToMainArea(window[datafile], contextcrumb);
30
    });
31
}
32
 
33
/**
34
 * Load content to be displayed.
35
 *
36
 * @param  {String}   datafile The json data to be displayed.
37
 * @param  {Function} callback The function to run after loading the json file.
38
 */
39
function loadContent(datafile, callback) {
40
 
41
    // Check to see if this file has already been loaded. If so just go straight to the callback.
42
    if (fileIsLoaded(datafile)) {
43
        callback();
44
        return;
45
    }
46
 
47
    // This (user_data_index) is defined in data_index.js
48
    var data = window.user_data_index[datafile];
49
    var newscript = document.createElement('script');
50
 
51
    if (newscript.readyState) {
52
        newscript.onreadystatechange = function() {
53
            if (this.readyState == 'complete' || this.readyState == 'loaded') {
54
                this.onreadystatechange = null;
55
                callback();
56
            }
57
        };
58
    } else {
59
        newscript.onload = function() {
60
            callback();
61
        };
62
    }
63
 
64
    newscript.type = 'text/javascript';
65
    var dataParts = data.split('/');
66
    dataParts.forEach(function(part, index) {
67
        this[index] = encodeURIComponent(part);
68
    }, dataParts);
69
    newscript.src = dataParts.join('/');
70
    newscript.charset = 'utf-8';
71
    document.getElementsByTagName("head")[0].appendChild(newscript);
72
 
73
    // Keep track that this file has already been loaded.
74
    currentlyloaded.push(datafile);
75
}
76
 
77
/**
78
 * Checks to see if the datafile has already been loaded onto the page or not.
79
 *
80
 * @param  {String} datafile The file entry we are checking to see if it is already loaded.
81
 * @return {Boolean} True if already loaded otherwise false.
82
 */
83
function fileIsLoaded(datafile) {
84
    for (var index in currentlyloaded) {
85
        if (currentlyloaded[index] == datafile) {
86
            return true;
87
        }
88
    }
89
    return false;
90
}
91
 
92
/**
93
 * Adds the loaded data to the main content area of the page.
94
 *
95
 * @param {Object} data  Data to be added to the main content area of the page.
96
 * @param {String} title Title for the content area.
97
 */
98
function addFileDataToMainArea(data, title) {
99
    var dataarea = window.$('[data-main-content]');
100
    while (dataarea[0].firstChild) {
101
        dataarea[0].removeChild(dataarea[0].firstChild);
102
    }
103
    var htmldata = makeList(data);
104
 
105
    var areatitle = document.createElement('h2');
106
    areatitle.innerHTML = title;
107
    dataarea[0].appendChild(areatitle);
108
 
109
    var maincontentlist = document.createElement('div');
110
    maincontentlist.innerHTML = htmldata;
111
    dataarea[0].appendChild(maincontentlist.firstChild);
112
}
113
 
114
/**
115
 * Creates an unordered list with the json data provided.
116
 *
117
 * @param  {Object} jsondata The json data to turn into an unordered list.
118
 * @return {String} The html string of the unordered list.
119
 */
120
function makeList(jsondata) {
121
    var html = '<ul>';
122
    for (var key in jsondata) {
123
        html += '<li>';
124
        if (typeof jsondata[key] == 'object') {
125
            html += key;
126
            html += makeList(jsondata[key]);
127
        } else {
128
            html += key + ': ' + jsondata[key];
129
        }
130
        html += '</li>';
131
    }
132
    html += '</ul>';
133
    return html;
134
}
135
 
136
window.$(document).ready(function() {
137
    window.$('[data-var]').click(function(e) {
138
        e.preventDefault();
139
        e.stopPropagation();
140
        handleClick(window.$(this));
141
    });
142
});