-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.js
106 lines (80 loc) · 2.6 KB
/
gallery.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
function Gallery() {
this.visuals = [];
this.selectedVisual = null;
var self = this;
// Add a new visualisation to the navigation bar.
this.addVisual = function(vis) {
// Check that the vis object has an id and name.
if (!vis.hasOwnProperty('id')
&& !vis.hasOwnProperty('name')) {
alert('Make sure your visualisation has an id and name!');
}
// Check that the vis object has a unique id.
if (this.findVisIndex(vis.id) != null) {
alert(`Vis '${vis.name}' has a duplicate id: '${vis.id}'`);
}
this.visuals.push(vis);
// Create menu item.
var menuItem = createElement('li', vis.name);
menuItem.addClass('menu-item');
menuItem.id(vis.id);
menuItem.mouseOver(function(e)
{
var el = select('#' + e.srcElement.id);
el.addClass("hover");
})
menuItem.mouseOut(function(e)
{
var el = select('#' + e.srcElement.id);
el.removeClass("hover");
})
menuItem.mouseClicked(function(e)
{
//remove selected class from any other menu-items
var menuItems = selectAll('.menu-item');
for(var i = 0; i < menuItems.length; i++)
{
menuItems[i].removeClass('selected');
}
var el = select('#' + e.srcElement.id);
el.addClass('selected');
self.selectVisual(e.srcElement.id);
})
var visMenu = select('#visuals-menu');
visMenu.child(menuItem);
// Preload data if necessary.
if (vis.hasOwnProperty('preload')) {
vis.preload();
}
};
this.findVisIndex = function(visId) {
// Search through the visualisations looking for one with the id
// matching visId.
for (var i = 0; i < this.visuals.length; i++) {
if (this.visuals[i].id == visId) {
return i;
}
}
// Visualisation not found.
return null;
};
this.selectVisual = function(visId){
var visIndex = this.findVisIndex(visId);
if (visIndex != null) {
// If the current visualisation has a deselect method run it.
if (this.selectedVisual != null
&& this.selectedVisual.hasOwnProperty('destroy')) {
this.selectedVisual.destroy();
}
// Select the visualisation in the gallery.
this.selectedVisual = this.visuals[visIndex];
// Initialise visualisation if necessary.
if (this.selectedVisual.hasOwnProperty('setup')) {
this.selectedVisual.setup();
}
// Enable animation in case it has been paused by the current
// visualisation.
loop();
}
};
}