Gallery failure when CSS does not exist.
[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 92
304c1fbb
RK
93 if (css) {
94 cssLink.onclick = function() {
95 Gallery.textarea.show("CSS", css);
96 };
97 }
e2f62472 98
c1f22b5a
RK
99 demo.run(Gallery.workareaChild);
100 Gallery.runningDemo = demo;
101 }; }(demo, id);
102 }
103
104 Gallery.hashChange();
105
106 window.onhashchange = Gallery.setHash;("hashchange", Gallery.hashChange, false);
107};
108
fa20d2ef
RK
109var getCss = function(id) {
110 for (var i = 0; i < document.styleSheets.length; i++) {
111 var ss = document.styleSheets[i];
112 if (ss.title == "gallery") {
113 try {
114 var rules = ss.rules || ss.cssRules;
115 if (rules) {
116 var arry = [];
117 for (var j = 0; j < rules.length; j++) {
118 var rule = rules[j];
119 var cssText = rule.cssText;
120 var key = "#workarea #" + id + " ";
121 if (cssText.indexOf(key) == 0) {
122 arry.push(cssText.substr(key.length));
123 }
124 }
125 return arry.join("\n\n");
126 }
127 } catch(e) { // security error
128 console.log(e);
129 }
130 }
131 }
132 return "not found";
e2f62472
RK
133}
134
c1f22b5a
RK
135Gallery.register = function(id, demo) {
136 if (Gallery.entries[id]) {
137 throw id + " already registered";
138 }
139 Gallery.entries[id] = demo;
140 Gallery.entryOrder.push(id);
141};
142
143Gallery.hashChange = function(event) {
72c8bf99 144 if (location.hash) {
d53954c3
RK
145 if (location.hash.indexOf("#g/") == 0) {
146 var id = location.hash.substring(3) + "-toc";
147 var elem = document.getElementById(id);
148 elem.onclick();
6ced6086 149 return;
d53954c3 150 }
72c8bf99 151 }
6ced6086 152 Gallery.workareaChild.innerHTML = "<h3>Select a demo from the gallery on the left</h3>"
605b6119 153};