forked from matthewbuchanan/tumblr-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tumblr-kit.js
145 lines (129 loc) · 4.81 KB
/
jquery.tumblr-kit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Tumblr Kit 0.9.4 (Initial Public Release)
*
* A jQuery plugin for importing post data from Tumblr’s API.
* http://github.com/matthewbuchanan/tumblr-kit/
*
* Copyright (c) 2012 by Matthew Buchanan
* http://matthewbuchanan.name
*
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* First released June 15, 2012
*/
;(function ( $, window, undefined ) {
$.fn.getTumblrPosts = function(options) {
$.views.helpers({
getHostname: function() {
// Returns the blog hostname
return TUMBLR_HOSTNAME;
},
getTintedAudioPlayer: function(view, color) {
// Returns the embed code for the Flash audio player, optionally tinted with
// the specified hex "color" value.
var embed = view.data.player;
if (color != null) {
var regex = /color=FFFFFF/g;
color = color.split("#")[1];
return embed.replace(regex, "color=" + color);
} else {
return embed;
}
},
getPhotoURL: function(view, size) {
// Returns the best image URL from available photo sizes based on the specified
// "size" parameter. For non-native sizes, returns the next largest size (if available).
var images = view.data.alt_sizes,
src = "";
for (var i = 0; i < images.length; i++) {
if (images[i].width >= size) src = images[i].url; else break;
}
return (src === "") ? images[0].url : src;
},
getPhotoOrientation: function(view) {
// Returns the image’s aspect ratio.
var size = view.data.original_size;
if (size.width > size.height) {
return "landscape";
} else if (size.width < size.height) {
return "portrait";
} else {
return "square";
}
},
getVideoEmbed: function(view, size) {
// Returns the best embed code from available video sizes based on the specified
// "size" parameter. For non-native sizes, returns the next smallest size.
var players = view.data.player,
embed = "";
for (var i = 0; i < players.length; i++) {
if (players[i].width <= size) embed = players[i].embed_code; else break;
}
return (embed === "") ? players[0].embed_code : embed;
}
});
// Default settings
var settings = $.extend({
"hostname": TUMBLR_HOSTNAME,
"id": null, // post ID
"type": "", // text, quote, link, answer, video, audio, photo
"tag": "",
"limit": 20, // 1 — 20
"offset": 0, // 0 or integer
"format": "", // none (for html), text, raw
"template": "", // ID of JsRender template
"before": null,
"done": null, // replaces success() callback, to be deprecated in jQuery 1.8
"always": null // replaces complete() callback, to be deprecated in jQuery 1.8
}, options);
var target = this;
return this.each(function() {
// Construct URL to call API based on settings
if (settings.type != "") settings.type = "/" + settings.type;
var uri = "http://api.tumblr.com/v2/blog/" +
settings.hostname + "/posts" +
settings.type + "?api_key=" +
TUMBLR_API_KEY,
uriWithoutOffset = "";
// Either request a single post by ID or a set of posts by tag, limit, offset
// Retain URI without offset to pass to callback, for use with infinite scroll
if (settings.id) {
uri += "&id=" + parseInt(settings.id);
} else {
if (settings.tag) uri += "&tag=" + encodeURI(settings.tag);
if (settings.limit) uri += "&limit=" + parseInt(settings.limit);
uriWithoutOffset = uri;
if (settings.offset) uri += "&offset=" + parseInt(settings.offset);
}
// Send Ajax request to Tumblr API and render returned posts
var request = $.ajax({
url: uri,
dataType: "jsonp",
jsonp: "jsonp",
ifModified: true,
beforeSend: function() {
// Run before() function if set, maintaining context for 'this'
if (typeof settings.before === "function") settings.before.call(target);
}
}).done(function(data, textStatus, jqXHR) {
// Process each returned post
if (typeof data !== "undefined" && typeof data.response !== "undefined" > data.response.total_posts > 0) {
$.each(data.response.posts, function() {
// Set a default JsRender template if none was specified
var template = (settings.template != "") ? settings.template : "#tmpl-" + this.type;
// Render the post contents with the JsRender template
target.append($(template).render(this));
});
}
// Run success() function if set, maintaining context for 'this'
if (typeof settings.done === "function") settings.done.call(target, data, textStatus, jqXHR, uriWithoutOffset);
}).always(function(jqXHR, textStatus) {
// Hide ‘loading’ messages included in markup, if any
target.find(".tumblr-api-loading").hide();
// Run complete() function if set, maintaining context for 'this'
if (typeof settings.always === "function") settings.always.call(target, jqXHR, textStatus, uriWithoutOffset);
});
});
};
}(jQuery, window));