<!--
@license GNU AGPL version 3 or any later version

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
    <div id="side-menu" class="side-menu-big">
        <div class="side-menu-header">
            <button class="side-menu-opener"></button>
        </div>

        <div class="side-menu-categories-wrapper">
            <div class="side-menu-categories">
                <div class="side-menu-category" v-for="category in items">
                    <h2 class="side-menu-category-title" v-if="category.name != ''" v-html="category.name"></h2>

                    <ul class="side-menu-apps-list">
                        <li v-bind:class="{'side-menu-app': true, 'active': activeApp === appId}" v-for="(app, appId) in category.apps">
                            <a v-bind:href="app.href" v-bind:title="app.name">
                                 <img class="side-menu-app-icon" v-bind:src="app.icon"></span>
                                 <span class="side-menu-app-text" v-html="app.name"></span>
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import trim from 'trim'
import axios from 'axios'

export default {
    name: 'SideMenuBig',
    data() {
        return {
            items: [],
            activeApp: null,
        }
    },
    methods: {
        retrieveApps() {
            this.apps = []
            let that = this

            axios
                .get(OC.generateUrl('/apps/side_menu/nav/items'))
                .then(function(response) {
                    that.items = response.data.items

                    let apps = []

                    for (let category of that.items) {
                        for (let app of category.apps) {
                            apps.push(app)
                        }
                    }

                    jQuery('body').trigger('side-menu.apps', [apps])
                });
        },
        retrieveActiveApp() {
            let activeAppLink = document.querySelector('#appmenu a.active')
            this.activeApp = activeAppLink ? activeAppLink.parentNode.getAttribute('data-id') : null
        },
    },
    mounted() {
        this.retrieveApps()
        this.retrieveActiveApp()
    }
}
</script>
