Skip to content

Commit 0576ea7

Browse files
committed
up
1 parent 7a51c05 commit 0576ea7

File tree

18 files changed

+375
-109
lines changed

18 files changed

+375
-109
lines changed

1-js/02-first-steps/14-function-basics/2-rewrite-function-question-or/task.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function checkAge(age) {
1313
if (age > 18) {
1414
return true;
1515
} else {
16-
return confirm('Did parents allow you?');
16+
return confirm('Do you have your parents permission to access this page?');
1717
}
1818
}
1919
```
@@ -24,4 +24,3 @@ Make two variants of `checkAge`:
2424

2525
1. Using a question mark operator `'?'`
2626
2. Using OR `||`
27-

2-ui/1-document/01-browser-environment/article.md

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Browser environment, specs
22

3-
The JavaScript language was initially created for web browsers. But as of now, it evolved and became a language with many uses and platforms.
3+
The JavaScript language was initially created for web browsers. Since then, it evolved and became a language with many uses and platforms.
44

5-
A platform may be either a browser or a web-server or a washing machine or another *host*. Each of them provides platform-specific functionality. The JavaScript standard called that a *host environment*.
5+
A platform may be either a browser or a web-server or a washing machine or another *host*. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
66

7-
That host environment provides additional objects and functions to the language core. Web browsers provide means to control web pages. Node.JS provides server-side features. There are other host environments too.
7+
A host environment provides platform-specific objects and functions additionally to the language core. Web browsers give means to control web pages. Node.JS provides server-side features, and so on.
88

99
[cut]
1010

1111
Here's a bird-eye view of what we have when JavaScript runs in a web-browser:
1212

1313
![](windowObjects.png)
1414

15-
The "root object" called `window` has two roles:
15+
There's a "root" object called `window`. It has two roles:
1616

17-
1. First, it is a [global object](info:global-object) for JavaScript code.
18-
2. Second, it represents the "browser window" object, provides methods to control it.
17+
1. First, it is a global object for JavaScript code, as described in the chapter <info:global-object>.
18+
2. Second, it represents the "browser window" and provides methods to control it.
1919

2020
For instance, here we use it as a global object:
2121

@@ -24,18 +24,21 @@ function sayHi() {
2424
alert("Hello");
2525
}
2626

27-
alert(window.sayHi); // global function is a property of window
27+
// global functions are accessible as properties of window
28+
alert(window.sayHi);
2829
```
2930

3031
And here we use it as a browser window, to see the window height:
3132

3233
```js run
33-
alert(window.innerHeight); // some number
34+
alert(window.innerHeight); // inner window height
3435
```
3536

37+
There are more window-specific methods and properties, we'll cover them later.
38+
3639
## Document Object Model (DOM)
3740

38-
The `document` object gives access to the page content. We can change or create literally anything.
41+
The `document` object gives access to the page content. We can change or create anything on the page using it.
3942

4043
For instance:
4144
```js run
@@ -46,24 +49,20 @@ document.body.style.background = 'red';
4649
setTimeout(() => document.body.style.background = '', 1000);
4750
```
4851

49-
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification.
50-
51-
There are two working groups who develop it:
52+
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification. By chance, there are two working groups who develop it:
5253

5354
1. [W3C](https://en.wikipedia.org/wiki/World_Wide_Web_Consortium) -- the documentation is at <https://www.w3.org/TR/dom>.
5455
2. [WhatWG](https://en.wikipedia.org/wiki/WHATWG), publishing at <https://dom.spec.whatwg.org>.
5556

56-
As it happens, the two groups don't always agree, so we have like 2 sets of standards. But they are in tight contact and eventually things merge. So the documentation that you can find on the given resources is very similar, like 99%. There are very minor differences, but you probably won't notice them.
57+
As it happens, the two groups don't always agree, so we have like 2 sets of standards. But they are in the tight contact and eventually things merge. So the documentation that you can find on the given resources is very similar, there's like 99% match. There are very minor differences, you probably won't notice them.
5758

58-
I find <https://dom.spec.whatwg.org> more pleasant to use, and so recommend it.
59+
Personally, I find <https://dom.spec.whatwg.org> more pleasant to use.
5960

60-
In the ancient past, once there was no standard at all -- each browser did whatever it wanted. So different browsers had different sets methods and properties for the same thing, and developers had to write different code for each of them. Dark times indeed.
61+
In the ancient past, there was no standard at all -- each browser implemented whatever it wanted. So different browsers had different sets methods and properties for the same thing, and developers had to write different code for each of them. Dark, messy times.
6162

6263
Even now we can sometimes meet old code that uses browser-specific properties and works around incompatibilities. But in this tutorial we'll use modern stuff: there's no need to learn old things until you really need those (chances are high you won't).
6364

64-
Then the DOM standard appeared, in an attempt to bring everyone to an agreement. The first version was DOM Level 1, then it was extended by DOM Level 2, then DOM Level 3, and now it's DOM Level 4.
65-
66-
People from WhatWG group got tired of version and are calling that just "DOM", without a number. So will do we.
65+
Then the DOM standard appeared, in an attempt to bring everyone to an agreement. The first version was "DOM Level 1", then it was extended by DOM Level 2, then DOM Level 3, and now it's DOM Level 4. People from WhatWG group got tired of version and are calling that just "DOM", without a number. So will do we.
6766

6867
```smart header="DOM is not only for browsers"
6968
The DOM specification explains the structure of a document and provides objects to manipulate it. There are non-browser instruments that use it too.
@@ -72,9 +71,9 @@ For instance, server-side tools that download HTML pages and process them. They
7271
```
7372

7473
```smart header="CSSOM for styling"
75-
CSS styles and stylesheets are structured not like HTML. So there's a separate specification [CSSOM](https://www.w3.org/TR/cssom-1/) that explains how CSS styles and rules can be represented as objects, how to read and write them.
74+
CSS rules and stylesheets are structured not like HTML. So there's a separate specification [CSSOM](https://www.w3.org/TR/cssom-1/) that explains how they are represented as objects, how to read and write them.
7675
77-
Usually we take a style somewhere from the document and apply it to the document, so CSSOM is used together with DOM. But CSSOM is applied not often, because we rarely need to modify CSS rules from JavaScript, so we won't cover it right now.
76+
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because usually CSS rules are static. We rarely need to add/remove CSS rules from JavaScript, so we won't cover it right now.
7877
```
7978

8079
## BOM (part of HTML spec)
@@ -95,7 +94,7 @@ if (confirm("Go to wikipedia?")) {
9594
}
9695
```
9796

98-
Functions `alert/confirm/prompt` -- are also a part of BOM: they are directly not related to the document, but represent pure browser methods of communicating with the user.
97+
Functions `alert/confirm/prompt` are also a part of BOM: they are directly not related to the document, but represent pure browser methods of communicating with the user.
9998

10099

101100
```smart header="HTML specification"
@@ -112,11 +111,13 @@ DOM specification
112111
: Describes the document structure, manipulations and events, see <https://dom.spec.whatwg.org>.
113112

114113
CSSOM specification
115-
: Describes styles, manipulations with them and their binding to documents, see <https://www.w3.org/TR/cssom-1/>.
114+
: Describes stylesheets and style rules, manipulations with them and their binding to documents, see <https://www.w3.org/TR/cssom-1/>.
116115

117116
HTML specification
118117
: Describes HTML language (tags etc) and also BOM (browser object model) -- various browser functions: `setTimeout`, `alert`, `location` and so on, see <https://html.spec.whatwg.org>. It takes DOM specification and extends it with many additional properties and methods.
119118

120119
Now we'll get down to learning DOM, because the document plays the central role in the UI, and working with it is the most complex part.
121120

122-
Please note the links above, because there's so many stuff to learn, it's impossible to cover and remember everything. When you'd like to read about a property or a method -- the Mozilla manual at <https://developer.mozilla.org/en-US/search> is a nice one, but reading the corresponding spec may be better. More complex and longer to read, but will make your fundamental knowledge sound and complete.
121+
Please note the links above, because there's so many stuff to learn, it's impossible to cover and remember everything.
122+
123+
When you'd like to read about a property or a method -- the Mozilla manual at <https://developer.mozilla.org/en-US/search> is a nice one, but reading the corresponding spec may be better: more complex and longer to read, but will make your fundamental knowledge sound and complete.

2-ui/1-document/02-dom-nodes/article.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ libs:
66

77
# DOM tree
88

9-
The essential part of HTML is tags, right?
9+
The backbone of an HTML document is tags.
1010

11-
According to Document Object Model (DOM), every HTML-tag is an object. Nested tags are called "children". And the text inside it is an object as well. All these objects are accessible using JavaScript, we'll see that now.
11+
According to Document Object Model (DOM), every HTML-tag is an object. Nested tags are called "children" of the enclosing one.
12+
13+
The text inside a tag it is an object as well.
14+
15+
All these objects are accessible using JavaScript.
1216

1317
## An example of DOM
1418

5-animation/2-css-animations/3-animate-circle/solution.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<style>
7+
.circle {
8+
transition-property: width, height, margin-left, margin-top;
9+
transition-duration: 2s;
10+
position: fixed;
11+
transform: translateX(-50%) translateY(-50%);
12+
background-color: red;
13+
border-radius: 50%;
14+
}
15+
</style>
16+
</head>
17+
18+
<body>
19+
20+
<button onclick="showCircle(150, 150, 100)">showCircle(150, 150, 100)</button>
21+
22+
<script>
23+
function showCircle(cx, cy, radius) {
24+
let div = document.createElement('div');
25+
div.style.width = 0;
26+
div.style.height = 0;
27+
div.style.left = cx + 'px';
28+
div.style.top = cy + 'px';
29+
div.className = 'circle';
30+
document.body.append(div);
31+
32+
setTimeout(() => {
33+
div.style.width = radius * 2 + 'px';
34+
div.style.height = radius * 2 + 'px';
35+
}, 0);
36+
}
37+
</script>
38+
39+
40+
</body>
41+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<style>
7+
.circle {
8+
transition-property: width, height, margin-left, margin-top;
9+
transition-duration: 2s;
10+
position: fixed;
11+
transform: translateX(-50%) translateY(-50%);
12+
background-color: red;
13+
border-radius: 50%;
14+
15+
width: 200px;
16+
height: 200px;
17+
top: 150px;
18+
left: 150px;
19+
}
20+
</style>
21+
</head>
22+
23+
<body>
24+
25+
<div class="circle"></div>
26+
27+
</body>
28+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
importance: 5
2+
3+
---
4+
5+
# Animated circle
6+
7+
Create a function `showCircle(cx, cy, radius)` that shows an animated growing circle.
8+
9+
- `cx,cy` are window-relative coordinates of the center of the circle,
10+
- `radius` is the radius of the circle.
11+
12+
Click the button below to see how it should look like:
13+
14+
[iframe src="solution" height=260]
15+
16+
The source document has an example of a circle with right styles, so the task is precisely to do the animation right.

8-async/01-callback-hell/01-animate-circle-callback/solution.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<style>
7+
.message-ball {
8+
font-size: 20px;
9+
line-height: 200px;
10+
text-align: center;
11+
}
12+
.circle {
13+
transition-property: width, height, margin-left, margin-top;
14+
transition-duration: 2s;
15+
position: fixed;
16+
transform: translateX(-50%) translateY(-50%);
17+
background-color: red;
18+
border-radius: 50%;
19+
}
20+
</style>
21+
</head>
22+
23+
<body>
24+
25+
<button onclick="go()">Click me</button>
26+
27+
<script>
28+
29+
function go() {
30+
showCircle(150, 150, 100, div => {
31+
div.classList.add('message-ball');
32+
div.append("Hello, world!");
33+
});
34+
}
35+
36+
function showCircle(cx, cy, radius, callback) {
37+
let div = document.createElement('div');
38+
div.style.width = 0;
39+
div.style.height = 0;
40+
div.style.left = cx + 'px';
41+
div.style.top = cy + 'px';
42+
div.className = 'circle';
43+
document.body.append(div);
44+
45+
setTimeout(() => {
46+
div.style.width = radius * 2 + 'px';
47+
div.style.height = radius * 2 + 'px';
48+
49+
div.addEventListener('transitionend', function handler() {
50+
div.removeEventListener('transitionend', handler);
51+
callback(div);
52+
});
53+
}, 0);
54+
}
55+
</script>
56+
57+
58+
</body>
59+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Animated circle with callback
3+
4+
In the task <info:task/animate-circle> an animated growing circle is shown.
5+
6+
Now let's say we need not just a circle, but to show a message inside it. The message should appear *after* the animation is complete (the circle is fully grown), otherwise it would look ugly.
7+
8+
In the solution of the task, the function `showCircle(cx, cy, radius)` draws the circle, but gives no way to track when it's ready.
9+
10+
Add a callback argument: `showCircle(cx, cy, radius, callback)` to be called when the animation is complete. The `callback` should receive the circle `<div>` as an argument.
11+
12+
Here's the example:
13+
14+
```js
15+
showCircle(150, 150, 100, div => {
16+
div.classList.add('message-ball');
17+
div.append("Hello, world!");
18+
});
19+
```
20+
21+
Demo:
22+
23+
[iframe src="solution" height=260]
24+
25+
Take the solution of the task <info:task/animate-circle> as the base.

0 commit comments

Comments
 (0)