Issue 392 Project Dialogs Shared Element Ids - edmozley/freeitsm GitHub Wiki
๐งฉ #392 "Add someone" on a project did nothing, and the Income dialog never saved
GitHub #392, reported by @acelr22 ยท Projects ยท People broken in 5.5.0, Income broken since 5.1.0, both fixed in 5.6.0 (#2999)
What you saw
After updating to 5.5.0: open a project, People โ + Add someone, choose an analyst, team, person or contractor and a role, click Add - and nothing happens. No message, nothing in the browser console.
While tracking it down, a second fault turned up on the same page: the Income dialog on the Budget tab (5.1.0) never saved either, and saving a Scope deliverable quietly sent an income save as well.
What was wrong
Every element on a page should have its own id. When two share one, document.getElementById() returns the first one in the page and says nothing about the second.
The project page draws all its dialogs at once, and two pairs of them shared ids:
| Dialog added | Reused ids from | Effect |
|---|---|---|
| Move to company (5.5.0, #370) | People dialog: pmSave, pmError |
The Move dialog comes first in the page, so the People code wired its Add handler to the Move button. People's own Add button had no handler at all |
| Income (5.1.0, #282) | Scope dialog: piTitle, piId, piName, piError, piDelete, piSave |
The Scope dialog comes first, so the Income code filled in, read and wired the Scope dialog's elements. Income's Save did nothing; Scope's Save ran both the deliverable save and an income save |
// assets/js/projects-tools.js - the People dialog
document.getElementById('pmSave').addEventListener('click', saveMember); // found the Move button
// assets/js/projects-view.js - the Move dialog (5.5.0)
document.getElementById('pmSave').addEventListener('click', saveMove); // the same Move button
Nothing failed loudly: a handler on the wrong button is valid JavaScript, and a button with no handler just does nothing. That is why both slipped through, and why the second had gone unnoticed since 5.1.0.
Was it more than one place? On this page, two pairs (8 ids). A scan of every page in the app found 6 more repeats elsewhere, all harmless: two alternative branches of which only one is ever drawn (the sign-in page, the portal's My / Team tickets) or a JavaScript string that replaces the element holding the first.
Did the stray income save do any damage? It sends the deliverable's name as the income's name, which is all an income line needs. On the two installs checked (a test install and the maintainer's own) there were no income lines named after a deliverable. On an install that used income and scope together since 5.1.0, look for income lines with no amounts named exactly like a deliverable; they can simply be deleted.
The fix
- The Move dialog's ids are now
pmvCompany,pmvError,pmvSave. - The Income dialog's six are now
pinTitle,pinId,pinName,pinError,pinDelete,pinSave. - A guard against the whole class:
tests/duplicate-element-ids.phpcounts every literalid="..."in every page file and fails on a repeat, unless it is listed with the reason only one is ever on the page. It runs in CI (Web exposure job). On the old page it finds all 8.
๐ Files
| File | Change |
|---|---|
๐ต projects/view.php |
The Move and Income dialogs' ids; cache-busters |
๐ก assets/js/projects-view.js |
The Move dialog uses pmv* |
๐ก assets/js/projects-budget.js |
The Income dialog uses pin* |
๐ข tests/duplicate-element-ids.php |
The guard, with the 6 safe repeats explained |
โช .github/workflows/web-exposure.yml |
Runs the guard |
How it was verified
Reproduced first in a real browser on a test install, with requests recorded instead of sent: clicking Add in the People dialog sent nothing, and getElementById('pmSave') resolved to the Move dialog. Clicking Income's Save sent nothing; clicking the deliverable's Save sent item_save and income_line_save.
After the fix, the same run: Add sends member_add; Income's Save sends income_line_save; the deliverable's Save sends only item_save. The guard passes on all 899 page files and fails on the old project page.
What this means for you
Update to 5.6.0 and adding people to a project, and adding income lines, work again.
For developers: dialogs drawn together on one page share one id space. Prefix a new dialog's ids with something unique to it (pmv for Move, pin for Income), not with its neighbour's prefix. The guard will tell you if you forget.
Related: Projects ยท Projects - Developer Guide ยท #370 move a project