* Convert a potential selector into a single HTML element. * Similar to getElements() but returns only the first match. * * @param els selector string or HTMLElement * @param root optional root element to search within (defaults to document) * @returns the first HTML element matching
(els: GridStackElement, root: HTMLElement | Document = document)
| 153 | * const first = Utils.getElement('.grid-item'); |
| 154 | */ |
| 155 | static getElement(els: GridStackElement, root: HTMLElement | Document = document): HTMLElement { |
| 156 | if (typeof els === 'string') { |
| 157 | const doc = ('getElementById' in root) ? root as Document : undefined; |
| 158 | if (!els.length) return null; |
| 159 | if (doc && els[0] === '#') { |
| 160 | return doc.getElementById(els.substring(1)); |
| 161 | } |
| 162 | if (els[0] === '#' || els[0] === '.' || els[0] === '[') { |
| 163 | return root.querySelector(els); |
| 164 | } |
| 165 | |
| 166 | // if we start with a digit, assume it's an id (error calling querySelector('#1')) as class are not valid CSS |
| 167 | if (doc && !isNaN(+els[0])) { // start with digit |
| 168 | return doc.getElementById(els); |
| 169 | } |
| 170 | |
| 171 | // finally try string, then id, then class |
| 172 | let el = root.querySelector(els); |
| 173 | if (doc && !el) { el = doc.getElementById(els) } |
| 174 | if (!el) { el = root.querySelector('.' + els) } |
| 175 | return el as HTMLElement; |
| 176 | } |
| 177 | return els; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Check if a widget should be lazy loaded based on node or grid settings. |
no outgoing calls
no test coverage detected