Debian Codegraph @

closure-library/closure/goog/ui/media/picasa.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 /**
17 * @fileoverview provides a reusable picasa album UI component given a public
18 * picasa album URL.
19 *
20 * TODO(user): implement the javascript viewer, for users without flash. Get it
21 * from the Gmail Picasa gadget.
22 *
23 * goog.ui.media.PicasaAlbum is actually a {@link goog.ui.ControlRenderer}, a
24 * stateless class - that could/should be used as a Singleton with the static
25 * method {@code goog.ui.media.PicasaAlbum.getInstance} -, that knows how to
26 * render picasa albums. It is designed to be used with a
27 * {@link goog.ui.Control}, which will actually control the media renderer and
28 * provide the {@link goog.ui.Component} base. This design guarantees that all
29 * different types of medias will behave alike but will look different.
30 *
31 * goog.ui.media.PicasaAlbum expects {@code goog.ui.media.PicasaAlbumModel}s on
32 * {@code goog.ui.Control.getModel} as data models, and render a flash object
33 * that will show a slideshow with the contents of that album URL.
34 *
35 * Example of usage:
36 *
37 * <pre>
38 * var album = goog.ui.media.PicasaAlbumModel.newInstance(
39 * 'http://picasaweb.google.com/username/SanFranciscoCalifornia');
40 * goog.ui.media.PicasaAlbum.newControl(album).render();
41 * </pre>
42 *
43 * picasa medias currently support the following states:
44 *
45 * <ul>
46 * <li> {@link goog.ui.Component.State.DISABLED}: shows 'flash not available'
47 * <li> {@link goog.ui.Component.State.HOVER}: mouse cursor is over the album
48 * <li> {@link goog.ui.Component.State.SELECTED}: flash album is shown
49 * </ul>
50 *
51 * Which can be accessed by
52 *
53 * <pre>
54 * picasa.setEnabled(true);
55 * picasa.setHighlighted(true);
56 * picasa.setSelected(true);
57 * </pre>
58 *
59 * Requires flash to actually work.
60 */
61
62 goog.provide('goog.ui.media.PicasaAlbum');
63 goog.provide('goog.ui.media.PicasaAlbumModel');
64
65 goog.require('goog.html.TrustedResourceUrl');
66 goog.require('goog.string.Const');
67 goog.require('goog.ui.media.FlashObject');
68 goog.require('goog.ui.media.Media');
69 goog.require('goog.ui.media.MediaModel');
70 goog.require('goog.ui.media.MediaRenderer');
71
72
73
74 /**
75 * Subclasses a goog.ui.media.MediaRenderer to provide a Picasa specific media
76 * renderer.
77 *
78 * This class knows how to parse picasa URLs, and render the DOM structure
79 * of picasa album players and previews. This class is meant to be used as a
80 * singleton static stateless class, that takes {@code goog.ui.media.Media}
81 * instances and renders it. It expects {@code goog.ui.media.Media.getModel} to
82 * return a well formed, previously constructed, object with a user and album
83 * fields {@see goog.ui.media.PicasaAlbum.parseUrl}, which is the data model
84 * this renderer will use to construct the DOM structure.
85 * {@see goog.ui.media.PicasaAlbum.newControl} for a example of constructing a
86 * control with this renderer.
87 *
88 * goog.ui.media.PicasaAlbum currently displays a picasa-made flash slideshow
89 * with the photos, but could possibly display a handwritten js photo viewer,
90 * in case flash is not available.
91 *
92 * This design is patterned after http://go/closure_control_subclassing
93 *
94 * It uses {@link goog.ui.media.FlashObject} to embed the flash object.
95 *
96 * @constructor
97 * @extends {goog.ui.media.MediaRenderer}
98 * @final
99 */
100 goog.ui.media.PicasaAlbum = function() {
101 goog.ui.media.MediaRenderer.call(this);
102 };
103 goog.inherits(goog.ui.media.PicasaAlbum, goog.ui.media.MediaRenderer);
104 goog.addSingletonGetter(goog.ui.media.PicasaAlbum);
105
106
107 /**
108 * Default CSS class to be applied to the root element of components rendered
109 * by this renderer.
110 *
111 * @type {string}
112 */
113 goog.ui.media.PicasaAlbum.CSS_CLASS = goog.getCssName('goog-ui-media-picasa');
114
115
116 /**
117 * A static convenient method to construct a goog.ui.media.Media control out of
118 * a picasa data model. It sets it as the data model goog.ui.media.PicasaAlbum
119 * renderer uses, sets the states supported by the renderer, and returns a
120 * Control that binds everything together. This is what you should be using for
121 * constructing Picasa albums, except if you need finer control over the
122 * configuration.
123 *
124 * @param {goog.ui.media.PicasaAlbumModel} dataModel A picasa album data model.
125 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
126 * document interaction.
127 * @return {!goog.ui.media.Media} A Control instance binded to the Picasa
128 * renderer.
129 */
130 goog.ui.media.PicasaAlbum.newControl = function(dataModel, opt_domHelper) {
131 var control = new goog.ui.media.Media(
132 dataModel, goog.ui.media.PicasaAlbum.getInstance(), opt_domHelper);
133 control.setSelected(true);
134 return control;
135 };
136
137
138 /**
139 * Creates the initial DOM structure of the picasa album, which is basically a
140 * the flash object pointing to a flash picasa album player.
141 *
142 * @param {goog.ui.Control} c The media control.
143 * @return {!Element} The DOM structure that represents the control.
144 * @override
145 */
146 goog.ui.media.PicasaAlbum.prototype.createDom = function(c) {
147 var control = /** @type {goog.ui.media.Media} */ (c);
148 var div = goog.ui.media.PicasaAlbum.superClass_.createDom.call(this, control);
149
150 var picasaAlbum =
151 /** @type {goog.ui.media.PicasaAlbumModel} */ (control.getDataModel());
152 var flash = new goog.ui.media.FlashObject(
153 picasaAlbum.getPlayer().getTrustedResourceUrl(), control.getDomHelper());
154 flash.addFlashVars(picasaAlbum.getPlayer().getVars());
155 flash.render(div);
156
157 return div;
158 };
159
160
161 /**
162 * Returns the CSS class to be applied to the root element of components
163 * rendered using this renderer.
164 * @return {string} Renderer-specific CSS class.
165 * @override
166 */
167 goog.ui.media.PicasaAlbum.prototype.getCssClass = function() {
168 return goog.ui.media.PicasaAlbum.CSS_CLASS;
169 };
170
171
172
173 /**
174 * The {@code goog.ui.media.PicasaAlbum} media data model. It stores a required
175 * {@code userId} and {@code albumId} fields, sets the picasa album URL, and
176 * allows a few optional parameters.
177 *
178 * @param {string} userId The picasa userId associated with this album.
179 * @param {string} albumId The picasa albumId associated with this album.
180 * @param {string=} opt_authKey An optional authentication key, used on private
181 * albums.
182 * @param {string=} opt_caption An optional caption of the picasa album.
183 * @param {string=} opt_description An optional description of the picasa album.
184 * @param {boolean=} opt_autoplay Whether to autoplay the slideshow.
185 * @constructor
186 * @extends {goog.ui.media.MediaModel}
187 * @final
188 */
189 goog.ui.media.PicasaAlbumModel = function(
190 userId, albumId, opt_authKey, opt_caption, opt_description, opt_autoplay) {
191 goog.ui.media.MediaModel.call(
192 this, goog.ui.media.PicasaAlbumModel.buildUrl(userId, albumId),
193 opt_caption, opt_description, goog.ui.media.MediaModel.MimeType.FLASH);
194
195 /**
196 * The Picasa user id.
197 * @type {string}
198 * @private
199 */
200 this.userId_ = userId;
201
202 /**
203 * The Picasa album id.
204 * @type {string}
205 * @private
206 */
207 this.albumId_ = albumId;
208
209 /**
210 * The Picasa authentication key, used on private albums.
211 * @type {?string}
212 * @private
213 */
214 this.authKey_ = opt_authKey || null;
215
216 var authParam = opt_authKey ? ('&authkey=' + opt_authKey) : '';
217
218 var flashVars = {
219 'host': 'picasaweb.google.com',
220 'RGB': '0x000000',
221 'feed': 'http://picasaweb.google.com/data/feed/api/user/' + userId +
222 '/album/' + albumId + '?kind=photo&alt=rss' + authParam
223 };
224 flashVars[opt_autoplay ? 'autoplay' : 'noautoplay'] = '1';
225
226 var flashUrl = goog.html.TrustedResourceUrl.fromConstant(
227 goog.string.Const.from(
228 'http://picasaweb.google.com/s/c/bin/slideshow.swf'));
229 var player = new goog.ui.media.MediaModel.Player(flashUrl, flashVars);
230
231 this.setPlayer(player);
232 };
233 goog.inherits(goog.ui.media.PicasaAlbumModel, goog.ui.media.MediaModel);
234
235
236 /**
237 * Regular expression used to extract the picasa username and albumid out of
238 * picasa URLs.
239 *
240 * Copied from http://go/markdownlite.js,
241 * and {@link PicasaWebExtractor.xml}.
242 *
243 * @type {RegExp}
244 * @private
245 * @const
246 */
247 goog.ui.media.PicasaAlbumModel.MATCHER_ =
248 /https?:\/\/(?:www\.)?picasaweb\.(?:google\.)?com\/([\d\w\.]+)\/([\d\w_\-\.]+)(?:\?[\w\d\-_=&amp;;\.]*&?authKey=([\w\d\-_=;\.]+))?(?:#([\d]+)?)?/im;
249
250
251 /**
252 * Gets a {@code picasaUrl} and extracts the user and album id.
253 *
254 * @param {string} picasaUrl A picasa album URL.
255 * @param {string=} opt_caption An optional caption of the picasa album.
256 * @param {string=} opt_description An optional description of the picasa album.
257 * @param {boolean=} opt_autoplay Whether to autoplay the slideshow.
258 * @return {!goog.ui.media.PicasaAlbumModel} The picasa album data model that
259 * represents the picasa URL.
260 * @throws exception in case the parsing fails
261 */
262 goog.ui.media.PicasaAlbumModel.newInstance = function(
263 picasaUrl, opt_caption, opt_description, opt_autoplay) {
264 if (goog.ui.media.PicasaAlbumModel.MATCHER_.test(picasaUrl)) {
265 var data = goog.ui.media.PicasaAlbumModel.MATCHER_.exec(picasaUrl);
266 return new goog.ui.media.PicasaAlbumModel(
267 data[1], data[2], data[3], opt_caption, opt_description, opt_autoplay);
268 }
269 throw new Error(
270 'failed to parse user and album from picasa url: ' + picasaUrl);
271 };
272
273
274 /**
275 * The opposite of {@code newInstance}: takes an {@code userId} and an
276 * {@code albumId} and builds a URL.
277 *
278 * @param {string} userId The user that owns the album.
279 * @param {string} albumId The album id.
280 * @return {string} The URL of the album.
281 */
282 goog.ui.media.PicasaAlbumModel.buildUrl = function(userId, albumId) {
283 return 'http://picasaweb.google.com/' + userId + '/' + albumId;
284 };
285
286
287 /**
288 * Gets the Picasa user id.
289 * @return {string} The Picasa user id.
290 */
291 goog.ui.media.PicasaAlbumModel.prototype.getUserId = function() {
292 return this.userId_;
293 };
294
295
296 /**
297 * Gets the Picasa album id.
298 * @return {string} The Picasa album id.
299 */
300 goog.ui.media.PicasaAlbumModel.prototype.getAlbumId = function() {
301 return this.albumId_;
302 };
303
304
305 /**
306 * Gets the Picasa album authentication key.
307 * @return {?string} The Picasa album authentication key.
308 */
309 goog.ui.media.PicasaAlbumModel.prototype.getAuthKey = function() {
310 return this.authKey_;
311 };