Skip to content

Commit 85a24d1

Browse files
authored
Merge pull request #30 from biojs/visualization-frontend
Visualization frontend
2 parents 7b22ec7 + 1967592 commit 85a24d1

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

main/templates/main/visualizations.html

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{% comment %}
2+
!!ADD VERSION TO WZRD!!
3+
24

35
Template for rendering visualizations.
46

@@ -13,9 +15,79 @@
1315
6. no_browserify : {{sniper_data.no_browserify}}
1416
{% endcomment %}
1517

18+
<!DOCTYPE html>
1619
<html>
17-
<body>
18-
Hello World!
19-
You requested {{snippet.name}} snippet of {{component.name}}. Wzrd URL is {{sniper_data.wzrd_url}}.
20-
</body>
20+
<head>
21+
<meta charset="UTF-8">
22+
{% for dependency in css_dependencies %}
23+
<link rel="stylesheet" type="text/css" href="{{dependency.css_url}}" />
24+
{% endfor %}
25+
{% for dependency in js_dependencies %}
26+
<script src="{{dependency.js_url}}"></script>
27+
{% endfor %}
28+
<script src="{{sniper_data.wzrd_url}}"></script>
29+
</head>
30+
<body>
31+
<div id="placeholderDiv" style="display: none">
32+
{{snippet_script}}
33+
</div>
34+
</div>
35+
<div id='snippetDiv'></div>
36+
</body>
37+
<script>
38+
//
39+
String.prototype.replaceAll = function(search, replacement) {
40+
var target = this;
41+
return target.replace(new RegExp(search, 'g'), replacement);
42+
};
43+
44+
// find main element variable
45+
findMainVar = function(content) {
46+
var tags = ["yourDiv", "mainDiv", "rootDiv", "masterDiv", "biojsDiv"];
47+
var defaultDiv = tags[0];
48+
tags.forEach(function(tag) {
49+
if (content.indexOf(tag) >= 0) {
50+
defaultDiv = tag;
51+
}
52+
});
53+
return defaultDiv;
54+
};
55+
56+
// translate relative paths
57+
translateRelative = function(body, baseLocal, path) {
58+
if (body.indexOf("./") >= 0) {
59+
var htmlUrl = baseLocal + "/" + path + "/";
60+
body = body.replace(/\.\.\//g, baseLocal + "/");
61+
body = body.replace(/\.\//g, htmlUrl);
62+
}
63+
return body;
64+
};
65+
66+
67+
var script = document.getElementById("placeholderDiv").innerHTML;
68+
script = script.replaceAll("&lt;", "<");
69+
script = script.replaceAll("&gt;", ">");
70+
71+
var githubURL = "{{component.github_url}}";
72+
var lastCommitHash = "{{component.latest_commit_hash}}";
73+
74+
var div = findMainVar(script);
75+
var mainDiv = "var "+div+" = document.getElementById('snippetDiv');"
76+
77+
// get rawgit base URL
78+
var rawgitURL = githubURL.replace("github.com", "cdn.rawgit.com");
79+
if(rawgitURL.substr(rawgitURL.length - 1) === '/') {
80+
rawgitURL = rawgitURL + '' + lastCommitHash;
81+
} else {
82+
rawgitURL = rawgitURL + '/' + lastCommitHash;
83+
}
84+
85+
// change relative paths in script
86+
script = translateRelative(script, rawgitURL, "snippets");
87+
88+
// complete script and evaluate the script
89+
script = mainDiv + script;
90+
eval(script);
91+
92+
</script>
2193
</html>

main/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def render_visualization(request, url_name, visualization_name):
6767
snippet = Snippet.objects.get(sniperData=sniper_data, name=visualization_name)
6868
data = urllib.urlopen(snippet.url).read()
6969
context = {
70-
'component' : component,
70+
'component' : DetailComponentSerializer(component).data,
7171
'js_dependencies' : js_dependencies,
7272
'css_dependencies' : css_dependencies,
7373
'snippet' : snippet,

0 commit comments

Comments
 (0)