Skip to content

Commit b4a4398

Browse files
authored
Merge pull request #240 from vplentinax/sync-ui-event-details
sync-ui-event-details
2 parents 27c9d6a + 1bdb925 commit b4a4398

File tree

64 files changed

+972
-723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+972
-723
lines changed

2-ui/3-event-details/1-mouse-events-basics/01-selectable-list/task.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ The demo:
1414
[iframe border="1" src="solution" height=180]
1515

1616
P.S. For this task we can assume that list items are text-only. No nested tags.
17+
1718
P.P.S. Prevent the native browser selection of the text on clicks.

2-ui/3-event-details/1-mouse-events-basics/article.md

Lines changed: 80 additions & 112 deletions
Large diffs are not rendered by default.

2-ui/3-event-details/1-mouse-events-basics/head.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@
44

55
function showmesg(t, form) {
66

7-
if (timer==0) timer = new Date()
7+
if (timer == 0) {
8+
timer = new Date();
9+
}
10+
11+
let tm = new Date();
812

9-
let tm = new Date()
10-
if (tm-timer > 300) {
11-
t = '------------------------------\n'+t
13+
if (tm - timer > 300) {
14+
t = '------------------------------\n' + t;
1215
}
1316

14-
let area = document.forms[form+'form'].getElementsByTagName('textarea')[0]
17+
let area = document.forms[form + 'form'].getElementsByTagName('textarea')[0];
1518

1619
area.value += t + '\n';
17-
area.scrollTop = area.scrollHeight
20+
area.scrollTop = area.scrollHeight;
1821

19-
timer = tm
22+
timer = tm;
2023
}
2124

2225
function logMouse(e) {
2326
let evt = e.type;
2427
while (evt.length < 11) evt += ' ';
25-
showmesg(evt+" which="+e.which, 'test')
28+
showmesg(evt + " button=" + e.button, 'test')
2629
return false;
2730
}
2831

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/1-behavior-nested-tooltip/solution.view/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@
7878
}
7979

8080
document.onmouseout = function() {
81-
// it is possible that mouseout triggered, but we're still inside the element (cause of bubbling)
81+
// it is possible that mouseout triggered, but we're still inside the element
82+
// (its target was inside, and it bubbled)
8283
// but in this case we'll have an immediate mouseover,
8384
// so the tooltip will be destroyed and shown again
8485
//
85-
// that's an overhead, but here it's not visible
86-
// can be fixed with additional checks
86+
// luckily, the "blinking" won't be visible,
87+
// as both events happen almost at the same time
8788
if (tooltip) {
8889
tooltip.remove();
8990
tooltip = false;

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/1-behavior-nested-tooltip/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ importance: 5
44

55
# Improved tooltip behavior
66

7-
Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`.
7+
Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`. The value of this attribute should become the tooltip text.
88

99
That's like the task <info:task/behavior-tooltip>, but here the annotated elements can be nested. The most deeply nested tooltip is shown.
1010

11+
Only one tooltip may show up at the same time.
12+
1113
For instance:
1214

1315
```html
@@ -21,5 +23,3 @@ For instance:
2123
The result in iframe:
2224

2325
[iframe src="solution" height=300 border=1]
24-
25-
P.S. Hint: only one tooltip may show up at the same time.

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/solution.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ The algorithm looks simple:
33
1. Put `onmouseover/out` handlers on the element. Also can use `onmouseenter/leave` here, but they are less universal, won't work if we introduce delegation.
44
2. When a mouse cursor entered the element, start measuring the speed on `mousemove`.
55
3. If the speed is slow, then run `over`.
6-
4. Later if we're out of the element, and `over` was executed, run `out`.
6+
4. When we're going out of the element, and `over` was executed, run `out`.
77

8-
The question is: "How to measure the speed?"
8+
But how to measure the speed?
99

10-
The first idea would be: to run our function every `100ms` and measure the distance between previous and new coordinates. If it's small, then the speed is small.
10+
The first idea can be: run a function every `100ms` and measure the distance between previous and new coordinates. If it's small, then the speed is small.
1111

1212
Unfortunately, there's no way to get "current mouse coordinates" in JavaScript. There's no function like `getCurrentMouseCoordinates()`.
1313

14-
The only way to get coordinates is to listen to mouse events, like `mousemove`.
14+
The only way to get coordinates is to listen for mouse events, like `mousemove`, and take coordinates from the event object.
1515

16-
So we can set a handler on `mousemove` to track coordinates and remember them. Then we can compare them, once per `100ms`.
16+
So let's set a handler on `mousemove` to track coordinates and remember them. And then compare them, once per `100ms`.
1717

1818
P.S. Please note: the solution tests use `dispatchEvent` to see if the tooltip works right.

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/solution.view/index.html

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@
2020
<div id="tooltip" hidden>Tooltip</div>
2121

2222
<script>
23-
// for the demo
24-
setTimeout(function() {
25-
new HoverIntent({
26-
elem,
27-
over() {
28-
tooltip.style.left = elem.getBoundingClientRect().left + 5 + 'px';
29-
tooltip.style.top = elem.getBoundingClientRect().bottom + 5 + 'px';
30-
tooltip.hidden = false;
31-
},
32-
out() {
33-
tooltip.hidden = true;
34-
}
35-
});
36-
}, 2000);
23+
new HoverIntent({
24+
elem,
25+
over() {
26+
tooltip.style.left = elem.getBoundingClientRect().left + 5 + 'px';
27+
tooltip.style.top = elem.getBoundingClientRect().bottom + 5 + 'px';
28+
tooltip.hidden = false;
29+
},
30+
out() {
31+
tooltip.hidden = true;
32+
}
33+
});
3734
</script>
3835

3936
</body>

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/solution.view/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ describe("hoverIntent", function() {
4949
}
5050
})
5151

52-
it("mouseover -> immediately no tooltip", function() {
52+
it("mouseover -> when the pointer just arrived, no tooltip", function() {
5353
mouse('mouseover', 10, 10);
5454
assert.isFalse(isOver);
5555
});
5656

57-
it("mouseover -> pause shows tooltip", function() {
57+
it("mouseover -> after a delay, the tooltip shows up", function() {
5858
mouse('mouseover', 10, 10);
5959
this.clock.tick(100);
6060
assert.isTrue(isOver);
6161
});
6262

63-
it("mouseover -> fast mouseout no tooltip", function() {
63+
it("mouseover -> followed by fast mouseout leads doesn't show tooltip", function() {
6464
mouse('mouseover', 10, 10);
6565
setTimeout(
6666
() => mouse('mouseout', 300, 300, { relatedTarget: document.body}),

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/source.view/hoverIntent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class HoverIntent {
4545

4646
destroy() {
4747
/* your code to "disable" the functionality, remove all handlers */
48+
/* it's needed for the tests to work */
4849
}
4950

5051
}

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/source.view/index.html

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<!doctype html>
2-
<html lang="en">
2+
<html>
33

44
<head>
55
<meta charset="UTF-8">
6-
<title>Document</title>
76
<link rel="stylesheet" href="style.css">
87
<script src="hoverIntent.js"></script>
9-
<script src="https://js.cx/test/libs.js"></script>
8+
<script src="https://en.js.cx/test/libs.js"></script>
109
<script src="test.js"></script>
1110
</head>
1211

@@ -21,18 +20,17 @@
2120
<div id="tooltip" hidden>Tooltip</div>
2221

2322
<script>
24-
// for the demo
25-
setTimeout(function() {
26-
new HoverIntent({
27-
elem,
28-
over() {
29-
tooltip.hidden = false;
30-
},
31-
out() {
32-
tooltip.hidden = true;
33-
}
34-
});
35-
}, 2000);
23+
new HoverIntent({
24+
elem,
25+
over() {
26+
tooltip.style.left = elem.getBoundingClientRect().left + 5 + 'px';
27+
tooltip.style.top = elem.getBoundingClientRect().bottom + 5 + 'px';
28+
tooltip.hidden = false;
29+
},
30+
out() {
31+
tooltip.hidden = true;
32+
}
33+
});
3634
</script>
3735

3836
</body>

0 commit comments

Comments
 (0)