OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2013 Google Inc. All Rights Reserved. | |
3 * | |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 * you may not use this file except in compliance with the License. | |
6 * You may obtain a copy of the License at | |
7 * | |
8 * http://www.apache.org/licenses/LICENSE-2.0 | |
9 * | |
10 * Unless required by applicable law or agreed to in writing, software | |
11 * distributed under the License is distributed on an "AS IS" BASIS, | |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 * See the License for the specific language governing permissions and | |
14 * limitations under the License. | |
15 */ | |
16 "use strict"; | |
17 | |
18 // TryItDisplay creates and represents the input and output display. | |
19 function TryItDisplay(containerElement) { | |
20 this.iframe = new Iframe(); | |
21 this.create(containerElement); | |
22 } | |
23 | |
24 // Function that creates the input and output for the TryItYourself object. | |
25 TryItDisplay.prototype.create = function(containerElement) { | |
26 var heading = document.createElement("div"); | |
27 heading.setAttribute("class", "heading"); | |
28 heading.setAttribute("id", "heading") | |
29 heading.innerHTML = "TRY IT YOURSELF"; | |
30 containerElement.appendChild(heading); | |
31 | |
32 var button = document.createElement("button"); | |
33 button.onclick = function(me) { return function() { me.update(); }; }(this); | |
34 button.setAttribute("id", "update"); | |
35 button.innerHTML = "Update"; | |
36 heading.appendChild(button); | |
37 | |
38 var code = document.createElement("div"); | |
39 code.setAttribute("class", "code"); | |
40 code.setAttribute("id", "allCode"); | |
41 containerElement.appendChild(code); | |
42 | |
43 var display = document.createElement("div"); | |
44 display.setAttribute("class", "display"); | |
45 containerElement.appendChild(display); | |
46 display.appendChild(this.iframe.getDom()); | |
47 | |
48 var htmlHeader = document.createElement("div"); | |
49 htmlHeader.setAttribute("class", "label"); | |
50 htmlHeader.setAttribute("id", "htmlLabel") | |
51 htmlHeader.innerHTML = "HTML Code"; | |
52 code.appendChild(htmlHeader); | |
53 | |
54 var htmlCode = document.createElement("textarea"); | |
55 htmlCode.setAttribute("id", "htmlCode"); | |
56 htmlCode.setAttribute("class", "code"); | |
57 code.appendChild(htmlCode); | |
58 | |
59 var cssHeader = document.createElement("div"); | |
60 cssHeader.setAttribute("class", "label"); | |
61 cssHeader.setAttribute("id", "cssLabel") | |
62 cssHeader.innerHTML = "CSS Style"; | |
63 code.appendChild(cssHeader); | |
64 | |
65 var cssCode = document.createElement("textarea"); | |
66 cssCode.setAttribute("id", "cssCode"); | |
67 cssCode.setAttribute("class", "code"); | |
68 code.appendChild(cssCode); | |
69 | |
70 var jsHeader = document.createElement("div"); | |
71 jsHeader.setAttribute("class", "label"); | |
72 jsHeader.setAttribute("id", "jsLabel") | |
73 jsHeader.innerHTML = "Javascript"; | |
74 code.appendChild(jsHeader); | |
75 | |
76 var jsCode = document.createElement("textarea"); | |
77 jsCode.setAttribute("id", "jsCode"); | |
78 jsCode.setAttribute("class", "code"); | |
79 code.appendChild(jsCode); | |
80 | |
81 var heading = document.createElement("div"); | |
82 heading.setAttribute("class", "heading fail"); | |
83 heading.setAttribute("id", "passOrFail") | |
84 heading.innerHTML = "YOU PASSED!"; | |
85 containerElement.appendChild(heading); | |
86 } | |
87 | |
88 TryItDisplay.prototype.setDefaultHtml = function(newHtml) { | |
89 document.getElementById('htmlCode').innerHTML = newHtml ? newHtml : ""; | |
90 } | |
91 | |
92 TryItDisplay.prototype.setDefaultCss = function(newCss) { | |
93 document.getElementById('cssCode').innerHTML = newCss ? newCss : ""; | |
94 } | |
95 | |
96 TryItDisplay.prototype.setDefaultJs = function(newJs) { | |
97 document.getElementById('jsCode').innerHTML = newJs ? newJs : ""; | |
98 } | |
99 | |
100 TryItDisplay.prototype.addCheck = function(object, property, time) { | |
101 this.iframe.checks.push("check(" | |
102 + object + ", " + property + ", " + time + ", 'default');"); | |
103 } | |
104 | |
105 // Set the default end time for the animation clock. | |
106 // Note: this will be overwritten if the user creates an animation longer than | |
107 // the time set here. | |
108 TryItDisplay.prototype.setTime = function(newTime) { | |
109 this.iframe.time = newTime; | |
110 } | |
111 | |
112 // Update takes the information currently in the HTML, CSS, and JS input boxes | |
113 // and displays it in the iframe. | |
114 TryItDisplay.prototype.update = function() { | |
115 // Reload the iframe by resetting the source. | |
116 document.getElementById("display").src = document.getElementById("display").sr
c; | |
117 document.getElementById("display").onload = function(me) { | |
118 return function() { | |
119 me.addCssHtml(); | |
120 me.addAnimScript(); | |
121 }; | |
122 }(this); | |
123 } | |
124 | |
125 // Add the CSS and HTML into the iframe. | |
126 TryItDisplay.prototype.addCssHtml = function() { | |
127 var iframeDoc = this.iframe.doc.contentDocument; | |
128 var htmlVal = document.getElementById("htmlCode").value; | |
129 var cssVal = document.getElementById('cssCode').value | |
130 +"\n" +"#dummy { display: none; }"; | |
131 iframeDoc.getElementsByTagName("body")[0].innerHTML = htmlVal; | |
132 iframeDoc.getElementsByTagName("style")[0].innerHTML = cssVal; | |
133 | |
134 // dummDiv allows the animation time to display properly the first time | |
135 // the page loads. | |
136 var dummyDiv = document.createElement("div"); | |
137 dummyDiv.setAttribute("id", "dummy"); | |
138 dummyDiv.setAttribute("class", "test"); | |
139 iframeDoc.getElementsByTagName("body")[0].appendChild(dummyDiv); | |
140 } | |
141 | |
142 // Add the Javascript value to the iframe. | |
143 TryItDisplay.prototype.addAnimScript = function() { | |
144 var scriptElement = document.createElement('script'); | |
145 var jsVal = this.getJsVal(); | |
146 var iframeDoc = this.iframe.doc.contentDocument; | |
147 | |
148 var scriptDivs = iframeDoc.getElementsByTagName('script'); | |
149 scriptElement.innerHTML = jsVal; | |
150 par = iframeDoc.getElementsByTagName('body')[0]; | |
151 par.appendChild(scriptElement); | |
152 } | |
153 | |
154 // Get the user provided Javascript value, and append additional information | |
155 // to run the tests properly. | |
156 TryItDisplay.prototype.getJsVal = function() { | |
157 var iframeDoc = this.iframe.doc.contentDocument; | |
158 // The animation added to the javascript input allows the animation time to | |
159 // display even when the user does not input an animation. | |
160 var jsVal = "setupTutorialTests(); \n" | |
161 + document.getElementById('jsCode').value + "\n" | |
162 + "new Animation(document.getElementById('dummy'), {left: '100px'}, " | |
163 + this.iframe.time + ");"; | |
164 | |
165 for (var i = 0; i < this.iframe.checks.length; i++) { | |
166 jsVal += "\n" + this.iframe.checks[i]; | |
167 } | |
168 jsVal += " \nrunTests();"; | |
169 return jsVal; | |
170 } | |
171 | |
172 // Function to call once the user passes the tutorial. | |
173 TryItDisplay.pass = function() { | |
174 document.getElementById("passOrFail").className = "heading pass"; | |
175 } | |
176 | |
177 // Function to call if the user fails the tutorial. | |
178 TryItDisplay.fail = function() { | |
179 document.getElementById("passOrFail").className = "heading fail"; | |
180 } | |
181 | |
182 // Constructor for the Iframe object. | |
183 function Iframe() { | |
184 this.doc = document.createElement('iframe'); | |
185 // Checks will store all the tests done on the animations in the iframe. | |
186 // Developers can add to it using the addCheck function. | |
187 this.checks = []; | |
188 // Time stores the defualt animation time. It controls how long the hidden | |
189 // animation runs and therefore the default animation timer. | |
190 this.time = 5; | |
191 | |
192 this.pass = false; | |
193 | |
194 this.doc.setAttribute('id', 'display'); | |
195 this.doc.setAttribute('class', 'display'); | |
196 this.doc.setAttribute('src', 'iframe-contents.html'); | |
197 } | |
198 | |
199 Iframe.prototype.getDom = function() { | |
200 return this.doc; | |
201 } | |
202 | |
203 // Make the solution box toggleable. | |
204 var toggleSolution = function() { | |
205 var element = document.getElementById('toggleText'); | |
206 var elementStyle = getComputedStyle(element, null); | |
207 var label = document.getElementById('hideLabel'); | |
208 | |
209 if (elementStyle.display === 'none') { | |
210 element.style.display = 'block'; | |
211 label.innerHTML = 'Hide Solution'; | |
212 } else if (elementStyle.display === 'block') { | |
213 element.style.display = 'none'; | |
214 label.innerHTML = 'Show Solution'; | |
215 } | |
216 } | |
OLD | NEW |