1 /** The minplayer namespace. */
  2 minplayer = minplayer || {};
  3 
  4 /**
  5  * @constructor
  6  * @extends minplayer.plugin
  7  * @class Base class used to provide the display and options for any component
  8  * deriving from this class.  Components who derive are expected to provide
  9  * the elements that they define by implementing the getElements method.
 10  *
 11  * @param {object} context The jQuery context this component resides.
 12  * @param {object} options The options for this component.
 13  */
 14 minplayer.display = function(context, options) {
 15 
 16   // See if we allow resize on this display.
 17   this.allowResize = false;
 18 
 19   if (context) {
 20 
 21     // Set the display and options.
 22     this.display = jQuery(context);
 23     this.options = options;
 24 
 25     // Extend all display elements.
 26     this.options.elements = this.options.elements || {};
 27     jQuery.extend(this.options.elements, this.getElements());
 28     this.elements = this.options.elements;
 29   }
 30 
 31   // Derive from plugin
 32   minplayer.plugin.call(this, context, options);
 33 };
 34 
 35 /** Derive from minplayer.plugin. */
 36 minplayer.display.prototype = new minplayer.plugin();
 37 
 38 /** Reset the constructor. */
 39 minplayer.display.prototype.constructor = minplayer.display;
 40 
 41 /**
 42  * @see minplayer.plugin.construct
 43  */
 44 minplayer.display.prototype.construct = function() {
 45 
 46   // Call the plugin constructor.
 47   minplayer.plugin.prototype.construct.call(this);
 48 
 49   // Only do this if they allow resize for this display.
 50   if (this.allowResize) {
 51 
 52     // Set the resize timeout and this pointer.
 53     var resizeTimeout = 0;
 54     var _this = this;
 55 
 56     // Add a handler to trigger a resize event.
 57     jQuery(window).resize(function() {
 58       clearTimeout(resizeTimeout);
 59       resizeTimeout = setTimeout(function() {
 60         _this.onResize();
 61       }, 200);
 62     });
 63   }
 64 };
 65 
 66 /**
 67  * Called when the window resizes.
 68  */
 69 minplayer.display.prototype.onResize = function() {
 70 };
 71 
 72 
 73 /**
 74  * Trigger a media event.
 75  *
 76  * @param {string} type The event type.
 77  * @param {object} data The event data object.
 78  * @return {object} The jQuery prototype.
 79  */
 80 minplayer.display.prototype.trigger = function(type, data) {
 81   return this.display.trigger(type, data);
 82 };
 83 
 84 /**
 85  * Bind to a media event.
 86  *
 87  * @param {string} types The event type.
 88  * @param {object} data The data to bind with the event.
 89  * @param {function} fn The callback function.
 90  * @return {object} The jQuery prototype.
 91  **/
 92 minplayer.display.prototype.bind = function(types, data, fn) {
 93 
 94   // We will always unbind first for media events.
 95   return this.display.unbind(types, fn).bind(types, data, fn);
 96 };
 97 
 98 /**
 99  * Returns a scaled rectangle provided a ratio and the container rect.
100  *
101  * @param {number} ratio The width/height ratio of what is being scaled.
102  * @param {object} rect The bounding rectangle for scaling.
103  * @return {object} The Rectangle object of the scaled rectangle.
104  */
105 minplayer.display.prototype.getScaledRect = function(ratio, rect) {
106   var scaledRect = {};
107   scaledRect.x = rect.x ? rect.x : 0;
108   scaledRect.y = rect.y ? rect.y : 0;
109   scaledRect.width = rect.width ? rect.width : 0;
110   scaledRect.height = rect.height ? rect.height : 0;
111   if (ratio) {
112     if ((rect.width / rect.height) > ratio) {
113       scaledRect.height = rect.height;
114       scaledRect.width = Math.floor(rect.height * ratio);
115     }
116     else {
117       scaledRect.height = Math.floor(rect.width / ratio);
118       scaledRect.width = rect.width;
119     }
120     scaledRect.x = Math.floor((rect.width - scaledRect.width) / 2);
121     scaledRect.y = Math.floor((rect.height - scaledRect.height) / 2);
122   }
123   return scaledRect;
124 };
125 
126 /**
127  * Returns all the jQuery elements that this component uses.
128  *
129  * @return {object} An object which defines all the jQuery elements that
130  * this component uses.
131  */
132 minplayer.display.prototype.getElements = function() {
133   return {};
134 };
135 
136 /**
137  * Returns if this component is valid and exists within the DOM.
138  *
139  * @return {boolean} TRUE if the plugin display is valid.
140  */
141 minplayer.display.prototype.isValid = function() {
142   return (this.display.length > 0);
143 };
144