debian-mirror-gitlab/app/assets/javascripts/header.js

143 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-12-05 23:21:45 +05:30
import Vue from 'vue';
2023-03-04 22:38:38 +05:30
import NewNavToggle from '~/nav/components/new_nav_toggle.vue';
2018-03-17 18:26:18 +05:30
import { highCountTrim } from '~/lib/utils/text_utility';
2020-04-22 19:07:51 +05:30
import Tracking from '~/tracking';
2021-03-11 19:13:27 +05:30
import Translate from '~/vue_shared/translate';
2023-03-04 22:38:38 +05:30
import { parseBoolean } from '~/lib/utils/common_utils';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
/**
* Updates todo counter when todos are toggled.
* When count is 0, we hide the badge.
*
* @param {jQuery.Event} e
* @param {String} count
*/
export default function initTodoToggle() {
2022-08-13 15:12:31 +05:30
document.addEventListener('todo:toggle', (e) => {
const updatedCount = e.detail.count || 0;
const todoPendingCount = document.querySelector('.js-todos-count');
2018-03-17 18:26:18 +05:30
2022-08-13 15:12:31 +05:30
if (todoPendingCount) {
todoPendingCount.textContent = highCountTrim(updatedCount);
if (updatedCount === 0) {
todoPendingCount.classList.add('hidden');
} else {
todoPendingCount.classList.remove('hidden');
}
}
2018-03-17 18:26:18 +05:30
});
}
2018-12-05 23:21:45 +05:30
2023-05-27 22:25:52 +05:30
export function initStatusTriggers() {
2018-12-05 23:21:45 +05:30
const setStatusModalTriggerEl = document.querySelector('.js-set-status-modal-trigger');
2020-11-24 15:15:51 +05:30
if (setStatusModalTriggerEl) {
setStatusModalTriggerEl.addEventListener('click', () => {
2023-04-23 21:23:45 +05:30
const topNavbar = document.querySelector('.navbar-gitlab');
const buttonWithinTopNav = topNavbar && topNavbar.contains(setStatusModalTriggerEl);
Tracking.event(undefined, 'click_button', {
label: 'user_edit_status',
2023-06-20 00:43:36 +05:30
property: buttonWithinTopNav ? 'navigation_top' : 'nav_user_menu',
2023-04-23 21:23:45 +05:30
});
2020-11-24 15:15:51 +05:30
import(
/* webpackChunkName: 'statusModalBundle' */ './set_status_modal/set_status_modal_wrapper.vue'
)
.then(({ default: SetStatusModalWrapper }) => {
const setStatusModalWrapperEl = document.querySelector('.js-set-status-modal-wrapper');
const statusModalElement = document.createElement('div');
setStatusModalWrapperEl.appendChild(statusModalElement);
2018-12-05 23:21:45 +05:30
2020-11-24 15:15:51 +05:30
Vue.use(Translate);
2018-12-05 23:21:45 +05:30
2020-11-24 15:15:51 +05:30
// eslint-disable-next-line no-new
new Vue({
el: statusModalElement,
data() {
2021-01-29 00:20:46 +05:30
const {
currentEmoji,
defaultEmoji,
currentMessage,
currentAvailability,
2021-04-29 21:17:54 +05:30
currentClearStatusAfter,
2021-01-29 00:20:46 +05:30
} = setStatusModalWrapperEl.dataset;
2018-12-05 23:21:45 +05:30
2020-11-24 15:15:51 +05:30
return {
currentEmoji,
2021-01-29 00:20:46 +05:30
defaultEmoji,
2020-11-24 15:15:51 +05:30
currentMessage,
2021-01-29 00:20:46 +05:30
currentAvailability,
2021-04-29 21:17:54 +05:30
currentClearStatusAfter,
2020-11-24 15:15:51 +05:30
};
},
render(createElement) {
2021-01-29 00:20:46 +05:30
const {
currentEmoji,
defaultEmoji,
currentMessage,
currentAvailability,
2021-04-29 21:17:54 +05:30
currentClearStatusAfter,
2021-01-29 00:20:46 +05:30
} = this;
2018-12-05 23:21:45 +05:30
2020-11-24 15:15:51 +05:30
return createElement(SetStatusModalWrapper, {
props: {
currentEmoji,
2021-01-29 00:20:46 +05:30
defaultEmoji,
2020-11-24 15:15:51 +05:30
currentMessage,
2021-01-29 00:20:46 +05:30
currentAvailability,
2021-04-29 21:17:54 +05:30
currentClearStatusAfter,
2020-11-24 15:15:51 +05:30
},
});
},
});
})
.catch(() => {});
2018-12-05 23:21:45 +05:30
});
2023-01-13 00:05:48 +05:30
setStatusModalTriggerEl.classList.add('ready');
2018-12-05 23:21:45 +05:30
}
2019-03-02 22:35:43 +05:30
}
2020-05-24 23:13:21 +05:30
function trackShowUserDropdownLink(trackEvent, elToTrack, el) {
const { trackLabel, trackProperty } = elToTrack.dataset;
2022-08-13 15:12:31 +05:30
el.addEventListener('shown.bs.dropdown', () => {
2020-05-24 23:13:21 +05:30
Tracking.event(document.body.dataset.page, trackEvent, {
label: trackLabel,
property: trackProperty,
});
});
}
2023-03-04 22:38:38 +05:30
2020-04-22 19:07:51 +05:30
export function initNavUserDropdownTracking() {
const el = document.querySelector('.js-nav-user-dropdown');
2020-06-23 00:09:42 +05:30
const buyEl = document.querySelector('.js-buy-pipeline-minutes-link');
2020-04-22 19:07:51 +05:30
if (el && buyEl) {
2020-05-24 23:13:21 +05:30
trackShowUserDropdownLink('show_buy_ci_minutes', buyEl, el);
}
2020-04-22 19:07:51 +05:30
}
2023-03-04 22:38:38 +05:30
function initNewNavToggle() {
const el = document.querySelector('.js-new-nav-toggle');
if (!el) return false;
return new Vue({
el,
render(h) {
return h(NewNavToggle, {
props: {
enabled: parseBoolean(el.dataset.enabled),
endpoint: el.dataset.endpoint,
},
});
},
});
}
2023-06-20 00:43:36 +05:30
if (!gon?.use_new_navigation) {
requestIdleCallback(initStatusTriggers);
}
2021-03-11 19:13:27 +05:30
requestIdleCallback(initNavUserDropdownTracking);
2023-03-04 22:38:38 +05:30
requestIdleCallback(initNewNavToggle);