debian-mirror-gitlab/app/assets/javascripts/frequent_items/index.js

80 lines
2 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import $ from 'jquery';
import Vue from 'vue';
import Translate from '~/vue_shared/translate';
2020-06-23 00:09:42 +05:30
import eventHub from './event_hub';
2018-11-08 19:23:39 +05:30
Vue.use(Translate);
const frequentItemDropdowns = [
{
namespace: 'projects',
key: 'project',
},
{
namespace: 'groups',
key: 'group',
},
];
2021-01-03 14:25:43 +05:30
const initFrequentItemList = (namespace, key) => {
const el = document.getElementById(`js-${namespace}-dropdown`);
// Don't do anything if element doesn't exist (No groups dropdown)
// This is for when the user accesses GitLab without logging in
if (!el) {
return;
}
import('./components/app.vue')
.then(({ default: FrequentItems }) => {
// eslint-disable-next-line no-new
new Vue({
el,
data() {
const { dataset } = this.$options.el;
const item = {
id: Number(dataset[`${key}Id`]),
name: dataset[`${key}Name`],
namespace: dataset[`${key}Namespace`],
webUrl: dataset[`${key}WebUrl`],
avatarUrl: dataset[`${key}AvatarUrl`] || null,
lastAccessedOn: Date.now(),
};
return {
currentUserName: dataset.userName,
currentItem: item,
};
},
render(createElement) {
return createElement(FrequentItems, {
props: {
namespace,
currentUserName: this.currentUserName,
currentItem: this.currentItem,
},
});
},
});
})
.catch(() => {});
};
2020-06-23 00:09:42 +05:30
export default function initFrequentItemDropdowns() {
2018-11-08 19:23:39 +05:30
frequentItemDropdowns.forEach(dropdown => {
const { namespace, key } = dropdown;
const navEl = document.getElementById(`nav-${namespace}-dropdown`);
// Don't do anything if element doesn't exist (No groups dropdown)
// This is for when the user accesses GitLab without logging in
2021-01-03 14:25:43 +05:30
if (!navEl) {
2018-11-08 19:23:39 +05:30
return;
}
2020-11-24 15:15:51 +05:30
$(navEl).on('shown.bs.dropdown', () => {
2021-01-03 14:25:43 +05:30
initFrequentItemList(namespace, key);
2020-11-24 15:15:51 +05:30
eventHub.$emit(`${namespace}-dropdownOpen`);
});
2020-06-23 00:09:42 +05:30
});
}