4 Gallery
.entryOrder
= [];
5 Gallery
.runningDemo
= null;
8 * Shortcut for creating HTML associated with a parent.
10 Gallery
.create
= function(type
, parent
, className
) {
11 var elem
= document
.createElement(type
);
12 parent
.appendChild(elem
);
14 elem
.className
= className
;
19 Gallery
.start
= function() {
20 Gallery
.toc
= document
.getElementById("toc");
21 Gallery
.workarea
= document
.getElementById("workarea");
22 Gallery
.subtitle
= Gallery
.create("div", Gallery
.workarea
);
23 Gallery
.subtitle
.id
= "subtitle";
24 Gallery
.workareaChild
= Gallery
.create("div", Gallery
.workarea
);
25 Gallery
.demotitle
= document
.getElementById("demotitle");
26 Gallery
.textarea
= new TextArea();
27 Gallery
.textarea
.cancel
.style
.display
= "none";
28 Gallery
.textarea
.width
= 600;
29 Gallery
.textarea
.height
= 400;
31 for (var idx
in Gallery
.entryOrder
) {
32 var id
= Gallery
.entryOrder
[idx
];
33 var demo
= Gallery
.entries
[id
];
35 var div
= Gallery
.create("div", Gallery
.toc
, "entry");
37 var innerDiv
= Gallery
.create("div", div
, "");
39 // Storing extra data in the demo object.
41 demo
.innerDiv
= innerDiv
;
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
);
51 Gallery
.subtitle
.innerHTML
= "";
53 Gallery
.workareaChild
.id
= id
;
54 location
.hash
= "g/" + id
;
56 Gallery
.workareaChild
.innerHTML
='';
58 demo
.setup(Gallery
.workareaChild
);
61 Gallery
.demotitle
.textContent
= demo
.title
? demo
.title
: "";
62 demo
.innerDiv
.className
= "selected";
64 var codeSpan
= Gallery
.create("span", Gallery
.subtitle
);
67 var htmlLink
= Gallery
.create("a", codeSpan
);
68 htmlLink
.textContent
= "HTML";
70 Gallery
.create("span", codeSpan
).textContent
= " | ";
72 var javascriptLink
= Gallery
.create("a", codeSpan
);
73 javascriptLink
.textContent
= "Javascript";
77 Gallery
.create("span", codeSpan
).textContent
= " | ";
78 var cssLink
= Gallery
.create("a", codeSpan
);
79 cssLink
.textContent
= "CSS";
82 var jsFiddleForm
= Gallery
.create("form", codeSpan
);
83 var jsfs
= $(jsFiddleForm
);
84 jsFiddleForm
.method
= "post";
85 jsFiddleForm
.action
= "http://jsfiddle.net/api/post/jquery/1.4/";
87 jsfs
.html("<input type='submit' value='Edit in jsFiddle'/>\n" +
88 "<span style='display:none'>\n" +
89 "<textarea name='resources'>http://dygraphs.com/dygraph-combined.js</textarea>\n" +
90 "<input type='text' name='dtd' value='html 5'/></span>\n");
92 var javascript
= demo
.run
.toString();
93 var html
= Gallery
.workareaChild
.innerHTML
;
95 // tweak for use in jsfiddle
96 javascript
= " $(document).ready(" + javascript
+ "\n);"
97 jQuery('<textarea/>', { name
: 'html' })
102 jQuery('<textarea/>', { name
: 'js' })
108 jQuery('<textarea/>', { name
: 'css' })
128 htmlLink
.onclick
= function() {
129 Gallery
.textarea
.show("HTML", html
);
132 javascriptLink
.onclick
= function() {
133 Gallery
.textarea
.show("Javascript", javascript
);
137 cssLink
.onclick
= function() {
138 Gallery
.textarea
.show("CSS", css
);
142 demo
.run(Gallery
.workareaChild
);
143 Gallery
.runningDemo
= demo
;
147 Gallery
.hashChange();
149 window
.onhashchange
= Gallery
.setHash
;("hashchange", Gallery
.hashChange
, false);
152 var getCss
= function(id
) {
153 for (var i
= 0; i
< document
.styleSheets
.length
; i
++) {
154 var ss
= document
.styleSheets
[i
];
155 if (ss
.title
== "gallery") {
157 var rules
= ss
.rules
|| ss
.cssRules
;
160 for (var j
= 0; j
< rules
.length
; j
++) {
162 var cssText
= rule
.cssText
;
163 var key
= "#workarea #" + id
+ " ";
164 if (cssText
.indexOf(key
) == 0) {
165 arry
.push(cssText
.substr(key
.length
));
168 return arry
.join("\n\n");
170 } catch(e
) { // security error
178 Gallery
.register
= function(id
, demo
) {
179 if (Gallery
.entries
[id
]) {
180 throw id
+ " already registered";
182 Gallery
.entries
[id
] = demo
;
183 Gallery
.entryOrder
.push(id
);
186 Gallery
.hashChange
= function(event
) {
188 if (location
.hash
.indexOf("#g/") == 0) {
189 var id
= location
.hash
.substring(3) + "-toc";
190 var elem
= document
.getElementById(id
);
195 Gallery
.workareaChild
.innerHTML
= "<h3>Select a demo from the gallery on the left</h3>"