Skip to content

Commit 5ae2b1d

Browse files
committed
Update appended elements cleanup
1 parent e567885 commit 5ae2b1d

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/browser/CameraProxy.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ function capture (successCallback, errorCallback, opts) {
4444
const customCaptureButton = opts[13];
4545
const customCancelButton = opts[14];
4646

47+
const customElements = {customCameraContainer, customCaptureButton, customCancelButton};
48+
4749
let parent = customCameraContainer ? document.getElementById(customCameraContainer) : createCameraContainer();
4850
let video = createVideoStreamContainer(parent, targetWidth, targetHeight);
4951
let captureButton = customCaptureButton ? document.getElementById(customCaptureButton) : createButton(parent, 'Capture');
5052
let cancelButton = customCancelButton ? document.getElementById(customCancelButton) : createButton(parent, 'Cancel');
51-
5253
// start video stream
5354
startLocalMediaStream(errorCallback, video);
5455

@@ -59,8 +60,8 @@ function capture (successCallback, errorCallback, opts) {
5960
}
6061

6162
// handle button click events
62-
handleCaptureButton(successCallback, errorCallback, captureButton, video, customCameraContainer);
63-
handleCancelButton(cancelButton, video, customCameraContainer);
63+
handleCaptureButton(successCallback, errorCallback, captureButton, video, customElements);
64+
handleCancelButton(cancelButton, video, customElements);
6465
}
6566

6667
function createCameraContainer () {
@@ -94,7 +95,7 @@ function createButton (parent, innerText) {
9495
return button;
9596
}
9697

97-
function handleCaptureButton (successCallback, errorCallback, captureButton, video, customCameraContainer) {
98+
function handleCaptureButton (successCallback, errorCallback, captureButton, video, customElements) {
9899
captureButton.onclick = function () {
99100
// create a canvas and capture a frame from video stream
100101
let canvas = document.createElement('canvas');
@@ -107,16 +108,16 @@ function handleCaptureButton (successCallback, errorCallback, captureButton, vid
107108
imageData = imageData.replace('data:image/png;base64,', '');
108109

109110
// stop video stream
110-
stopLocalMediaStream(video, customCameraContainer);
111+
stopLocalMediaStream(video, customElements);
111112

112113
return successCallback(imageData);
113114
};
114115
}
115116

116-
function handleCancelButton (cancelButton, video, customCameraContainer) {
117+
function handleCancelButton (cancelButton, video, customElements) {
117118
cancelButton.onclick = function () {
118119
// stop video stream
119-
stopLocalMediaStream(video, customCameraContainer);
120+
stopLocalMediaStream(video, customElements);
120121
};
121122
}
122123

@@ -140,7 +141,7 @@ function startLocalMediaStream (errorCallback, video) {
140141
}
141142
}
142143

143-
function stopLocalMediaStream (video, customCameraContainer) {
144+
function stopLocalMediaStream (video, customElements) {
144145
// stop video stream, remove video and captureButton.
145146
// note: MediaStream.stop() is deprecated as of Chrome 47.
146147
if (localMediaStream.stop) {
@@ -152,13 +153,25 @@ function stopLocalMediaStream (video, customCameraContainer) {
152153
}
153154

154155
// remove newly created elements
155-
removeAppendedCameraElements(video, customCameraContainer);
156+
removeAppendedCameraElements(video, customElements);
156157
}
157158

158-
function removeAppendedCameraElements (video, customCameraContainer) {
159+
function removeAppendedCameraElements (video, customElements) {
160+
159161
const parent = video.parentNode;
160-
if (!customCameraContainer) {
162+
163+
if (!customElements.customCameraContainer) {
161164
parent.parentNode.removeChild(parent);
165+
} else if (!customElements.customCaptureButton && !customElements.customCancelButton) {
166+
while (parent.hasChildNodes()) {
167+
parent.removeChild(parent.lastChild);
168+
}
169+
} else if (parent.hasChildNodes() && !customElements.customCaptureButton) {
170+
parent.removeChild(video);
171+
parent.removeChild(parent.lastChild);
172+
} else if (parent.hasChildNodes() && !customElements.customCancelButton) {
173+
parent.removeChild(video);
174+
parent.removeChild(parent.lastChild);
162175
} else {
163176
parent.removeChild(video);
164177
}

0 commit comments

Comments
 (0)