| 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 FlickrSet photo UI component given a public |
| 18 |
* FlickrSetModel. |
| 19 |
* |
| 20 |
* goog.ui.media.FlickrSet is actually a {@link goog.ui.ControlRenderer}, a |
| 21 |
* stateless class - that could/should be used as a Singleton with the static |
| 22 |
* method {@code goog.ui.media.FlickrSet.getInstance} -, that knows how to |
| 23 |
* render Flickr sets. It is designed to be used with a {@link goog.ui.Control}, |
| 24 |
* which will actually control the media renderer and provide the |
| 25 |
* {@link goog.ui.Component} base. This design guarantees that all different |
| 26 |
* types of medias will behave alike but will look different. |
| 27 |
* |
| 28 |
* goog.ui.media.FlickrSet expects a {@code goog.ui.media.FlickrSetModel} on |
| 29 |
* {@code goog.ui.Control.getModel} as data models, and renders a flash object |
| 30 |
* that will show the contents of that set. |
| 31 |
* |
| 32 |
* Example of usage: |
| 33 |
* |
| 34 |
* <pre> |
| 35 |
* var flickrSet = goog.ui.media.FlickrSetModel.newInstance(flickrSetUrl); |
| 36 |
* goog.ui.media.FlickrSet.newControl(flickrSet).render(); |
| 37 |
* </pre> |
| 38 |
* |
| 39 |
* FlickrSet medias currently support the following states: |
| 40 |
* |
| 41 |
* <ul> |
| 42 |
* <li> {@link goog.ui.Component.State.DISABLED}: shows 'flash not available' |
| 43 |
* <li> {@link goog.ui.Component.State.HOVER}: mouse cursor is over the video |
| 44 |
* <li> {@link goog.ui.Component.State.SELECTED}: flash video is shown |
| 45 |
* </ul> |
| 46 |
* |
| 47 |
* Which can be accessed by |
| 48 |
* <pre> |
| 49 |
* video.setEnabled(true); |
| 50 |
* video.setHighlighted(true); |
| 51 |
* video.setSelected(true); |
| 52 |
* </pre> |
| 53 |
* |
| 54 |
* Requires flash to actually work. |
| 55 |
* |
| 56 |
* |
| 57 |
* TODO(user): Support non flash users. Maybe show a link to the Flick set, |
| 58 |
* or fetch the data and rendering it using javascript (instead of a broken |
| 59 |
* 'You need to install flash' message). |
| 60 |
*/ |
| 61 |
|
| 62 |
goog.provide('goog.ui.media.FlickrSet'); |
| 63 |
goog.provide('goog.ui.media.FlickrSetModel'); |
| 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 FlickrSet specific |
| 76 |
* media renderer. |
| 77 |
* |
| 78 |
* This class knows how to parse FlickrSet URLs, and render the DOM structure |
| 79 |
* of flickr set players. This class is meant to be used as a singleton static |
| 80 |
* stateless class, that takes {@code goog.ui.media.Media} instances and renders |
| 81 |
* it. It expects {@code goog.ui.media.Media.getModel} to return a well formed, |
| 82 |
* previously constructed, set id {@see goog.ui.media.FlickrSet.parseUrl}, |
| 83 |
* which is the data model this renderer will use to construct the DOM |
| 84 |
* structure. {@see goog.ui.media.FlickrSet.newControl} for a example of |
| 85 |
* constructing a control with this renderer. |
| 86 |
* |
| 87 |
* This design is patterned after |
| 88 |
* http://go/closure_control_subclassing |
| 89 |
* |
| 90 |
* It uses {@link goog.ui.media.FlashObject} to embed the flash object. |
| 91 |
* |
| 92 |
* @constructor |
| 93 |
* @extends {goog.ui.media.MediaRenderer} |
| 94 |
* @final |
| 95 |
*/ |
| 96 |
goog.ui.media.FlickrSet = function() { |
| 97 |
goog.ui.media.MediaRenderer.call(this); |
| 98 |
}; |
| 99 |
goog.inherits(goog.ui.media.FlickrSet, goog.ui.media.MediaRenderer); |
| 100 |
goog.addSingletonGetter(goog.ui.media.FlickrSet); |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Default CSS class to be applied to the root element of components rendered |
| 105 |
* by this renderer. |
| 106 |
* |
| 107 |
* @type {string} |
| 108 |
*/ |
| 109 |
goog.ui.media.FlickrSet.CSS_CLASS = goog.getCssName('goog-ui-media-flickrset'); |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Flash player URL. Uses Flickr's flash player by default. |
| 114 |
* |
| 115 |
* @type {!goog.html.TrustedResourceUrl} |
| 116 |
* @private |
| 117 |
*/ |
| 118 |
goog.ui.media.FlickrSet.flashUrl_ = goog.html.TrustedResourceUrl.fromConstant( |
| 119 |
goog.string.Const.from( |
| 120 |
'http://www.flickr.com/apps/slideshow/show.swf?v=63961')); |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* A static convenient method to construct a goog.ui.media.Media control out of |
| 125 |
* a FlickrSet URL. It extracts the set id information on the URL, sets it |
| 126 |
* as the data model goog.ui.media.FlickrSet renderer uses, sets the states |
| 127 |
* supported by the renderer, and returns a Control that binds everything |
| 128 |
* together. This is what you should be using for constructing FlickrSet videos, |
| 129 |
* except if you need more fine control over the configuration. |
| 130 |
* |
| 131 |
* @param {goog.ui.media.FlickrSetModel} dataModel The Flickr Set data model. |
| 132 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for |
| 133 |
* document interaction. |
| 134 |
* @return {!goog.ui.media.Media} A Control binded to the FlickrSet renderer. |
| 135 |
* @throws exception in case {@code flickrSetUrl} is an invalid flickr set URL. |
| 136 |
* TODO(user): use {@link goog.ui.media.MediaModel} once it is checked in. |
| 137 |
*/ |
| 138 |
goog.ui.media.FlickrSet.newControl = function(dataModel, opt_domHelper) { |
| 139 |
var control = new goog.ui.media.Media( |
| 140 |
dataModel, goog.ui.media.FlickrSet.getInstance(), opt_domHelper); |
| 141 |
control.setSelected(true); |
| 142 |
return control; |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* A static method that sets which flash URL this class should use. Use this if |
| 148 |
* you want to host your own flash flickr player. |
| 149 |
* |
| 150 |
* @param {!goog.html.TrustedResourceUrl} flashUrl The URL of the flash flickr |
| 151 |
* player. |
| 152 |
*/ |
| 153 |
goog.ui.media.FlickrSet.setFlashUrl = function(flashUrl) { |
| 154 |
goog.ui.media.FlickrSet.flashUrl_ = flashUrl; |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Creates the initial DOM structure of the flickr set, which is basically a |
| 160 |
* the flash object pointing to a flickr set player. |
| 161 |
* |
| 162 |
* @param {goog.ui.Control} c The media control. |
| 163 |
* @return {!Element} The DOM structure that represents this control. |
| 164 |
* @override |
| 165 |
*/ |
| 166 |
goog.ui.media.FlickrSet.prototype.createDom = function(c) { |
| 167 |
var control = /** @type {goog.ui.media.Media} */ (c); |
| 168 |
var div = goog.ui.media.FlickrSet.superClass_.createDom.call(this, control); |
| 169 |
|
| 170 |
var model = |
| 171 |
/** @type {goog.ui.media.FlickrSetModel} */ (control.getDataModel()); |
| 172 |
|
| 173 |
// TODO(user): find out what is the policy about hosting this SWF. figure out |
| 174 |
// if it works over https. |
| 175 |
var flash = new goog.ui.media.FlashObject( |
| 176 |
model.getPlayer().getTrustedResourceUrl(), control.getDomHelper()); |
| 177 |
flash.addFlashVars(model.getPlayer().getVars()); |
| 178 |
flash.render(div); |
| 179 |
|
| 180 |
return div; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Returns the CSS class to be applied to the root element of components |
| 186 |
* rendered using this renderer. |
| 187 |
* @return {string} Renderer-specific CSS class. |
| 188 |
* @override |
| 189 |
*/ |
| 190 |
goog.ui.media.FlickrSet.prototype.getCssClass = function() { |
| 191 |
return goog.ui.media.FlickrSet.CSS_CLASS; |
| 192 |
}; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* The {@code goog.ui.media.FlickrAlbum} media data model. It stores a required |
| 198 |
* {@code userId} and {@code setId} fields, sets the flickr Set URL, and |
| 199 |
* allows a few optional parameters. |
| 200 |
* |
| 201 |
* @param {string} userId The flickr userId associated with this set. |
| 202 |
* @param {string} setId The flickr setId associated with this set. |
| 203 |
* @param {string=} opt_caption An optional caption of the flickr set. |
| 204 |
* @param {string=} opt_description An optional description of the flickr set. |
| 205 |
* @constructor |
| 206 |
* @extends {goog.ui.media.MediaModel} |
| 207 |
* @final |
| 208 |
*/ |
| 209 |
goog.ui.media.FlickrSetModel = function( |
| 210 |
userId, setId, opt_caption, opt_description) { |
| 211 |
goog.ui.media.MediaModel.call( |
| 212 |
this, goog.ui.media.FlickrSetModel.buildUrl(userId, setId), opt_caption, |
| 213 |
opt_description, goog.ui.media.MediaModel.MimeType.FLASH); |
| 214 |
|
| 215 |
/** |
| 216 |
* The Flickr user id. |
| 217 |
* @type {string} |
| 218 |
* @private |
| 219 |
*/ |
| 220 |
this.userId_ = userId; |
| 221 |
|
| 222 |
/** |
| 223 |
* The Flickr set id. |
| 224 |
* @type {string} |
| 225 |
* @private |
| 226 |
*/ |
| 227 |
this.setId_ = setId; |
| 228 |
|
| 229 |
var flashVars = { |
| 230 |
'offsite': 'true', |
| 231 |
'lang': 'en', |
| 232 |
'page_show_url': '/photos/' + userId + '/sets/' + setId + '/show/', |
| 233 |
'page_show_back_url': '/photos/' + userId + '/sets/' + setId, |
| 234 |
'set_id': setId |
| 235 |
}; |
| 236 |
|
| 237 |
var player = new goog.ui.media.MediaModel.Player( |
| 238 |
goog.ui.media.FlickrSet.flashUrl_, flashVars); |
| 239 |
|
| 240 |
this.setPlayer(player); |
| 241 |
}; |
| 242 |
goog.inherits(goog.ui.media.FlickrSetModel, goog.ui.media.MediaModel); |
| 243 |
|
| 244 |
|
| 245 |
/** |
| 246 |
* Regular expression used to extract the username and set id out of the flickr |
| 247 |
* URLs. |
| 248 |
* |
| 249 |
* Copied from http://go/markdownlite.js and {@link FlickrExtractor.xml}. |
| 250 |
* |
| 251 |
* @type {RegExp} |
| 252 |
* @private |
| 253 |
* @const |
| 254 |
*/ |
| 255 |
goog.ui.media.FlickrSetModel.MATCHER_ = |
| 256 |
/(?:http:\/\/)?(?:www\.)?flickr\.com\/(?:photos\/([\d\w@\-]+)\/sets\/(\d+))\/?/i; |
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* Takes a {@code flickrSetUrl} and extracts the flickr username and set id. |
| 261 |
* |
| 262 |
* @param {string} flickrSetUrl A Flickr set URL. |
| 263 |
* @param {string=} opt_caption An optional caption of the flickr set. |
| 264 |
* @param {string=} opt_description An optional description of the flickr set. |
| 265 |
* @return {!goog.ui.media.FlickrSetModel} The data model that represents the |
| 266 |
* Flickr set. |
| 267 |
* @throws exception in case the parsing fails |
| 268 |
*/ |
| 269 |
goog.ui.media.FlickrSetModel.newInstance = function( |
| 270 |
flickrSetUrl, opt_caption, opt_description) { |
| 271 |
if (goog.ui.media.FlickrSetModel.MATCHER_.test(flickrSetUrl)) { |
| 272 |
var data = goog.ui.media.FlickrSetModel.MATCHER_.exec(flickrSetUrl); |
| 273 |
return new goog.ui.media.FlickrSetModel( |
| 274 |
data[1], data[2], opt_caption, opt_description); |
| 275 |
} |
| 276 |
throw new Error('failed to parse flickr url: ' + flickrSetUrl); |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* Takes a flickr username and set id and returns an URL. |
| 282 |
* |
| 283 |
* @param {string} userId The owner of the set. |
| 284 |
* @param {string} setId The set id. |
| 285 |
* @return {string} The URL of the set. |
| 286 |
*/ |
| 287 |
goog.ui.media.FlickrSetModel.buildUrl = function(userId, setId) { |
| 288 |
return 'http://flickr.com/photos/' + userId + '/sets/' + setId; |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* Gets the Flickr user id. |
| 294 |
* @return {string} The Flickr user id. |
| 295 |
*/ |
| 296 |
goog.ui.media.FlickrSetModel.prototype.getUserId = function() { |
| 297 |
return this.userId_; |
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* Gets the Flickr set id. |
| 303 |
* @return {string} The Flickr set id. |
| 304 |
*/ |
| 305 |
goog.ui.media.FlickrSetModel.prototype.getSetId = function() { |
| 306 |
return this.setId_; |
| 307 |
}; |