Better gallery; separates out the HTML, Javascript and CSS. Although the CSS is missi...
[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();
e2f62472 27 Gallery.textarea.cancel.style.display = "none";
2a3953eb
RK
28 Gallery.textarea.width = 600;
29 Gallery.textarea.height = 400;
a0e87b38 30
c1f22b5a
RK
31 for (var idx in Gallery.entryOrder) {
32 var id = Gallery.entryOrder[idx];
33 var demo = Gallery.entries[id];
34
35 var div = Gallery.create("div", Gallery.toc, "entry");
36 div.id = id + "-toc";
37 var innerDiv = Gallery.create("div", div, "");
38
39 // Storing extra data in the demo object.
40 demo.div = div;
41 demo.innerDiv = innerDiv;
42
43 innerDiv.textContent = demo.name;
44 div.onclick = function(demo, id) { return function() {
45 if (Gallery.runningDemo != null) {
46 Gallery.runningDemo.innerDiv.className = "";
47 if (Gallery.runningDemo.clean != null) {
48 Gallery.runningDemo.clean(Gallery.workareaChild);
49 }
50 }
a0e87b38 51 Gallery.subtitle.innerHTML = "";
e2f62472 52
19d9e7c5 53 Gallery.demotitle.textContent = demo.title ? demo.title : "";
c1f22b5a 54 demo.innerDiv.className = "selected";
e2f62472
RK
55
56 var htmlLink = Gallery.create("a", Gallery.subtitle);
57 htmlLink.textContent = "HTML";
58 htmlLink.href = "#";
59
60 Gallery.subtitle.appendChild(document.createTextNode(" "));
61
62 var javascriptLink = Gallery.create("a", Gallery.subtitle);
63 javascriptLink.textContent = "Javascript";
64 javascriptLink.href = "#";
65
66 Gallery.subtitle.appendChild(document.createTextNode(" "));
67
68 var cssLink = Gallery.create("a", Gallery.subtitle);
69 cssLink.textContent = "CSS";
70 cssLink.href = "#";
71
c1f22b5a 72 Gallery.workareaChild.id = id;
d53954c3 73 location.hash = "g/" + id;
e2f62472 74
c1f22b5a
RK
75 Gallery.workareaChild.innerHTML='';
76 if (demo.setup) {
77 demo.setup(Gallery.workareaChild);
78 }
e2f62472 79
a0e87b38 80 var html = Gallery.workareaChild.innerHTML;
e2f62472
RK
81
82 htmlLink.onclick = function() {
83 Gallery.textarea.show("HTML", html);
84 };
85
86 javascriptLink.onclick = function() {
87 Gallery.textarea.show("Javascript", demo.run.toString());
a0e87b38 88 };
e2f62472
RK
89
90 cssLink.onclick = function() {
91 var css = getCss(demo.title);
92 Gallery.textarea.show("CSS", css);
93 };
94
c1f22b5a
RK
95 demo.run(Gallery.workareaChild);
96 Gallery.runningDemo = demo;
97 }; }(demo, id);
98 }
99
100 Gallery.hashChange();
101
102 window.onhashchange = Gallery.setHash;("hashchange", Gallery.hashChange, false);
103};
104
e2f62472
RK
105var getCss = function(section) {
106 // Pretty presumptive of me to assume the first style sheet is the correct one.
107 // TODO(konigsberg): find the right style sheet by searching.
108 var ss = document.styleSheets[0];
109 return ss.toString();
110}
111
c1f22b5a
RK
112Gallery.register = function(id, demo) {
113 if (Gallery.entries[id]) {
114 throw id + " already registered";
115 }
116 Gallery.entries[id] = demo;
117 Gallery.entryOrder.push(id);
118};
119
120Gallery.hashChange = function(event) {
72c8bf99 121 if (location.hash) {
d53954c3
RK
122 if (location.hash.indexOf("#g/") == 0) {
123 var id = location.hash.substring(3) + "-toc";
124 var elem = document.getElementById(id);
125 elem.onclick();
6ced6086 126 return;
d53954c3 127 }
72c8bf99 128 }
6ced6086 129 Gallery.workareaChild.innerHTML = "<h3>Select a demo from the gallery on the left</h3>"
605b6119 130};