vimeo.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 Vimeo video UI component given a public
  16. * Vimeo video URL.
  17. *
  18. * goog.ui.media.Vimeo 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.Vimeo.getInstance} -, that knows how to render
  21. * video videos. 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.Vimeo expects vimeo video IDs on
  27. * {@code goog.ui.Control.getModel} as data models, and renders a flash object
  28. * that will show the contents of that video.
  29. *
  30. * Example of usage:
  31. *
  32. * <pre>
  33. * var video = goog.ui.media.VimeoModel.newInstance('http://vimeo.com/30012');
  34. * goog.ui.media.Vimeo.newControl(video).render();
  35. * </pre>
  36. *
  37. * Vimeo 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. goog.provide('goog.ui.media.Vimeo');
  55. goog.provide('goog.ui.media.VimeoModel');
  56. goog.require('goog.html.uncheckedconversions');
  57. goog.require('goog.string');
  58. goog.require('goog.ui.media.FlashObject');
  59. goog.require('goog.ui.media.Media');
  60. goog.require('goog.ui.media.MediaModel');
  61. goog.require('goog.ui.media.MediaRenderer');
  62. /**
  63. * Subclasses a goog.ui.media.MediaRenderer to provide a Vimeo specific media
  64. * renderer.
  65. *
  66. * This class knows how to parse Vimeo URLs, and render the DOM structure
  67. * of vimeo video players. This class is meant to be used as a singleton static
  68. * stateless class, that takes {@code goog.ui.media.Media} instances and renders
  69. * it. It expects {@code goog.ui.media.Media.getModel} to return a well formed,
  70. * previously constructed, vimeoId {@see goog.ui.media.Vimeo.parseUrl}, which is
  71. * the data model this renderer will use to construct the DOM structure.
  72. * {@see goog.ui.media.Vimeo.newControl} for a example of constructing a control
  73. * with this renderer.
  74. *
  75. * This design is patterned after http://go/closure_control_subclassing
  76. *
  77. * It uses {@link goog.ui.media.FlashObject} to embed the flash object.
  78. *
  79. * @constructor
  80. * @extends {goog.ui.media.MediaRenderer}
  81. * @final
  82. */
  83. goog.ui.media.Vimeo = function() {
  84. goog.ui.media.MediaRenderer.call(this);
  85. };
  86. goog.inherits(goog.ui.media.Vimeo, goog.ui.media.MediaRenderer);
  87. goog.addSingletonGetter(goog.ui.media.Vimeo);
  88. /**
  89. * Default CSS class to be applied to the root element of components rendered
  90. * by this renderer.
  91. *
  92. * @type {string}
  93. */
  94. goog.ui.media.Vimeo.CSS_CLASS = goog.getCssName('goog-ui-media-vimeo');
  95. /**
  96. * A static convenient method to construct a goog.ui.media.Media control out of
  97. * a Vimeo URL. It extracts the videoId information on the URL, sets it
  98. * as the data model goog.ui.media.Vimeo renderer uses, sets the states
  99. * supported by the renderer, and returns a Control that binds everything
  100. * together. This is what you should be using for constructing Vimeo videos,
  101. * except if you need more fine control over the configuration.
  102. *
  103. * @param {goog.ui.media.VimeoModel} dataModel A vimeo video URL.
  104. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
  105. * document interaction.
  106. * @return {!goog.ui.media.Media} A Control binded to the Vimeo renderer.
  107. */
  108. goog.ui.media.Vimeo.newControl = function(dataModel, opt_domHelper) {
  109. var control = new goog.ui.media.Media(
  110. dataModel, goog.ui.media.Vimeo.getInstance(), opt_domHelper);
  111. // vimeo videos don't have any thumbnail for now, so we show the
  112. // "selected" version of the UI at the start, which is the
  113. // flash player.
  114. control.setSelected(true);
  115. return control;
  116. };
  117. /**
  118. * Creates the initial DOM structure of the vimeo video, which is basically a
  119. * the flash object pointing to a vimeo video player.
  120. *
  121. * @param {goog.ui.Control} c The media control.
  122. * @return {!Element} The DOM structure that represents this control.
  123. * @override
  124. */
  125. goog.ui.media.Vimeo.prototype.createDom = function(c) {
  126. var control = /** @type {goog.ui.media.Media} */ (c);
  127. var div = goog.ui.media.Vimeo.superClass_.createDom.call(this, control);
  128. var dataModel =
  129. /** @type {goog.ui.media.VimeoModel} */ (control.getDataModel());
  130. var flash = new goog.ui.media.FlashObject(
  131. dataModel.getPlayer().getTrustedResourceUrl(), control.getDomHelper());
  132. flash.render(div);
  133. return div;
  134. };
  135. /**
  136. * Returns the CSS class to be applied to the root element of components
  137. * rendered using this renderer.
  138. * @return {string} Renderer-specific CSS class.
  139. * @override
  140. */
  141. goog.ui.media.Vimeo.prototype.getCssClass = function() {
  142. return goog.ui.media.Vimeo.CSS_CLASS;
  143. };
  144. /**
  145. * The {@code goog.ui.media.Vimeo} media data model. It stores a required
  146. * {@code videoId} field, sets the vimeo URL, and allows a few optional
  147. * parameters.
  148. *
  149. * @param {string} videoId The vimeo video id.
  150. * @param {string=} opt_caption An optional caption of the vimeo video.
  151. * @param {string=} opt_description An optional description of the vimeo video.
  152. * @param {boolean=} opt_autoplay Whether to autoplay video.
  153. * @constructor
  154. * @extends {goog.ui.media.MediaModel}
  155. * @final
  156. */
  157. goog.ui.media.VimeoModel = function(
  158. videoId, opt_caption, opt_description, opt_autoplay) {
  159. goog.ui.media.MediaModel.call(
  160. this, goog.ui.media.VimeoModel.buildUrl(videoId), opt_caption,
  161. opt_description, goog.ui.media.MediaModel.MimeType.FLASH);
  162. /**
  163. * The Vimeo video id.
  164. * @type {string}
  165. * @private
  166. */
  167. this.videoId_ = videoId;
  168. this.setPlayer(
  169. new goog.ui.media.MediaModel.Player(
  170. goog.ui.media.VimeoModel.buildFlashUrl(videoId, opt_autoplay)));
  171. };
  172. goog.inherits(goog.ui.media.VimeoModel, goog.ui.media.MediaModel);
  173. /**
  174. * Regular expression used to extract the vimeo video id out of vimeo URLs.
  175. *
  176. * Copied from http://go/markdownlite.js
  177. *
  178. * TODO(user): add support to https.
  179. *
  180. * @type {RegExp}
  181. * @private
  182. * @const
  183. */
  184. goog.ui.media.VimeoModel.MATCHER_ =
  185. /https?:\/\/(?:www\.)?vimeo\.com\/(?:hd#)?([0-9]+)/i;
  186. /**
  187. * Takes a {@code vimeoUrl} and extracts the video id.
  188. *
  189. * @param {string} vimeoUrl A vimeo video URL.
  190. * @param {string=} opt_caption An optional caption of the vimeo video.
  191. * @param {string=} opt_description An optional description of the vimeo video.
  192. * @param {boolean=} opt_autoplay Whether to autoplay video.
  193. * @return {!goog.ui.media.VimeoModel} The vimeo data model that represents this
  194. * URL.
  195. * @throws exception in case the parsing fails
  196. */
  197. goog.ui.media.VimeoModel.newInstance = function(
  198. vimeoUrl, opt_caption, opt_description, opt_autoplay) {
  199. if (goog.ui.media.VimeoModel.MATCHER_.test(vimeoUrl)) {
  200. var data = goog.ui.media.VimeoModel.MATCHER_.exec(vimeoUrl);
  201. return new goog.ui.media.VimeoModel(
  202. data[1], opt_caption, opt_description, opt_autoplay);
  203. }
  204. throw Error('failed to parse vimeo url: ' + vimeoUrl);
  205. };
  206. /**
  207. * The opposite of {@code goog.ui.media.Vimeo.parseUrl}: it takes a videoId
  208. * and returns a vimeo URL.
  209. *
  210. * @param {string} videoId The vimeo video ID.
  211. * @return {string} The vimeo URL.
  212. */
  213. goog.ui.media.VimeoModel.buildUrl = function(videoId) {
  214. return 'http://vimeo.com/' + goog.string.urlEncode(videoId);
  215. };
  216. /**
  217. * Builds a flash url from the vimeo {@code videoId}.
  218. *
  219. * @param {string} videoId The vimeo video ID.
  220. * @param {boolean=} opt_autoplay Whether the flash movie should start playing
  221. * as soon as it is shown, or if it should show a 'play' button.
  222. * @return {!goog.html.TrustedResourceUrl} The vimeo flash URL.
  223. */
  224. goog.ui.media.VimeoModel.buildFlashUrl = function(videoId, opt_autoplay) {
  225. var autoplay = opt_autoplay ? '&autoplay=1' : '';
  226. return goog.html.uncheckedconversions.
  227. trustedResourceUrlFromStringKnownToSatisfyTypeContract(
  228. goog.string.Const.from('Fixed domain, encoded parameters.'),
  229. 'http://vimeo.com/moogaloop.swf?clip_id=' +
  230. goog.string.urlEncode(videoId) +
  231. '&server=vimeo.com&show_title=1&show_byline=1&' +
  232. 'show_portrait=0color=&fullscreen=1' + autoplay);
  233. };
  234. /**
  235. * Gets the Vimeo video id.
  236. * @return {string} The Vimeo video id.
  237. */
  238. goog.ui.media.VimeoModel.prototype.getVideoId = function() {
  239. return this.videoId_;
  240. };