photo.js 4.6 KB

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