-
Notifications
You must be signed in to change notification settings - Fork 0
/
crisis_common.js
124 lines (110 loc) · 4.32 KB
/
crisis_common.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
/**
* Current Crisis Evaluator (runs on a timer from header.html).
*/
var firstTime = true;
function currentCrisisEvaluator(){
// console.log("... running again");
var changed = false;
//HANDLE INITIAL CONDITIONS {{
if (!localStorage.getItem('current_crisis')){
localStorage.setItem('current_crisis', 'haiyan');
}
if (!window.crisis_select.value || firstTime){
console.log("--> updating new window.crisis_select variable to reflect localStorage: "+localStorage.getItem('current_crisis'));
window.crisis_select.value= localStorage.getItem('current_crisis');
firstTime = false;
changed = true;
}
//}}
else if (localStorage.getItem('current_crisis') !== window.crisis_select.value){
console.log("--> updating localStorage 'current_crisis' variable to reflect new selection: "+window.crisis_select.value);
localStorage.setItem('current_crisis', window.crisis_select.value);
changed = true;
}
if (changed){
/*NOTICE: Changing class to allow a class listener to take action */
window.crisis_select.className = window.crisis_select.value;
}
}
/**
* Add a prototype method to String (careful to escape special chars used by regex)
* @param find
* @param replace
* @returns {string}
*/
String.prototype.replaceAll = function (find, replace) {
var str = this;
return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};
/**
* Be notified when a registered element has a class change.
* This is useful to be notified when window.crisis_select has a value change.
* @param elemId
* @param callback
*/
function addClassNameListener(elemId, callback) {
var elem = document.getElementById(elemId);
var lastClassName = elem.className;
window.setInterval( function() {
var className = elem.className;
if (className !== lastClassName) {
callback();
lastClassName = className;
}
},10);
}
/**
* Friendly print for numbers, considering decimals.
* @param x
* @returns {string}
*/
function numberWithCommas(x) {
if (isNaN(x)) return x;
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
/**
* Crisis summary changer
*/
function resetSummary(summary){
/* Triangles */
// revert to original color on other triangles and shrink
d3.selectAll('.storyTriangle')
.transition()
.style('fill','#919191')
.attr('d',d3.svg.symbol().type('triangle-down').size(256));
/* Get content correct */
// overview page
if (document.getElementById("overview") != undefined && document.getElementById("tab_1_compared").className === "content-tab active") {
d3.select("#crisisTitle").html('<h3>Country Interest In Crises</h3>');
d3.select('#crisisStory').html("Countries providing top-ten results for each of the crises can be compared. Consistently interested countries include the US, UK, India, and Canada. Single crisis countries include Philippines, Ukraine, Malaysia, South Africa, and Kenya. Prominent countries like Australia, France and Italy are consistently interested, even when outside the top-ten.");
// hide selector
$('#crisis_selector').hide();
// timeline page
} else if (document.getElementById("timeline") != undefined && document.getElementById("tab_1_compared").className === "content-tab active") {
d3.select("#crisisTitle").html('<h3>Relative Interest In Crises</h3>');
d3.select('#crisisStory').html('Percentage of change in crisis coverage can be compared from month to month to determine increased, same, or decreased media attention aggregated across all Google results.');
// hide selector
$('#crisis_selector').hide();
} else {
// show selector
$('#crisis_selector').show();
// add title
d3.select('#crisisTitle').data(summary)
.html(function (d) {
return '<h3>' + d.title + '</h3>';
});
// add summary
d3.select('#crisisStory').data(summary)
.html(function (d) {
return '<p>' + d.content + '</p>';
});
}
}
/**
* Show crisis selector on non-auto pages
*/
function showSelector(){
$('#crisis_selector').show();
}