MDL-65087 message: add button role to action links on bootstrapbase
[moodle.git] / privacy / export_files / general.js
blobb88786184afcffe7656c16d9275eba6db5a60d8a
1 var currentlyloaded = [];
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     });
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) {
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     }
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');
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     }
64     newscript.type = 'text/javascript';
65     newscript.src = data;
66     newscript.charset = 'utf-8';
67     document.getElementsByTagName("head")[0].appendChild(newscript);
69     // Keep track that this file has already been loaded.
70     currentlyloaded.push(datafile);
73 /**
74  * Checks to see if the datafile has already been loaded onto the page or not.
75  *
76  * @param  {String} datafile The file entry we are checking to see if it is already loaded.
77  * @return {Boolean} True if already loaded otherwise false.
78  */
79 function fileIsLoaded(datafile) {
80     for (var index in currentlyloaded) {
81         if (currentlyloaded[index] == datafile) {
82             return true;
83         }
84     }
85     return false;
88 /**
89  * Adds the loaded data to the main content area of the page.
90  *
91  * @param {Object} data  Data to be added to the main content area of the page.
92  * @param {String} title Title for the content area.
93  */
94 function addFileDataToMainArea(data, title) {
95     var dataarea = window.$('[data-main-content]');
96     while (dataarea[0].firstChild) {
97         dataarea[0].removeChild(dataarea[0].firstChild);
98     }
99     var htmldata = makeList(data);
101     var areatitle = document.createElement('h2');
102     areatitle.innerHTML = title;
103     dataarea[0].appendChild(areatitle);
105     var maincontentlist = document.createElement('div');
106     maincontentlist.innerHTML = htmldata;
107     dataarea[0].appendChild(maincontentlist.firstChild);
111  * Creates an unordered list with the json data provided.
113  * @param  {Object} jsondata The json data to turn into an unordered list.
114  * @return {String} The html string of the unordered list.
115  */
116 function makeList(jsondata) {
117     var html = '<ul>';
118     for (var key in jsondata) {
119         html += '<li>';
120         if (typeof jsondata[key] == 'object') {
121             html += key;
122             html += makeList(jsondata[key]);
123         } else {
124             html += key + ': ' + jsondata[key];
125         }
126         html += '</li>';
127     }
128     html += '</ul>';
129     return html;
132 window.$(document).ready(function() {
133     window.$('[data-var]').click(function(e) {
134         e.preventDefault();
135         e.stopPropagation();
136         handleClick(window.$(this));
137     });