1 /** The minplayer namespace. */
  2 minplayer = minplayer || {};
  3 
  4 /**
  5  * @constructor
  6  * @class The base class for all plugins.
  7  *
  8  * @param {object} context The jQuery context.
  9  * @param {object} options This components options.
 10  */
 11 minplayer.plugin = function(context, options) {
 12 
 13   // The media player.
 14   this.player = null;
 15 
 16   // Only call the constructor if we have a context.
 17   if (context) {
 18     this.construct();
 19   }
 20 };
 21 
 22 /** Static array to keep track of plugin instances. */
 23 minplayer.plugin.instances = {};
 24 
 25 /**
 26  * The constructor which is called once the context is set.
 27  * Any class deriving from the plugin class should place all context
 28  * dependant functionality within this function instead of the standard
 29  * constructor function since it is called on object derivation as well
 30  * as object creation.
 31  */
 32 minplayer.plugin.prototype.construct = function() {
 33 
 34   // If this is a player, then it needs a new plugin
 35   if (this.options.name == 'player') {
 36     this.loadPlugins();
 37     this.options.name = 'player';
 38   }
 39 
 40   // Add this plugin.
 41   this.addPlugin(this.options.name, this);
 42 };
 43 
 44 /**
 45  * Destructor.
 46  */
 47 minplayer.plugin.prototype.destroy = function() {
 48   // Remove the pointer to the plugins array so it will be garbage collected.
 49   if (minplayer.plugin.instances[this.options.id][this.options.name]) {
 50     minplayer.plugin.instances[this.options.id][this.options.name] = null;
 51   }
 52 };
 53 
 54 /**
 55  * Adds a new plugin to this player.
 56  *
 57  * @param {string} name The name of this plugin.
 58  * @param {object} plugin A new plugin object, derived from media.plugin.
 59  */
 60 minplayer.plugin.prototype.addPlugin = function(name, plugin) {
 61 
 62   // Only continue if the plugin exists.
 63   if (plugin.isValid()) {
 64 
 65     // Add this to the plugins.
 66     minplayer.plugin.instances[this.options.id][name] = plugin;
 67   }
 68 };
 69 
 70 /**
 71  * Gets a plugin by name.
 72  *
 73  * @param {string} name The name of the plugin.
 74  * @return {object} The plugin for the provided name.
 75  */
 76 minplayer.plugin.prototype.getPlugin = function(name) {
 77   if (minplayer.plugin.instances[this.options.id][name]) {
 78     return minplayer.plugin.instances[this.options.id][name];
 79   }
 80   return null;
 81 };
 82 
 83 /**
 84  * Loads all of the available plugins.
 85  */
 86 minplayer.plugin.prototype.loadPlugins = function() {
 87   var plugin = null;
 88   var pluginInfo = {};
 89   var pluginContext = null;
 90 
 91   // Initialize this plugins array.
 92   minplayer.plugin.instances[this.options.id] = {};
 93 
 94   // Iterate through all the plugins.
 95   var i = minplayer.plugins.length;
 96   while (i--) {
 97 
 98     // Get the plugin information.
 99     pluginInfo = minplayer.plugins[i];
100     if (pluginInfo.element) {
101       pluginContext = jQuery(pluginInfo.element, this.display);
102     }
103     else {
104       pluginContext = this.display;
105     }
106 
107     // Create the new plugin.
108     plugin = new pluginInfo.object(pluginContext, this.options);
109   }
110 };
111 
112 /**
113  * Iterate over each plugin.
114  *
115  * @param {function} callback Called for each plugin in this player.
116  */
117 minplayer.plugin.prototype.eachPlugin = function(callback) {
118   for (var name in minplayer.plugin.instances[this.options.id]) {
119     if (minplayer.plugin.instances[this.options.id].hasOwnProperty(name)) {
120       callback.call(this, minplayer.plugin.instances[this.options.id][name]);
121     }
122   }
123 };
124 
125 /**
126  * Sets the current media player.
127  *
128  * @param {object} player The current media player.
129  */
130 minplayer.plugin.prototype.setPlayer = function(player) {
131   this.player = player;
132 };
133