This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.rna2d.js
201 lines (167 loc) · 5.03 KB
/
jquery.rna2d.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
(function($) {
"use strict";
// Generic controls
$.fn.rna2d = function(opts) {
var plot = opts.plot,
options = {
"jmol": true,
"failed_fetch": Object,
"chains": {
"url": false,
"parser": function(text) { },
},
"interactions": {
"url": false,
"parser": function(text) { },
},
"motifs": {
"url": false,
"parser": function(text) { },
},
"controls": {
"brush": {
"selector": "#brush-toggle",
"callback": Object
},
"motifs": {
"selector": ".motif-toggle",
'callback': Object,
'defaultVisible': ['IL', 'HL', 'J3']
},
"interactions": {
"selector": ".interaction-toggle",
'callback': Object,
'near': true,
'defaultVisible': ['cWW', 'ncWW']
},
"views": {
"selector": ".view-toggle",
'pre': Object,
'post': Object,
'data': 'view'
}
}
};
$.extend(true, options, opts);
plot.selection(this.get(0));
// Attach handlers to each control.
$.each(options.controls, function(type, given) {
given.plot = plot;
$(given.selector).rna2d[type](given);
});
var setter = function(type, parser) {
return function(data, status, xhr) {
// TODO: Handle failure
// Parse given data
try {
var parsed = parser(data);
plot[type](parsed);
} catch(err) {
console.log("Error caught when trying to parse loaded data for " + type);
console.log(err);
}
};
};
// If we are given urls then fire off requests for each element.
var requests = $.map(['chains', 'interactions', 'motifs'], function(type, i) {
if (options[type].url) {
return $.ajax({
url: options[type].url,
success: setter(type, options[type].parser),
dataType: "text"
});
}
return null;
});
// Draw if we have nothing to fetch.
if (requests.length === 0) {
plot();
} else {
// Otherwise wait until all requests are done to attempt to draw.
$.when.apply($, requests).done(plot);
}
};
// Brush controls.
$.fn.rna2d.brush = function(options) {
var plot = options.plot;
$(options.selector).on('click', function(event) {
plot.brush.toggle();
options.callback(event);
});
};
var visibleControl = function(type) {
return function(options) {
var plot = options.plot,
currentlyVisible = {};
$.each(options.defaultVisible, function(_, value) {
currentlyVisible[value] = true;
});
$(options.selector).on('click', function(event) {
var $btn = $(this);
plot[type].visible(function(d, i) {
var klasses = plot[type].classOf()(d, i),
visible = false;
if (currentlyVisible.all) {
return true;
}
$.each(klasses, function(_, value) {
if (currentlyVisible[value]) {
visible = true;
}
});
return visible;
});
function getKlassess(name) {
var data = $btn.data(name);
data = (data ? data.split(',') : []);
if (options.near) {
$.each(data, function(_, k) {
data.push('n' + k);
});
}
return data;
}
var toggleKlasses = getKlassess('toggable');
if (!$btn.hasClass('active')) {
toggleKlasses = toggleKlasses.concat(getKlassess('activate'));
$.each(toggleKlasses, function(_, value) {
$(options.selector)
.filter("[data-toggable=" + value + "]")
.filter(":not(#" + $btn.attr('id') + ")")
.addClass('active');
currentlyVisible[value] = true;
});
} else {
toggleKlasses = toggleKlasses.concat(getKlassess('deactivate'));
$.each(toggleKlasses, function(_, value) {
$(options.selector)
.filter("[data-toggable*=" + value + "]")
.filter(":not(#" + $btn.attr('id') + ")")
.removeClass('active');
currentlyVisible[value] = false;
});
}
plot[type].updateVisibility();
options.callback(event);
});
};
};
// Interaction controls.
$.fn.rna2d.interactions = visibleControl('interactions');
// Motif controls.
$.fn.rna2d.motifs = visibleControl('motifs');
// View controls.
$.fn.rna2d.views = function(options) {
var plot = options.plot;
$(options.selector).on('click', function(event) {
var view = $(this).data(options.data);
if (view === plot.view()) {
return false;
}
options.pre(event);
plot.view(view);
plot();
options.post(event);
});
};
}(jQuery));