2019-05-02 01:03:39 +00:00
|
|
|
subscribe_button = document.getElementById('subscribe');
|
|
|
|
|
2019-01-28 03:06:28 +00:00
|
|
|
if (subscribe_button.getAttribute('onclick')) {
|
2019-05-02 01:03:39 +00:00
|
|
|
subscribe_button['href'] = 'javascript:void(0)';
|
2019-01-28 03:06:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 04:12:07 +00:00
|
|
|
function subscribe(timeouts = 0) {
|
2019-05-02 01:03:39 +00:00
|
|
|
subscribe_button = document.getElementById('subscribe');
|
2019-01-28 04:12:07 +00:00
|
|
|
|
|
|
|
if (timeouts > 10) {
|
2019-05-02 01:03:39 +00:00
|
|
|
console.log('Failed to subscribe.');
|
2019-01-28 04:12:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-23 19:05:13 +00:00
|
|
|
|
2019-05-02 01:03:39 +00:00
|
|
|
var url = '/subscription_ajax?action_create_subscription_to_channel=1&redirect=false' +
|
|
|
|
'&c=<%= ucid %>&referer=<%= env.get("current_page") %>';
|
2019-01-28 03:06:28 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
2019-05-02 01:03:39 +00:00
|
|
|
xhr.responseType = 'json';
|
2019-01-28 03:06:28 +00:00
|
|
|
xhr.timeout = 20000;
|
2019-05-02 01:03:39 +00:00
|
|
|
xhr.open('POST', url, true);
|
2019-04-16 04:23:40 +00:00
|
|
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
2019-05-02 01:03:39 +00:00
|
|
|
xhr.send('csrf_token=<%= URI.escape(env.get?("csrf_token").try &.as(String) || "") %>');
|
2019-01-28 03:06:28 +00:00
|
|
|
|
2019-01-28 04:12:07 +00:00
|
|
|
var fallback = subscribe_button.innerHTML;
|
|
|
|
subscribe_button.onclick = unsubscribe;
|
2019-05-02 01:03:39 +00:00
|
|
|
subscribe_button.innerHTML = '<b><%= translate(locale, "Unsubscribe").gsub("'", "\\'") %> | <%= sub_count_text %></b>';
|
2019-03-23 19:05:13 +00:00
|
|
|
|
2019-01-28 03:06:28 +00:00
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4) {
|
2019-01-28 04:12:07 +00:00
|
|
|
if (xhr.status != 200) {
|
|
|
|
subscribe_button.onclick = subscribe;
|
|
|
|
subscribe_button.innerHTML = fallback;
|
2019-01-28 03:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:05:13 +00:00
|
|
|
|
2019-01-28 04:12:07 +00:00
|
|
|
xhr.ontimeout = function() {
|
2019-05-02 01:03:39 +00:00
|
|
|
console.log('Subscribing timed out.');
|
2019-01-28 04:12:07 +00:00
|
|
|
subscribe(timeouts + 1);
|
|
|
|
};
|
2019-01-28 03:06:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 04:12:07 +00:00
|
|
|
function unsubscribe(timeouts = 0) {
|
2019-05-02 01:03:39 +00:00
|
|
|
subscribe_button = document.getElementById('subscribe');
|
2019-03-23 19:05:13 +00:00
|
|
|
|
2019-01-28 04:12:07 +00:00
|
|
|
if (timeouts > 10) {
|
2019-05-02 01:03:39 +00:00
|
|
|
console.log('Failed to subscribe');
|
2019-01-28 04:12:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-02 01:03:39 +00:00
|
|
|
var url = '/subscription_ajax?action_remove_subscriptions=1&redirect=false' +
|
|
|
|
'&c=<%= ucid %>&referer=<%= env.get("current_page") %>';
|
2019-01-28 03:06:28 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
2019-05-02 01:03:39 +00:00
|
|
|
xhr.responseType = 'json';
|
2019-01-28 03:06:28 +00:00
|
|
|
xhr.timeout = 20000;
|
2019-05-02 01:03:39 +00:00
|
|
|
xhr.open('POST', url, true);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
xhr.send('csrf_token=<%= URI.escape(env.get?("csrf_token").try &.as(String) || "") %>');
|
2019-01-28 03:06:28 +00:00
|
|
|
|
2019-01-28 04:12:07 +00:00
|
|
|
var fallback = subscribe_button.innerHTML;
|
|
|
|
subscribe_button.onclick = subscribe;
|
2019-05-02 01:03:39 +00:00
|
|
|
subscribe_button.innerHTML = '<b><%= translate(locale, "Subscribe").gsub("'", "\\'") %> | <%= sub_count_text %></b>';
|
2019-01-28 04:12:07 +00:00
|
|
|
|
2019-01-28 03:06:28 +00:00
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == 4) {
|
2019-01-28 04:12:07 +00:00
|
|
|
if (xhr.status != 200) {
|
|
|
|
subscribe_button.onclick = unsubscribe;
|
|
|
|
subscribe_button.innerHTML = fallback;
|
2019-01-28 03:06:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 04:12:07 +00:00
|
|
|
|
|
|
|
xhr.ontimeout = function() {
|
2019-05-02 01:03:39 +00:00
|
|
|
console.log('Unsubscribing timed out.');
|
2019-01-28 04:12:07 +00:00
|
|
|
unsubscribe(timeouts + 1);
|
|
|
|
};
|
2019-01-28 03:06:28 +00:00
|
|
|
}
|