PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/galleria/1.4.1/plugins/picasa/galleria.picasa.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 320 lines | 175 code | 69 blank | 76 comment | 24 complexity | 4ba828c5d713043fe43e697e7d7c79f8 MD5 | raw file
  1. /**
  2. * Galleria Picasa Plugin 2012-04-04
  3. * http://galleria.io
  4. *
  5. * Licensed under the MIT license
  6. * https://raw.github.com/aino/galleria/master/LICENSE
  7. *
  8. */
  9. (function($) {
  10. /*global jQuery, Galleria, window */
  11. Galleria.requires(1.25, 'The Picasa Plugin requires Galleria version 1.2.5 or later.');
  12. // The script path
  13. var PATH = Galleria.utils.getScriptPath();
  14. /**
  15. @class
  16. @constructor
  17. @example var picasa = new Galleria.Picasa();
  18. @author http://aino.se
  19. @requires jQuery
  20. @requires Galleria
  21. @returns Instance
  22. */
  23. Galleria.Picasa = function() {
  24. this.options = {
  25. max: 30, // photos to return
  26. imageSize: 'medium', // photo size ( thumb,small,medium,big,original ) or a number
  27. thumbSize: 'thumb', // thumbnail size ( thumb,small,medium,big,original ) or a number
  28. complete: function(){} // callback to be called inside the Galleria.prototype.load
  29. };
  30. };
  31. Galleria.Picasa.prototype = {
  32. // bring back the constructor reference
  33. constructor: Galleria.Picasa,
  34. /**
  35. Search for anything at Picasa
  36. @param {String} phrase The string to search for
  37. @param {Function} [callback] The callback to be called when the data is ready
  38. @returns Instance
  39. */
  40. search: function( phrase, callback ) {
  41. return this._call( 'search', 'all', {
  42. q: phrase
  43. }, callback );
  44. },
  45. /**
  46. Get a user's public photos
  47. @param {String} username The username to fetch photos from
  48. @param {Function} [callback] The callback to be called when the data is ready
  49. @returns Instance
  50. */
  51. user: function( username, callback ) {
  52. return this._call( 'user', 'user/' + username, callback );
  53. },
  54. /**
  55. Get photos from an album
  56. @param {String} username The username that owns the album
  57. @param {String} album The album ID
  58. @param {Function} [callback] The callback to be called when the data is ready
  59. @returns Instance
  60. */
  61. useralbum: function( username, album, callback ) {
  62. return this._call( 'useralbum', 'user/' + username + '/album/' + album, callback );
  63. },
  64. /**
  65. Set picasa options
  66. @param {Object} options The options object to blend
  67. @returns Instance
  68. */
  69. setOptions: function( options ) {
  70. $.extend(this.options, options);
  71. return this;
  72. },
  73. // call Picasa
  74. _call: function( type, url, params, callback ) {
  75. url = 'https://picasaweb.google.com/data/feed/api/' + url + '?';
  76. if (typeof params == 'function') {
  77. callback = params;
  78. params = {};
  79. }
  80. var self = this;
  81. params = $.extend({
  82. 'kind': 'photo',
  83. 'access': 'public',
  84. 'max-results': this.options.max,
  85. 'thumbsize': this._getSizes().join(','),
  86. 'alt': 'json-in-script',
  87. 'callback': '?'
  88. }, params );
  89. $.each(params, function( key, value ) {
  90. url += '&' + key + '=' + value;
  91. });
  92. // since Picasa throws 404 when the call is malformed, we must set a timeout here:
  93. var data = false;
  94. Galleria.utils.wait({
  95. until: function() {
  96. return data;
  97. },
  98. success: function() {
  99. self._parse.call( self, data.feed.entry, callback );
  100. },
  101. error: function() {
  102. var msg = '';
  103. if ( type == 'user' ) {
  104. msg = 'user not found.';
  105. } else if ( type == 'useralbum' ) {
  106. msg = 'album or user not found.';
  107. }
  108. Galleria.raise('Picasa request failed' + (msg ? ': ' + msg : '.'));
  109. },
  110. timeout: 5000
  111. });
  112. $.getJSON( url, function( result ) {
  113. data = result;
  114. });
  115. return self;
  116. },
  117. // parse image sizes and return an array of three
  118. _getSizes: function() {
  119. var self = this,
  120. norm = {
  121. small: '72c',
  122. thumb: '104u',
  123. medium: '640u',
  124. big: '1024u',
  125. original: '1600u'
  126. },
  127. op = self.options,
  128. t = {},
  129. n,
  130. sz = [32,48,64,72,94,104,110,128,144,150,160,200,220,288,320,400,512,576,640,720,800,912,1024,1152,1280,1440,1600];
  131. $(['thumbSize', 'imageSize']).each(function() {
  132. if( op[this] in norm ) {
  133. t[this] = norm[ op[this] ];
  134. } else {
  135. n = Galleria.utils.parseValue( op[this] );
  136. if (n > 1600) {
  137. n = 1600;
  138. } else {
  139. $.each( sz, function(i) {
  140. if ( n < this ) {
  141. n = sz[i-1];
  142. return false;
  143. }
  144. });
  145. }
  146. t[this] = n;
  147. }
  148. });
  149. return [ t.thumbSize, t.imageSize, '1280u'];
  150. },
  151. // parse the result and call the callback with the galleria-ready data array
  152. _parse: function( data, callback ) {
  153. var self = this,
  154. gallery = [],
  155. img;
  156. $.each( data, function() {
  157. img = this.media$group.media$thumbnail;
  158. gallery.push({
  159. thumb: img[0].url,
  160. image: img[1].url,
  161. big: img[2].url,
  162. title: this.summary.$t
  163. });
  164. });
  165. callback.call( this, gallery );
  166. }
  167. };
  168. /**
  169. Galleria modifications
  170. We fake-extend the load prototype to make Picasa integration as simple as possible
  171. */
  172. // save the old prototype in a local variable
  173. var load = Galleria.prototype.load;
  174. // fake-extend the load prototype using the picasa data
  175. Galleria.prototype.load = function() {
  176. // pass if no data is provided or picasa option not found
  177. if ( arguments.length || typeof this._options.picasa !== 'string' ) {
  178. load.apply( this, Galleria.utils.array( arguments ) );
  179. return;
  180. }
  181. // define some local vars
  182. var self = this,
  183. args = Galleria.utils.array( arguments ),
  184. picasa = this._options.picasa.split(':'),
  185. p,
  186. opts = $.extend({}, self._options.picasaOptions),
  187. loader = typeof opts.loader !== 'undefined' ?
  188. opts.loader : $('<div>').css({
  189. width: 48,
  190. height: 48,
  191. opacity: 0.7,
  192. background:'#000 url('+PATH+'loader.gif) no-repeat 50% 50%'
  193. });
  194. if ( picasa.length ) {
  195. // validate the method
  196. if ( typeof Galleria.Picasa.prototype[ picasa[0] ] !== 'function' ) {
  197. Galleria.raise( picasa[0] + ' method not found in Picasa plugin' );
  198. return load.apply( this, args );
  199. }
  200. // validate the argument
  201. if ( !picasa[1] ) {
  202. Galleria.raise( 'No picasa argument found' );
  203. return load.apply( this, args );
  204. }
  205. // apply the preloader
  206. window.setTimeout(function() {
  207. self.$( 'target' ).append( loader );
  208. },100);
  209. // create the instance
  210. p = new Galleria.Picasa();
  211. // apply Flickr options
  212. if ( typeof self._options.picasaOptions === 'object' ) {
  213. p.setOptions( self._options.picasaOptions );
  214. }
  215. // call the picasa method and trigger the DATA event
  216. var arg = [];
  217. if ( picasa[0] == 'useralbum' ) {
  218. arg = picasa[1].split('/');
  219. if (arg.length != 2) {
  220. Galleria.raise( 'Picasa useralbum not correctly formatted (should be [user]/[album])');
  221. return;
  222. }
  223. } else {
  224. arg.push( picasa[1] );
  225. }
  226. arg.push(function(data) {
  227. self._data = data;
  228. loader.remove();
  229. self.trigger( Galleria.DATA );
  230. p.options.complete.call(p, data);
  231. });
  232. p[ picasa[0] ].apply( p, arg );
  233. } else {
  234. // if flickr array not found, pass
  235. load.apply( this, args );
  236. }
  237. };
  238. }( jQuery ) );