2016-11-30 18:21:28 -05:00
|
|
|
(() => {
|
2016-11-17 10:48:45 -05:00
|
|
|
class Subscription {
|
2016-12-01 01:14:12 -05:00
|
|
|
constructor(containerElm) {
|
|
|
|
this.containerElm = containerElm;
|
2016-11-17 10:48:45 -05:00
|
|
|
|
2016-12-01 01:14:12 -05:00
|
|
|
const subscribeButton = containerElm.querySelector('.js-subscribe-button');
|
2016-11-17 10:48:45 -05:00
|
|
|
if (subscribeButton) {
|
|
|
|
// remove class so we don't bind twice
|
|
|
|
subscribeButton.classList.remove('js-subscribe-button');
|
|
|
|
subscribeButton.addEventListener('click', this.toggleSubscription.bind(this));
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2016-11-17 10:48:45 -05:00
|
|
|
toggleSubscription(event) {
|
|
|
|
const button = event.currentTarget;
|
|
|
|
const buttonSpan = button.querySelector('span');
|
|
|
|
if (!buttonSpan || button.classList.contains('disabled')) {
|
|
|
|
return;
|
2016-10-20 03:54:33 -04:00
|
|
|
}
|
2016-11-17 10:48:45 -05:00
|
|
|
button.classList.add('disabled');
|
|
|
|
|
2016-11-22 16:56:52 -05:00
|
|
|
const isSubscribed = buttonSpan.innerHTML.trim().toLowerCase() !== 'subscribe';
|
2016-12-01 01:11:21 -05:00
|
|
|
const toggleActionUrl = this.containerElm.dataset.url;
|
2016-11-17 10:48:45 -05:00
|
|
|
|
|
|
|
$.post(toggleActionUrl, () => {
|
|
|
|
button.classList.remove('disabled');
|
|
|
|
|
|
|
|
// hack to allow this to work with the issue boards Vue object
|
|
|
|
if (document.querySelector('html').classList.contains('issue-boards-page')) {
|
2017-04-07 15:36:09 -04:00
|
|
|
gl.issueBoards.boardStoreIssueSet(
|
2016-11-17 10:48:45 -05:00
|
|
|
'subscribed',
|
2016-11-30 18:20:06 -05:00
|
|
|
!gl.issueBoards.BoardsStore.detail.issue.subscribed,
|
2016-11-17 10:48:45 -05:00
|
|
|
);
|
|
|
|
} else {
|
2016-11-30 18:43:21 -05:00
|
|
|
buttonSpan.innerHTML = isSubscribed ? 'Subscribe' : 'Unsubscribe';
|
2016-11-17 10:48:45 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-11-17 10:48:45 -05:00
|
|
|
static bindAll(selector) {
|
|
|
|
[].forEach.call(document.querySelectorAll(selector), elm => new Subscription(elm));
|
|
|
|
}
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-11-30 18:21:28 -05:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
window.gl.Subscription = Subscription;
|
|
|
|
})();
|