Merge pull request #136 from klausw-g/non-logscale-x
[dygraphs.git] / gallery / gallery.js
index 8e0aa71..e854a7a 100644 (file)
@@ -53,21 +53,24 @@ Gallery.start = function() {
       Gallery.demotitle.textContent = demo.title ? demo.title : "";
       demo.innerDiv.className = "selected";
 
-      var htmlLink = Gallery.create("a", Gallery.subtitle);
+      var codeSpan = Gallery.create("span", Gallery.subtitle);
+      codeSpan.id = "code";
+
+      var htmlLink = Gallery.create("a", codeSpan);
       htmlLink.textContent = "HTML";
-      htmlLink.href = "#";
 
-      Gallery.subtitle.appendChild(document.createTextNode(" "));
+      codeSpan.appendChild(document.createTextNode(" "));
 
-      var javascriptLink = Gallery.create("a", Gallery.subtitle);
+      var javascriptLink = Gallery.create("a", codeSpan);
       javascriptLink.textContent = "Javascript";
-      javascriptLink.href = "#";
 
-      Gallery.subtitle.appendChild(document.createTextNode(" "));
+      codeSpan.appendChild(document.createTextNode(" "));
 
-      var cssLink = Gallery.create("a", Gallery.subtitle);
-      cssLink.textContent = "CSS";
-      cssLink.href = "#";
+      var css = getCss(id);
+      if (css) {
+        var cssLink = Gallery.create("a", codeSpan);
+        cssLink.textContent = "CSS";
+      }
 
       Gallery.workareaChild.id = id;
       location.hash = "g/" + id;
@@ -87,10 +90,11 @@ Gallery.start = function() {
         Gallery.textarea.show("Javascript", demo.run.toString());
       };
 
-      cssLink.onclick = function() {
-        var css = getCss(demo.title);
-        Gallery.textarea.show("CSS", css);
-      };
+      if (css) {
+        cssLink.onclick = function() {
+          Gallery.textarea.show("CSS", css);
+        };
+      }
 
       demo.run(Gallery.workareaChild);
       Gallery.runningDemo = demo;
@@ -102,11 +106,30 @@ Gallery.start = function() {
   window.onhashchange = Gallery.setHash;("hashchange", Gallery.hashChange, false);
 };
 
-var getCss = function(section) {
-  // Pretty presumptive of me to assume the first style sheet is the correct one.
-  // TODO(konigsberg): find the right style sheet by searching.
-  var ss = document.styleSheets[0];
-  return ss.toString();
+var getCss = function(id) {
+  for (var i = 0; i < document.styleSheets.length; i++) {
+    var ss = document.styleSheets[i];
+    if (ss.title == "gallery") {
+      try {
+        var rules = ss.rules || ss.cssRules;
+        if (rules) {
+          var arry = [];
+          for (var j = 0; j < rules.length; j++) {
+            var rule = rules[j];
+            var cssText = rule.cssText;
+            var key = "#workarea #" + id + " ";
+            if (cssText.indexOf(key) == 0) {
+              arry.push(cssText.substr(key.length));
+            }
+          }
+          return arry.join("\n\n");
+        }
+      } catch(e) { // security error
+        console.log(e);
+      }
+    }
+  }
+  return "not found";
 }
 
 Gallery.register = function(id, demo) {