Changes due to code review.
[dygraphs.git] / gallery / gallery.js
CommitLineData
c1f22b5a
RK
1var Gallery = {};
2
3Gallery.entries = {};
4Gallery.entryOrder = [];
5Gallery.runningDemo = null;
6
7/*
8 * Shortcut for creating HTML associated with a parent.
9 */
10Gallery.create = function(type, parent, className) {
11 var elem = document.createElement(type);
12 parent.appendChild(elem);
13 if (className) {
14 elem.className = className;
15 }
16 return elem;
17};
18
19Gallery.start = function() {
c1f22b5a
RK
20 Gallery.toc = document.getElementById("toc");
21 Gallery.workarea = document.getElementById("workarea");
a0e87b38 22 Gallery.subtitle = Gallery.create("div", Gallery.workarea);
2a3953eb 23 Gallery.subtitle.id = "subtitle";
c1f22b5a 24 Gallery.workareaChild = Gallery.create("div", Gallery.workarea);
19d9e7c5 25 Gallery.demotitle = document.getElementById("demotitle");
a0e87b38 26 Gallery.textarea = new TextArea();
2a3953eb
RK
27 Gallery.textarea.width = 600;
28 Gallery.textarea.height = 400;
a0e87b38 29
c1f22b5a
RK
30 for (var idx in Gallery.entryOrder) {
31 var id = Gallery.entryOrder[idx];
32 var demo = Gallery.entries[id];
33
34 var div = Gallery.create("div", Gallery.toc, "entry");
35 div.id = id + "-toc";
36 var innerDiv = Gallery.create("div", div, "");
37
38 // Storing extra data in the demo object.
39 demo.div = div;
40 demo.innerDiv = innerDiv;
41
42 innerDiv.textContent = demo.name;
43 div.onclick = function(demo, id) { return function() {
44 if (Gallery.runningDemo != null) {
45 Gallery.runningDemo.innerDiv.className = "";
46 if (Gallery.runningDemo.clean != null) {
47 Gallery.runningDemo.clean(Gallery.workareaChild);
48 }
49 }
a0e87b38
RK
50 Gallery.subtitle.innerHTML = "";
51 var codeLink = Gallery.create("a", Gallery.subtitle);
52 codeLink.textContent = "code";
53 codeLink.href = "#";
19d9e7c5 54 Gallery.demotitle.textContent = demo.title ? demo.title : "";
c1f22b5a
RK
55 demo.innerDiv.className = "selected";
56 Gallery.workareaChild.id = id;
d53954c3 57 location.hash = "g/" + id;
c1f22b5a
RK
58 Gallery.workareaChild.innerHTML='';
59 if (demo.setup) {
60 demo.setup(Gallery.workareaChild);
61 }
a0e87b38
RK
62 var html = Gallery.workareaChild.innerHTML;
63 codeLink.onclick = function() {
64 var javascript = demo.run.toString();
65 Gallery.textarea.show("Code", "HTML\n\n" + html + "\n\njavascript\n\n" + javascript);
66 };
c1f22b5a
RK
67 demo.run(Gallery.workareaChild);
68 Gallery.runningDemo = demo;
69 }; }(demo, id);
70 }
71
72 Gallery.hashChange();
73
74 window.onhashchange = Gallery.setHash;("hashchange", Gallery.hashChange, false);
75};
76
77Gallery.register = function(id, demo) {
78 if (Gallery.entries[id]) {
79 throw id + " already registered";
80 }
81 Gallery.entries[id] = demo;
82 Gallery.entryOrder.push(id);
83};
84
85Gallery.hashChange = function(event) {
72c8bf99 86 if (location.hash) {
d53954c3
RK
87 if (location.hash.indexOf("#g/") == 0) {
88 var id = location.hash.substring(3) + "-toc";
89 var elem = document.getElementById(id);
90 elem.onclick();
6ced6086 91 return;
d53954c3 92 }
72c8bf99 93 }
6ced6086 94 Gallery.workareaChild.innerHTML = "<h3>Select a demo from the gallery on the left</h3>"
605b6119 95};