debian-mirror-gitlab/app/assets/javascripts/pages/users/user_tabs.js

271 lines
7.1 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-27 19:54:05 +05:30
import axios from '~/lib/utils/axios_utils';
import Activities from '~/activities';
import { localTimeAgo } from '~/lib/utils/datetime_utility';
2018-12-05 23:21:45 +05:30
import { __, sprintf } from '~/locale';
2018-03-27 19:54:05 +05:30
import flash from '~/flash';
2017-09-10 17:25:29 +05:30
import ActivityCalendar from './activity_calendar';
2018-12-05 23:21:45 +05:30
import UserOverviewBlock from './user_overview_block';
2017-09-10 17:25:29 +05:30
/**
* UserTabs
*
* Handles persisting and restoring the current tab selection and lazily-loading
* content on the Users#show page.
*
* ### Example Markup
*
* <ul class="nav-links">
* <li class="activity-tab active">
* <a data-action="activity" data-target="#activity" data-toggle="tab" href="/u/username">
* Activity
* </a>
* </li>
* <li class="groups-tab">
* <a data-action="groups" data-target="#groups" data-toggle="tab" href="/u/username/groups">
* Groups
* </a>
* </li>
* <li class="contributed-tab">
* ...
* </li>
* <li class="projects-tab">
* ...
* </li>
* <li class="snippets-tab">
* ...
* </li>
* </ul>
*
* <div class="tab-content">
* <div class="tab-pane" id="activity">
* Activity Content
* </div>
* <div class="tab-pane" id="groups">
* Groups Content
* </div>
* <div class="tab-pane" id="contributed">
* Contributed projects content
* </div>
* <div class="tab-pane" id="projects">
* Projects content
* </div>
* <div class="tab-pane" id="snippets">
* Snippets content
* </div>
* </div>
*
* <div class="loading-status">
* <div class="loading">
* Loading Animation
* </div>
* </div>
*/
2018-12-05 23:21:45 +05:30
const CALENDAR_TEMPLATES = {
activity: `
<div class="clearfix calendar">
<div class="js-contrib-calendar"></div>
<div class="calendar-hint bottom-right"></div>
2017-09-10 17:25:29 +05:30
</div>
2018-12-05 23:21:45 +05:30
`,
overview: `
<div class="clearfix calendar">
<div class="calendar-hint"></div>
<div class="js-contrib-calendar prepend-top-20"></div>
</div>
`,
};
const CALENDAR_PERIOD_6_MONTHS = 6;
const CALENDAR_PERIOD_12_MONTHS = 12;
2017-09-10 17:25:29 +05:30
export default class UserTabs {
constructor({ defaultAction, action, parentEl }) {
this.loaded = {};
2018-12-05 23:21:45 +05:30
this.defaultAction = defaultAction || 'overview';
2017-09-10 17:25:29 +05:30
this.action = action || this.defaultAction;
this.$parentEl = $(parentEl) || $(document);
this.windowLocation = window.location;
2018-11-08 19:23:39 +05:30
this.$parentEl.find('.nav-links a').each((i, navLink) => {
this.loaded[$(navLink).attr('data-action')] = false;
});
2017-09-10 17:25:29 +05:30
this.actions = Object.keys(this.loaded);
this.bindEvents();
if (this.action === 'show') {
this.action = this.defaultAction;
}
this.activateTab(this.action);
}
bindEvents() {
this.$parentEl
.off('shown.bs.tab', '.nav-links a[data-toggle="tab"]')
.on('shown.bs.tab', '.nav-links a[data-toggle="tab"]', event => this.tabShown(event))
.on('click', '.gl-pagination a', event => this.changeProjectsPage(event));
}
changeProjectsPage(e) {
e.preventDefault();
$('.tab-pane.active').empty();
const endpoint = $(e.target).attr('href');
this.loadTab(this.getCurrentAction(), endpoint);
}
tabShown(event) {
const $target = $(event.target);
const action = $target.data('action');
const source = $target.attr('href');
const endpoint = $target.data('endpoint');
this.setTab(action, endpoint);
return this.setCurrentAction(source);
}
activateTab(action) {
2018-11-08 19:23:39 +05:30
return this.$parentEl.find(`.nav-links .js-${action}-tab a`).tab('show');
2017-09-10 17:25:29 +05:30
}
setTab(action, endpoint) {
if (this.loaded[action]) {
return;
}
if (action === 'activity') {
this.loadActivities();
2018-12-05 23:21:45 +05:30
} else if (action === 'overview') {
this.loadOverviewTab();
2017-09-10 17:25:29 +05:30
}
const loadableActions = ['groups', 'contributed', 'projects', 'snippets'];
if (loadableActions.indexOf(action) > -1) {
this.loadTab(action, endpoint);
}
}
loadTab(action, endpoint) {
2018-03-17 18:26:18 +05:30
this.toggleLoading(true);
2018-11-08 19:23:39 +05:30
return axios
.get(endpoint)
2018-03-17 18:26:18 +05:30
.then(({ data }) => {
2017-09-10 17:25:29 +05:30
const tabSelector = `div#${action}`;
this.$parentEl.find(tabSelector).html(data.html);
this.loaded[action] = true;
2018-03-17 18:26:18 +05:30
localTimeAgo($('.js-timeago', tabSelector));
this.toggleLoading(false);
})
.catch(() => {
this.toggleLoading(false);
});
2017-09-10 17:25:29 +05:30
}
loadActivities() {
if (this.loaded.activity) {
return;
}
2018-12-05 23:21:45 +05:30
this.loadActivityCalendar('activity');
// eslint-disable-next-line no-new
2018-12-13 13:39:08 +05:30
new Activities('#activity');
2018-12-05 23:21:45 +05:30
this.loaded.activity = true;
}
loadOverviewTab() {
if (this.loaded.overview) {
return;
}
this.loadActivityCalendar('overview');
UserTabs.renderMostRecentBlocks('#js-overview .activities-block', {
requestParams: { limit: 5 },
});
UserTabs.renderMostRecentBlocks('#js-overview .projects-block', {
requestParams: { limit: 10, skip_pagination: true },
});
this.loaded.overview = true;
}
static renderMostRecentBlocks(container, options) {
// eslint-disable-next-line no-new
new UserOverviewBlock({
container,
url: $(`${container} .overview-content-list`).data('href'),
...options,
});
}
loadActivityCalendar(action) {
const monthsAgo = action === 'overview' ? CALENDAR_PERIOD_6_MONTHS : CALENDAR_PERIOD_12_MONTHS;
const $calendarWrap = this.$parentEl.find('.tab-pane.active .user-calendar');
2017-09-10 17:25:29 +05:30
const calendarPath = $calendarWrap.data('calendarPath');
const calendarActivitiesPath = $calendarWrap.data('calendarActivitiesPath');
const utcOffset = $calendarWrap.data('utcOffset');
let utcFormatted = 'UTC';
if (utcOffset !== 0) {
2018-11-08 19:23:39 +05:30
utcFormatted = `UTC${utcOffset > 0 ? '+' : ''}${utcOffset / 3600}`;
2017-09-10 17:25:29 +05:30
}
2018-11-08 19:23:39 +05:30
axios
.get(calendarPath)
2018-03-17 18:26:18 +05:30
.then(({ data }) => {
2018-12-05 23:21:45 +05:30
$calendarWrap.html(CALENDAR_TEMPLATES[action]);
let calendarHint = '';
if (action === 'activity') {
calendarHint = sprintf(
__(
'Summary of issues, merge requests, push events, and comments (Timezone: %{utcFormatted})',
),
{ utcFormatted },
);
} else if (action === 'overview') {
calendarHint = __('Issues, merge requests, pushes and comments.');
}
$calendarWrap.find('.calendar-hint').text(calendarHint);
2017-09-10 17:25:29 +05:30
// eslint-disable-next-line no-new
2018-12-05 23:21:45 +05:30
new ActivityCalendar(
'.tab-pane.active .js-contrib-calendar',
'.tab-pane.active .user-calendar-activities',
data,
calendarActivitiesPath,
utcOffset,
0,
monthsAgo,
);
2018-03-17 18:26:18 +05:30
})
.catch(() => flash(__('There was an error loading users activity calendar.')));
2017-09-10 17:25:29 +05:30
}
toggleLoading(status) {
2018-11-08 19:23:39 +05:30
return this.$parentEl.find('.loading-status .loading').toggleClass('hide', !status);
2017-09-10 17:25:29 +05:30
}
setCurrentAction(source) {
let newState = source;
newState = newState.replace(/\/+$/, '');
newState += this.windowLocation.search + this.windowLocation.hash;
2018-11-08 19:23:39 +05:30
window.history.replaceState(
{
url: newState,
},
document.title,
newState,
);
2017-09-10 17:25:29 +05:30
return newState;
}
getCurrentAction() {
2018-11-08 19:23:39 +05:30
return this.$parentEl.find('.nav-links a.active').data('action');
2017-09-10 17:25:29 +05:30
}
}