Skip to content

Commit fc000b0

Browse files
committed
Added user interactions demo
1 parent f8493bb commit fc000b0

File tree

7 files changed

+3311
-0
lines changed

7 files changed

+3311
-0
lines changed

05 User interactions/README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
## In this demo we are goin to polay with the user interactions.
2+
* References: https://developer.mozilla.org/es/docs/Web/API/DataTransfer
3+
https://codepen.io/patrickhlauke/pen/azbYWZ
4+
5+
* Folder structure:
6+
7+
05 User interactions/
8+
├── src/
9+
│ ├── content/
10+
| | ├── site.css
11+
│ ├── js/
12+
| | ├── main.js
13+
│ ├── index.html
14+
│ └── bootstrap-theme.min.css
15+
├── gulpfile.js
16+
├── package.json
17+
18+
## Steps.
19+
20+
### 1. Create the `index.html` for our demo.
21+
22+
* Just paste this code.
23+
24+
```html
25+
<!doctype html>
26+
27+
<html lang="en">
28+
<head>
29+
<meta charset="utf-8">
30+
<title>LEMONCODE 16/17 jQuery</title>
31+
<link rel="stylesheet" href="content/site.css">
32+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
33+
</head>
34+
35+
<body>
36+
<h1>API Rest</h1>
37+
<div id="drag-element" draggable="true" class="drag"></div>
38+
<br>
39+
<div id="drop-element-a" class="drop"></div>
40+
<div id="drop-element-b" class="drop"></div>
41+
<script src="./js/main.js"></script>
42+
</body>
43+
</html>
44+
45+
```
46+
47+
### 2. Create `site.css`
48+
49+
* Just paste this code,
50+
51+
```css
52+
@import url('https://fonts.googleapis.com/css?family=Raleway');
53+
54+
body {
55+
font-family: 'Raleway', sans-serif;
56+
}
57+
58+
.container {
59+
padding: 2em;
60+
}
61+
62+
.container p {
63+
max-width: 70%;
64+
font-size: 0.9em;
65+
margin-top: 0.2em;
66+
margin-bottom: 0.2em;
67+
height: 2.3em;
68+
}
69+
70+
.container p img {
71+
max-width: 95%;
72+
max-height: 95%;
73+
position: relative;
74+
top: 0.7em;
75+
left: 1em;
76+
}
77+
78+
.container ul {
79+
list-style: none;
80+
}
81+
82+
.drag {
83+
background-color: cyan;
84+
width: 200px;
85+
height: 200px;
86+
}
87+
88+
.drop {
89+
border: 1px solid black;
90+
width: 300px;
91+
height: 300px;
92+
float: left;
93+
}
94+
95+
```
96+
* Run the application and have a look
97+
98+
### 3. Lets type the javascript code to manage the drag and drop
99+
100+
```javascript
101+
document.onreadystatechange = () => {
102+
if(document.readyState === 'complete') {
103+
104+
const dragStart = (event) => {
105+
event.dataTransfer.setData('text/plain', event.target.id);
106+
console.log("dragStart", event);
107+
};
108+
109+
const dragElement = document.getElementById('drag-element');
110+
dragElement.addEventListener('dragstart', dragStart);
111+
112+
const dragOver = (event) => {
113+
event.preventDefault();
114+
event.dataTransfer.dropEffect = 'move';
115+
console.log({
116+
x: event.pageX,
117+
y: event.pageY
118+
});
119+
};
120+
121+
const drop = (event) => {
122+
const id = event.dataTransfer.getData('text');
123+
console.log('drop', id);
124+
event.target.appendChild(dragElement);
125+
};
126+
127+
const dropElementA = document.getElementById('drop-element-a');
128+
const dropElementB = document.getElementById('drop-element-b');
129+
130+
dropElementA.addEventListener('dragover', dragOver);
131+
dropElementB.addEventListener('dragover', dragOver);
132+
133+
dropElementA.addEventListener('drop', drop);
134+
dropElementB.addEventListener('drop', drop);
135+
}
136+
};
137+
138+
```
139+
* Drag and drop it's a great example of async sources, code that it's executed later.
140+
* Obviously these are user interactions, that it's getting the elements and moving around the page.
141+
* When we click the drag element and start the dragging around, the `dragstart` event it's triggered. The associated handler will be pushed to the queue, and later on executed.
142+
* How many little programs do we have here?

05 User interactions/gulpfile.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const gulp = require('gulp'),
4+
connect = require('gulp-connect'),
5+
open = require('gulp-open'),
6+
options = {
7+
port: 9005,
8+
root: ['src'],
9+
devBase: 'http://localhost:',
10+
browsers: ['safari', 'chrome']
11+
},
12+
openOptions = {
13+
uri: options.devBase + options.port,
14+
app: options.browser
15+
};
16+
17+
gulp.task('connect', () => connect.server({
18+
root: options.root,
19+
port: options.port
20+
}));
21+
// https://github.com/stevelacy/gulp-open/issues/15
22+
gulp.task('open', () => gulp.src(__filename).pipe(open(openOptions)));
23+
24+
gulp.task('default', ['connect', 'open']);

0 commit comments

Comments
 (0)