-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathview.js
217 lines (188 loc) · 6.23 KB
/
view.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*global VIS, d3, window */
"use strict";
var view = (function () {
var that = { },
my = { },
dfb,
loading,
calculating,
error,
warning,
tooltip,
frame,
update_annotations,
weight_tds,
setup_plot,
append_plot,
scroll_top,
scroll_origin;
dfb = function (controller) {
if (controller !== undefined) {
my.dfb = controller;
}
return my.dfb;
};
that.dfb = dfb;
loading = function (flag) {
d3.select("#working_icon").classed("invisible", !flag);
};
that.loading = loading;
calculating = function (sel, flag) {
d3.select("#working_icon").classed("invisible", !flag);
d3.selectAll(sel + " .calc").classed("hidden", !flag);
d3.selectAll(sel + " .calc-done").classed("hidden", flag);
};
that.calculating = calculating;
error = function (msg) {
d3.select("div#error")
.classed("hidden", false)
.append("p").text(msg);
this.loading(false);
};
that.error = error;
warning = function (msg) {
d3.select("div#warning")
.classed("hidden", false)
.append("p").text(msg);
};
that.warning = warning;
// singleton tooltip
tooltip = function () {
var tt = my.tooltip;
// does it already exist?
if (tt) {
return tt;
}
// otherwise, initialize
tt = {
offset: VIS.tooltip.offset
};
tt.div = d3.select("body").append("div")
.attr("id", "tooltip")
.classed("bar_tooltip", true);
tt.container = d3.select("body").node();
tt.div.append("p");
tt.update_pos = function () {
var mouse_pos = d3.mouse(this.container);
this.div.style({
left: (mouse_pos[0] + this.offset.x) + 'px',
top: (mouse_pos[1] + this.offset.y) + 'px',
position: "absolute"
});
};
tt.text = function (text) {
this.div.select("p").text(text);
};
tt.show = function () {
this.div.classed("hidden", false);
};
tt.hide = function () {
this.div.classed("hidden", true);
};
my.tooltip = tt;
return tt;
};
that.tooltip = tooltip;
// render global framing elements of the view
frame = function (p) {
var m0 = p.models.find(function (m) {
return m.id === p.id;
});
if (p.title) {
d3.selectAll(".model_title")
.html(p.title);
}
// multiple models: data dropdown menu
if (p.models && p.models.length > 1) {
d3.select("ul#data_dropdown").selectAll("li")
.data(p.models)
.enter().append("li").append("a")
.attr("href", "#")
.text(function (m) {
return m.name || m.id;
})
.on("click", function (m) {
d3.event.preventDefault();
d3.select("#data_name")
.text(m.name || m.id);
my.dfb.switch_model(m.id);
});
d3.select("#data_name").text(m0.name || m0.id);
d3.select("li#nav_data").classed("hidden", false);
}
};
that.frame = frame;
update_annotations = function (v, param) {
var j, cs = ["." + v];
d3.selectAll(".annote").classed("hidden", true);
for (j = 0; j < param.length; j += 1) {
// we have to sanitize param to use in selectors:
cs.push(cs[j] + "_" + String(param[j]).replace(/\W/g, "_"));
}
cs.forEach(function (c) {
d3.selectAll(".annote" + c).classed("hidden", false);
});
};
that.update_annotations = update_annotations;
// add columns to a table corresponding to weights
// p.sel: table row selection with data bound
// p.enter: entering table rows
// Three columns may be added if the appropriate function is supplied in
// the parameters:
// p.w: function giving proportional size of horizontal bar
// p.frac: function giving string for display of proportion
// p.raw: function giving string for display of raw weight
weight_tds = function (p) {
if (p.w) {
p.enter.append("td").classed("weight", true)
.append("div")
.classed("proportion", true)
.append("span")
.classed("proportion", true)
.html(" ");
p.sel.select("td.weight div.proportion")
.style("margin-left", function (d) {
return d3.format(".1%")(1 - p.w(d));
});
}
if (p.frac) {
p.enter.append("td")
.classed("td-right", true)
.classed("weight-percent", true);
p.sel.select("td.weight-percent")
.text(p.frac);
}
if (p.raw) {
p.enter.append("td")
.classed("td-right", true)
.classed("weight-raw", true);
p.sel.select("td.weight-raw")
.text(p.raw);
}
};
that.weight_tds = weight_tds;
setup_plot = function (sel, spec) {
// mbostock margin convention
// http://bl.ocks.org/mbostock/3019563
sel.attr("width", spec.w + spec.m.left + spec.m.right)
.attr("height", spec.h + spec.m.top + spec.m.bottom);
// g element passes on xform to all contained elements
return sel.select("g")
.attr("transform",
"translate(" + spec.m.left + "," + spec.m.top + ")");
};
that.setup_plot = setup_plot;
append_plot = function (sel) {
return sel.append("svg").append("g");
};
that.append_plot = append_plot;
scroll_top = function () {
window.scrollTo(window.scrollX, 0);
};
that.scroll_top = scroll_top;
scroll_origin = function () {
window.scrollTo(0, 0);
};
that.scroll_origin = scroll_origin;
return that;
}());