scinote-web/app/javascript/vue/navigation/top_menu.vue

186 lines
6.4 KiB
Vue
Raw Normal View History

<template>
<div class="sci--navigation--top-menu-container">
<div v-if="user" class="sci--navigation--top-menu-search left-icon sci-input-container-v2" :class="{'disabled' : !currentTeam}" :title="i18n.t('nav.search')">
2023-06-13 18:38:55 +08:00
<input type="text" :placeholder="i18n.t('nav.search')" @change="searchValue"/>
<i class="sn-icon sn-icon-search"></i>
</div>
<div v-if="currentTeam" class="w-64">
<Select
:value="currentTeam"
:options="teams"
:placeholder="'test'"
:noOptionsPlaceholder="'test'"
v-bind:disabled="false"
@change="switchTeam"
></Select>
</div>
2023-09-22 17:59:39 +08:00
<MenuDropdown
class="ml-auto"
2023-10-02 20:31:28 +08:00
v-if="settingsMenu && settingsMenu.length > 0"
:listItems="settingsMenuItems"
2023-10-16 15:52:06 +08:00
:title="i18n.t('nav.settings')"
2023-09-22 17:59:39 +08:00
:btnClasses="'btn btn-light icon-btn btn-black'"
:position="'right'"
:btnIcon="'sn-icon sn-icon-settings'"
:data-e2e="'e2e-DD-topMenu-settings'"
2023-09-22 17:59:39 +08:00
></MenuDropdown>
<div v-if="user" class="sci--navigation--notificaitons-flyout-container">
2023-06-14 20:40:26 +08:00
<button class="btn btn-light icon-btn btn-black"
:data-e2e="'e2e-DD-topMenu-notifications'"
2023-06-22 17:55:23 +08:00
:title="i18n.t('nav.notifications.title')"
:class="{ 'has-unseen': unseenNotificationsCount > 0 }"
:data-unseen="unseenNotificationsCount"
data-toggle="dropdown"
@click="notificationsOpened = !notificationsOpened">
2023-06-08 14:33:37 +08:00
<i class="sn-icon sn-icon-notifications"></i>
</button>
<div v-if="notificationsOpened" class="sci--navigation--notificaitons-flyout-backdrop" @click="notificationsOpened = false"></div>
<NotificationsFlyout
v-if="notificationsOpened"
:notificationsUrl="notificationsUrl"
2024-01-16 17:34:06 +08:00
:preferencesUrl="this.userMenu.find((item) => item.name === i18n.t('users.settings.sidebar.account_nav.preferences'))?.url"
:unseenNotificationsCount="unseenNotificationsCount"
@update:unseenNotificationsCount="checkUnseenNotifications()"
@close="notificationsOpened = false" />
</div>
<div v-if="user" class="dropdown" :title="i18n.t('nav.user_profile')">
<div class="sci--navigation--top-menu-user btn btn-light icon-btn btn-black" data-toggle="dropdown" data-e2e="e2e-DD-topMenu-avatar">
2023-06-13 18:38:55 +08:00
<img class="avatar w-6 h-6" :src="user.avatar_url">
</div>
2023-09-22 17:59:39 +08:00
<div class="dropdown-menu dropdown-menu-right rounded !p-2.5 sn-shadow-menu-sm">
2023-05-08 20:44:50 +08:00
<li v-for="(item, i) in userMenu" :key="i">
2023-09-22 17:59:39 +08:00
<a :href="item.url" class="!px-3 !py-2.5 rounded hover:!bg-sn-super-light-grey !text-sn-blue block">
{{ item.name }}
</a>
</li>
<li>
2023-09-22 17:59:39 +08:00
<a rel="nofollow" data-method="delete" :href="user.sign_out_url" class="!px-3 !py-2.5 rounded hover:!bg-sn-super-light-grey !text-sn-blue block">
{{ i18n.t('nav.user.logout') }}
</a>
</li>
</div>
</div>
</div>
</template>
<script>
import NotificationsFlyout from './notifications/notifications_flyout.vue';
import DropdownSelector from '../shared/dropdown_selector.vue';
import Select from '../shared/select.vue';
import MenuDropdown from '../shared/menu_dropdown.vue';
export default {
name: 'TopMenuContainer',
components: {
DropdownSelector,
NotificationsFlyout,
Select,
MenuDropdown
},
props: {
url: String,
notificationsUrl: String,
unseenNotificationsUrl: String
},
data() {
return {
rootUrl: null,
teamSwitchUrl: null,
currentTeam: null,
teams: null,
searchUrl: null,
user: null,
helpMenu: null,
settingsMenu: null,
userMenu: null,
notificationsOpened: false,
unseenNotificationsCount: 0
};
},
created() {
this.fetchData();
this.checkUnseenNotifications();
$(document).on('turbolinks:load', () => {
this.notificationsOpened = false;
this.checkUnseenNotifications();
this.refreshCurrentTeam();
});
// Track name update in user profile settings
$(document).on('inlineEditing::updated', '.inline-editing-container[data-field-to-update="full_name"]', this.fetchData);
},
beforeUnmount() {
clearTimeout(this.unseenNotificationsTimeout);
},
computed: {
settingsMenuItems() {
return this.settingsMenu.map((item) => ({ text: item.name, url: item.url })).concat(
{
text: this.i18n.t('left_menu_bar.support_links.core_version'),
modalTarget: '#aboutModal',
url: ''
}
);
}
},
watch: {
notificationsOpened(newVal) {
if (newVal === true) {
document.body.style.overflow = 'hidden';
} else if (newVal === false) {
document.body.style.overflow = 'scroll';
}
}
},
methods: {
fetchData() {
$.get(this.url, (result) => {
this.rootUrl = result.root_url;
this.teamSwitchUrl = result.team_switch_url;
this.currentTeam = result.current_team;
this.teams = result.teams;
this.searchUrl = result.search_url;
this.helpMenu = result.help_menu;
this.settingsMenu = result.settings_menu;
this.userMenu = result.user_menu;
this.user = result.user;
});
},
switchTeam(team) {
if (this.currentTeam == team) return;
const newTeam = this.teams.find((e) => e[0] == team);
if (!newTeam) return;
$.post(this.teamSwitchUrl, { team_id: team }, (result) => {
this.currentTeam = result.current_team;
dropdownSelector.selectValues('#sciNavigationTeamSelector', this.currentTeam);
$('body').attr('data-current-team-id', this.currentTeam);
window.open(this.rootUrl, '_self');
}).fail((msg) => {
HelperModule.flashAlertMsg(msg.responseJSON.message, 'danger');
});
},
searchValue(e) {
window.open(`${this.searchUrl}?q=${e.target.value}`, '_self');
},
checkUnseenNotifications() {
clearTimeout(this.unseenNotificationsTimeout);
$.get(this.unseenNotificationsUrl, (result) => {
this.unseenNotificationsCount = result.unseen;
this.unseenNotificationsTimeout = setTimeout(this.checkUnseenNotifications, 30000);
});
},
refreshCurrentTeam() {
const newTeam = parseInt($('body').attr('data-current-team-id'));
if (newTeam !== this.currentTeam) {
this.currentTeam = newTeam;
dropdownSelector.selectValues('#sciNavigationTeamSelector', this.currentTeam);
}
}
}
};
</script>