Commit 18df76b8 authored by Gabriel Monnerat's avatar Gabriel Monnerat

erp5_document_scanner: Improve code

parent 20a63d96
...@@ -14,6 +14,9 @@ if active_preference and preferred_cropped_canvas_data: ...@@ -14,6 +14,9 @@ if active_preference and preferred_cropped_canvas_data:
active_preference.setPreferredCroppedCanvasData(preferred_cropped_canvas_data) active_preference.setPreferredCroppedCanvasData(preferred_cropped_canvas_data)
if not image_str: if not image_str:
if batch_mode:
return None
return context.Base_renderForm('Base_viewUploadDocumentFromCameraDialog', return context.Base_renderForm('Base_viewUploadDocumentFromCameraDialog',
message='Nothing to capture') message='Nothing to capture')
...@@ -26,5 +29,9 @@ else: ...@@ -26,5 +29,9 @@ else:
active_process.postActiveResult(detail=image_str) active_process.postActiveResult(detail=image_str)
context.REQUEST.form.pop("field_your_document_scanner_gadget") context.REQUEST.form.pop("field_your_document_scanner_gadget")
context.REQUEST.form.pop('document_scanner_gadget') context.REQUEST.form.pop('document_scanner_gadget')
if batch_mode:
return active_process
return context.Base_renderForm('Base_viewUploadDocumentFromCameraDialog', return context.Base_renderForm('Base_viewUploadDocumentFromCameraDialog',
message='Captured') message='Captured')
portal = context.getPortalObject() portal = context.getPortalObject()
translateString = portal.Base_translateString translateString = portal.Base_translateString
active_process = context.Base_storeDocumentFromCameraInActiveProcess(
active_process_url,
batch_mode=True,
**kw)
# Avoid to pass huge images to the activity # Avoid to pass huge images to the activity
kw.pop("your_document_scanner_gadget", None) kw.pop("your_document_scanner_gadget", None)
context.activate().Base_uploadDocumentFromCamera( context.activate().Base_uploadDocumentFromCamera(
active_process_url=active_process_url, **kw) active_process_url=active_process.getRelativeUrl(),
**kw)
return context.Base_redirect('view', return context.Base_redirect('view',
keep_items={'portal_status_message': keep_items={'portal_status_message':
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
<div class="camera-output" style="display: none"> <div class="camera-output" style="display: none">
<img class="photo" alt="Photo"> <img class="photo" alt="Photo">
<input class="photoInput" type="hidden"> <input class="photoInput" type="hidden">
<input type="hidden" name="page-number" value="1">
</div> </div>
<div class="edit-picture"> <div class="edit-picture">
<button type="button" class="reset-btn ui-btn-icon-left ui-icon-times"></button> <button type="button" class="reset-btn ui-btn-icon-left ui-icon-times"></button>
......
...@@ -9,13 +9,10 @@ ...@@ -9,13 +9,10 @@
cropper, cropper,
video, video,
stream, stream,
canvas,
dialogMethod, dialogMethod,
photo, currentDeviceId,
photoInput,
deviceId,
imageCapture, imageCapture,
pageNumber = 1, pageNumber,
cameraList = []; cameraList = [];
function readBlobAsDataURL(blob) { function readBlobAsDataURL(blob) {
...@@ -32,7 +29,6 @@ ...@@ -32,7 +29,6 @@
} }
function takePicture(el) { function takePicture(el) {
var start, end;
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
return imageCapture.takePhoto({imageWidth: imageWidth}); return imageCapture.takePhoto({imageWidth: imageWidth});
...@@ -41,6 +37,8 @@ ...@@ -41,6 +37,8 @@
return readBlobAsDataURL(blob); return readBlobAsDataURL(blob);
}) })
.push(function (result) { .push(function (result) {
var photoInput = el.querySelector(".photoInput"),
photo = el.querySelector("img");
photo.setAttribute("src", result); photo.setAttribute("src", result);
photo.setAttribute("width", imageWidth); photo.setAttribute("width", imageWidth);
photo.setAttribute("height", imageHeight); photo.setAttribute("height", imageHeight);
...@@ -49,6 +47,13 @@ ...@@ -49,6 +47,13 @@
}); });
} }
function enableButton(root) {
[".reset-btn", ".take-picture-btn",
".confirm-btn", ".change-camera-btn"].forEach(function (e) {
root.querySelector(e).disabled = false;
});
}
function setPageOne(root) { function setPageOne(root) {
root.querySelector(".page-number").innerText = pageNumber; root.querySelector(".page-number").innerText = pageNumber;
root.querySelector(".reset-btn").style.display = "none"; root.querySelector(".reset-btn").style.display = "none";
...@@ -58,6 +63,7 @@ ...@@ -58,6 +63,7 @@
if (cameraList.length > 1) { if (cameraList.length > 1) {
root.querySelector(".change-camera-btn").style.display = "inline-block"; root.querySelector(".change-camera-btn").style.display = "inline-block";
} }
return enableButton(root);
} }
function setPageTwo(root) { function setPageTwo(root) {
...@@ -71,20 +77,14 @@ ...@@ -71,20 +77,14 @@
function disableButton(root) { function disableButton(root) {
[".reset-btn", ".take-picture-btn", [".reset-btn", ".take-picture-btn",
".confirm-btn", ".change-camera-btn"].forEach(function (e) { ".confirm-btn", ".change-camera-btn"].forEach(function (e) {
root.querySelector(e).disabled = true; root.querySelector(e).disabled = true;
}); });
}
function enableButton(root) {
[".reset-btn", ".take-picture-btn",
".confirm-btn", ".change-camera-btn"].forEach(function (e) {
root.querySelector(e).disabled = false;
});
} }
function drawCanvas(gadget, img) { function drawCanvas(gadget, img) {
var ratio, x, y; var ratio, x, y,
canvas = gadget.querySelector("canvas");
canvas.width = imageWidth; canvas.width = imageWidth;
canvas.height = imageHeight; canvas.height = imageHeight;
ratio = Math.min(canvas.width / img.width, canvas.height / img.height); ratio = Math.min(canvas.width / img.width, canvas.height / img.height);
...@@ -211,33 +211,37 @@ ...@@ -211,33 +211,37 @@
}); });
} }
function startup(root, device_id) { function startStream(root, device_id) {
video = root.querySelector(".video"); video = root.querySelector(".video");
canvas = root.querySelector(".canvas"); currentDeviceId = device_id;
photo = root.querySelector("img");
photoInput = root.querySelector(".photoInput");
return handleUserMedia(root, device_id, gotStream); return handleUserMedia(root, device_id, gotStream);
} }
rJS(window) rJS(window)
.declareAcquiredMethod("customSubmitDialog", "customSubmitDialog") .declareAcquiredMethod("customSubmitDialog", "customSubmitDialog")
.declareAcquiredMethod("notifySubmitted", "notifySubmitted") .declareAcquiredMethod("notifySubmitted", "notifySubmitted")
.declareJob("startStream", function (deviceId) {
return this.getElement()
.push(function (element) {
return startStream(element, deviceId);
});
})
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
var root; var root,
gadget = this;
return this.getElement() return this.getElement()
.push(function (element) { .push(function (element) {
root = element; root = element;
pageNumber = 1;
preferredCroppedCanvasData = preferredCroppedCanvasData || JSON.parse( preferredCroppedCanvasData = preferredCroppedCanvasData || JSON.parse(
options.preferred_cropped_canvas_data options.preferred_cropped_canvas_data
); );
dialogMethod = preferredCroppedCanvasData.dialog_method; dialogMethod = preferredCroppedCanvasData.dialog_method;
// Clear photo input // Clear photo input
element.querySelector('.photoInput').value = ""; //element.querySelector('.photoInput').value = "";
if (deviceId) { pageNumber = parseInt(element.querySelector('input[name="page-number"]').value, 10);
root.querySelector(".camera-input").style.display = ""; root.querySelector(".camera-input").style.display = "";
root.querySelector(".camera-output").style.display = "none"; root.querySelector(".camera-output").style.display = "none";
}
if (!navigator.mediaDevices) { if (!navigator.mediaDevices) {
throw ("mediaDevices is not supported"); throw ("mediaDevices is not supported");
} }
...@@ -246,6 +250,7 @@ ...@@ -246,6 +250,7 @@
.push(function (info_list) { .push(function (info_list) {
var j, var j,
device, device,
deviceId,
len = info_list.length; len = info_list.length;
if (cameraList.length === 0) { if (cameraList.length === 0) {
...@@ -260,10 +265,7 @@ ...@@ -260,10 +265,7 @@
// trick to select back camera in mobile // trick to select back camera in mobile
deviceId = cameraList[cameraList.length - 1].deviceId; deviceId = cameraList[cameraList.length - 1].deviceId;
} }
gadget.startStream(deviceId);
if (deviceId) {
return startup(root, deviceId);
}
}); });
}) })
.declareMethod('getContent', function () { .declareMethod('getContent', function () {
...@@ -277,27 +279,30 @@ ...@@ -277,27 +279,30 @@
return result; return result;
}) })
.onEvent("click", function (evt) { .onEvent("click", function (evt) {
var root, var e,
root,
deviceId,
newPreferredCroppedCanvasData, newPreferredCroppedCanvasData,
gadget = this; gadget = this;
if (evt.target.name === "grayscale") { if (evt.target.name === "grayscale") {
return this.getElement() return this.getElement()
.push(function () { .push(function (el) {
return grayscale(canvas, photo); return grayscale(el.querySelector(".canvas"), el.querySelector('.photo'));
}); });
} }
if (evt.target.className.indexOf("change-camera-btn") !== -1) { if (evt.target.className.indexOf("change-camera-btn") !== -1) {
evt.preventDefault(); evt.preventDefault();
for (var e in cameraList) {
if (cameraList[e].deviceId !== deviceId) { for (e in cameraList) {
deviceId = cameraList[e].deviceId; if (cameraList.hasOwnProperty(e)) {
break; if (cameraList[e].deviceId !== currentDeviceId) {
deviceId = cameraList[e].deviceId;
break;
}
} }
} }
return gadget.getElement() gadget.startStream(deviceId);
.push(function (root) {
return startup(root, deviceId);
});
} }
if (evt.target.className.indexOf("take-picture-btn") !== -1) { if (evt.target.className.indexOf("take-picture-btn") !== -1) {
evt.preventDefault(); evt.preventDefault();
...@@ -305,10 +310,10 @@ ...@@ -305,10 +310,10 @@
.push(function (el) { .push(function (el) {
root = el; root = el;
el.querySelector(".camera").style.maxWidth = video.offsetWidth + "px"; el.querySelector(".camera").style.maxWidth = video.offsetWidth + "px";
el.querySelector(".camera-input").style.display = "none";
return takePicture(el); return takePicture(el);
}) })
.push(function () { .push(function () {
root.querySelector(".camera-input").style.display = "none";
return setPageTwo(root); return setPageTwo(root);
}); });
} }
...@@ -325,9 +330,9 @@ ...@@ -325,9 +330,9 @@
if (evt.target.className.indexOf("confirm-btn") !== -1) { if (evt.target.className.indexOf("confirm-btn") !== -1) {
evt.preventDefault(); evt.preventDefault();
newPreferredCroppedCanvasData = cropper.getData(); newPreferredCroppedCanvasData = cropper.getData();
for (var p in newPreferredCroppedCanvasData) { for (e in newPreferredCroppedCanvasData) {
if (newPreferredCroppedCanvasData.hasOwnProperty(p)) { if (newPreferredCroppedCanvasData.hasOwnProperty(e)) {
preferredCroppedCanvasData[p] = newPreferredCroppedCanvasData[p]; preferredCroppedCanvasData[e] = newPreferredCroppedCanvasData[e];
} }
} }
return this.getElement() return this.getElement()
...@@ -351,16 +356,16 @@ ...@@ -351,16 +356,16 @@
block = base64data.split(";"), block = base64data.split(";"),
realData = block[1].split(",")[1]; realData = block[1].split(",")[1];
photo.src = base64data; root.querySelector(".photo").src = base64data;
photoInput.value = realData; root.querySelector(".photoInput").value = realData;
cropper.destroy(); cropper.destroy();
}) })
.push(function () { .push(function () {
return gadget.customSubmitDialog(dialogMethod); return gadget.customSubmitDialog(dialogMethod);
}) })
.push(function () { .push(function () {
enableButton(root);
pageNumber = pageNumber + 1; pageNumber = pageNumber + 1;
root.querySelector('input[name="page-number"]').value = pageNumber;
return setPageOne(root); return setPageOne(root);
}); });
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment