-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
182 lines (174 loc) · 5.89 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Draggable Shelves</title>
</head>
<body>
<h1 style="text-align: center">Draggable elements playground</h1>
<script>
'use strict'
//Global variables
var cabinet = {},
shelves = [],
dragSource = null; // will hold the initial dragged element
/*
* Cabinet - Singleton Constructor
* Call it without the "new"
* @return - object
*/
function Cabinet(){
var cabinet = {};
//Rewrite the constructor
Cabinet = function() {
console.log("I'm a singleton, I'm creating the cabinet only once!");
return cabinet;
}
cabinet = document.createElement('div');
cabinet.id = "cabinet";
cabinet.style.margin = "50px auto";
cabinet.style.width = '400px';
cabinet.style.height = '300px';
cabinet.style.backgroundColor = '#fff';
cabinet.style.border = 'solid 1px #000';
return cabinet;
}
/*
* Shelf - Constructor, creates a shelf
* Call it with the "new"
* @return - object
*/
function Shelf(i){
var shelf = {}, shelfTitle;
shelf = document.createElement('div');
shelf.title = 'Double click on me in order to edit my content, ESC/Enter to finish';
shelf.className = 'shelf';
shelf.style.MozBoxSizing = 'border-box';
shelf.style.boxSizing = "border-box";
shelf.style.width = '100%';
shelf.style.height = '100px';
shelf.style.backgroundColor = '#dfdfdf';
shelf.style.border = 'solid 1px #000';
shelf.style.borderTop = 'solid 1px #dfdfdf';
shelf.style.transition = 'transform .1s ease-out';
shelf.style.lineHeight = shelf.style.height;
shelf.style.textAlign = 'center';
shelf.style.cursor = 'pointer';
shelf.draggable = true;
shelfTitle = document.createTextNode('Shelf #' + i);
shelf.appendChild(shelfTitle);
shelf.addEventListener("dragstart", DragHandler, false);
shelf.addEventListener("dragend", DragHandler, false);
shelf.addEventListener("drop", DragHandler, false);
shelf.addEventListener("dragenter", DragHandler, false);
shelf.addEventListener("dragleave", DragHandler, false);
shelf.addEventListener("dragover", DragHandler, false);
shelf.addEventListener("drag", DragHandler, false);
shelf.addEventListener("mousedown", DragHandler, false);
shelf.addEventListener("mouseup", DragHandler, false);
shelf.addEventListener("keydown", EditHandler, false);
shelf.addEventListener("keyup", EditHandler, false);
shelf.addEventListener("dblclick", EditHandler, false);
shelf.addEventListener("blur", EditHandler, false);
return shelf;
}
//Let's create the cabinet and add it to the page
cabinet = Cabinet();
document.body.appendChild(cabinet);
//Fill the cabinet with the shelves;
shelves.push(new Shelf(1)); //create first manually in order to get its dimensions
cabinet.appendChild(shelves[0]);
var numOfShelves = parseInt(cabinet.clientHeight/shelves[0].clientHeight);
for(var i = 1; i < numOfShelves; i++) {
shelves.push(new Shelf(i+1));
cabinet.appendChild(shelves[i]);
}
//Handles edit events
function EditHandler(event){
if(event.type==='dblclick'){
event.preventDefault();
this.contentEditable = true;
this.style.backgroundColor = '#fff';
if(this.textContent.indexOf('Shelf #') !== -1){ // Give the tip only for the default shelf title
EditHandler.firstKeyStroke = true;
EditHandler.originalTitle = this.textContent;
this.innerHTML = 'Start Typing...';
}
this.focus();
}
else if(event.type==='keydown') {
if(EditHandler.firstKeyStroke){
this.innerHTML = '';
}
if(event.keyCode === 27 || event.keyCode == 13){
if(this.textContent === ''){
this.textContent = EditHandler.originalTitle;
}
this.blur();
this.contentEditable = false;
this.style.backgroundColor = '#dfdfdf';
}
EditHandler.firstKeyStroke = false; // taking advantage that function is actually an object in JS, let us keep global namespace cleaner
}
else if(event.type==='blur'){
this.contentEditable = false;
if(this.textContent === 'Start Typing...'){ // bring back the original title if it wasn't changed
this.textContent = EditHandler.originalTitle;
}
this.style.backgroundColor = '#dfdfdf';
}
return false;
}
//Handles dragging events
function DragHandler(event) {
if(event.type==='dragstart'){
dragSource = this;
event.dataTransfer.effectAllowed = 'move';
event.dataTransfer.setData('text/html', this.innerHTML);
this.style.cursor = 'move';
this.style.opacity = '0.4';;
this.style.transform = 'scale(1.2)';
}
else if(event.type==='mousedown'){
this.contentEditable = false;
this.style.transform = 'scale(1.2) rotate(4deg)';
this.style.cursor = 'move';
this.style.opacity = '0.4';
}
else if(event.type==='mouseup'){
this.style.border = 'solid 1px #000';
this.style.transform = 'scale(1)';
this.style.cursor = 'pointer';
this.style.opacity = '1';
}
else if(event.type==='dragend'){
this.style.border = 'solid 1px #000';
this.style.cursor = 'pointer';
this.style.opacity = '1';
this.style.transform = 'scale(1)';
}
else if(event.type==='dragenter'){
this.style.border = 'dashed 2px blue';
dragSource.style.transform = 'scale(1)';
}
else if(event.type==='dragleave'){
this.style.border = 'solid 1px #000';
}
else if(event.type==='dragover'){
event.preventDefault(); // Required - allows to drop!
event.dataTransfer.dropEffect = 'move';
this.style.border = 'dashed 2px #df7439';
}
else if(event.type==='drop'){
event.stopPropagation() // Stops some browsers from redirecting.
if (dragSource != this) { // if not the same element, copy its content
dragSource.innerHTML = this.innerHTML;
this.innerHTML = event.dataTransfer.getData('text/html');
this.style.border = 'solid 1px #000';
}
}
return false;
}
</script>
</body>
</html>