Skip to content

Conversation

thet
Copy link
Member

@thet thet commented Nov 11, 2020

Plus:

  • Implement documented behavior to not focus on prefilled element, if there is another autofocus element which is empty.
  • Instead of calling autofocus for each element call it only once.
  • Register event handler only once.

@thet thet changed the title Scr 699 Do not autofocus in iframes + other fixes. Nov 11, 2020
setFocus(target) {
const $all = $(target);
const visible = [...$all].filter((it) => $(it).is(":visible"));
const empty = visible.filter((it) => it.value === "");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pilz: this fix is for the documented behavior (in documentation.md + the index.html) to not autofocus if the input is not empty and there is another autofocus element.

// setFocus normally operates on the whole DOM anyways and if scoped
// (via the event handler in init) the last one wins.
// This is OK since only one element can have the focus.
clearTimeout(scheduled_task);
Copy link
Member Author

@thet thet Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pilz this is an optimization to call focus() only once and not multiple times for each pat-autofocus element which can be found in the DOM.

$visible.get(0) && $visible.get(0).focus();
}, 10);

if (!registered_event_handler) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pilz: We want the global event handler registered only once.

export default autofocus;
setFocus(target) {
// Exit if task is scheduled. setFocus operates on whole DOM anyways.
if (scheduled_task) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pilz: If there is already a setFocus task scheduled, exit. No need to do it multiple times as this method operates on the whole DOM tree and is not scoped for the element itself.


init: function init() {
init() {
if (window.self !== window.top) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pilz: requested fix.

@thet thet removed the hold merge label Nov 11, 2020
@thet thet requested a review from pilz November 11, 2020 19:17
@pilz pilz merged commit 60a47bc into master Nov 16, 2020
@pilz pilz deleted the scr-699 branch November 16, 2020 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pat-autofocus in an iframe context

2 participants