diff --git a/demo/public/index.html b/demo/public/index.html index 9eee8dc..52eb9c4 100644 --- a/demo/public/index.html +++ b/demo/public/index.html @@ -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(); --> diff --git a/demo/public/js/page.js b/demo/public/js/page.js index f3acbab..adea0bf 100644 --- a/demo/public/js/page.js +++ b/demo/public/js/page.js @@ -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() { diff --git a/demo/public/js/stacosys.js b/demo/public/js/stacosys.js index 24ea418..d8cd38b 100644 --- a/demo/public/js/stacosys.js +++ b/demo/public/js/stacosys.js @@ -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; - } - - // 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; - }; - - xhr.onerror = function() { - console.log('Woops, there was an error making the request.'); - return 0; - }; - - xhr.send(); -} - -function stacosys_load() { - 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'); + callback(0); 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; + var count = jsonResponse.count; + callback(count); + }; + + xhr.onerror = function() { + console.log('Woops, there was an error making the request.'); + callback(0); + }; + + xhr.send(); +} + +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) { + console.log('CORS not supported'); + return; + } + + // Response handlers. + xhr.onload = function() { + var jsonResponse = JSON.parse(xhr.responseText); + callback(jsonResponse); }; xhr.onerror = function() {