| const { menavSanitizeClassList, menavSanitizeUrl } = require('../shared'); |
|
|
| |
| module.exports = function updateElement(type, id, newData) { |
| const element = this._findElement(type, id); |
| if (!element) return false; |
|
|
| if (type === 'site') { |
| |
| if (newData.url) { |
| const safeUrl = menavSanitizeUrl(newData.url, 'updateElement(site).url'); |
| element.setAttribute('href', safeUrl); |
| |
| element.setAttribute('data-url', String(newData.url).trim()); |
| } |
| if (newData.name) { |
| element.querySelector('h3').textContent = newData.name; |
| element.setAttribute('data-name', newData.name); |
| } |
| if (newData.description) { |
| element.querySelector('p').textContent = newData.description; |
| element.setAttribute('data-description', newData.description); |
| } |
| if (newData.icon) { |
| const iconElement = |
| element.querySelector('i.icon-fallback') || |
| element.querySelector('i.site-icon') || |
| element.querySelector('.site-card-icon i') || |
| element.querySelector('i'); |
|
|
| if (iconElement) { |
| const nextIconClass = menavSanitizeClassList(newData.icon, 'updateElement(site).icon'); |
| const preservedClasses = []; |
|
|
| if (iconElement.classList.contains('icon-fallback')) { |
| preservedClasses.push('icon-fallback'); |
| } |
| if (iconElement.classList.contains('site-icon')) { |
| preservedClasses.push('site-icon'); |
| } |
|
|
| if (nextIconClass) { |
| iconElement.className = nextIconClass; |
| preservedClasses.forEach((cls) => iconElement.classList.add(cls)); |
| } |
| } |
| element.setAttribute( |
| 'data-icon', |
| menavSanitizeClassList(newData.icon, 'updateElement(site).data-icon') |
| ); |
| } |
| if (newData.title) element.title = newData.title; |
|
|
| |
| this.events.emit('elementUpdated', { |
| id: id, |
| type: 'site', |
| data: newData, |
| }); |
|
|
| return true; |
| } else if (type === 'category') { |
| |
| if (newData.name) { |
| const titleElement = element.querySelector('h2'); |
| if (titleElement) { |
| const iconElement = titleElement.querySelector('i'); |
| const iconClass = iconElement ? iconElement.className : ''; |
| const nextIcon = menavSanitizeClassList( |
| newData.icon || iconClass, |
| 'updateElement(category).icon' |
| ); |
|
|
| |
| titleElement.textContent = ''; |
| const nextIconEl = document.createElement('i'); |
| if (nextIcon) nextIconEl.className = nextIcon; |
| titleElement.appendChild(nextIconEl); |
| titleElement.appendChild(document.createTextNode(' ' + String(newData.name))); |
| } |
| element.setAttribute('data-name', newData.name); |
| } |
| if (newData.icon) { |
| element.setAttribute( |
| 'data-icon', |
| menavSanitizeClassList(newData.icon, 'updateElement(category).data-icon') |
| ); |
| } |
|
|
| |
| this.events.emit('elementUpdated', { |
| id: id, |
| type: 'category', |
| data: newData, |
| }); |
|
|
| return true; |
| } else if (type === 'nav-item') { |
| |
| if (newData.name) { |
| const textElement = element.querySelector('.nav-text'); |
| if (textElement) { |
| textElement.textContent = newData.name; |
| } |
| element.setAttribute('data-name', newData.name); |
| } |
| if (newData.icon) { |
| const iconElement = element.querySelector('i'); |
| if (iconElement) { |
| iconElement.className = menavSanitizeClassList( |
| newData.icon, |
| 'updateElement(nav-item).icon' |
| ); |
| } |
| element.setAttribute( |
| 'data-icon', |
| menavSanitizeClassList(newData.icon, 'updateElement(nav-item).data-icon') |
| ); |
| } |
|
|
| |
| this.events.emit('elementUpdated', { |
| id: id, |
| type: 'nav-item', |
| data: newData, |
| }); |
|
|
| return true; |
| } else if (type === 'social-link') { |
| |
| if (newData.url) { |
| const safeUrl = menavSanitizeUrl(newData.url, 'updateElement(social-link).url'); |
| element.setAttribute('href', safeUrl); |
| |
| element.setAttribute('data-url', String(newData.url).trim()); |
| } |
| if (newData.name) { |
| const textElement = element.querySelector('.nav-text'); |
| if (textElement) { |
| textElement.textContent = newData.name; |
| } |
| element.setAttribute('data-name', newData.name); |
| } |
| if (newData.icon) { |
| const iconElement = element.querySelector('i'); |
| if (iconElement) { |
| iconElement.className = menavSanitizeClassList( |
| newData.icon, |
| 'updateElement(social-link).icon' |
| ); |
| } |
| element.setAttribute( |
| 'data-icon', |
| menavSanitizeClassList(newData.icon, 'updateElement(social-link).data-icon') |
| ); |
| } |
|
|
| |
| this.events.emit('elementUpdated', { |
| id: id, |
| type: 'social-link', |
| data: newData, |
| }); |
|
|
| return true; |
| } |
|
|
| return false; |
| }; |
|
|