-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmilestoneVis.js
executable file
·115 lines (97 loc) · 3.29 KB
/
milestoneVis.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
/**
* MilestoneVis object for the Music Milestone Guide visualization.
* @constructor
* @param {object} _parentElement -- the HTML or SVG element to which to attach
* this visualization object
* @param {array} _data -- the array of data
* @param {object} _eventHandler -- the Event Handling object to emit data to
* @param {string} _imageDir -- the directory containing the milestone images
* @returns {MilestoneVis}
*/
MilestoneVis = function(_parentElement, _data, _eventHandler, _imageDir) {
this.parentElement = _parentElement;
this.data = _data;
this.eventHandler = _eventHandler;
this.imageDir = _imageDir;
this.dataIndex = -1;
this.initVis();
};
/**
* Method that sets up the visualization.
*/
MilestoneVis.prototype.initVis = function() {
var that = this;
// bind to the eventHandler
$(this.eventHandler).bind("milestoneChanged",
function(event, year) {
that.onMilestoneChange(year);
}
);
// append an img element to display the milestone images
this.img = this.parentElement.append("img")
.attr("height", 100);
// append a paragraph element to display the data
this.p = this.parentElement.append("p");
};
/**
* Method to update the visualization.
* @param {object} _options -- update option parameters
*/
MilestoneVis.prototype.updateVis = function(_options) {
// if milestone exists, update image and paragraph with data
var milestoneInfo = this.data[this.dataIndex];
if (milestoneInfo) {
this.img.attr("src", this.imageDir + "/" + milestoneInfo.image);
this.p.html("<b>" + milestoneInfo.year + ":</b> " +
milestoneInfo.milestone);
}
// otherwise, clear the image and paragraph and reset index
else {
this.img.remove();
this.img = this.parentElement.append("img")
.attr("height", 100);
this.p.remove();
this.p = this.parentElement.append("p");
this.dataIndex = -1;
}
// trigger milestoneChanged event
$(this.eventHandler).trigger("milestoneChanged",
milestoneInfo ? milestoneInfo.year : undefined);
};
/**
* Gets called by the Event Handler on a "milestoneChanged" event and
* updates the visualization.
* @param {number} year - the year of the milestone currently set
*/
MilestoneVis.prototype.onMilestoneChange = function(year) {
// check if milestone year is in the data and has changed
var milestoneIndex = this.data.map(function(d) { return d.year; })
.indexOf(year);
if (milestoneIndex !== this.dataIndex) {
// set the milestone data index and update the visualization
this.dataIndex = milestoneIndex;
this.updateVis();
}
};
/**
* Returns a function that increments to next milestone in the data.
*/
MilestoneVis.prototype.incrementMilestone = function(that) {
return function() {
if (++that.dataIndex >= that.data.length) {
that.dataIndex = 0;
}
that.updateVis();
};
};
/**
* Returns a function that decrements to previous milestone in the data.
*/
MilestoneVis.prototype.decrementMilestone = function(that) {
return function() {
if (--that.dataIndex < 0) {
that.dataIndex = that.data.length - 1;
}
that.updateVis();
};
};