flickr.js 10.0 KB

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