2019-05-05 19:05:54 +05:30
|
|
|
var subscribe_button = document.getElementById('subscribe');
|
|
|
|
subscribe_button.parentNode['action'] = 'javascript:void(0)';
|
2019-05-02 06:33:39 +05:30
|
|
|
|
2019-05-05 19:05:54 +05:30
|
|
|
if (subscribe_button.getAttribute('data-type') === 'subscribe') {
|
|
|
|
subscribe_button.onclick = subscribe;
|
|
|
|
} else {
|
|
|
|
subscribe_button.onclick = unsubscribe;
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|
|
|
|
|
2019-06-08 06:26:41 +05:30
|
|
|
function subscribe(timeouts = 1) {
|
2019-05-05 18:16:01 +05:30
|
|
|
if (timeouts >= 10) {
|
2019-05-02 06:33:39 +05:30
|
|
|
console.log('Failed to subscribe.');
|
2019-01-28 09:42:07 +05:30
|
|
|
return;
|
|
|
|
}
|
2019-03-24 00:35:13 +05:30
|
|
|
|
2019-05-02 06:33:39 +05:30
|
|
|
var url = '/subscription_ajax?action_create_subscription_to_channel=1&redirect=false' +
|
2019-05-08 19:45:27 +05:30
|
|
|
'&c=' + subscribe_data.ucid;
|
2019-01-28 08:36:28 +05:30
|
|
|
var xhr = new XMLHttpRequest();
|
2019-05-02 06:33:39 +05:30
|
|
|
xhr.responseType = 'json';
|
2019-01-28 08:36:28 +05:30
|
|
|
xhr.timeout = 20000;
|
2019-05-02 06:33:39 +05:30
|
|
|
xhr.open('POST', url, true);
|
2019-06-08 06:26:41 +05:30
|
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
2019-05-05 19:05:54 +05:30
|
|
|
xhr.send('csrf_token=' + subscribe_data.csrf_token);
|
2019-01-28 08:36:28 +05:30
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
var fallback = subscribe_button.innerHTML;
|
|
|
|
subscribe_button.onclick = unsubscribe;
|
2019-05-05 19:05:54 +05:30
|
|
|
subscribe_button.innerHTML = '<b>' + subscribe_data.unsubscribe_text + ' | ' + subscribe_data.sub_count_text + '</b>';
|
2019-03-24 00:35:13 +05:30
|
|
|
|
2019-05-05 19:05:54 +05:30
|
|
|
xhr.onreadystatechange = function () {
|
2019-01-28 08:36:28 +05:30
|
|
|
if (xhr.readyState == 4) {
|
2019-01-28 09:42:07 +05:30
|
|
|
if (xhr.status != 200) {
|
|
|
|
subscribe_button.onclick = subscribe;
|
|
|
|
subscribe_button.innerHTML = fallback;
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 00:35:13 +05:30
|
|
|
|
2019-05-05 19:05:54 +05:30
|
|
|
xhr.ontimeout = function () {
|
2019-06-08 06:26:41 +05:30
|
|
|
console.log('Subscribing timed out... ' + timeouts + '/10');
|
2019-05-05 18:16:01 +05:30
|
|
|
subscribe(timeouts++);
|
2019-05-06 20:18:33 +05:30
|
|
|
}
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|
|
|
|
|
2019-06-08 06:26:41 +05:30
|
|
|
function unsubscribe(timeouts = 1) {
|
2019-05-05 18:16:01 +05:30
|
|
|
if (timeouts >= 10) {
|
2019-05-02 06:33:39 +05:30
|
|
|
console.log('Failed to subscribe');
|
2019-01-28 09:42:07 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-02 06:33:39 +05:30
|
|
|
var url = '/subscription_ajax?action_remove_subscriptions=1&redirect=false' +
|
2019-05-08 19:45:27 +05:30
|
|
|
'&c=' + subscribe_data.ucid;
|
2019-01-28 08:36:28 +05:30
|
|
|
var xhr = new XMLHttpRequest();
|
2019-05-02 06:33:39 +05:30
|
|
|
xhr.responseType = 'json';
|
2019-01-28 08:36:28 +05:30
|
|
|
xhr.timeout = 20000;
|
2019-05-02 06:33:39 +05:30
|
|
|
xhr.open('POST', url, true);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
2019-05-05 19:05:54 +05:30
|
|
|
xhr.send('csrf_token=' + subscribe_data.csrf_token);
|
2019-01-28 08:36:28 +05:30
|
|
|
|
2019-01-28 09:42:07 +05:30
|
|
|
var fallback = subscribe_button.innerHTML;
|
|
|
|
subscribe_button.onclick = subscribe;
|
2019-05-05 19:05:54 +05:30
|
|
|
subscribe_button.innerHTML = '<b>' + subscribe_data.subscribe_text + ' | ' + subscribe_data.sub_count_text + '</b>';
|
2019-01-28 09:42:07 +05:30
|
|
|
|
2019-05-05 19:05:54 +05:30
|
|
|
xhr.onreadystatechange = function () {
|
2019-01-28 08:36:28 +05:30
|
|
|
if (xhr.readyState == 4) {
|
2019-01-28 09:42:07 +05:30
|
|
|
if (xhr.status != 200) {
|
|
|
|
subscribe_button.onclick = unsubscribe;
|
|
|
|
subscribe_button.innerHTML = fallback;
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 09:42:07 +05:30
|
|
|
|
2019-05-05 19:05:54 +05:30
|
|
|
xhr.ontimeout = function () {
|
2019-06-08 06:26:41 +05:30
|
|
|
console.log('Unsubscribing timed out... ' + timeouts + '/10');
|
2019-05-05 18:16:01 +05:30
|
|
|
unsubscribe(timeouts++);
|
2019-05-06 20:18:33 +05:30
|
|
|
}
|
2019-01-28 08:36:28 +05:30
|
|
|
}
|