EN · DE · RU · FR · ES

#2760: select.js

projectforge-webapp/src/utilities/select.js DOM Utility (single export) · projectforge-webapp/src/utilities/select.js 17 lines · 14 code · 2 comments · 1 blank
A cross-browser text selection utility that programmatically selects the entire text content of a DOM node, enabling a single-click-to-copy behavior for read-only text fields. The function handles two eras of browser APIs: the legacy Internet Explorer createTextRange approach and the modern standards-based window.getSelection() / document.createRange() approach. This is invoked by the DynamicReadonlyField component so that clicking a read-only field selects its text for easy copy-paste.

Architecture

Dual Code Path

The function branches on document.body.createTextRange — this property only exists in Internet Explorer (and early Edge versions). If present, it uses the IE-proprietary TextRange object, calling moveToElementText to bound the range to the node's content, then select() to highlight it. If the property is absent (all modern browsers), it uses the W3C Selection API: creates a Range object, calls selectNodeContents on the target node, clears any existing selection, and adds the new range.

Usage Context

Imported by the DynamicLayout input components, specifically DynamicReadonlyField. When a user clicks a read-only field (e.g., a calculated cost center number or system-generated identifier), all its text content is automatically selected, allowing immediate copy via Ctrl+C / Cmd+C without a manual drag-select. Source reference for the algorithm: Stack Overflow answer #987376.

Git History

CommitWhat changed
21305e884Added two comments: a source attribution linking to the Stack Overflow answer the algorithm was adapted from, and an inline label // internet-explorer above the createTextRange branch to clarify why two different selection APIs are used. No functional changes.
117f7fe25Created the file with the cross-browser text selection function and its default export. This was part of implementing the "click to select" behavior for read-only fields in the webapp's dynamic form layout.