Better gallery; separates out the HTML, Javascript and CSS. Although the CSS is missi...
[dygraphs.git] / gallery / gallery.js
index 08299aa..8e0aa71 100644 (file)
@@ -17,11 +17,17 @@ Gallery.create = function(type, parent, className) {
 };
 
 Gallery.start = function() {
-  google.load('visualization', '1', {'packages':['annotatedtimeline']});
   Gallery.toc = document.getElementById("toc");
   Gallery.workarea = document.getElementById("workarea");
+  Gallery.subtitle = Gallery.create("div", Gallery.workarea);
+  Gallery.subtitle.id = "subtitle";
   Gallery.workareaChild = Gallery.create("div", Gallery.workarea);
-  Gallery.workarea.style.visibility = "hidden";
+  Gallery.demotitle = document.getElementById("demotitle");
+  Gallery.textarea = new TextArea();
+  Gallery.textarea.cancel.style.display = "none";
+  Gallery.textarea.width = 600;
+  Gallery.textarea.height = 400;
+
   for (var idx in Gallery.entryOrder) {
     var id = Gallery.entryOrder[idx];
     var demo = Gallery.entries[id];
@@ -42,15 +48,50 @@ Gallery.start = function() {
           Gallery.runningDemo.clean(Gallery.workareaChild);
         }
       }
-      Gallery.workarea.style.visibility = "visible";
-      document.getElementById("title").textContent = demo.title ? demo.title : "";
+      Gallery.subtitle.innerHTML = "";
+
+      Gallery.demotitle.textContent = demo.title ? demo.title : "";
       demo.innerDiv.className = "selected";
+
+      var htmlLink = Gallery.create("a", Gallery.subtitle);
+      htmlLink.textContent = "HTML";
+      htmlLink.href = "#";
+
+      Gallery.subtitle.appendChild(document.createTextNode(" "));
+
+      var javascriptLink = Gallery.create("a", Gallery.subtitle);
+      javascriptLink.textContent = "Javascript";
+      javascriptLink.href = "#";
+
+      Gallery.subtitle.appendChild(document.createTextNode(" "));
+
+      var cssLink = Gallery.create("a", Gallery.subtitle);
+      cssLink.textContent = "CSS";
+      cssLink.href = "#";
+
       Gallery.workareaChild.id = id;
       location.hash = "g/" + id;
+
       Gallery.workareaChild.innerHTML='';
       if (demo.setup) {
         demo.setup(Gallery.workareaChild);
       }
+
+      var html = Gallery.workareaChild.innerHTML;
+
+      htmlLink.onclick = function() {
+        Gallery.textarea.show("HTML", html);
+      };
+
+      javascriptLink.onclick = function() {
+        Gallery.textarea.show("Javascript", demo.run.toString());
+      };
+
+      cssLink.onclick = function() {
+        var css = getCss(demo.title);
+        Gallery.textarea.show("CSS", css);
+      };
+
       demo.run(Gallery.workareaChild);
       Gallery.runningDemo = demo;
     }; }(demo, id);
@@ -61,6 +102,13 @@ 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();
+}
+
 Gallery.register = function(id, demo) {
   if (Gallery.entries[id]) {
     throw id + " already registered";
@@ -75,6 +123,8 @@ Gallery.hashChange = function(event) {
       var id = location.hash.substring(3) + "-toc";
       var elem = document.getElementById(id);
       elem.onclick();
+      return;
     }
   }
-};
\ No newline at end of file
+  Gallery.workareaChild.innerHTML = "<h3>Select a demo from the gallery on the left</h3>"
+};