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 {string} name The name of this plugin.
 12  * @param {object} context The jQuery context this component resides.
 13  * @param {object} options The options for this component.
 14  */
 15 minplayer.display = function(name, context, options) {
 16 
 17   // See if we allow resize on this display.
 18   this.allowResize = false;
 19 
 20   if (context) {
 21 
 22     // Set the display.
 23     this.display = this.getDisplay(context, options);
 24   }
 25 
 26   // Derive from plugin
 27   minplayer.plugin.call(this, name, context, options);
 28 };
 29 
 30 /** Derive from minplayer.plugin. */
 31 minplayer.display.prototype = new minplayer.plugin();
 32 
 33 /** Reset the constructor. */
 34 minplayer.display.prototype.constructor = minplayer.display;
 35 
 36 /**
 37  * Returns the display for this component.
 38  *
 39  * @param {object} context The original context.
 40  * @param {object} options The options for this component.
 41  * @return {object} The jQuery context for this display.
 42  */
 43 minplayer.display.prototype.getDisplay = function(context, options) {
 44   return jQuery(context);
 45 };
 46 
 47 /**
 48  * @see minplayer.plugin.construct
 49  */
 50 minplayer.display.prototype.construct = function() {
 51 
 52   // Call the plugin constructor.
 53   minplayer.plugin.prototype.construct.call(this);
 54 
 55   // Extend all display elements.
 56   this.options.elements = this.options.elements || {};
 57   jQuery.extend(this.options.elements, this.getElements());
 58   this.elements = this.options.elements;
 59 
 60   // Only do this if they allow resize for this display.
 61   if (this.allowResize) {
 62 
 63     // Set the resize timeout and this pointer.
 64     var resizeTimeout = 0;
 65     var _this = this;
 66 
 67     // Add a handler to trigger a resize event.
 68     jQuery(window).resize(function() {
 69       clearTimeout(resizeTimeout);
 70       resizeTimeout = setTimeout(function() {
 71         _this.onResize();
 72       }, 200);
 73     });
 74   }
 75 };
 76 
 77 /**
 78  * Called when the window resizes.
 79  */
 80 minplayer.display.prototype.onResize = function() {
 81 };
 82 
 83 /**
 84  * Returns a scaled rectangle provided a ratio and the container rect.
 85  *
 86  * @param {number} ratio The width/height ratio of what is being scaled.
 87  * @param {object} rect The bounding rectangle for scaling.
 88  * @return {object} The Rectangle object of the scaled rectangle.
 89  */
 90 minplayer.display.prototype.getScaledRect = function(ratio, rect) {
 91   var scaledRect = {};
 92   scaledRect.x = rect.x ? rect.x : 0;
 93   scaledRect.y = rect.y ? rect.y : 0;
 94   scaledRect.width = rect.width ? rect.width : 0;
 95   scaledRect.height = rect.height ? rect.height : 0;
 96   if (ratio) {
 97     if ((rect.width / rect.height) > ratio) {
 98       scaledRect.height = rect.height;
 99       scaledRect.width = Math.floor(rect.height * ratio);
100     }
101     else {
102       scaledRect.height = Math.floor(rect.width / ratio);
103       scaledRect.width = rect.width;
104     }
105     scaledRect.x = Math.floor((rect.width - scaledRect.width) / 2);
106     scaledRect.y = Math.floor((rect.height - scaledRect.height) / 2);
107   }
108   return scaledRect;
109 };
110 
111 /**
112  * Returns all the jQuery elements that this component uses.
113  *
114  * @return {object} An object which defines all the jQuery elements that
115  * this component uses.
116  */
117 minplayer.display.prototype.getElements = function() {
118   return {};
119 };
120 
121 /**
122  * Returns if this component is valid and exists within the DOM.
123  *
124  * @return {boolean} TRUE if the plugin display is valid.
125  */
126 minplayer.display.prototype.isValid = function() {
127   return (this.display.length > 0);
128 };
129