Skip to content

Instantly share code, notes, and snippets.

@janmarsicek
Last active April 19, 2016 05:12
Show Gist options
  • Save janmarsicek/67f4a3681a5253ec68e8 to your computer and use it in GitHub Desktop.
Save janmarsicek/67f4a3681a5253ec68e8 to your computer and use it in GitHub Desktop.
/*
This widget sets language and shows Recent Posts on your Tumblr blog.
Its dependency is jQuery.
Usage:
1) Add html:
<div id="recent-posts"></div>
2) Add code before closing </body> tag
License:
Copyright (c) 2012 Jarmo Pertman
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
$(document).ready(function () {
var siteLang;
function detectLanguage() {
var czechLink,
englishLink;
czechLink = $('.ppcbee-tags a[href*="tagged/cesky"]');
englishLink = $('.ppcbee-tags a[href*="tagged/english"]');
siteLang = 'both';
function setCzech() {
$('#czech-flag').addClass('active');
$('#logo-img').attr('href', 'http://blog.ppcbee.com/tagged/cesky');
siteLang = 'cesky';
};
function setEnglish() {
$('#english-flag').addClass('active');
$('#logo-img').attr('href', 'http://blog.ppcbee.com/tagged/english');
siteLang = 'english';
$('a.read_more').addClass('english-btn').attr('button-text', 'Read more');
};
if (czechLink.length >= 1 && englishLink.length == 0) {
setCzech();
} else if (englishLink.length >= 1 && czechLink.length == 0) {
setEnglish();
}
return siteLang;
};
detectLanguage();
var Tumblr = Tumblr || {};
Tumblr.RecentPosts = function(el, postsCount) {
var apiUrl = 'http://' + document.domain + '/api/read/json?callback=?&filter=text&num=' + (postsCount || 10);
var titleTypes = {
regular: 'regular-title',
link: 'link-text',
quote: 'quote-source',
photo: 'photo-caption',
conversation: 'conversation-title',
video: 'video-caption',
audio: 'audio-caption',
answer: 'question'
};
var renderPosts = function(posts) {
return $.map($.map(posts, postInfo), renderPost).slice(0,5);
};
var renderPost = function(post) {
return '<li><a href="' + post.url + '">' + post.title + '</a></li>';
};
if (siteLang == 'cesky') {
var loadingText = 'Načítám...';
var recentPostsTitle = 'Novinky na blogu';
} else {
var loadingText = 'Loading...';
var recentPostsTitle = 'Newest releases';
}
var postInfo = function(post) {
var titleType = titleTypes[post.type];
if (titleType in post && post.tags.indexOf(siteLang) > -1 || titleType in post && siteLang == 'both') {
return {
title: post[titleType],
url: post['url-with-slug']
};
}
};
return {
render: function() {
var loadingEl = $('<div>').text(loadingText).appendTo($(el));
$.getJSON(apiUrl, function(data) {
loadingEl.remove();
$('<ul>').appendTo($(el)).hide().append('<h2>' + recentPostsTitle +'</h2>').append(renderPosts(data.posts).join(' ')).slideDown('slow');
});
return this;
}
}
};
$(function() {
new Tumblr.RecentPosts($('#recent-posts')).render()
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment