-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (149 loc) · 5.64 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var Tumblr = require('tumblr'),
strftime = require('strftime');
function TumblrPosts(options){
var tumblr, self = this['posts'] = {};
this.plugin.push(function(){ tumblr = this });
self._time = strftime;
self._format = '%B %d, %Y %H:%M:%S';
self._size = {player:0, photo:0};
self._limit = 10;
self._offset = 0;
self.get = function(options,requestOptions){
options = mergeOptions({limit:this._limit,offset:this._offset},options);
return tumblr.get('posts',options,requestOptions);
}
self.size = function(options){
mergeOptions(this._size,options);
return this;
}
self.limit = function(limit){
this._limit = limit;
return this;
}
self.offset = function(offset){
if(offset === true)
this._offset+= this._limit;
else if(offset === false)
this._offset-= this._limit;
else this._offset = offset;
if(this._offset < 0) this._offset = 0;
return this;
}
self.dateFormat = function(format,i18n){
this._format = format;
if(i18n) this._time = strftime.localizedStrftime(i18n);
return this;
}
self.target = function(container){
if(typeof container === 'string')
this.elem = document.getElementById(container);
else this.elem = container;
return this;
}
self.date = function(date){
return this._time(this._format,new Date(date*1000));
}
self.hasNext = function(){
return self.total && self.total > self._offset + self._limit;
}
self.hasPrevious = function(){
return self.total && self._offset > 0;
}
self.onError = function(reason){
console.log("Error: ", reason)
}
self.fetch = function(){
var i = 0, target, options, requestOptions;
if(typeof arguments[i] === 'function') target = arguments[i++];
else if(typeof arguments[i] === 'string') target = arguments[i++];
else if(typeof arguments[i] === 'object' && arguments[i].nodeType) target = arguments[i++];
else target = this.elem;
if(typeof arguments[i] === 'object') options = arguments[i++];
if(typeof arguments[i] === 'object') requestOptions = arguments[i++];
this.get(options,requestOptions)
.then(function(data){
self.total = data.response.total_posts;
self.next = function(){
self.offset(true);
self.fetch(target,options,requestOptions)
};
self.previous = function(){
self.offset(false);
self.fetch(target,options,requestOptions);
};
try{
self.render(target,data.response);
} catch(error){
self.onError(error);
}
},function(error){
self.onError((error.message||(error.meta && error.meta.msg)||error.code||'?'));
}
);
return this;
}
self.render = function(target,data,size){
if(typeof target === 'string'){
target = document.getElementById(container);
}
if(!target) throw new Error("Tumblr posts invalid target");
var posts = data.posts;
if(!Array.isArray(posts)) posts = [posts];
size = mergeOptions(this._size,size);
var html = '';
for(var i = 0, post; post = posts[i]; i++){
if(['text','photo','video','audio'].indexOf(post.type)<0) continue;
html+= '<article class="'+post.type+'">\n';
html+='<time datetime="'+ post.date +'">'+this.date(post.timestamp)+'</time>\n';
switch(post.type){
case 'text':
html+='<header>'+post.title+'</header>\n';
html+='<span>'+post.body+'</span>';
break;
case 'photo':
html+='<header>'+post.caption+'</header>\n';
post.photos.forEach(function(photo){
html+='<figure>\n';
html+='<img src="'+altSizeUrl(photo.alt_sizes,size)+'">\n';
if(photo.caption) html+='<figcaption>'+photo.caption+'</figcaption>\n';
html+='</figure>';
});
break;
case 'audio':
html+='<header>'+post.caption+'</header>\n';
html+='<span>'+post.embed+'</span>\n';
break;
case 'video':
html+='<header>'+post.caption+'</header>\n';
html+='<span>\n';
html+=playerSizeEmbed(post.player,size);
html+='</span>\n';
break;
}
if(post.tags.length)
html+='<footer>'+post.tags.join(' ')+'</footer>\n';
html+= '</article>\n';
}
if(typeof target === 'function') {
target({html:html,data:data});
}
else target.innerHTML = html;
return this;
}
}
function mergeOptions(target,source){
for(var key in source) {
target[key] = source[key];
}
return target;
}
function altSizeUrl(alts,size){
var l = alts.length-1, s = size.photo > l ? l : size.photo;
return alts[s].url;
}
function playerSizeEmbed(players,size){
var l = players.length-1, s = size.player > l ? l : size.player;
return players[s].embed_code;
}
TumblrPosts.call(Tumblr.prototype);
module.exports = Tumblr;