forked from lvidgen/WebRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraggable.js
202 lines (184 loc) · 6.84 KB
/
draggable.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
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
function makeWindow(tag, indata, txt, conn) {
var sender = getById("theirname").textContent,
wrap = crEl("div", getById("main"));
wrap.className = "draggable";
wrap.style.zIndex = highZ();
var hdrwrap = crEl("div", wrap);
hdrwrap.className = "hdwrp";
var hdr = crEl("div", hdrwrap);
hdr.className = "dragheader";
hdr.textContent = txt;
makeDraggable(hdrwrap);
if (tag == "calling") {
var clsr = makeCloser(hdrwrap);
makeVid(indata, clsr, wrap, hdr);
return wrap;
}
var cnfwrp = crEl("div", wrap);
var btn_y = crEl("button", cnfwrp);
btn_y.appendChild(document.createTextNode("Accept"));
btn_y.className = "conf_btn accept_stream";
var btn_n = crEl("button", cnfwrp);
btn_n.appendChild(document.createTextNode("Reject"));
btn_n.className = "conf_btn reject_stream";
btn_n.onclick = function() {
switch (tag) {
case "vid":
indata.answer();
indata.on('stream', function(peerstream) {
setTimeout(function() {
indata.close();
indata = null;
getById("main").removeChild(wrap);
}, 0);
});
break;
case "pdf":
case "photo":
indata = null;
getById("main").removeChild(wrap);
break;
}
}
if (tag == "info") {
var dv = document.createElement("div");
dv.appendChild(document.createTextNode(indata));
btn_y.textContent = "OK";
wrap.lastChild.insertBefore(dv, btn_y);
cnfwrp.removeChild(btn_n);
}
btn_y.onclick = function() {
wrap.removeChild(cnfwrp);
wrap.style.resize = "both";
var cont = null,
clsr = makeCloser(hdrwrap);
switch (tag) {
case "vid":
makeVid(indata, clsr, wrap);
if (indata.metadata == "scrn") {
indata.answer();
hdr.textContent = "screen shared from " + sender;
indata.once('close', function() {
indata = null;
getById("main").removeChild(wrap);
});
} else {
hdr.textContent = "call with " + sender;
enumDevices().then(function(result) {
if (result.hasmic || result.hascam) {
navigator.mediaDevices.getUserMedia({
audio: result.hasmic,
video: result.hascam
})
.then(function(stream) {
indata.answer(stream);
indata.once('close', function() {
closeMediaConn(stream);
indata = null;
getById("main").removeChild(wrap);
});
});
} else { // no cam or mic, just answer the call
indata.answer();
indata.once('close', function() {
indata = null;
getById("main").removeChild(wrap);
});
}
})
.catch(function(err) {
console.log(err.name + ": " + err.message);
});
}
break;
case "pic_show":
hdr.textContent = "photo shared by " + sender;
cont = crEl("img", wrap);
cont.src = indata;
cont.className = "pic_cont";
clsr.onclick = function() {
indata = null;
getById("main").removeChild(wrap);
}
break;
case "pdf_show":
hdr.textContent = "pdf shared by " + sender;
wrap.style.width = "40%";
wrap.style.height = "50%";
cont = crEl("object", wrap);
cont.type = "application/pdf";
cont.data = indata;
cont.className = "pdf_cont";
wrap.appendChild(cont);
clsr.onclick = function() {
indata = null;
getById("main").removeChild(wrap);
}
break;
case "info":
getById("main").removeChild(wrap);
break;
}
}
}
function makeDraggable(hdr) {
var wrp = hdr.parentElement;
wrp.onpointerdown = function(e) {
if (wrp.style.zIndex < highZ() - 1) {
wrp.style.zIndex = highZ();
}
}
hdr.onpointerdown = function(e) {
e = e || window.event;
// get the mouse cursor position at startup:
var pos3 = e.clientX,
pos4 = e.clientY;
document.onmouseup = function() {
document.onpointerup = null;
document.onmousemove = null;
document.ontouchmove = null;
document.ontouchend = null;
};
// call a function whenever the cursor moves:
setTouch(document, "touchend");
setTouch(document, "touchmove");
document.onmousemove = function(e) {
if (e.clientY > 0) { // stop windows from getting dragged off the top of the screen
var e = e || window.event,
// calculate the new cursor position:
pos1 = pos3 - e.clientX,
pos2 = pos4 - e.clientY;
e.preventDefault();
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
wrp.style.top = (wrp.offsetTop - pos2) + "px";
wrp.style.left = (wrp.offsetLeft - pos1) + "px";
}
}
}
}
function highZ() {
return Math.max.apply(null, Array.from(document.querySelectorAll('#main > div')).map(item => Number(item.style.zIndex))) + 1;
}
function makeVid(con, cls, wrp, hdr) {
var cont = null,
sender = getById("theirname").textContent;
con.once('stream', function(peerstream) {
if (hdr) {
hdr.textContent = "call with " + sender;
}
cont = crEl("video", wrp);
cont.autoplay = true;
cont.srcObject = peerstream;
});
cls.onclick = function() {
con.close();
}
}
function makeCloser(hwrap) {
var clsr = crEl("div", hwrap);
clsr.className = "closer";
clsr.textContent = "X";
return clsr;
}