Gallery code links reset the URL anchor. This fixes it.
[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 55
a6c2695d
RK
56 var codeSpan = Gallery.create("span", Gallery.subtitle);
57 codeSpan.id = "code";
58
59 var htmlLink = Gallery.create("a", codeSpan);
e2f62472 60 htmlLink.textContent = "HTML";
e2f62472 61
a6c2695d 62 codeSpan.appendChild(document.createTextNode(" "));
e2f62472 63
a6c2695d 64 var javascriptLink = Gallery.create("a", codeSpan);
e2f62472 65 javascriptLink.textContent = "Javascript";
e2f62472 66
a6c2695d 67 codeSpan.appendChild(document.createTextNode(" "));
e2f62472 68
fa20d2ef
RK
69 var css = getCss(id);
70 if (css) {
a6c2695d 71 var cssLink = Gallery.create("a", codeSpan);
fa20d2ef 72 cssLink.textContent = "CSS";
fa20d2ef 73 }
e2f62472 74
c1f22b5a 75 Gallery.workareaChild.id = id;
d53954c3 76 location.hash = "g/" + id;
e2f62472 77
c1f22b5a
RK
78 Gallery.workareaChild.innerHTML='';
79 if (demo.setup) {
80 demo.setup(Gallery.workareaChild);
81 }
e2f62472 82
a0e87b38 83 var html = Gallery.workareaChild.innerHTML;
e2f62472
RK
84
85 htmlLink.onclick = function() {
86 Gallery.textarea.show("HTML", html);
87 };
88
89 javascriptLink.onclick = function() {
90 Gallery.textarea.show("Javascript", demo.run.toString());
a0e87b38 91 };
e2f62472
RK
92
93 cssLink.onclick = function() {
e2f62472
RK
94 Gallery.textarea.show("CSS", css);
95 };
96
c1f22b5a
RK
97 demo.run(Gallery.workareaChild);
98 Gallery.runningDemo = demo;
99 }; }(demo, id);
100 }
101
102 Gallery.hashChange();
103
104 window.onhashchange = Gallery.setHash;("hashchange", Gallery.hashChange, false);
105};
106
fa20d2ef
RK
107var getCss = function(id) {
108 for (var i = 0; i < document.styleSheets.length; i++) {
109 var ss = document.styleSheets[i];
110 if (ss.title == "gallery") {
111 try {
112 var rules = ss.rules || ss.cssRules;
113 if (rules) {
114 var arry = [];
115 for (var j = 0; j < rules.length; j++) {
116 var rule = rules[j];
117 var cssText = rule.cssText;
118 var key = "#workarea #" + id + " ";
119 if (cssText.indexOf(key) == 0) {
120 arry.push(cssText.substr(key.length));
121 }
122 }
123 return arry.join("\n\n");
124 }
125 } catch(e) { // security error
126 console.log(e);
127 }
128 }
129 }
130 return "not found";
e2f62472
RK
131}
132
c1f22b5a
RK
133Gallery.register = function(id, demo) {
134 if (Gallery.entries[id]) {
135 throw id + " already registered";
136 }
137 Gallery.entries[id] = demo;
138 Gallery.entryOrder.push(id);
139};
140
141Gallery.hashChange = function(event) {
72c8bf99 142 if (location.hash) {
d53954c3
RK
143 if (location.hash.indexOf("#g/") == 0) {
144 var id = location.hash.substring(3) + "-toc";
145 var elem = document.getElementById(id);
146 elem.onclick();
6ced6086 147 return;
d53954c3 148 }
72c8bf99 149 }
6ced6086 150 Gallery.workareaChild.innerHTML = "<h3>Select a demo from the gallery on the left</h3>"
605b6119 151};