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

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import { __ } from './locale';
import axios from './lib/utils/axios_utils';
import flash from './flash';
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
const tooltipTitles = {
group: {
subscribed: __('Unsubscribe at group level'),
unsubscribed: __('Subscribe at group level'),
},
project: {
subscribed: __('Unsubscribe at project level'),
unsubscribed: __('Subscribe at project level'),
},
};
2018-03-17 18:26:18 +05:30
export default class ProjectLabelSubscription {
constructor(container) {
this.$container = $(container);
this.$buttons = this.$container.find('.js-subscribe-button');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.$buttons.on('click', this.toggleSubscription.bind(this));
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
toggleSubscription(event) {
event.preventDefault();
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
const $btn = $(event.currentTarget);
const url = $btn.attr('data-url');
const oldStatus = $btn.attr('data-status');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
$btn.addClass('disabled');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
axios.post(url).then(() => {
let newStatus;
let newAction;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if (oldStatus === 'unsubscribed') {
[newStatus, newAction] = ['subscribed', 'Unsubscribe'];
} else {
[newStatus, newAction] = ['unsubscribed', 'Subscribe'];
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
$btn.removeClass('disabled');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.$buttons.attr('data-status', newStatus);
this.$buttons.find('> span').text(newAction);
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
this.$buttons.map((i, button) => {
2018-03-17 18:26:18 +05:30
const $button = $(button);
2018-11-08 19:23:39 +05:30
const originalTitle = $button.attr('data-original-title');
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
if (originalTitle) {
ProjectLabelSubscription.setNewTitle($button, originalTitle, newStatus, newAction);
2018-03-17 18:26:18 +05:30
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
return button;
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
}).catch(() => flash(__('There was an error subscribing to this label.')));
2017-08-17 22:00:37 +05:30
}
2018-11-08 19:23:39 +05:30
static setNewTitle($button, originalTitle, newStatus) {
const type = /group/.test(originalTitle) ? 'group' : 'project';
const newTitle = tooltipTitles[type][newStatus];
$button.attr('title', newTitle).tooltip('_fixTitle');
}
2018-03-17 18:26:18 +05:30
}