www
[scribo] / www / plugins / galleria.flickr.js
1 /*!
2  * Galleria Flickr Plugin v 1.1
3  * http://galleria.aino.se
4  *
5  * Copyright 2010, Aino
6  * Licensed under the MIT license.
7  */
8
9 (function() {
10    
11 var G = window.Galleria; 
12 if (typeof G == 'undefined') {
13     return;
14 }
15
16 var F = G.Flickr = function(api_key) {
17     if (!api_key) {
18         G.raise('No API key found');
19     }
20         this.callback = function(){};
21         this.api_key = api_key;
22         this.options = {
23                 max: 40,
24                 size: 'big',
25                 sort: 'interestingness-desc',
26                 description: false
27         }
28 }
29
30 F.prototype = {
31         search: function(str) {
32                 this._set(arguments);
33                 return this._find({
34                         text: str
35                 });
36         },
37         getTags: function(str) {
38                 this._set(arguments);
39                 return this._find({
40                         tags: str
41                 });
42         },
43         getUser: function(username) {
44                 var args = arguments;
45                 return this._call({
46                         method: 'flickr.urls.lookupUser',
47                         url: 'flickr.com/photos/'+username
48                 }, function(data) {
49                         this._set(args);
50                         this._find({
51                                 user_id: data.user.id,
52                                 method: 'flickr.people.getPublicPhotos'
53                         });
54                 });
55         },
56         getSet: function(photoset_id) {
57                 this._set(arguments);
58                 return this._find({
59                         photoset_id: photoset_id,
60                         method: 'flickr.photosets.getPhotos'
61                 });
62         },
63         getGallery: function(gallery_id) {
64                 this._set(arguments);
65                 return this._find({
66                         gallery_id: gallery_id,
67                         method: 'flickr.galleries.getPhotos'
68                 });
69         },
70         setOptions: function(options) {
71                 jQuery.extend(this.options, options);
72                 return this;
73         },
74         _set: function(args) {
75                 args = Array.prototype.slice.call(args);
76                 this.callback = args[2] || args[1];
77                 if (typeof args[1] == 'object') {
78                         this.setOptions(args[1]);
79                 }
80                 return this;
81         },
82         _call: function(params, callback) {
83                 var url = 'http://api.flickr.com/services/rest/?';
84                 var scope = this;
85                 params = jQuery.extend({
86                         format : 'json',
87                         jsoncallback : '?',
88                         api_key: this.api_key
89                 }, params);
90                 jQuery.each(params, function(key, value) {
91                         url += '&'+ key + '=' +value;
92                 });
93                 jQuery.getJSON(url, function(data) {
94                         if (data.stat == 'ok') {
95                                 callback.call(scope, data);
96                         } else {
97                                 G.raise(data.code.toString() + ' ' + data.stat + ': ' + data.message);
98                         }
99                 });
100                 return scope;
101         },
102         _find: function(params) {
103                 params = jQuery.extend({
104                         method: 'flickr.photos.search',
105                     extras: 'url_t, url_m, url_o, url_s, url_l, description',
106                     sort: this.options.sort
107                 }, params);
108                 
109                 return this._call(params, function(data) {
110                         var obj = { length: 0 };
111                         var photos = data.photos ? data.photos.photo : data.photoset.photo;
112                         var len = Math.min(this.options.max, photos.length);
113                         var loaded = 0;
114                     
115                         for (var i=0; i<len; i++) {
116                     var photo = photos[i],
117                         img = photo.url_m;
118                     switch(this.options.size) {
119                         case 'small':
120                             img = photo.url_s;
121                             break;
122                         case ( 'big' || 'large' ):
123                             if (photo.url_l) {
124                                 img = photo.url_l;
125                             } else if (parseInt(photo.width_o) > 1280) {
126                                 img = 'http://farm'+photo['farm']+'.static.flickr.com/'+photo['server']+
127                                       '/'+photo['id']+'_' + photo['secret'] + '_b.jpg';
128                             
129                             } else if(photo.url_o) {
130                                 img = photo.url_o;
131                             }
132                             break;
133                         case 'original':
134                             if(photo.url_o) {
135                                 img = photo.url_o;
136                             }
137                             break;    
138                     }
139                 var item = {
140                                 thumb: photos[i].url_t,
141                                 image: img,
142                                 title: photos[i].title,
143                                 description: this.options.description && photos[i].description ? photos[i].description._content : ''
144                         };
145                         Array.prototype.push.call(obj, item);
146                         }
147                         this.callback.call(this, obj);
148                 });
149         }
150 }
151
152
153 // Static
154 F.getFeed = function(type, params) {
155         
156 }
157
158 })();