finally fix blog issue

This commit is contained in:
Estevao Soares dos Santos 2018-10-17 02:33:10 +01:00
parent 413cedecf0
commit 66294bf67a
3 changed files with 50 additions and 85 deletions

View File

@ -23,16 +23,3 @@
</section> </section>
</article> </article>
</template> </template>
<script>
(function () {
var canonical = $('#param-canonical').attr('value');
$.when(blogPosts.getPostByCanonical(canonical)).then(function(post) {
var article = Mustache.render($('#blog-article-tpl').html(), post);
$('#blog-post-container').html(article);
router.updatePageLinks();
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
});
})();
</script>

View File

@ -24,13 +24,14 @@
</article> </article>
</template> </template>
<script> <script>
$.when(blogPosts.getList()).then(function(list) { function loadBlogPosts(list) {
var articleWrapper = $('#articles-wrapper'); var articleWrapper = $('#articles-wrapper');
for (var i = 0; i < list.length; ++i) { for (var i = 0; i < list.length; ++i) {
var article = Mustache.render($('#blog-list-article-tpl').html(), list[i]); var article = Mustache.render($('#blog-list-article-tpl').html(), list[i]);
articleWrapper.prepend(article); articleWrapper.prepend(article);
}
router.updatePageLinks();
} }
router.updatePageLinks(); $.when(blogPosts.getList()).then(loadBlogPosts);
});
</script> </script>

107
js/app.js
View File

@ -15,57 +15,34 @@ $(document).ready(function() {
blogPosts = new BlogPosts(); blogPosts = new BlogPosts();
function BlogPosts () { function BlogPosts () {
var posts = [];
var indexedList = {};
var getList = this.getList = function() { var getList = this.getList = function() {
var dfd = jQuery.Deferred(); var dfd = jQuery.Deferred();
if (posts.length !== 0) { $.getJSON('blog/posts.json', function(data) {
dfd.resolve(posts); for (var i = 0; i < data.length; ++i) {
} else { data[i].metadata.summary = converter.makeHtml(data[i].metadata.summary);
$.getJSON('blog/posts.json', function(data) { }
for (var i = 0; i < data.length; ++i) { dfd.resolve(data);
data[i].metadata.summary = converter.makeHtml(data[i].metadata.summary); });
}
dfd.resolve(data);
});
}
return dfd.promise(); return dfd.promise();
}; };
this.getPostByCanonical = function(canonical) { this.getPostByCanonical = function(canonical) {
var dfd = jQuery.Deferred(); var dfd = jQuery.Deferred();
var idx = null;
if (typeof indexedList[canonical] !== 'undefined') {
idx = indexedList[canonical];
}
$.when(getList()).then(function(list) { $.when(getList()).then(function(list) {
if (idx !== null) { for (var i = 0; i < list.length; ++i) {
if (list[i].canonical === canonical) {
dfd.resolve(list[idx]); var promise = $.ajax({
} else { url: list[i].url,
for (var i = 0; i < list.length; ++i) { dataType: "text"
console.log(idx, list[i]); });
if (list[i].canonical === canonical) { promise.done((function (index, listItem) {
var promise = $.ajax({ return function (md) {
url: list[i].url, listItem.post = converter.makeHtml(md);
dataType: "text" dfd.resolve(listItem);
}); };
promise.done((function (index, listItem) { })(i, list[i]));
return function (md) { return;
listItem.post = converter.makeHtml(md);
posts[index] = listItem;
indexedList[canonical] = index;
dfd.resolve(listItem);
};
})(i, list[i]));
return;
}
} }
dfd.resolve(null);
} }
}); });
return dfd.promise(); return dfd.promise();
@ -104,9 +81,22 @@ $(document).ready(function() {
return dfd.promise(); return dfd.promise();
} }
function loadPage(url) { function loadPage(url, params) {
var $page = $('#page'); var $page = $('#page');
return $page.load(url, function () { return $page.load(url, function () {
if (params && params.page === 'blog-article') {
$.when(blogPosts.getPostByCanonical(params.canonical)).then(function(post) {
var article = Mustache.render($('#blog-article-tpl').html(), post);
$('#blog-post-container').html(article);
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
hljs.lineNumbersBlock(block);
});
router.updatePageLinks();
});
}
$page.find('pre>code').each(function(i, block) { $page.find('pre>code').each(function(i, block) {
hljs.highlightBlock(block); hljs.highlightBlock(block);
hljs.lineNumbersBlock(block); hljs.lineNumbersBlock(block);
@ -115,19 +105,6 @@ $(document).ready(function() {
}); });
} }
function injectParams(params) {
if (typeof params !== 'undefined') {
// pass params to page
for (var param in params) {
if (params.hasOwnProperty(param)) {
var $pageParams = $('#page-params');
$pageParams.html();
$pageParams.append('<data id="param-' + param + '" value="'+ params[param] +'" hidden style="display: none;"></data>');
}
}
}
}
function changeActiveLink(name) { function changeActiveLink(name) {
$('.internal-navigation').removeClass('active'); $('.internal-navigation').removeClass('active');
$('#navlink-' + name).addClass('active'); $('#navlink-' + name).addClass('active');
@ -141,26 +118,26 @@ $(document).ready(function() {
); );
router router
.on('/releases', function () { .on('/releases', function (params) {
changeActiveLink('releases'); changeActiveLink('releases');
return loadPage('html/releases.html'); return loadPage('html/releases.html', params);
}) })
.on('/documentation', function () { .on('/documentation', function (params) {
changeActiveLink('documentation'); changeActiveLink('documentation');
return loadPage('html/documentation.html'); return loadPage('html/documentation.html', params);
}) })
.on('/blog/:canonical', function (params) { .on('/blog/:canonical', function (params) {
params.page = 'blog-article';
changeActiveLink('blog'); changeActiveLink('blog');
injectParams(params);
return loadPage('html/blog-article.html', params); return loadPage('html/blog-article.html', params);
}) })
.on('/blog', function () { .on('/blog', function (params) {
changeActiveLink('blog'); changeActiveLink('blog');
return loadPage('html/blog.html'); return loadPage('html/blog.html', params);
}) })
.on('*', function () { .on('*', function (params) {
changeActiveLink('home'); changeActiveLink('home');
return loadPage('html/main.html'); return loadPage('html/main.html', params);
}) })
.resolve(); .resolve();