Rewrite HTML generation.
[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);
c1f22b5a
RK
23 Gallery.workareaChild = Gallery.create("div", Gallery.workarea);
24 Gallery.workarea.style.visibility = "hidden";
a0e87b38
RK
25 Gallery.title = document.getElementById("title");
26 Gallery.textarea = new TextArea();
27
c1f22b5a
RK
28 for (var idx in Gallery.entryOrder) {
29 var id = Gallery.entryOrder[idx];
30 var demo = Gallery.entries[id];
31
32 var div = Gallery.create("div", Gallery.toc, "entry");
33 div.id = id + "-toc";
34 var innerDiv = Gallery.create("div", div, "");
35
36 // Storing extra data in the demo object.
37 demo.div = div;
38 demo.innerDiv = innerDiv;
39
40 innerDiv.textContent = demo.name;
41 div.onclick = function(demo, id) { return function() {
42 if (Gallery.runningDemo != null) {
43 Gallery.runningDemo.innerDiv.className = "";
44 if (Gallery.runningDemo.clean != null) {
45 Gallery.runningDemo.clean(Gallery.workareaChild);
46 }
47 }
a0e87b38
RK
48 Gallery.subtitle.innerHTML = "";
49 var codeLink = Gallery.create("a", Gallery.subtitle);
50 codeLink.textContent = "code";
51 codeLink.href = "#";
c1f22b5a 52 Gallery.workarea.style.visibility = "visible";
a0e87b38 53 Gallery.title.textContent = demo.title ? demo.title : "";
c1f22b5a
RK
54 demo.innerDiv.className = "selected";
55 Gallery.workareaChild.id = id;
d53954c3 56 location.hash = "g/" + id;
c1f22b5a
RK
57 Gallery.workareaChild.innerHTML='';
58 if (demo.setup) {
59 demo.setup(Gallery.workareaChild);
60 }
a0e87b38
RK
61 var html = Gallery.workareaChild.innerHTML;
62 codeLink.onclick = function() {
63 var javascript = demo.run.toString();
64 Gallery.textarea.show("Code", "HTML\n\n" + html + "\n\njavascript\n\n" + javascript);
65 };
c1f22b5a
RK
66 demo.run(Gallery.workareaChild);
67 Gallery.runningDemo = demo;
68 }; }(demo, id);
69 }
70
71 Gallery.hashChange();
72
73 window.onhashchange = Gallery.setHash;("hashchange", Gallery.hashChange, false);
74};
75
76Gallery.register = function(id, demo) {
77 if (Gallery.entries[id]) {
78 throw id + " already registered";
79 }
80 Gallery.entries[id] = demo;
81 Gallery.entryOrder.push(id);
82};
83
84Gallery.hashChange = function(event) {
72c8bf99 85 if (location.hash) {
d53954c3
RK
86 if (location.hash.indexOf("#g/") == 0) {
87 var id = location.hash.substring(3) + "-toc";
88 var elem = document.getElementById(id);
89 elem.onclick();
90 }
72c8bf99 91 }
605b6119 92};