-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjquery.flickr.js
123 lines (109 loc) · 4.2 KB
/
jquery.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
/*****************************************
* Flickr API (in jQuery)
* version: 1.0 (02/23/2009)
* written for jQuery 1.3.2
* by Ryan Heath (http://rpheath.com)
*****************************************/
(function($) {
// core extensions
$.extend({
// determines if an object is empty
// $.isEmpty({}) // => true
// $.isEmpty({user: 'rph'}) // => false
isEmpty: function(obj) {
for (var i in obj) { return false }
return true
}
})
// base flickr object
$.flickr = {
// the actual request url
// (constructs extra params as they come in)
url: function(method, params) {
return 'http://api.flickr.com/services/rest/?method=' + method + '&format=json' +
'&api_key=' + $.flickr.settings.api_key + ($.isEmpty(params) ? '' : '&' + $.param(params)) + '&jsoncallback=?'
},
// translate plugin image sizes to flickr sizes
translate: function(size) {
switch(size) {
case 'sq': return '_s' // square
case 't' : return '_t' // thumbnail
case 's' : return '_m' // small
case 'm' : return '' // medium
default : return '' // medium
}
},
// determines what to do with the links
linkTag: function(text, photo, href) {
if (href === undefined) href = ['http://www.flickr.com/photos', photo.owner, photo.id].join('/')
return '<a href="' + href + '" title="' + photo.title + '">' + text + '</a>'
}
}
// helper methods for thumbnails
$.flickr.thumbnail = {
src: function(photo, size) {
if (size === undefined) size = $.flickr.translate($.flickr.settings.thumbnail_size)
return 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server +
'/' + photo.id + '_' + photo.secret + size + '.jpg'
},
imageTag: function(image) {
return '<img src="' + image.src + '" alt="' + image.alt + '" />'
}
}
// accepts a series of photos and constructs
// the thumbnails that link back to Flickr
$.flickr.thumbnail.process = function(photos) {
var thumbnails = $.map(photos.photo, function(photo) {
var image = new Image(), html = '', href = undefined
image.src = $.flickr.thumbnail.src(photo)
image.alt = photo.title
var size = $.flickr.settings.link_to_size
if (size != undefined && size.match(/sq|t|s|m|o/))
href = $.flickr.thumbnail.src(photo, $.flickr.translate(size))
html = $.flickr.linkTag($.flickr.thumbnail.imageTag(image), photo, href)
return ['<li>' + html + '</li>']
}).join("\n")
return $('<ul class="flickr"></ul>').append(thumbnails)
}
// handles requesting and thumbnailing photos
$.flickr.photos = function(method, options) {
var options = $.extend($.flickr.settings, options || {}),
elements = $.flickr.self, photos
return elements.each(function() {
$.getJSON($.flickr.url(method, options), function(data) {
photos = (data.photos === undefined ? data.photoset : data.photos)
elements.append($.flickr.thumbnail.process(photos))
})
})
}
// namespace to hold available API methods
// note: options available to each method match that of Flickr's docs
$.flickr.methods = {
// http://www.flickr.com/services/api/flickr.photos.getRecent.html
photosGetRecent: function(options) {
$.flickr.photos('flickr.photos.getRecent', options)
},
// http://www.flickr.com/services/api/flickr.photos.getContactsPublicPhotos.html
photosGetContactsPublicPhotos: function(options) {
$.flickr.photos('flickr.photos.getContactsPublicPhotos', options)
},
// http://www.flickr.com/services/api/flickr.photos.search.html
photosSearch: function(options) {
$.flickr.photos('flickr.photos.search', options)
},
// http://www.flickr.com/services/api/flickr.photosets.getPhotos.html
photosetsGetPhotos: function(options) {
$.flickr.photos('flickr.photosets.getPhotos', options)
}
}
// the plugin
$.fn.flickr = function(options) {
$.flickr.self = $(this)
// base configuration
$.flickr.settings = $.extend({
api_key: 'YOUR API KEY',
thumbnail_size: 'sq'
}, options || {})
return $.flickr.methods
}
})(jQuery);