-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (64 loc) · 1.86 KB
/
script.js
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
/**
* TITLE:
* @component: Table of Contents
*
* AUTHOR:
* Citizens Advice: Front-end
*
* DESCRIPTION:
* Populating Table of contents:
* TOC will be added if the advisor is logged in
*
* README:
*
*
* API:
*
*
*/
'use strict';
$( document ).ready(function() {
var TableOfContents = (function () {
var tocItems = $('.ref-heading__heading');
var tocItemsID = $('.ref-heading');
var adviceContent = document.getElementsByClassName("articleContent");
var advisorLogin = ($('input[name*="astat"]').val());
var tocArray = [];
var tocList;
function addingToTOC(text,id) {
$.each(text, function (i, item) {
var tempObj = {
id: id[i].id,
content: item.textContent
};
tocArray.push(tempObj);
});
};
function populateTOC() {
var tempItems = '';
addingToTOC(tocItems,tocItemsID);
tocArray.shift(); //removing the first element in the array. As this is the title of the page
tocArray.forEach(function(item){
tempItems += '<li><a href=#' + item.id + '>' + item.content + '</a></li>';
});
return tocList = (
'<div class="well tertiary">'+
'<h5> On this page </h5>'+
'<ul class="ul--2-column">'+
tempItems+
'</ul>'+
'</div>'
);
};
function renderList() {
if (advisorLogin === 'True' ) {
populateTOC();
$(adviceContent).prepend(tocList);
}
}
return{
render: renderList
}
})();
TableOfContents.render(); //This will return a list of all the Titles in a Div
});