-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[18.0][IMP] sign_oca: Add guided arrow flow to sign_oca
- Loading branch information
1 parent
9074efa
commit bd5a9ad
Showing
4 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
sign_oca/static/src/components/sign_oca_pdf_common/sign_oca_navigator.esm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/** @odoo-module QWeb **/ | ||
/* global document, window, console */ | ||
import {_t} from "@web/core/l10n/translation"; | ||
|
||
export function offset(el) { | ||
const box = el.getBoundingClientRect(); | ||
const docElem = document.documentElement; | ||
return { | ||
top: box.top + window.scrollY - docElem.clientTop, | ||
left: box.left + window.scrollY - docElem.clientLeft, | ||
}; | ||
} | ||
|
||
/** | ||
* Starts the sign item navigator | ||
* @param { SignablePDFIframe } parent | ||
* @param { HTMLElement } target | ||
* @param { Environment } env | ||
*/ | ||
export function startSignItemNavigator(parent, target, env) { | ||
const state = { | ||
started: false, | ||
isScrolling: false, | ||
}; | ||
const checkSignItemsCompletion = parent.checkSignItemsCompletion(); | ||
let signItemsToComplete = checkSignItemsCompletion; | ||
|
||
const navigator = document.createElement("div"); | ||
navigator.classList.add("o_sign_sign_item_navigator"); | ||
const navLine = document.createElement("div"); | ||
navLine.classList.add("o_sign_sign_item_navline"); | ||
|
||
function _scrollToSignItemPromise(item) { | ||
return new Promise((resolve) => { | ||
if (env.isSmall) { | ||
state.isScrolling = true; | ||
item.scrollIntoView({ | ||
behavior: "smooth", | ||
block: "center", | ||
inline: "center", | ||
}); | ||
return resolve(); | ||
} | ||
|
||
state.isScrolling = true; | ||
const viewer = parent.iframe.el.contentDocument.getElementById("viewer"); | ||
|
||
// Recalculate container height each time | ||
const containerHeight = target.offsetHeight; | ||
const viewerHeight = viewer.offsetHeight; | ||
|
||
let scrollOffset = containerHeight / 4; | ||
|
||
// Get the latest scroll position | ||
const updatedScrollTop = | ||
offset(item).top - offset(viewer).top - scrollOffset; | ||
|
||
// Adjust for overscroll cases | ||
if (updatedScrollTop + containerHeight > viewerHeight) { | ||
scrollOffset += updatedScrollTop + containerHeight - viewerHeight; | ||
} | ||
if (updatedScrollTop < 0) { | ||
scrollOffset += updatedScrollTop; | ||
} | ||
|
||
// Ensure navigator updates properly | ||
scrollOffset += | ||
offset(target).top - | ||
navigator.offsetHeight / 2 + | ||
item.getBoundingClientRect().height / 2; | ||
|
||
// Dynamic animation duration | ||
const duration = Math.max( | ||
Math.min( | ||
500, | ||
5 * | ||
(Math.abs(target.scrollTop - updatedScrollTop) + | ||
Math.abs(navigator.getBoundingClientRect().top) - | ||
scrollOffset) | ||
), | ||
100 | ||
); | ||
|
||
// Perform scroll | ||
target.scrollTo({top: updatedScrollTop, behavior: "smooth"}); | ||
|
||
// Update navigator animation | ||
const an = navigator.animate( | ||
{top: `${scrollOffset}px`}, | ||
{duration, fill: "forwards"} | ||
); | ||
const an2 = navLine.animate( | ||
{top: `${scrollOffset}px`}, | ||
{duration, fill: "forwards"} | ||
); | ||
|
||
Promise.all([an.finished, an2.finished]).then(() => { | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
function setTip(text) { | ||
navigator.style.fontFamily = "Helvetica"; | ||
navigator.innerText = text; | ||
} | ||
|
||
/** | ||
* Sets the entire radio set on focus. | ||
* @param {Number} radio_set_id | ||
*/ | ||
function highligtRadioSet(radio_set_id) { | ||
parent | ||
.checkSignItemsCompletion() | ||
.filter((item) => item.data.radio_set_id === radio_set_id) | ||
.forEach((item) => { | ||
item.el.classList.add("ui-selected"); | ||
}); | ||
} | ||
|
||
function scrollToSignItem({el: item, data}) { | ||
_scrollToSignItemPromise(item).then(() => { | ||
// Define input to deal with input fields if present | ||
const input = item.querySelector("input"); | ||
if (input && input.type === "text") { | ||
item.value = item.querySelector("input").value; | ||
item.focus = () => item.querySelector("input").focus(); | ||
} | ||
// Maybe store signature in data rather than in the dataset | ||
if (item.value === "" && !item.dataset.signature) { | ||
setTip(_t("next")); | ||
} | ||
if (data.type === "radio") { | ||
// We need to highligt the entire radio set items | ||
highligtRadioSet(data.radio_set_id); | ||
} else { | ||
item.focus(); | ||
item.classList.add("ui-selected"); | ||
} | ||
|
||
if (input && ["signature", "initial"].includes(input.type)) { | ||
if (item.dataset.hasFocus) { | ||
const clickableElement = data.isSignItemEditable | ||
? item.querySelector(".o_sign_item_display") | ||
: item; | ||
clickableElement.click(); | ||
} else { | ||
item.dataset.hasFocus = true; | ||
} | ||
} | ||
state.isScrolling = false; | ||
}); | ||
} | ||
|
||
function goToNextSignItem() { | ||
if (!state.started) { | ||
state.started = true; | ||
goToNextSignItem(); | ||
return false; | ||
} | ||
const selectedElements = target.querySelectorAll(".ui-selected"); | ||
selectedElements.forEach((selectedElement) => { | ||
selectedElement.classList.remove("ui-selected"); | ||
}); | ||
if (signItemsToComplete.length > 0) { | ||
console.log( | ||
"nbPages", | ||
parent.iframe.el.contentDocument.getElementsByClassName("page").length | ||
); | ||
scrollToSignItem(signItemsToComplete[0]); | ||
} | ||
} | ||
|
||
target.append(navigator); | ||
navigator.before(navLine); | ||
navigator.addEventListener("click", () => { | ||
if (checkSignItemsCompletion.length > 0) { | ||
goToNextSignItem(); | ||
signItemsToComplete = signItemsToComplete.slice(1); | ||
} | ||
}); | ||
|
||
setTip(_t("Click to start")); | ||
navigator.focus(); | ||
|
||
function toggle(force) { | ||
navigator.style.display = force ? "" : "none"; | ||
navLine.style.display = force ? "" : "none"; | ||
} | ||
|
||
return { | ||
setTip, | ||
goToNextSignItem, | ||
toggle, | ||
state, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters