This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.fancy-photoset.js
75 lines (70 loc) · 2.36 KB
/
jquery.fancy-photoset.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
/*! Copyright (c) 2010 Phil Cohen (http://phlippers.net)
* Dual licensed under the MIT (MIT_LICENSE.txt)
* and GPL Version 2 (GPL_LICENSE.txt) licenses.
*
* Version: 0.1.1
* Requires jQuery 1.4.2+, Fancybox 1.3.1+
* Docs: http://phlippers.net/code/fancy-photoset
*/
(function($) {
$.fn.fancyPhotoset = function(options) {
var opts = $.extend($.fn.fancyPhotoset.defaults, options);
var domId = 'fancyPhotoset-' + opts.photosetId;
var jsonUrl = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key={apiKey}&photoset_id={photosetId}&format=json&jsoncallback=?'.replace(/\{\w+\}/g, function(match) {
return opts[match.replace(/\{|\}/g, '')];
});
return this.each(function() {
var obj = $(this);
$(obj).append(
$('<ul/>').attr('id', domId).addClass('fancyPhotoset')
);
$.getJSON(jsonUrl, function(data) {
$.each(data.photoset.photo, function(index, photo) {
$(obj).find('ul').append(
$('<li/>').html(
$('<a/>').attr({'href': $.fn.fancyPhotoset.urlFor(photo, {size: opts.large}), 'rel': 'flickr-' + opts.photosetId, 'title': photo.title}).html(
$('<img/>').attr({'src': $.fn.fancyPhotoset.urlFor(photo, {size: opts.small}), 'title': photo.title, 'alt': photo.title}).after(
(opts.captions ? $('<span/>').addClass('caption').text(photo.title) : '')
)
).fancybox()
)
);
})
});
});
};
// generate static image url
$.fn.fancyPhotoset.urlFor = function(photo, options) {
var url = 'http://farm{farm}.static.flickr.com/{server}/{id}_{secret}{size}.{format}';
var opts = $.extend({size: 'square', format: 'jpg'}, options);
var sizes = {
'small' : '_m',
'original' : '_o',
'medium' : '',
'large' : '_b',
'thumbnail' : '_t',
'square' : '_s'
};
return url.replace(/\{\w+\}/g, function(match) {
match = match.replace(/\{|\}/g, '');
switch (match) {
case 'size':
return sizes[opts.size];
break;
case 'format':
return opts.format;
break;
default:
return photo[match];
}
});
};
// default options
$.fn.fancyPhotoset.defaults = {
apiKey : '',
photosetId : '',
small : 'square',
large : 'medium',
captions : true
};
})(jQuery);