Merge pull request #106 from kberg/master
[dygraphs.git] / gallery / gallery.js
1 var Gallery = {};
2
3 Gallery.entries = {};
4 Gallery.entryOrder = [];
5 Gallery.runningDemo = null;
6
7 /*
8 * Shortcut for creating HTML associated with a parent.
9 */
10 Gallery.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
19 Gallery.start = function() {
20 google.load('visualization', '1', {'packages':['annotatedtimeline']});
21 Gallery.toc = document.getElementById("toc");
22 Gallery.workarea = document.getElementById("workarea");
23 Gallery.workareaChild = Gallery.create("div", Gallery.workarea);
24 Gallery.workarea.style.visibility = "hidden";
25 for (var idx in Gallery.entryOrder) {
26 var id = Gallery.entryOrder[idx];
27 var demo = Gallery.entries[id];
28
29 var div = Gallery.create("div", Gallery.toc, "entry");
30 div.id = id + "-toc";
31 var innerDiv = Gallery.create("div", div, "");
32
33 // Storing extra data in the demo object.
34 demo.div = div;
35 demo.innerDiv = innerDiv;
36
37 innerDiv.textContent = demo.name;
38 div.onclick = function(demo, id) { return function() {
39 if (Gallery.runningDemo != null) {
40 Gallery.runningDemo.innerDiv.className = "";
41 if (Gallery.runningDemo.clean != null) {
42 Gallery.runningDemo.clean(Gallery.workareaChild);
43 }
44 }
45 Gallery.workarea.style.visibility = "visible";
46 document.getElementById("title").textContent = demo.title ? demo.title : "";
47 demo.innerDiv.className = "selected";
48 Gallery.workareaChild.id = id;
49 location.hash = "g/" + id;
50 Gallery.workareaChild.innerHTML='';
51 if (demo.setup) {
52 demo.setup(Gallery.workareaChild);
53 }
54 demo.run(Gallery.workareaChild);
55 Gallery.runningDemo = demo;
56 }; }(demo, id);
57 }
58
59 Gallery.hashChange();
60
61 window.onhashchange = Gallery.setHash;("hashchange", Gallery.hashChange, false);
62 };
63
64 Gallery.register = function(id, demo) {
65 if (Gallery.entries[id]) {
66 throw id + " already registered";
67 }
68 Gallery.entries[id] = demo;
69 Gallery.entryOrder.push(id);
70 };
71
72 Gallery.hashChange = function(event) {
73 if (location.hash) {
74 if (location.hash.indexOf("#g/") == 0) {
75 var id = location.hash.substring(3) + "-toc";
76 var elem = document.getElementById(id);
77 elem.onclick();
78 }
79 }
80 };