-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
141 lines (113 loc) · 4.17 KB
/
content.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
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
/**
* Toggle the flyout
*/
var toggleFlyout = function() {
console.log('Clicked');
var boxout = document.getElementById('flyout');
console.log(boxout.getAttribute('style'));
if (boxout.getAttribute('style') === 'margin-left: -9999px; position: absolute' || boxout.offsetParent === null) {
boxout.setAttribute('style', 'margin-left: 0; position: relative');
} else {
boxout.setAttribute('style', 'margin-left: -9999px; position: absolute');
}
}
/**
* Insert the chart with a given title
*/
var insertChart = function(title, dataSets) {
// Get the body element
var bodyElement = document.body;
// Wrapper div for flyout
var flyoutContainer = document.createElement('div');
// Boxout
var boxoutDiv = document.createElement('div');
boxoutDiv.setAttribute('class', 'boxout6Columns left');
boxoutDiv.setAttribute('id', 'flyout');
boxoutDiv.setAttribute('style', 'position: absolute;');
var boxoutTop = document.createElement('div');
boxoutTop.setAttribute('class', 'boxoutNoTop');
boxoutDiv.appendChild(boxoutTop);
var boxoutContent = document.createElement('div');
boxoutContent.setAttribute('class', 'boxoutContent');
boxoutDiv.appendChild(boxoutContent);
var boxoutBottom = document.createElement('div');
boxoutBottom.setAttribute('class', 'boxoutNoBottom');
boxoutDiv.appendChild(boxoutBottom);
// Create a heading
var newText = document.createTextNode('Platform stats for ' + title);
var newElement = document.createElement('h2');
newElement.appendChild(newText);
// Stick the header into the container div
boxoutContent.appendChild(newElement);
// A thing to click on
var label = document.createElement('span');
label.appendChild(document.createTextNode('Click me!'));
label.addEventListener('click', toggleFlyout, false);
flyoutContainer.appendChild(boxoutDiv);
flyoutContainer.appendChild(label);
//Stick the container div into the body of the page
bodyElement.insertBefore(flyoutContainer, bodyElement.firstChild);
var chartElement = document.createElement('canvas');
chartElement.setAttribute('id', 'myChart');
chartElement.setAttribute('width', '400');
chartElement.setAttribute('height', '400');
boxoutContent.appendChild(chartElement);
var context = chartElement.getContext('2d');
new Chart(context).Pie(dataSets);
};
// Setup our AJAX request to the API
var xhr = new XMLHttpRequest();
var data;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// console.log(xhr.responseText);
// Parse the data into JSON
data = JSON.parse(xhr.responseText);
var chartData = {};
var dataSets = [];
dataSets.push(
{
label: "Mobile",
value: parseInt(data.data[0].mobile, 10),
color: "#F38630",
labelColor : 'white',
labelFontSize : '16'
},
{
label: "Tablet",
value: parseInt(data.data[0].tablet, 10),
color : "#E0E4CC",
labelColor : 'white',
labelFontSize : '16'
},
{
label: "Desktop",
value: parseInt(data.data[0].desktop, 10),
color : "#69D2E7",
labelColor : 'white',
labelFontSize : '16'
}
);
// chartData.datasets = dataSets;
insertChart(title, dataSets);
}
};
// Work out what we're looking at and set which report to pull accordingly
var url = window.location.href;
// console.log("Url is " + url);
// Default to stats for all of the site
// var reportDataPoint = 'http://opendatapress.appspot.com/bathweb/platform.json';
// Don't show it for pages that aren't in our list
var reportDataPoint = '';
var title = " all of www.bath.ac.uk";
// Stats for the UG landing page
var ugSiteRegex = /http:\/\/www\.bath\.ac\.uk\/study\/\ug(\/|\/index.html)?$/;
if (url.match(ugSiteRegex)) {
console.log("You're in the UG website");
reportDataPoint = 'http://opendatapress.appspot.com/bathweb/study-ug-landing-page-platform.json';
title = "Study UG";
}
if (reportDataPoint.length > 0) {
xhr.open('get', reportDataPoint);
xhr.send({});
}