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