Skip to content

Commit 8d20764

Browse files
committed
Isolated dashboard app JavaScript
This patch moves the `dashboard` app's JavaScript inclusion from the project's `require.js` structure to the app itself. This was achieved with a new block in the base template called `javascript`. The rest of the site gets the default JavaScript includes, and the `dashboard` app overrides the block to include the JavaScript specific to it. The `dashboard` app benefits by not knowing about JavaScript related to the rest of the site and vice versa. None of the actual `dashboard` JavaScript code was changed. It was only adapted to the new structure. - Added `javascript` block to base template - Added `dashboard_javascript` block to `dashboard` base template - Included `dashboard` JavaScript directly in `dashboard` templates - Removed `dashboard`-related entries in `main.js` - Remove references to `jquery.flot.min.js` from base template - This library is only used by the `dashboard` app - Removed `dashboard-*` CSS classes only used by `require.js` - Removed `require.js` structure in `dashboard` JavaScript files - Simplified structure of `utils.js`
1 parent eb44f49 commit 8d20764

File tree

8 files changed

+183
-186
lines changed

8 files changed

+183
-186
lines changed

dashboard/templates/base_dashboard.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends "base.html" %}
22
{% load i18n %}
3+
{% load static %}
34

45
{% block sectionid %}dashboard{% endblock %}
56

@@ -8,3 +9,11 @@
89
{% block header %}
910
<h1>Development <em>dashboard</em></h1>
1011
{% endblock %}
12+
13+
{% block javascript %}
14+
<script src="{% static "js/lib/jquery.min.js" %}"></script>
15+
<script src="{% static "js/lib/jquery.flot.min.js" %}"></script>
16+
<script src="{% static "js/dashboard/utils.js" %}"></script>
17+
18+
{% block dashboard_javascript %}{% endblock %}
19+
{% endblock %}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
{% extends "base_dashboard.html" %}
22
{% load i18n %}
3+
{% load static %}
34

45
{% block title %}{{ metric }} | {{ block.super }}{% endblock %}
56

67
{% block content %}
7-
<div class="dashboard-detail">
8+
<div>
89
<h2><a href="{{ metric.link }}">{{ metric }}</a></h2>
910
<div class="graph-wrapper">
1011
<div id="graph" class="graph" data-path="{% url "metric-list" host "dashboard" %}" data-metric="{{ metric.slug }}"></div>
1112
</div>
1213
<a class="link-readmore back-link" href="{% url "dashboard-index" host "dashboard" %}">{% translate "All metrics" %}</a>
1314
</div>
1415
{% endblock %}
16+
17+
{% block dashboard_javascript %}
18+
<script src="{% static "js/dashboard/detail.js" %}"></script>
19+
{% endblock %}

dashboard/templates/dashboard/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{% extends "base_dashboard.html" %}
22
{% load i18n %}
3+
{% load static %}
34

45
{% block content %}
5-
<div class="dashboard-index">
6+
<div>
67
{% for report in data %}
78
{% ifchanged report.metric.category %}
89
{% if report.metric.category %}<h2>{{ report.metric.category }}</h2>{% endif %}
@@ -23,3 +24,7 @@ <h3><a href="{{ report.metric.link }}">{{ report.metric.name }}</a></h3>
2324
</p>
2425
</div>
2526
{% endblock %}
27+
28+
{% block dashboard_javascript %}
29+
<script src="{% static "js/dashboard/index.js" %}"></script>
30+
{% endblock %}
Lines changed: 73 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,83 @@
1-
define('dashboard/detail', [
2-
'jquery',
3-
'jquery.flot',
4-
'dashboard/utils',
5-
], function ($, plot, utils) {
6-
$(function () {
7-
var element = $('#graph');
8-
var url = element.data('path') + element.data('metric') + '.json?days=365';
9-
var hover = {
10-
show: function (x, y, message) {
11-
$('<div id="hover">')
12-
.html(message)
13-
.css({ top: y, left: x })
14-
.appendTo('body')
15-
.show();
16-
},
17-
hide: function () {
18-
$('#hover').remove();
1+
$(function () {
2+
var element = $('#graph');
3+
var url = element.data('path') + element.data('metric') + '.json?days=365';
4+
var hover = {
5+
show: function (x, y, message) {
6+
$('<div id="hover">')
7+
.html(message)
8+
.css({ top: y, left: x })
9+
.appendTo('body')
10+
.show();
11+
},
12+
hide: function () {
13+
$('#hover').remove();
14+
},
15+
};
16+
17+
$.getJSON(url, function (response) {
18+
for (var i = 0; i < response.data.length; i++) {
19+
response.data[i][0] = response.data[i][0] * 1000;
20+
}
21+
var options = {
22+
xaxis: {
23+
mode: 'time',
24+
tickColor: 'rgba(0,0,0,0)',
25+
minTickSize: [1, 'day'],
1926
},
27+
yaxis: { min: 0, ticks: 4 },
28+
grid: { borderWidth: 0, hoverable: true, color: '#0C3C26' },
29+
colors: ['#0C4B33'],
2030
};
21-
22-
$.getJSON(url, function (response) {
23-
for (var i = 0; i < response.data.length; i++) {
24-
response.data[i][0] = response.data[i][0] * 1000;
25-
}
26-
var options = {
27-
xaxis: {
28-
mode: 'time',
29-
tickColor: 'rgba(0,0,0,0)',
30-
minTickSize: [1, 'day'],
31-
},
32-
yaxis: { min: 0, ticks: 4 },
33-
grid: { borderWidth: 0, hoverable: true, color: '#0C3C26' },
34-
colors: ['#0C4B33'],
31+
if (response.period == 'daily') {
32+
options.bars = {
33+
show: true,
34+
barWidth: 22 * 60 * 60 * 1000,
35+
align: 'center',
3536
};
36-
if (response.period == 'daily') {
37-
options.bars = {
38-
show: true,
39-
barWidth: 22 * 60 * 60 * 1000,
40-
align: 'center',
41-
};
42-
} else if (response.period == 'weekly') {
43-
options.bars = {
44-
show: true,
45-
barWidth: 22 * 60 * 60 * 7 * 1000,
46-
align: 'center',
47-
};
48-
}
49-
var plot = $.plot(element, [response.data], options);
50-
51-
var format_message = function (timestamp, measurement) {
52-
var unit = measurement == 1 ? response.unit : response.unit_plural;
53-
return (
54-
utils.formatTimestamp(timestamp, response.period) +
55-
'<br>' +
56-
measurement +
57-
' ' +
58-
unit
59-
);
37+
} else if (response.period == 'weekly') {
38+
options.bars = {
39+
show: true,
40+
barWidth: 22 * 60 * 60 * 7 * 1000,
41+
align: 'center',
6042
};
43+
}
44+
var plot = $.plot(element, [response.data], options);
6145

62-
var previousPoint = null;
63-
element.bind('plothover', function (event, pos, item) {
64-
if (item) {
65-
if (previousPoint != item.dataIndex) {
66-
previousPoint = item.dataIndex;
67-
hover.hide();
68-
var x, y;
69-
var message = format_message.apply(null, item.datapoint);
70-
if (response.period == 'instant') {
71-
x = item.pageX + 10;
72-
y = item.pageY + 10;
73-
} else {
74-
// I'd like this hover to be centered over the bar. This
75-
// simple math sorta works, but it assumes a *lot* about
76-
// the plot and basically won't scale. Grr.
77-
x = item.pageX - 40;
78-
y = item.pageY - 50;
79-
}
80-
hover.show(x, y, message);
81-
}
82-
} else {
46+
var format_message = function (timestamp, measurement) {
47+
var unit = measurement == 1 ? response.unit : response.unit_plural;
48+
return (
49+
formatTimestamp(timestamp, response.period) +
50+
'<br>' +
51+
measurement +
52+
' ' +
53+
unit
54+
);
55+
};
56+
57+
var previousPoint = null;
58+
element.bind('plothover', function (event, pos, item) {
59+
if (item) {
60+
if (previousPoint != item.dataIndex) {
61+
previousPoint = item.dataIndex;
8362
hover.hide();
84-
previousPoint = null;
63+
var x, y;
64+
var message = format_message.apply(null, item.datapoint);
65+
if (response.period == 'instant') {
66+
x = item.pageX + 10;
67+
y = item.pageY + 10;
68+
} else {
69+
// I'd like this hover to be centered over the bar. This
70+
// simple math sorta works, but it assumes a *lot* about
71+
// the plot and basically won't scale. Grr.
72+
x = item.pageX - 40;
73+
y = item.pageY - 50;
74+
}
75+
hover.show(x, y, message);
8576
}
86-
});
77+
} else {
78+
hover.hide();
79+
previousPoint = null;
80+
}
8781
});
8882
});
8983
});

djangoproject/static/js/dashboard/index.js

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,41 @@
1-
define('dashboard/index', [
2-
'jquery',
3-
'jquery.flot',
4-
'dashboard/utils',
5-
], function ($, flot, utils) {
6-
$(function () {
7-
$('.metric .sparkline').each(function (index, elem) {
8-
var element = $(elem);
9-
var valueElement = element.parent().find('.value a');
10-
var timestampElement = element.parent().find('.timestamp');
11-
var originalValue = valueElement.html();
12-
var green = '#93D7B7';
1+
$(function () {
2+
$('.metric .sparkline').each(function (index, elem) {
3+
var element = $(elem);
4+
var valueElement = element.parent().find('.value a');
5+
var timestampElement = element.parent().find('.timestamp');
6+
var originalValue = valueElement.html();
7+
var green = '#93D7B7';
138

14-
var url = element.data('path') + element.data('metric') + '.json';
15-
$.getJSON(url, function (response) {
16-
response.data = utils.convertSecondsToMilliseconds(response.data);
17-
$.plot(element, [response.data], {
18-
xaxis: { show: false, mode: 'time' },
19-
yaxis: { show: false, min: 0 },
20-
grid: { borderWidth: 0, hoverable: true },
21-
colors: [green],
22-
bars: {
23-
show: true,
24-
barWidth:
25-
response.period == 'daily'
26-
? 24 * 60 * 60 * 1000
27-
: 24 * 60 * 60 * 7 * 1000,
28-
fillColor: green,
29-
lineWidth: 1,
30-
align: 'center',
31-
},
32-
});
9+
var url = element.data('path') + element.data('metric') + '.json';
10+
$.getJSON(url, function (response) {
11+
response.data = convertSecondsToMilliseconds(response.data);
12+
$.plot(element, [response.data], {
13+
xaxis: { show: false, mode: 'time' },
14+
yaxis: { show: false, min: 0 },
15+
grid: { borderWidth: 0, hoverable: true },
16+
colors: [green],
17+
bars: {
18+
show: true,
19+
barWidth:
20+
response.period == 'daily'
21+
? 24 * 60 * 60 * 1000
22+
: 24 * 60 * 60 * 7 * 1000,
23+
fillColor: green,
24+
lineWidth: 1,
25+
align: 'center',
26+
},
27+
});
3328

34-
element.bind('plothover', function (event, pos, item) {
35-
if (item) {
36-
valueElement.html(item.datapoint[1]);
37-
timestampElement.html(
38-
utils.formatTimestamp(item.datapoint[0], response.period),
39-
);
40-
} else {
41-
valueElement.html(originalValue);
42-
timestampElement.html('&nbsp;');
43-
}
44-
});
29+
element.bind('plothover', function (event, pos, item) {
30+
if (item) {
31+
valueElement.html(item.datapoint[1]);
32+
timestampElement.html(
33+
formatTimestamp(item.datapoint[0], response.period),
34+
);
35+
} else {
36+
valueElement.html(originalValue);
37+
timestampElement.html('&nbsp;');
38+
}
4539
});
4640
});
4741
});
Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
define('dashboard/utils', ['jquery'], function ($) {
2-
return {
3-
formatTimestamp: function formatTimestamp(timestamp, period) {
4-
var d = new Date(timestamp);
5-
if (period == 'instant') {
6-
return $.plot.formatDate(d, '%b %d, %h:%M %p');
7-
} else if (period == 'daily') {
8-
return $.plot.formatDate(d, '%b %d');
9-
} else if (period == 'weekly') {
10-
// A bit more complicated than the above: the timestamp is in the
11-
// middle of the week, so we have to bracket the date. This is
12-
// something of a fudge here, but it works well enough.
13-
var start = new Date(d.getTime() - 3 * 24 * 60 * 60 * 1000);
14-
var end = new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000);
15-
return (
16-
$.plot.formatDate(start, '%b %d') +
17-
' - ' +
18-
$.plot.formatDate(end, '%b %d')
19-
);
20-
}
21-
},
22-
convertSecondsToMilliseconds: function convertSecondsToMilliseconds(data) {
23-
for (var i = 0; i < data.length; i++) {
24-
data[i][0] = data[i][0] * 1000;
25-
}
26-
return data;
27-
},
28-
};
29-
});
1+
function formatTimestamp(timestamp, period) {
2+
var d = new Date(timestamp);
3+
if (period == 'instant') {
4+
return $.plot.formatDate(d, '%b %d, %h:%M %p');
5+
} else if (period == 'daily') {
6+
return $.plot.formatDate(d, '%b %d');
7+
} else if (period == 'weekly') {
8+
// A bit more complicated than the above: the timestamp is in the
9+
// middle of the week, so we have to bracket the date. This is
10+
// something of a fudge here, but it works well enough.
11+
var start = new Date(d.getTime() - 3 * 24 * 60 * 60 * 1000);
12+
var end = new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000);
13+
return (
14+
$.plot.formatDate(start, '%b %d') +
15+
' - ' +
16+
$.plot.formatDate(end, '%b %d')
17+
);
18+
}
19+
}
20+
21+
function convertSecondsToMilliseconds(data) {
22+
for (var i = 0; i < data.length; i++) {
23+
data[i][0] = data[i][0] * 1000;
24+
}
25+
return data;
26+
}

djangoproject/static/js/main.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ define(function () {
1212
mods.push('mod/list-collapsing');
1313
}
1414

15-
if (hasClass('dashboard-index')) {
16-
mods.push('dashboard/index');
17-
}
18-
19-
if (hasClass('dashboard-detail')) {
20-
mods.push('dashboard/detail');
21-
}
22-
2315
if (hasClass('stripe-donation')) {
2416
mods.push('mod/stripe-donation');
2517
}

0 commit comments

Comments
 (0)