plone / plone/mockup

Idea: Improve pat-formunloadalert

Open
#1,628 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

04 type: enhancement
Dominant language
JavaScript
Stars
57
Forks
103
Avg merge
10h 25m
Merged PRs (30d)
18

Description

Instead of marking a form as dirty as soon as events on input types are triggered we could instead on beforeunload compare the original input.value to input.defaultValue, available in browsers since 2015.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/defaultValue

I have a situation where opening the properties editing form in a mosaic layout edit view triggers a false form unload alert. If we would compare the original values to the current values, this would not happen.

const form = document.querySelector("form.pat-formunloadalert");

[...form.elements]
  .map(el => {
    let current, initial;

    if (el.type === "checkbox" || el.type === "radio") {
      current = el.checked;
      initial = el.defaultChecked;
    } else if (el.tagName === "SELECT") {
      current = [...el.options].map(o => o.selected);
      initial = [...el.options].map(o => o.defaultSelected);
    } else {
      current = el.value;
      initial = el.defaultValue;
    }

    return {
      name: el.name,
      id: el.id,
      type: el.type,
      current,
      initial,
      changed: JSON.stringify(current) !== JSON.stringify(initial)
    };
  })
  .filter(x => x.changed);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the pat-formunloadalert implementation and inspect its beforeunload handling. Compare each form control's current state with its default state as described in the issue, then verify that unchanged forms do not trigger an alert while changed forms still do.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.