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 /* | |
19 * Get the current topic of tutorial the user is in | |
20 * E.g. If the user is currently at basic-animation.html | |
21 * currentSection will equals to basic-animation. | |
22 * This variable is used to determine the name of a file. | |
23 * E.g. To get the name of the exercise 1 of basic-animation | |
24 * would add currentSection to '-exercise-' and the number | |
25 * in the <li> being clicked | |
26 */ | |
27 var currentSection = window.location.href.split('/').pop(); | |
28 currentSection = currentSection.split('.')[0]; | |
29 | |
30 // waits until all DOM elements are ready to perform | |
31 $(document.body).ready(function() { | |
32 | |
33 // if one of the side menu is clicked | |
34 // update page content without refreshing the whole page. | |
35 $('.sideMenu li').click(function(e) { | |
36 // get the exercise number from the <li> being clicked. | |
37 // e.g. 'Exercise 1' would return 1 | |
38 // Though 'Basic Info' would return 'Info'. | |
39 exerciseNum = $(this).html().split(' ')[1]; | |
40 | |
41 // determines if the input string is actually a number | |
42 // if it is not then load the info page of the section | |
43 if (parseInt(exerciseNum) !== exerciseNum && isNaN(exerciseNum)) { | |
44 $('.content').load(currentSection + '.html' + ' .content', function() { | |
45 $(this).children().unwrap(); | |
46 }); | |
47 } else { | |
48 var url = currentSection + '-exercise-' + exerciseNum + '.html'; | |
49 // checks if a file/link exist before adding contents | |
50 // into page | |
51 // after contents are loaded, load editor | |
52 $.ajax({ | |
53 url: url, | |
54 type: 'HEAD', | |
55 success: function() { | |
56 $('.content').load(url + ' .content', function() { | |
57 $(this).children().unwrap(); | |
58 loadEditor(); | |
59 }); | |
60 } | |
61 }); | |
62 } | |
63 }); | |
64 }); | |
65 | |
66 // This generate an editor object and put it into | |
67 // a div called 'tryIt' as well as passing in | |
68 // default HTML and CSS codes as animation divs | |
69 var loadEditor = function() { | |
70 var html = '', currentId = 'a'; | |
71 | |
72 // Get the number of animation divs needed | |
73 var animNum = findDivNum(); | |
74 | |
75 // generate a number of animation divs according to | |
76 // the requirements of the exercise | |
77 // such as in sequence section | |
78 for (var i = 0; i < animNum; i++) { | |
79 html += '<div id=\"' + currentId + '\" class=\"anim\"></div>' + '\n'; | |
80 currentId = nextId(currentId); | |
81 } | |
82 | |
83 // create a new editor object | |
84 var editor = new TryItDisplay(document.getElementById("tryIt")); | |
85 editor.setDefaultHtml(html); | |
86 | |
87 // common css for all divs | |
88 var css = '.anim {' + | |
89 '\n' + 'background-color: red;' + | |
90 '\n' + 'border-radius: 10px;' + | |
91 '\n' + 'width: 100px;' + | |
92 '\n' + 'height: 50px;' + | |
93 '\n' + 'top: 0px;' + | |
94 '\n' + 'left: 0px;' + | |
95 '\n' + 'position: relative;' + | |
96 '\n' + 'border: 1px solid black;' + | |
97 '\n' + '}'; | |
98 editor.setDefaultCss(css); | |
99 editor.update(); | |
100 } | |
101 | |
102 // Get the number of animation divs required | |
103 // which is stored in an invisible div with id | |
104 // 'animNum' and returns the value. | |
105 var findDivNum = function() { | |
106 var value = document.getElementById('animNum').innerHTML; | |
107 value = parseInt(value); | |
108 return value; | |
109 } | |
110 | |
111 // Generate 'a', 'b', 'c', 'd'... to put in as id. | |
112 var nextId = function(currentId) { | |
113 return String.fromCharCode(currentId.charCodeAt() + 1); | |
114 } | |
OLD | NEW |