Split page rendering and CORS requests against stacosys

pull/6/head
Yax 10 years ago
parent 71383eff9a
commit 37920f6c97

@ -205,7 +205,7 @@ STACOSYS_TOKEN = '9fb3fc042c572cb831005fd16186126765140fa2bd9bb2d4a28e47a9457dc2
//STACOSYS_PAGE = 'blogduyax.madyanne.fr/mes-applications-pour-blackberry.html'
STACOSYS_PAGE = 'blogduyax.madyanne.fr/migration-du-blog-sous-pelican.html'
window.onload = stacosys_count();
window.onload = initialize_comments();
--></script>

@ -8,8 +8,30 @@ function show_hide(panel_id, button_id){
}
function show_comments() {
stacosys_load();
stacosys_load(comments_loaded);
}
function comments_loaded(response) {
for (var i = 0, numTokens = response.data.length; i < numTokens; ++i) {
response.data[i].mdcontent = markdown.toHTML(response.data[i].content);
}
show_hide('stacosys-comments', 'show-comments-button');
var template = document.getElementById('stacosys-template').innerHTML;
var rendered = Mustache.render(template, response);
document.getElementById('stacosys-comments').innerHTML = rendered;
}
function initialize_comments() {
stacosys_count(comments_initialized);
}
function comments_initialized(count) {
if (count > 0) {
if (count > 1) {
document.getElementById('show-comment-label').innerHTML = 'Voir les ' + count + ' commentaires';
}
document.getElementById('show-comments-button').style.display = '';
}
}
function preview_markdown() {

@ -1,4 +1,3 @@
// Released under Apache license
// Copyright (c) 2015 Yannic ARNOUX
// Create the XHR object.
@ -18,53 +17,44 @@ function stacosys_get_cors_request(method, url) {
return xhr;
}
function stacosys_count() {
function stacosys_count(callback) {
var url = STACOSYS_URL + '/comments/count?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
var xhr = stacosys_get_cors_request('GET', url);
if (!xhr) {
console.log('CORS not supported');
return 0;
callback(0);
return;
}
// Response handlers.
xhr.onload = function() {
var jsonResponse = JSON.parse(xhr.responseText);
var count = jsonResponse.count;
if (count > 0) {
if (count > 1) {
document.getElementById('show-comment-label').innerHTML = 'Voir les ' + count + ' commentaires';
}
document.getElementById('show-comments-button').style.display = '';
}
return jsonResponse.count;
callback(count);
};
xhr.onerror = function() {
console.log('Woops, there was an error making the request.');
return 0;
callback(0);
};
xhr.send();
}
function stacosys_load() {
var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
function stacosys_load(callback) {
var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
var xhr = stacosys_get_cors_request('GET', url);
if (!xhr) {
alert('CORS not supported');
console.log('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var jsonResponse = JSON.parse(xhr.responseText);
for (var i = 0, numTokens = jsonResponse.data.length; i < numTokens; ++i) {
jsonResponse.data[i].mdcontent = markdown.toHTML(jsonResponse.data[i].content);
}
var template = document.getElementById('stacosys-template').innerHTML;
var rendered = Mustache.render(template, jsonResponse);
document.getElementById('stacosys-comments').innerHTML = rendered;
callback(jsonResponse);
};
xhr.onerror = function() {

Loading…
Cancel
Save