2018-02-15 18:07:04 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import Activities from '~/activities';
|
|
|
|
import { localTimeAgo } from '~/lib/utils/datetime_utility';
|
|
|
|
import { __ } from '~/locale';
|
|
|
|
import flash from '~/flash';
|
2017-07-18 18:05:41 -04:00
|
|
|
import ActivityCalendar from './activity_calendar';
|
|
|
|
|
2017-07-27 15:58:21 -04:00
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2016-09-08 08:26:43 -04:00
|
|
|
|
2017-07-18 18:05:41 -04:00
|
|
|
const CALENDAR_TEMPLATE = `
|
|
|
|
<div class="clearfix calendar">
|
|
|
|
<div class="js-contrib-calendar"></div>
|
|
|
|
<div class="calendar-hint">
|
|
|
|
Summary of issues, merge requests, push events, and comments
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
2017-06-30 18:48:54 -04:00
|
|
|
export default class UserTabs {
|
2017-07-27 15:58:21 -04:00
|
|
|
constructor({ defaultAction, action, parentEl }) {
|
2017-07-05 13:20:41 -04:00
|
|
|
this.loaded = {};
|
|
|
|
this.defaultAction = defaultAction || 'activity';
|
|
|
|
this.action = action || this.defaultAction;
|
|
|
|
this.$parentEl = $(parentEl) || $(document);
|
2017-07-27 15:58:21 -04:00
|
|
|
this.windowLocation = window.location;
|
2017-07-05 13:20:41 -04:00
|
|
|
this.$parentEl.find('.nav-links a')
|
|
|
|
.each((i, navLink) => {
|
|
|
|
this.loaded[$(navLink).attr('data-action')] = false;
|
|
|
|
});
|
|
|
|
this.actions = Object.keys(this.loaded);
|
|
|
|
this.bindEvents();
|
|
|
|
|
|
|
|
if (this.action === 'show') {
|
|
|
|
this.action = this.defaultAction;
|
2016-09-08 08:26:43 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
this.activateTab(this.action);
|
|
|
|
}
|
2017-03-17 11:38:41 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
bindEvents() {
|
2017-07-27 15:58:21 -04:00
|
|
|
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));
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2017-03-17 11:38:41 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
changeProjectsPage(e) {
|
|
|
|
e.preventDefault();
|
2016-09-08 08:26:43 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
$('.tab-pane.active').empty();
|
|
|
|
const endpoint = $(e.target).attr('href');
|
|
|
|
this.loadTab(this.getCurrentAction(), endpoint);
|
|
|
|
}
|
2016-09-08 08:26:43 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
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);
|
|
|
|
}
|
2016-09-08 08:26:43 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
activateTab(action) {
|
|
|
|
return this.$parentEl.find(`.nav-links .js-${action}-tab a`)
|
|
|
|
.tab('show');
|
|
|
|
}
|
2016-09-09 10:57:13 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
setTab(action, endpoint) {
|
|
|
|
if (this.loaded[action]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (action === 'activity') {
|
|
|
|
this.loadActivities();
|
2016-09-08 08:26:43 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
const loadableActions = ['groups', 'contributed', 'projects', 'snippets'];
|
|
|
|
if (loadableActions.indexOf(action) > -1) {
|
2017-07-27 15:58:21 -04:00
|
|
|
this.loadTab(action, endpoint);
|
2016-09-08 08:26:43 -04:00
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-09-08 08:26:43 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
loadTab(action, endpoint) {
|
2018-02-02 08:07:37 -05:00
|
|
|
this.toggleLoading(true);
|
|
|
|
|
|
|
|
return axios.get(endpoint)
|
|
|
|
.then(({ data }) => {
|
2017-07-05 13:20:41 -04:00
|
|
|
const tabSelector = `div#${action}`;
|
|
|
|
this.$parentEl.find(tabSelector).html(data.html);
|
|
|
|
this.loaded[action] = true;
|
2017-12-07 06:09:17 -05:00
|
|
|
localTimeAgo($('.js-timeago', tabSelector));
|
2018-02-02 08:07:37 -05:00
|
|
|
|
|
|
|
this.toggleLoading(false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.toggleLoading(false);
|
|
|
|
});
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-09-08 08:26:43 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
loadActivities() {
|
2017-07-27 15:41:26 -04:00
|
|
|
if (this.loaded.activity) {
|
2017-07-05 13:20:41 -04:00
|
|
|
return;
|
2016-09-08 08:26:43 -04:00
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
const $calendarWrap = this.$parentEl.find('.user-calendar');
|
2017-07-18 18:05:41 -04:00
|
|
|
const calendarPath = $calendarWrap.data('calendarPath');
|
|
|
|
const calendarActivitiesPath = $calendarWrap.data('calendarActivitiesPath');
|
2017-07-27 18:49:49 -04:00
|
|
|
const utcOffset = $calendarWrap.data('utcOffset');
|
2017-07-27 19:21:56 -04:00
|
|
|
let utcFormatted = 'UTC';
|
|
|
|
if (utcOffset !== 0) {
|
|
|
|
utcFormatted = `UTC${utcOffset > 0 ? '+' : ''}${(utcOffset / 3600)}`;
|
|
|
|
}
|
2017-07-18 18:05:41 -04:00
|
|
|
|
2018-02-02 08:07:37 -05:00
|
|
|
axios.get(calendarPath)
|
|
|
|
.then(({ data }) => {
|
2017-07-18 18:05:41 -04:00
|
|
|
$calendarWrap.html(CALENDAR_TEMPLATE);
|
2017-07-27 19:21:56 -04:00
|
|
|
$calendarWrap.find('.calendar-hint').append(`(Timezone: ${utcFormatted})`);
|
2017-07-27 15:58:21 -04:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-new
|
2018-02-02 08:07:37 -05:00
|
|
|
new ActivityCalendar('.js-contrib-calendar', data, calendarActivitiesPath, utcOffset);
|
|
|
|
})
|
2018-02-05 09:05:38 -05:00
|
|
|
.catch(() => flash(__('There was an error loading users activity calendar.')));
|
2017-07-18 18:05:41 -04:00
|
|
|
|
2017-07-27 15:58:21 -04:00
|
|
|
// eslint-disable-next-line no-new
|
2017-12-14 06:50:40 -05:00
|
|
|
new Activities();
|
2017-07-27 15:41:26 -04:00
|
|
|
this.loaded.activity = true;
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-09-08 08:26:43 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
toggleLoading(status) {
|
|
|
|
return this.$parentEl.find('.loading-status .loading')
|
|
|
|
.toggle(status);
|
|
|
|
}
|
2017-03-17 11:38:41 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
setCurrentAction(source) {
|
2017-07-27 15:58:21 -04:00
|
|
|
let newState = source;
|
|
|
|
newState = newState.replace(/\/+$/, '');
|
|
|
|
newState += this.windowLocation.search + this.windowLocation.hash;
|
2017-07-05 13:20:41 -04:00
|
|
|
history.replaceState({
|
2017-07-27 15:58:21 -04:00
|
|
|
url: newState,
|
|
|
|
}, document.title, newState);
|
|
|
|
return newState;
|
2016-09-08 08:26:43 -04:00
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
|
|
|
|
getCurrentAction() {
|
|
|
|
return this.$parentEl.find('.nav-links .active a').data('action');
|
|
|
|
}
|
|
|
|
}
|