/
popcorn.flickr.js
235 lines (212 loc) · 7 KB
/
popcorn.flickr.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// PLUGIN: Flickr
(function (Popcorn) {
/**
* Flickr popcorn plug-in
* Appends a users Flickr images to an element on the page.
* Options parameter will need a start, end, target and userid or username and api_key.
* Optional parameters are numberofimages, height, width, padding, and border
* Start is the time that you want this plug-in to execute (in seconds)
* End is the time that you want this plug-in to stop executing (in seconds)
* Userid is the id of who's Flickr images you wish to show
* Tags is a mutually exclusive list of image descriptor tags
* Username is the username of who's Flickr images you wish to show
* using both userid and username is redundant
* an api_key is required when using username
* Apikey is your own api key provided by Flickr
* Target is the id of the document element that the images are
* appended to, this target element must exist on the DOM
* Numberofimages specify the number of images to retreive from flickr, defaults to 4
* Height the height of the image, defaults to '50px'
* Width the width of the image, defaults to '50px'
* Padding number of pixels between images, defaults to '5px'
* Border border size in pixels around images, defaults to '0px'
*
* @param {Object} options
*
* Example:
var p = Popcorn('#video')
.flickr({
start: 5, // seconds, mandatory
end: 15, // seconds, mandatory
userid: '35034346917@N01', // optional
tags: 'dogs,cats', // optional
numberofimages: '8', // optional
height: '50px', // optional
width: '50px', // optional
padding: '5px', // optional
border: '0px', // optional
target: 'flickrdiv' // mandatory
} )
*
*/
var idx = 0;
Popcorn.plugin( "flickr" , function( options ) {
var containerDiv,
target = document.getElementById( options.target ),
_userid,
_uri,
_link,
_image,
_count = options.numberofimages || 4,
_height = options.height || "50px",
_width = options.width || "50px",
_padding = options.padding || "5px",
_border = options.border || "0px";
// create a new div this way anything in the target div is left intact
// this is later populated with Flickr images
containerDiv = document.createElement( "div" );
containerDiv.id = "flickr" + idx;
containerDiv.style.width = "100%";
containerDiv.style.height = "100%";
containerDiv.style.display = "none";
idx++;
// ensure the target container the user chose exists
if ( !target && Popcorn.plugin.debug ) {
throw new Error( "flickr target container doesn't exist" );
}
target && target.appendChild( containerDiv );
// get the userid from Flickr API by using the username and apikey
var isUserIDReady = function() {
if ( !_userid ) {
_uri = "http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&";
_uri += "username=" + options.username + "&api_key=" + options.apikey + "&format=json&jsoncallback=flickr";
Popcorn.getJSONP( _uri, function( data ) {
_userid = data.user.nsid;
getFlickrData();
});
} else {
setTimeout(function () {
isUserIDReady();
}, 5 );
}
};
// get the photos from Flickr API by using the user_id and/or tags
var getFlickrData = function() {
_uri = "http://api.flickr.com/services/feeds/photos_public.gne?";
if ( _userid ) {
_uri += "id=" + _userid + "&";
}
if ( options.tags ) {
_uri += "tags=" + options.tags + "&";
}
_uri += "lang=en-us&format=json&jsoncallback=flickr";
Popcorn.xhr.getJSONP( _uri, function( data ) {
var fragment = document.createElement( "p" );
fragment.innerHTML = "<p style='padding:" + _padding + ";'>" + data.title + "<p/>";
Popcorn.forEach( data.items, function ( item, i ) {
if ( i < _count ) {
_link = document.createElement( "a" );
_link.setAttribute( "href", item.link );
_link.setAttribute( "target", "_blank" );
_image = document.createElement( "img" );
_image.setAttribute( "src", item.media.m );
_image.setAttribute( "height",_height );
_image.setAttribute( "width", _width );
_image.setAttribute( "style", "border:" + _border + ";padding:" + _padding );
_link.appendChild( _image );
fragment.appendChild( _link );
} else {
return false;
}
});
containerDiv.appendChild( fragment );
});
};
if ( options.username && options.apikey ) {
isUserIDReady();
}
else {
_userid = options.userid;
getFlickrData();
}
return {
/**
* @member flickr
* The start function will be executed when the currentTime
* of the video reaches the start time provided by the
* options variable
*/
start: function( event, options ) {
containerDiv.style.display = "inline";
},
/**
* @member flickr
* The end function will be executed when the currentTime
* of the video reaches the end time provided by the
* options variable
*/
end: function( event, options ) {
containerDiv.style.display = "none";
},
_teardown: function( options ) {
document.getElementById( options.target ) && document.getElementById( options.target ).removeChild( containerDiv );
}
};
},
{
about: {
name: "Popcorn Flickr Plugin",
version: "0.2",
author: "Scott Downe, Steven Weerdenburg, Annasob",
website: "http://scottdowne.wordpress.com/"
},
options: {
start: {
elem: "input",
type: "number",
label: "In"
},
end: {
elem: "input",
type: "number",
label: "Out"
},
userid: {
elem: "input",
type: "text",
label: "UserID"
},
tags: {
elem: "input",
type: "text",
label: "Tags"
},
username: {
elem: "input",
type: "text",
label: "Username"
},
apikey: {
elem: "input",
type: "text",
label: "Api_key"
},
target: "flickr-container",
height: {
elem: "input",
type: "text",
label: "Height"
},
width: {
elem: "input",
type: "text",
label: "Width"
},
padding: {
elem: "input",
type: "text",
label: "Padding"
},
border: {
elem: "input",
type: "text",
label: "Border"
},
numberofimages: {
elem: "input",
type: "text",
label: "Number of Images"
}
}
});
})( Popcorn );