2017-07-05 13:20:41 -04:00
|
|
|
class Subscription {
|
|
|
|
constructor(containerElm) {
|
|
|
|
this.containerElm = containerElm;
|
2016-11-17 10:48:45 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
const subscribeButton = containerElm.querySelector('.js-subscribe-button');
|
|
|
|
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
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
toggleSubscription(event) {
|
|
|
|
const button = event.currentTarget;
|
|
|
|
const buttonSpan = button.querySelector('span');
|
|
|
|
if (!buttonSpan || button.classList.contains('disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
button.classList.add('disabled');
|
2016-11-17 10:48:45 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
const isSubscribed = buttonSpan.innerHTML.trim().toLowerCase() !== 'subscribe';
|
|
|
|
const toggleActionUrl = this.containerElm.dataset.url;
|
2016-11-17 10:48:45 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
$.post(toggleActionUrl, () => {
|
|
|
|
button.classList.remove('disabled');
|
2016-11-17 10:48:45 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
// hack to allow this to work with the issue boards Vue object
|
|
|
|
if (document.querySelector('html').classList.contains('issue-boards-page')) {
|
|
|
|
gl.issueBoards.boardStoreIssueSet(
|
|
|
|
'subscribed',
|
|
|
|
!gl.issueBoards.BoardsStore.detail.issue.subscribed,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
buttonSpan.innerHTML = isSubscribed ? 'Subscribe' : 'Unsubscribe';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
static bindAll(selector) {
|
|
|
|
[].forEach.call(document.querySelectorAll(selector), elm => new Subscription(elm));
|
2016-11-17 10:48:45 -05:00
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
window.gl.Subscription = Subscription;
|