-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
253 lines (229 loc) · 6.81 KB
/
main.js
File metadata and controls
253 lines (229 loc) · 6.81 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
(function(NameSpace){
"use strict"
var reqFrame = (function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
return requestAnimationFrame;
})();
var DBframes = {
0 : {
tag: "img",
src: "images/term.png",
type: 'pattern',
styles: {
width: '5rem',
height: '5rem'
}
},
1 : {
tag: "img",
src: "images/snow.png",
type: 'pattern',
styles: {
width: '3rem',
height: '3rem'
}
},
2 : {
tag: "img",
src: "images/drop.png",
type: 'pattern',
styles: {
width: '3rem',
height: '3rem'
}
},
4 : {
tag: "input",
value: "+1",
type: 'pattern',
styles: {
width: '2rem',
height: '2rem'
}
}
}
var BlockManager = {
managerTypes: {
pattern: NameSpace.PatternManager,
equip: NameSpace.EquipmentManager
},
getConcreteManager: function(type){
return this.managerTypes[type]
},
createPhysicBlocks: function(bloksInfo){
var classes = ["draggable", "drag-drop", "yes-drop", "element"],
physicBlock;
for(var k in bloksInfo){
bloksInfo[k].classes = classes;
physicBlock = NameSpace.PhysicBlock.createPhysicBlock(bloksInfo[k]);
this.placePhysicBlock(physicBlock);
}
this.placedFinished();
},
placePhysicBlock: function(physicBlock){
var concreteManager = this.getConcreteManager(physicBlock.type),
specificBlock = concreteManager.build(physicBlock);
NameSpace.HTML.append({ link : document.getElementById("side-bar"), rendered: true}, specificBlock);
NameSpace.HTML.render(specificBlock);
return specificBlock;
},
removePlacedBlock: function(block){
this.getConcreteManager(block.type).removeById(block.id);
},
createEquipment: function(block){
var pattern = this.managerTypes.pattern.findById(block.id),
clone = pattern.clone(true);
clone.type = 'equip';
return this.placePhysicBlock(clone);
},
createBlockByNode: function(node){
var nodeInfo = NameSpace.HTML.getNodeInfo(node);
return PhysicBlock.createSimpleBlock(nodeInfo);
},
findPhysicBlock: function(block){
return this.getConcreteManager(block.type).findById(block.id);
},
placedFinished: function(){
for(var m in this.managerTypes){
if(this.managerTypes.hasOwnProperty(m)){
this.managerTypes[m].placedFinished();
}
}
}
}
BlockManager.createPhysicBlocks(DBframes);
var i = 0;
interact('.draggable')
.draggable({
// enable inertial throwing,
inertia: true,
manualStart: true,
// keep the element within the area of it's parent
restrict: {
restriction: "#middle",
endOnly: true,
elementRect: { top: 1, left: 1, bottom: 1, right: 1 }
},
// enable autoScroll
autoScroll: true,
// call this function on every dragmove event
onmove: dragMoveListener,
// call this function on every dragend event
onstart: function(event){
},
onend: onEnd
}).on('move', function(event){
var interaction = event.interaction;
// if the pointer was moved while being held down
// and an interaction hasn't started yet
if (interaction.pointerIsDown && !interaction.interacting()) {
var link = event.currentTarget,
block = BlockManager.createBlockByNode(link);
if(block.type === 'pattern'){
link = BlockManager.createEquipment(block).link;
}
// insert the clone to the page
// TODO: position the clone appropriately
// start a drag interaction targeting the clone
interaction.start({ name: 'drag' },
event.interactable,
link);
}
});
// TODO create target Object an move betwen that
function dragMoveListener (event) {
var target = event.target,
// keep the dragged position in the data-x/data-y attributes
x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
// translate the element
target.style.webkitTransform =
target.style.transform =
'translate(' + x + 'px, ' + y + 'px)';
// update the posiion attributes
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
}
function onEnd(event){
var textEl = event.target.querySelector('p');
//console.log('')
var target = event.target;
var classList = target.classList;
var _x, _y,vectx, vecty, oneStepX, oneStepY,_frame, frame, finish = false;
var block ;
if(classList.toString().indexOf("can-drop") === -1){
function step(){
if(finish)return;
_x = parseInt(target.getAttribute('data-x'));
_y = parseInt(target.getAttribute('data-y'));
oneStepX = Math.abs(Math.ceil(_x / 10)) || 1;
oneStepY = Math.abs(Math.ceil(_y / 10)) || 1;
vectx = (_x > 0)? 1 : -1;
vecty = (_y > 0)? 1 : -1;
if(Math.abs(_x) > 0){
target.style.webkitTransform =
target.style.transform =
'translate(' + ((Math.abs(_x) - oneStepX) *vectx) + 'px, ' + _y + 'px)';
target.setAttribute('data-x', (Math.abs(_x) - oneStepX) *vectx);
frame = reqFrame(step);
}else if(Math.abs(_x) <= 0){
target.style.webkitTransform =
target.style.transform =
'translate(0px, ' + _y + 'px)';
target.setAttribute('data-x', 0);
}
if(Math.abs(_y) > 0){
target.style.webkitTransform =
target.style.transform =
'translate(' + _x + 'px, ' + ((Math.abs(_y) - oneStepY) *vecty)+ 'px)';
target.setAttribute('data-y', (Math.abs(_y) - oneStepY) *vecty);
frame = reqFrame(step);
}else if(_y <=0){
target.style.webkitTransform =
target.style.transform =
'translate(' + _x + 'px, 0px)';
target.setAttribute('data-y', 0);
}
if(frame === _frame){
finish = true;
//debugger;
block = BlockManager.createBlockByNode(target)
BlockManager.removePlacedBlock(block);
}
_frame = frame;
}
frame = reqFrame(step);
}
}
var t = interact('.dropzone').dropzone({
// only accept elements matching this CSS selector
accept: '.yes-drop',
// Require a 75% element overlap for a drop to be possible
overlap: 0.75,
// listen for drop related events:
ondropactivate: function (event) {
// add active dropzone feedback
event.target.classList.add('drop-active');
},
ondragenter: function (event) {
var draggableElement = event.relatedTarget,
dropzoneElement = event.target;
// feedback the possibility of a drop
dropzoneElement.classList.add('drop-target');
draggableElement.classList.add('can-drop');
},
ondragleave: function (event) {
// remove the drop feedback style
event.target.classList.remove('drop-target');
event.relatedTarget.classList.remove('can-drop');
},
ondrop: function (event) {
},
ondropdeactivate: function (event) {
// remove active dropzone feedback
event.target.classList.remove('drop-active');
event.target.classList.remove('drop-target');
}
});
})(this)