Debian Codegraph @

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

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