| 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 |
* @fileoverview provides a reusable photo UI component that renders photos that |
| 17 |
* contains metadata (such as captions, description, thumbnail/high resolution |
| 18 |
* versions, etc). |
| 19 |
* |
| 20 |
* goog.ui.media.Photo is actually a {@link goog.ui.ControlRenderer}, |
| 21 |
* a stateless class - that could/should be used as a Singleton with the static |
| 22 |
* method {@code goog.ui.media.Photo.getInstance} -, that knows how to render |
| 23 |
* Photos. It is designed to be used with a {@link goog.ui.Control}, which will |
| 24 |
* actually control the media renderer and provide the {@link goog.ui.Component} |
| 25 |
* base. This design guarantees that all different types of medias will behave |
| 26 |
* alike but will look different. |
| 27 |
* |
| 28 |
* goog.ui.media.Photo expects {@code goog.ui.media.MediaModel} on |
| 29 |
* {@code goog.ui.Control.getModel} as data models. |
| 30 |
* |
| 31 |
* Example of usage: |
| 32 |
* |
| 33 |
* <pre> |
| 34 |
* var photo = goog.ui.media.Photo.newControl( |
| 35 |
* new goog.ui.media.MediaModel('http://hostname/file.jpg')); |
| 36 |
* photo.render(goog.dom.getElement('parent')); |
| 37 |
* </pre> |
| 38 |
* |
| 39 |
* Photo medias currently support the following states: |
| 40 |
* |
| 41 |
* <ul> |
| 42 |
* <li> {@link goog.ui.Component.State.HOVER}: mouse cursor is over the photo. |
| 43 |
* <li> {@link goog.ui.Component.State.SELECTED}: photo is being displayed. |
| 44 |
* </ul> |
| 45 |
* |
| 46 |
* Which can be accessed by |
| 47 |
* |
| 48 |
* <pre> |
| 49 |
* photo.setHighlighted(true); |
| 50 |
* photo.setSelected(true); |
| 51 |
* </pre> |
| 52 |
* |
| 53 |
*/ |
| 54 |
|
| 55 |
goog.provide('goog.ui.media.Photo'); |
| 56 |
|
| 57 |
goog.require('goog.dom.TagName'); |
| 58 |
goog.require('goog.ui.media.Media'); |
| 59 |
goog.require('goog.ui.media.MediaRenderer'); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Subclasses a goog.ui.media.MediaRenderer to provide a Photo specific media |
| 65 |
* renderer. Provides a base class for any other renderer that wants to display |
| 66 |
* photos. |
| 67 |
* |
| 68 |
* This class is meant to be used as a singleton static stateless class, that |
| 69 |
* takes {@code goog.ui.media.Media} instances and renders it. |
| 70 |
* |
| 71 |
* This design is patterned after |
| 72 |
* http://go/closure_control_subclassing |
| 73 |
* |
| 74 |
* @constructor |
| 75 |
* @extends {goog.ui.media.MediaRenderer} |
| 76 |
* @final |
| 77 |
*/ |
| 78 |
goog.ui.media.Photo = function() { |
| 79 |
goog.ui.media.MediaRenderer.call(this); |
| 80 |
}; |
| 81 |
goog.inherits(goog.ui.media.Photo, goog.ui.media.MediaRenderer); |
| 82 |
goog.addSingletonGetter(goog.ui.media.Photo); |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Default CSS class to be applied to the root element of components rendered |
| 87 |
* by this renderer. |
| 88 |
* |
| 89 |
* @type {string} |
| 90 |
*/ |
| 91 |
goog.ui.media.Photo.CSS_CLASS = goog.getCssName('goog-ui-media-photo'); |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* A static convenient method to construct a goog.ui.media.Media control out of |
| 96 |
* a photo {@code goog.ui.media.MediaModel}. It sets it as the data model |
| 97 |
* goog.ui.media.Photo renderer uses, sets the states supported by the renderer, |
| 98 |
* and returns a Control that binds everything together. This is what you |
| 99 |
* should be using for constructing Photos, except if you need finer control |
| 100 |
* over the configuration. |
| 101 |
* |
| 102 |
* @param {goog.ui.media.MediaModel} dataModel The photo data model. |
| 103 |
* @return {!goog.ui.media.Media} A goog.ui.Control subclass with the photo |
| 104 |
* renderer. |
| 105 |
*/ |
| 106 |
goog.ui.media.Photo.newControl = function(dataModel) { |
| 107 |
var control = |
| 108 |
new goog.ui.media.Media(dataModel, goog.ui.media.Photo.getInstance()); |
| 109 |
return control; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
/** |
| 114 |
* Creates the initial DOM structure of a photo. |
| 115 |
* |
| 116 |
* @param {goog.ui.Control} c The media control. |
| 117 |
* @return {!Element} A DOM structure that represents the control. |
| 118 |
* @override |
| 119 |
*/ |
| 120 |
goog.ui.media.Photo.prototype.createDom = function(c) { |
| 121 |
var control = /** @type {goog.ui.media.Media} */ (c); |
| 122 |
var div = goog.ui.media.Photo.superClass_.createDom.call(this, control); |
| 123 |
|
| 124 |
var img = control.getDomHelper().createDom(goog.dom.TagName.IMG, { |
| 125 |
src: control.getDataModel().getPlayer().getUrl(), |
| 126 |
className: goog.getCssName(this.getCssClass(), 'image') |
| 127 |
}); |
| 128 |
|
| 129 |
div.appendChild(img); |
| 130 |
|
| 131 |
return div; |
| 132 |
}; |
| 133 |
|
| 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.Photo.prototype.getCssClass = function() { |
| 142 |
return goog.ui.media.Photo.CSS_CLASS; |
| 143 |
}; |