debian-mirror-gitlab/app/assets/javascripts/monitoring/components/dashboards_dropdown.vue

213 lines
5.8 KiB
Vue
Raw Normal View History

2020-03-13 15:44:24 +05:30
<script>
2020-05-24 23:13:21 +05:30
import { mapState, mapActions, mapGetters } from 'vuex';
2020-03-13 15:44:24 +05:30
import {
GlAlert,
2020-05-24 23:13:21 +05:30
GlIcon,
2020-03-13 15:44:24 +05:30
GlDropdown,
GlDropdownItem,
GlDropdownHeader,
GlDropdownDivider,
GlSearchBoxByType,
GlModal,
GlLoadingIcon,
GlModalDirective,
} from '@gitlab/ui';
import { s__ } from '~/locale';
import DuplicateDashboardForm from './duplicate_dashboard_form.vue';
const events = {
selectDashboard: 'selectDashboard',
};
export default {
components: {
GlAlert,
2020-05-24 23:13:21 +05:30
GlIcon,
2020-03-13 15:44:24 +05:30
GlDropdown,
GlDropdownItem,
GlDropdownHeader,
GlDropdownDivider,
GlSearchBoxByType,
GlModal,
GlLoadingIcon,
DuplicateDashboardForm,
},
directives: {
GlModal: GlModalDirective,
},
props: {
defaultBranch: {
type: String,
required: true,
},
},
data() {
return {
alert: null,
loading: false,
form: {},
searchTerm: '',
};
},
computed: {
...mapState('monitoringDashboard', ['allDashboards']),
2020-05-24 23:13:21 +05:30
...mapGetters('monitoringDashboard', ['selectedDashboard']),
2020-03-13 15:44:24 +05:30
isSystemDashboard() {
2020-05-24 23:13:21 +05:30
return this.selectedDashboard?.system_dashboard;
2020-03-13 15:44:24 +05:30
},
selectedDashboardText() {
2020-05-24 23:13:21 +05:30
return this.selectedDashboard?.display_name;
},
selectedDashboardPath() {
return this.selectedDashboard?.path;
2020-03-13 15:44:24 +05:30
},
2020-05-24 23:13:21 +05:30
2020-03-13 15:44:24 +05:30
filteredDashboards() {
2020-05-24 23:13:21 +05:30
return this.allDashboards.filter(({ display_name = '' }) =>
2020-03-13 15:44:24 +05:30
display_name.toLowerCase().includes(this.searchTerm.toLowerCase()),
);
},
shouldShowNoMsgContainer() {
return this.filteredDashboards.length === 0;
},
2020-05-24 23:13:21 +05:30
starredDashboards() {
return this.filteredDashboards.filter(({ starred }) => starred);
},
nonStarredDashboards() {
return this.filteredDashboards.filter(({ starred }) => !starred);
},
2020-03-13 15:44:24 +05:30
okButtonText() {
return this.loading ? s__('Metrics|Duplicating...') : s__('Metrics|Duplicate');
},
},
methods: {
...mapActions('monitoringDashboard', ['duplicateSystemDashboard']),
2020-05-24 23:13:21 +05:30
dashboardDisplayName(dashboard) {
return dashboard.display_name || dashboard.path || '';
},
2020-03-13 15:44:24 +05:30
selectDashboard(dashboard) {
this.$emit(events.selectDashboard, dashboard);
},
ok(bvModalEvt) {
// Prevent modal from hiding in case submit fails
bvModalEvt.preventDefault();
this.loading = true;
this.alert = null;
this.duplicateSystemDashboard(this.form)
.then(createdDashboard => {
this.loading = false;
this.alert = null;
// Trigger hide modal as submit is successful
this.$refs.duplicateDashboardModal.hide();
// Dashboards in the default branch become available immediately.
// Not so in other branches, so we refresh the current dashboard
const dashboard =
this.form.branch === this.defaultBranch ? createdDashboard : this.selectedDashboard;
this.$emit(events.selectDashboard, dashboard);
})
.catch(error => {
this.loading = false;
this.alert = error;
});
},
hide() {
this.alert = null;
},
formChange(form) {
this.form = form;
},
},
};
</script>
<template>
<gl-dropdown
toggle-class="dropdown-menu-toggle"
menu-class="monitor-dashboard-dropdown-menu"
:text="selectedDashboardText"
>
<div class="d-flex flex-column overflow-hidden">
<gl-dropdown-header class="monitor-dashboard-dropdown-header text-center">{{
__('Dashboard')
}}</gl-dropdown-header>
<gl-dropdown-divider />
<gl-search-box-by-type
ref="monitorDashboardsDropdownSearch"
v-model="searchTerm"
class="m-2"
/>
2020-05-24 23:13:21 +05:30
2020-03-13 15:44:24 +05:30
<div class="flex-fill overflow-auto">
<gl-dropdown-item
2020-05-24 23:13:21 +05:30
v-for="dashboard in starredDashboards"
:key="dashboard.path"
:active="dashboard.path === selectedDashboardPath"
active-class="is-active"
@click="selectDashboard(dashboard)"
>
<div class="d-flex">
{{ dashboardDisplayName(dashboard) }}
<gl-icon class="text-muted ml-auto" name="star" />
</div>
</gl-dropdown-item>
<gl-dropdown-divider
v-if="starredDashboards.length && nonStarredDashboards.length"
ref="starredListDivider"
/>
<gl-dropdown-item
v-for="dashboard in nonStarredDashboards"
2020-03-13 15:44:24 +05:30
:key="dashboard.path"
2020-05-24 23:13:21 +05:30
:active="dashboard.path === selectedDashboardPath"
2020-03-13 15:44:24 +05:30
active-class="is-active"
@click="selectDashboard(dashboard)"
>
2020-05-24 23:13:21 +05:30
{{ dashboardDisplayName(dashboard) }}
2020-03-13 15:44:24 +05:30
</gl-dropdown-item>
</div>
<div
v-show="shouldShowNoMsgContainer"
ref="monitorDashboardsDropdownMsg"
class="text-secondary no-matches-message"
>
{{ __('No matching results') }}
</div>
<template v-if="isSystemDashboard">
<gl-dropdown-divider />
<gl-modal
ref="duplicateDashboardModal"
modal-id="duplicateDashboardModal"
:title="s__('Metrics|Duplicate dashboard')"
ok-variant="success"
@ok="ok"
@hide="hide"
>
<gl-alert v-if="alert" class="mb-3" variant="danger" @dismiss="alert = null">
{{ alert }}
</gl-alert>
<duplicate-dashboard-form
:dashboard="selectedDashboard"
:default-branch="defaultBranch"
@change="formChange"
/>
<template #modal-ok>
<gl-loading-icon v-if="loading" inline color="light" />
{{ okButtonText }}
</template>
</gl-modal>
<gl-dropdown-item ref="duplicateDashboardItem" v-gl-modal="'duplicateDashboardModal'">
{{ s__('Metrics|Duplicate dashboard') }}
</gl-dropdown-item>
</template>
</div>
</gl-dropdown>
</template>