-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (97 loc) · 2.7 KB
/
index.js
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
data =[
{ id: 0 },
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
{ id: 6 },
{ id: 7 },
{ id: 8 }
];
/*
==============================================================
Drag And Drop
- Object / Controller to create boxes, handle box swapping
and maintain coordinance of the boxes
==============================================================
*/
var DragAndDrop = (function (container, boxData) {
var _content = document.getElementById(container);
var _windowWidth = window.innerWidth;
var _position = { x: 0, y: 0 };
var _boxMargin = 15;
var _boxWidth = 85;
var _coordinance = [];
var _boxes = [];
var _y = 60;
var _x = 0;
var _activeBox;
// Make array of boxes from data
_boxes = boxData.map(makeBox);
// handle swapping of tiles
_content.addEventListener('mousemove', move, false);
_content.addEventListener('mouseup', swap, false);
function swap(e) {
if(!_activeBox) { return; }
console.log( 'swap' );
// Coordinance of moved box
var dropBox = _boxes.find(_drop, e) || false;
var to_ = _coordinance[_activeBox.id];
_activeBox.setTransitionDuration(300);
if(dropBox) {
// Swap boxes
dropBox.setTransitionDuration(300);
dropBox.div.style.zIndex = "1";
// Get coordinance of drop box
var from_ = _coordinance[dropBox.id];
// Swap boxes
_activeBox.to(from_.x, from_.y);
dropBox.sendTo(to_.x, to_.y);
// Update grid to keep everything in order
_coordinance[_activeBox.id] = from_;
_coordinance[dropBox.id] = to_;
}
else {
// Send moved box back to original position
_activeBox.to(to_.x, to_.y);
}
_activeBox = null;
}
// Return box that is being moved
function findActiveBox(box) {
return box.moved();
}
// If: mouse up occurred over a box return the box
function _drop(box) {
var e = this;
var measureY = e.pageY >= box.pos.y && e.pageY <= box.pos.y + 85;
var measureX = e.pageX >= box.pos.x && e.pageX <= box.pos.x + 85;
return measureY && measureX && _activeBox.id !== box.id
}
function makeBox(box, i) {
var newBox = Box(box.id);
_x += _boxWidth + _boxMargin;
// Set Initial X and Y position
if(i % 3 === 0){
_y += 100;
// Center Boxes horizontally in 'content-box' div and screen
_x = Math.floor((_windowWidth - (_boxWidth * 3 + _boxMargin * 2)) / 2);
}
_coordinance.push({ x: _x, y: _y });
newBox.sendTo(_x, _y);
_content.appendChild(newBox.div);
return newBox;
}
function move(e) {
// Set active box
_activeBox = _boxes.find(findActiveBox);
if(!_activeBox) { return; }
// Move active box
_activeBox.to(e.pageX - 32, e.pageY - 32);
}
});
// DragAndDrop();
(function () {
DragAndDrop('content-box', data);
}());