1 /*jshint loopfunc:true */
5 Gallery
.entryOrder
= [];
6 Gallery
.runningDemo
= null;
9 * Shortcut for creating HTML associated with a parent.
11 Gallery
.create
= function(type
, parent
, className
) {
12 var elem
= document
.createElement(type
);
13 parent
.appendChild(elem
);
15 elem
.className
= className
;
20 Gallery
.start
= function() {
21 Gallery
.toc
= document
.getElementById("toc");
22 Gallery
.workarea
= document
.getElementById("workarea");
23 Gallery
.subtitle
= Gallery
.create("div", Gallery
.workarea
);
24 Gallery
.subtitle
.id
= "subtitle";
25 Gallery
.workareaChild
= Gallery
.create("div", Gallery
.workarea
);
26 Gallery
.demotitle
= document
.getElementById("demotitle");
27 Gallery
.textarea
= new TextArea();
28 Gallery
.textarea
.cancel
.style
.display
= "none";
29 Gallery
.textarea
.width
= 600;
30 Gallery
.textarea
.height
= 400;
32 for (var idx
in Gallery
.entryOrder
) {
33 var id
= Gallery
.entryOrder
[idx
];
34 var demo
= Gallery
.entries
[id
];
36 var div
= Gallery
.create("div", Gallery
.toc
, "entry");
38 var innerDiv
= Gallery
.create("div", div
, "");
40 // Storing extra data in the demo object.
42 demo
.innerDiv
= innerDiv
;
44 innerDiv
.textContent
= demo
.name
;
45 div
.onclick
= function(demo
, id
) { return function() {
46 if (Gallery
.runningDemo
!== null) {
47 Gallery
.runningDemo
.innerDiv
.className
= "";
48 if (Gallery
.runningDemo
.clean
) {
49 Gallery
.runningDemo
.clean(Gallery
.workareaChild
);
52 Gallery
.subtitle
.innerHTML
= "";
54 Gallery
.workareaChild
.id
= id
;
55 location
.hash
= "g/" + id
;
57 Gallery
.workareaChild
.innerHTML
='';
59 demo
.setup(Gallery
.workareaChild
);
62 Gallery
.demotitle
.textContent
= demo
.title
? demo
.title
: "";
63 demo
.innerDiv
.className
= "selected";
65 var codeSpan
= Gallery
.create("span", Gallery
.subtitle
);
68 var htmlLink
= Gallery
.create("a", codeSpan
);
69 htmlLink
.textContent
= "HTML";
71 Gallery
.create("span", codeSpan
).textContent
= " | ";
73 var javascriptLink
= Gallery
.create("a", codeSpan
);
74 javascriptLink
.textContent
= "Javascript";
79 Gallery
.create("span", codeSpan
).textContent
= " | ";
80 cssLink
= Gallery
.create("a", codeSpan
);
81 cssLink
.textContent
= "CSS";
84 var jsFiddleForm
= Gallery
.create("form", codeSpan
);
85 var jsfs
= $(jsFiddleForm
);
86 jsFiddleForm
.method
= "post";
87 jsFiddleForm
.action
= "http://jsfiddle.net/api/post/jquery/1.4/";
89 jsfs
.html("<input type='submit' value='Edit in jsFiddle'/>\n" +
90 "<span style='display:none'>\n" +
91 "<textarea name='resources'>http://dygraphs.com/dygraph-combined.js</textarea>\n" +
92 "<input type='text' name='dtd' value='html 5'/></span>\n");
94 var javascript
= demo
.run
.toString();
95 var html
= Gallery
.workareaChild
.innerHTML
;
97 // tweak for use in jsfiddle
98 javascript
= " $(document).ready(" + javascript
+ "\n);";
99 jQuery('<textarea/>', { name
: 'html' })
104 jQuery('<textarea/>', { name
: 'js' })
110 jQuery('<textarea/>', { name
: 'css' })
130 htmlLink
.onclick
= function() {
131 Gallery
.textarea
.show("HTML", html
);
134 javascriptLink
.onclick
= function() {
135 Gallery
.textarea
.show("Javascript", javascript
);
139 cssLink
.onclick
= function() {
140 Gallery
.textarea
.show("CSS", css
);
144 demo
.run(Gallery
.workareaChild
);
145 Gallery
.runningDemo
= demo
;
149 Gallery
.hashChange();
151 window
.onhashchange
= Gallery
.setHash
;
154 var getCss
= function(id
) {
155 for (var i
= 0; i
< document
.styleSheets
.length
; i
++) {
156 var ss
= document
.styleSheets
[i
];
157 if (ss
.title
== "gallery") {
159 var rules
= ss
.rules
|| ss
.cssRules
;
162 for (var j
= 0; j
< rules
.length
; j
++) {
164 var cssText
= rule
.cssText
;
165 var key
= "#workarea #" + id
+ " ";
166 if (cssText
.indexOf(key
) === 0) {
167 arry
.push(cssText
.substr(key
.length
));
170 return arry
.join("\n\n");
172 } catch(e
) { // security error
180 Gallery
.register
= function(id
, demo
) {
181 if (Gallery
.entries
[id
]) {
182 throw id
+ " already registered";
184 Gallery
.entries
[id
] = demo
;
185 Gallery
.entryOrder
.push(id
);
188 Gallery
.hashChange
= function(event
) {
190 if (location
.hash
.indexOf("#g/") === 0) {
191 var id
= location
.hash
.substring(3) + "-toc";
192 var elem
= document
.getElementById(id
);
197 Gallery
.workareaChild
.innerHTML
= "<h3>Select a demo from the gallery on the left</h3>";