import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
export default () => {
return axios.get(generateOcsUrl('core/navigation', 2) + '/apps?format=json')
.then(({ data }) => {
if (data.ocs.meta.statuscode !== 200) {
return
}
const addedApps = {}
const navEntries = data.ocs.data
const container = document.querySelector('#navigation #apps ul')
// remove disabled apps
navEntries.forEach((entry) => {
if (!container.querySelector('li[data-id="' + entry.id + '"]')) {
addedApps[entry.id] = true
}
})
container.querySelectorAll('li[data-id]').forEach((el, index) => {
const id = el.dataset.id
// remove all apps that are not in the correct order
if (!navEntries[index] || (navEntries[index] && navEntries[index].id !== id)) {
el.remove()
document.querySelector(`#appmenu li[data-id=${id}]`).remove()
}
})
let previousEntry = {}
// add enabled apps to #navigation and #appmenu
navEntries.forEach((entry) => {
if (container.querySelector(`li[data-id="${entry.id}"]`) === null) {
const li = document.createElement('li')
li.dataset.id = entry.id
const img = ``
const imgElement = document.createElement('template')
imgElement.innerHTML = img
const a = document.createElement('a')
a.setAttribute('href', entry.href)
const filename = document.createElement('span')
filename.appendChild(document.createTextNode(entry.name))
const loading = document.createElement('div')
loading.setAttribute('class', 'unread-counter')
loading.style.display = 'none'
// draw attention to the newly added app entry
// by flashing twice the more apps menu
if (addedApps[entry.id]) {
a.classList.add('animated')
}
a.prepend(imgElement.content.firstChild, loading, filename)
li.append(a)
// add app icon to the navigation
const previousElement = document.querySelector(`#navigation li[data-id=${previousEntry.id}]`)
if (previousElement) {
previousElement.insertAdjacentElement('afterend', li)
} else {
document.querySelector('#navigation #apps ul').prepend(li)
}
}
if (document.getElementById('appmenu').querySelector(`li[data-id="${entry.id}"]`) === null) {
const li = document.createElement('li')
li.dataset.id = entry.id
// Generating svg embedded image (see layout.user.php)
let img
if (OCA.Theming && OCA.Theming.inverted) {
img = ``
} else {
img = ``
}
const imgElement = document.createElement('template')
imgElement.innerHTML = img
const a = document.createElement('a')
a.setAttribute('href', entry.href)
const filename = document.createElement('span')
filename.appendChild(document.createTextNode(entry.name))
const loading = document.createElement('div')
loading.setAttribute('class', 'icon-loading-dark')
loading.style.display = 'none'
// draw attention to the newly added app entry
// by flashing twice the more apps menu
if (addedApps[entry.id]) {
a.classList.add('animated')
}
a.prepend(loading, filename, imgElement.content.firstChild)
li.append(a)
// add app icon to the navigation
const previousElement = document.querySelector('#appmenu li[data-id=' + previousEntry.id + ']')
if (previousElement) {
previousElement.insertAdjacentElement('afterend', li)
} else {
document.queryElementById('appmenu').prepend(li)
}
}
previousEntry = entry
})
window.dispatchEvent(new Event('resize'))
})
}