-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
110 lines (96 loc) · 3.11 KB
/
script.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
/*
*
* Demo 1: Elements
*
*/
var dragElements = document.getElementsByTagName('li');
var dropAreaBox = document.getElementById('dropAreaBox');
var currentlyDragElement = null;
for(var i = 0; i<dragElements.length; i++) {
// Event Listener for when the drag interaction starts.
dragElements[i].ondragstart = function(event) {
event.dataTransfer.effectAllowed = 'move';
event.dataTransfer.setData('text', this.innerHTML);
currentlyDragElement = this;
//event.dataTransfer.setDragImage(event.target, 100,0);
}
// Event Listener for when the drag interaction finishes.
dragElements[i].ondragend = function() {
currentlyDragElement = null;
}
};
//enable drop area
dropAreaBox.ondragover = function(event) {
event.preventDefault();
event.dataTransfer.dropEffect = 'move';
}
//hover effect on drop element
dropAreaBox.ondragenter = function(event) {
this.className = 'dropBoxHover';
}
dropAreaBox.ondragleave = function(event) {
this.className = '';
}
// Event Listener for when the dragged element dropped in the drop zone.
dropAreaBox.ondrop = function (event) {
event.preventDefault();
event.stopPropagation();
var data = event.dataTransfer.getData('text');
this.innerHTML = 'Dropped ' + data;
// Remove the element from the list.
document.getElementsByTagName('ul')[0].removeChild(currentlyDragElement);
//hover effect on drop area
if(data == "Blue Box") this.className = 'blueBoxHover';
else if(data == "Turquoise box") this.className = 'turquoiseBoxHover';
else if(data == "Green box") this.className = 'greenBoxHover';
else if(data == "Yellow box") this.className = 'yellowBoxHover';
else if(data == "Orange box") this.className = 'orangeBoxHover';
if(currentlyDragElement.id == 'resetBox') {
//location.reload();
history.go(0);
this.innerHTML = 'Drop a box Here!';
}
}
/**
*
* Demo 2: text files
*
*/
var dropAreaText = document.getElementById('dropAreaText');
var pre = document.getElementsByTagName('pre')[0];
var span = document.getElementsByTagName('span')[1];
dropAreaText.ondragover = function(e) {
e.preventDefault();
e.stopPropagation();
}
//hover effect on drop element
dropAreaText.ondragenter = function(e) {
this.className = 'dropBoxHover';
}
dropAreaText.ondragleave = function(e) {
this.className = '';
}
dropAreaText.ondrop = function(event) {
event.preventDefault();
event.stopPropagation();
this.className = '';
var file = event.dataTransfer.files[0];
//display information about file
pre.innerHTML = 'File name: ' + file.name + '<br>'
+ 'File size: ' + file.size + '<br>'
+ 'File type: ' + file.type + '<br><br>' ;
if(file.type == 'text/plain') {
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result;
span.innerHTML = '<strong>Context of file: </strong>' + text;
dropAreaText.innerHTML = 'Thank you';
dropAreaText.style.color = '#ACACAC';
}
reader.readAsText(file);
} else {
this.innerHTML = 'Wrong format';
this.style.color = 'red';
span.innerHTML = '';
}
}