[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/video/js/ -> jquery.media.js (source)

   1  /*

   2   * jQuery Media Plugin for converting elements into rich media content.

   3   *

   4   * Examples and documentation at: http://malsup.com/jquery/media/

   5   * Copyright (c) 2007-2008 M. Alsup

   6   * Dual licensed under the MIT and GPL licenses:

   7   * http://www.opensource.org/licenses/mit-license.php

   8   * http://www.gnu.org/licenses/gpl.html

   9   *

  10   * @author: M. Alsup

  11   * @version: 0.92 (24-SEP-2009)

  12   * @requires jQuery v1.1.2 or later

  13   *

  14   * Supported Media Players:

  15   *    - Flash

  16   *    - Quicktime

  17   *    - Real Player

  18   *    - Silverlight

  19   *    - Windows Media Player

  20   *    - iframe

  21   *

  22   * Supported Media Formats:

  23   *     Any types supported by the above players, such as:

  24   *     Video: asf, avi, flv, mov, mpg, mpeg, mp4, qt, smil, swf, wmv, 3g2, 3gp

  25   *     Audio: aif, aac, au, gsm, mid, midi, mov, mp3, m4a, snd, rm, wav, wma

  26   *     Other: bmp, html, pdf, psd, qif, qtif, qti, tif, tiff, xaml

  27   *

  28   * Thanks to Mark Hicken and Brent Pedersen for helping me debug this on the Mac!

  29   * Thanks to Dan Rossi for numerous bug reports and code bits!

  30   * Thanks to Skye Giordano for several great suggestions!

  31   * Thanks to Richard Connamacher for excellent improvements to the non-IE behavior!

  32   */
  33  ;(function($) {
  34  
  35  /**

  36   * Chainable method for converting elements into rich media.

  37   *

  38   * @param options

  39   * @param callback fn invoked for each matched element before conversion

  40   * @param callback fn invoked for each matched element after conversion

  41   */
  42  $.fn.media = function(options, f1, f2) {
  43      if (options == 'undo') {
  44          return this.each(function() {
  45              var $this = $(this);
  46              var html = $this.data('media.origHTML');
  47              if (html)
  48                  $this.replaceWith(html);
  49          });
  50      }
  51      
  52      return this.each(function() {
  53          if (typeof options == 'function') {
  54              f2 = f1;
  55              f1 = options;
  56              options = {};
  57          }
  58          var o = getSettings(this, options);
  59          // pre-conversion callback, passes original element and fully populated options

  60          if (typeof f1 == 'function') f1(this, o);
  61  
  62          var r = getTypesRegExp();
  63          var m = r.exec(o.src.toLowerCase()) || [''];
  64  
  65          o.type ? m[0] = o.type : m.shift();
  66          for (var i=0; i < m.length; i++) {
  67              fn = m[i].toLowerCase();
  68              if (isDigit(fn[0])) fn = 'fn' + fn; // fns can't begin with numbers

  69              if (!$.fn.media[fn])
  70                  continue;  // unrecognized media type

  71              // normalize autoplay settings

  72              var player = $.fn.media[fn+'_player'];
  73              if (!o.params) o.params = {};
  74              if (player) {
  75                  var num = player.autoplayAttr == 'autostart';
  76                  o.params[player.autoplayAttr || 'autoplay'] = num ? (o.autoplay ? 1 : 0) : o.autoplay ? true : false;
  77              }
  78              var $div = $.fn.media[fn](this, o);
  79  
  80              $div.css('backgroundColor', o.bgColor).width(o.width);
  81              
  82              if (o.canUndo) {
  83                  var $temp = $('<div></div>').append(this);
  84                  $div.data('media.origHTML', $temp.html()); // store original markup

  85              }
  86              
  87              // post-conversion callback, passes original element, new div element and fully populated options

  88              if (typeof f2 == 'function') f2(this, $div[0], o, player.name);
  89              break;
  90          }
  91      });
  92  };
  93  
  94  /**

  95   * Non-chainable method for adding or changing file format / player mapping

  96   * @name mapFormat

  97   * @param String format File format extension (ie: mov, wav, mp3)

  98   * @param String player Player name to use for the format (one of: flash, quicktime, realplayer, winmedia, silverlight or iframe

  99   */
 100  $.fn.media.mapFormat = function(format, player) {
 101      if (!format || !player || !$.fn.media.defaults.players[player]) return; // invalid

 102      format = format.toLowerCase();
 103      if (isDigit(format[0])) format = 'fn' + format;
 104      $.fn.media[format] = $.fn.media[player];
 105      $.fn.media[format+'_player'] = $.fn.media.defaults.players[player];
 106  };
 107  
 108  // global defautls; override as needed

 109  $.fn.media.defaults = {
 110      standards:  false,      // use object tags only (no embeds for non-IE browsers)
 111      canUndo:    true,       // tells plugin to store the original markup so it can be reverted via: $(sel).mediaUndo()
 112      width:        400,
 113      height:        400,
 114      autoplay:    0,               // normalized cross-player setting
 115      bgColor:    '#ffffff',     // background color
 116      params:        { wmode: 'transparent'},    // added to object element as param elements; added to embed element as attrs
 117      attrs:        {},            // added to object and embed elements as attrs
 118      flvKeyName: 'file',     // key used for object src param (thanks to Andrea Ercolino)
 119      flashvars:    {},            // added to flash content as flashvars param/attr
 120      flashVersion:    '7',    // required flash version
 121      expressInstaller: null,    // src for express installer
 122  
 123      // default flash video and mp3 player (@see: http://jeroenwijering.com/?item=Flash_Media_Player)

 124      flvPlayer:     'mediaplayer.swf',
 125      mp3Player:     'mediaplayer.swf',
 126  
 127      // @see http://msdn2.microsoft.com/en-us/library/bb412401.aspx

 128      silverlight: {
 129          inplaceInstallPrompt: 'true', // display in-place install prompt?
 130          isWindowless:          'true', // windowless mode (false for wrapping markup)
 131          framerate:              '24',      // maximum framerate
 132          version:              '0.9',  // Silverlight version
 133          onError:              null,      // onError callback
 134          onLoad:                  null,   // onLoad callback
 135          initParams:              null,      // object init params
 136          userContext:          null      // callback arg passed to the load callback
 137      }
 138  };
 139  
 140  // Media Players; think twice before overriding

 141  $.fn.media.defaults.players = {
 142      flash: {
 143          name:         'flash',
 144          title:         'Flash',
 145          types:         'flv,mp3,swf',
 146          mimetype:     'application/x-shockwave-flash',
 147          pluginspage: 'http://www.adobe.com/go/getflashplayer',
 148          ieAttrs: {
 149              classid:  'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
 150              type:      'application/x-oleobject',
 151              codebase: 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + $.fn.media.defaults.flashVersion
 152          }
 153      },
 154      quicktime: {
 155          name:         'quicktime',
 156          title:         'QuickTime',
 157          mimetype:     'video/quicktime',
 158          pluginspage: 'http://www.apple.com/quicktime/download/',
 159          types:         'aif,aiff,aac,au,bmp,gsm,mov,mid,midi,mpg,mpeg,mp4,m4a,psd,qt,qtif,qif,qti,snd,tif,tiff,wav,3g2,3gp',
 160          ieAttrs: {
 161              classid:  'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
 162              codebase: 'http://www.apple.com/qtactivex/qtplugin.cab'
 163          }
 164      },
 165      realplayer: {
 166          name:          'real',
 167          title:          'RealPlayer',
 168          types:          'ra,ram,rm,rpm,rv,smi,smil',
 169          mimetype:      'audio/x-pn-realaudio-plugin',
 170          pluginspage:  'http://www.real.com/player/',
 171          autoplayAttr: 'autostart',
 172          ieAttrs: {
 173              classid: 'clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA'
 174          }
 175      },
 176      winmedia: {
 177          name:          'winmedia',
 178          title:          'Windows Media',
 179          types:          'asx,asf,avi,wma,wmv',
 180          mimetype:      $.browser.mozilla && isFirefoxWMPPluginInstalled() ? 'application/x-ms-wmp' : 'application/x-mplayer2',
 181          pluginspage:  'http://www.microsoft.com/Windows/MediaPlayer/',
 182          autoplayAttr: 'autostart',
 183          oUrl:          'url',
 184          ieAttrs: {
 185              classid:  'clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6',
 186              type:      'application/x-oleobject'
 187          }
 188      },
 189      // special cases

 190      iframe: {
 191          name:  'iframe',
 192          types: 'html,pdf'
 193      },
 194      silverlight: {
 195          name:  'silverlight',
 196          types: 'xaml'
 197      }
 198  };
 199  
 200  //

 201  //    everything below here is private

 202  //

 203  
 204  
 205  // detection script for FF WMP plugin (http://www.therossman.org/experiments/wmp_play.html)

 206  // (hat tip to Mark Ross for this script)

 207  function isFirefoxWMPPluginInstalled() {
 208      var plugs = navigator.plugins;
 209      for (var i = 0; i < plugs.length; i++) {
 210          var plugin = plugs[i];
 211          if (plugin['filename'] == 'np-mswmp.dll')
 212              return true;
 213      }
 214      return false;
 215  }
 216  
 217  var counter = 1;
 218  
 219  for (var player in $.fn.media.defaults.players) {
 220      var types = $.fn.media.defaults.players[player].types;
 221      $.each(types.split(','), function(i,o) {
 222          if (isDigit(o[0])) o = 'fn' + o;
 223          $.fn.media[o] = $.fn.media[player] = getGenerator(player);
 224          $.fn.media[o+'_player'] = $.fn.media.defaults.players[player];
 225      });
 226  };
 227  
 228  function getTypesRegExp() {
 229      var types = '';
 230      for (var player in $.fn.media.defaults.players) {
 231          if (types.length) types += ',';
 232          types += $.fn.media.defaults.players[player].types;
 233      };
 234      return new RegExp('\\.(' + types.replace(/,/ig,'|') + ')\\b$');
 235  };
 236  
 237  function getGenerator(player) {
 238      return function(el, options) {
 239          return generate(el, options, player);
 240      };
 241  };
 242  
 243  function isDigit(c) {
 244      return '0123456789'.indexOf(c) > -1;
 245  };
 246  
 247  // flatten all possible options: global defaults, meta, option obj

 248  function getSettings(el, options) {
 249      options = options || {};
 250      var $el = $(el);
 251      var cls = el.className || '';
 252      // support metadata plugin (v1.0 and v2.0)

 253      var meta = $.metadata ? $el.metadata() : $.meta ? $el.data() : {};
 254      meta = meta || {};
 255      var w = meta.width     || parseInt(((cls.match(/w:(\d+)/)||[])[1]||0));
 256      var h = meta.height || parseInt(((cls.match(/h:(\d+)/)||[])[1]||0));
 257  
 258      if (w) meta.width    = w;
 259      if (h) meta.height = h;
 260      if (cls) meta.cls = cls;
 261  
 262      var a = $.fn.media.defaults;
 263      var b = options;
 264      var c = meta;
 265  
 266      var p = { params: { bgColor: options.bgColor || $.fn.media.defaults.bgColor } };
 267      var opts = $.extend({}, a, b, c);
 268      $.each(['attrs','params','flashvars','silverlight'], function(i,o) {
 269          opts[o] = $.extend({}, p[o] || {}, a[o] || {}, b[o] || {}, c[o] || {});
 270      });
 271  
 272      if (typeof opts.caption == 'undefined') opts.caption = $el.text();
 273  
 274      // make sure we have a source!

 275      opts.src = opts.src || $el.attr('href') || $el.attr('src') || 'unknown';
 276      return opts;
 277  };
 278  
 279  //

 280  //    Flash Player

 281  //

 282  
 283  // generate flash using SWFObject library if possible

 284  $.fn.media.swf = function(el, opts) {
 285      if (!window.SWFObject && !window.swfobject) {
 286          // roll our own

 287          if (opts.flashvars) {
 288              var a = [];
 289              for (var f in opts.flashvars)
 290                  a.push(f + '=' + opts.flashvars[f]);
 291              if (!opts.params) opts.params = {};
 292              opts.params.flashvars = a.join('&');
 293          }
 294          return generate(el, opts, 'flash');
 295      }
 296  
 297      var id = el.id ? (' id="'+el.id+'"') : '';
 298      var cls = opts.cls ? (' class="' + opts.cls + '"') : '';
 299      var $div = $('<div' + id + cls + '>');
 300  
 301      // swfobject v2+

 302      if (window.swfobject) {
 303          $(el).after($div).appendTo($div);
 304          if (!el.id) el.id = 'movie_player_' + counter++;
 305  
 306          // replace el with swfobject content

 307          swfobject.embedSWF(opts.src, el.id, opts.width, opts.height, opts.flashVersion,
 308              opts.expressInstaller, opts.flashvars, opts.params, opts.attrs);
 309      }
 310      // swfobject < v2

 311      else {
 312          $(el).after($div).remove();
 313          var so = new SWFObject(opts.src, 'movie_player_' + counter++, opts.width, opts.height, opts.flashVersion, opts.bgColor);
 314          if (opts.expressInstaller) so.useExpressInstall(opts.expressInstaller);
 315  
 316          for (var p in opts.params)
 317              if (p != 'bgColor') so.addParam(p, opts.params[p]);
 318          for (var f in opts.flashvars)
 319              so.addVariable(f, opts.flashvars[f]);
 320          so.write($div[0]);
 321      }
 322  
 323      if (opts.caption) $('<div>').appendTo($div).html(opts.caption);
 324      return $div;
 325  };
 326  
 327  // map flv and mp3 files to the swf player by default

 328  $.fn.media.flv = $.fn.media.mp3 = function(el, opts) {
 329      var src = opts.src;
 330      var player = /\.mp3\b/i.test(src) ? $.fn.media.defaults.mp3Player : $.fn.media.defaults.flvPlayer;
 331      var key = opts.flvKeyName;
 332      src = encodeURIComponent(src);
 333      opts.src = player;
 334      opts.src = opts.src + '?'+key+'=' + (src);
 335      var srcObj = {};
 336      srcObj[key] = src;
 337      opts.flashvars = $.extend({}, srcObj, opts.flashvars );
 338      return $.fn.media.swf(el, opts);
 339  };
 340  
 341  //

 342  //    Silverlight

 343  //

 344  $.fn.media.xaml = function(el, opts) {
 345      if (!window.Sys || !window.Sys.Silverlight) {
 346          if ($.fn.media.xaml.warning) return;
 347          $.fn.media.xaml.warning = 1;
 348          alert('You must include the Silverlight.js script.');
 349          return;
 350      }
 351  
 352      var props = {
 353          width: opts.width,
 354          height: opts.height,
 355          background: opts.bgColor,
 356          inplaceInstallPrompt: opts.silverlight.inplaceInstallPrompt,
 357          isWindowless: opts.silverlight.isWindowless,
 358          framerate: opts.silverlight.framerate,
 359          version: opts.silverlight.version
 360      };
 361      var events = {
 362          onError: opts.silverlight.onError,
 363          onLoad: opts.silverlight.onLoad
 364      };
 365  
 366      var id1 = el.id ? (' id="'+el.id+'"') : '';
 367      var id2 = opts.id || 'AG' + counter++;
 368      // convert element to div

 369      var cls = opts.cls ? (' class="' + opts.cls + '"') : '';
 370      var $div = $('<div' + id1 + cls + '>');
 371      $(el).after($div).remove();
 372  
 373      Sys.Silverlight.createObjectEx({
 374          source: opts.src,
 375          initParams: opts.silverlight.initParams,
 376          userContext: opts.silverlight.userContext,
 377          id: id2,
 378          parentElement: $div[0],
 379          properties: props,
 380          events: events
 381      });
 382  
 383      if (opts.caption) $('<div>').appendTo($div).html(opts.caption);
 384      return $div;
 385  };
 386  
 387  //

 388  // generate object/embed markup

 389  //

 390  function generate(el, opts, player) {
 391      var $el = $(el);
 392      var o = $.fn.media.defaults.players[player];
 393  
 394      if (player == 'iframe') {
 395          var o = $('<iframe' + ' width="' + opts.width + '" height="' + opts.height + '" >');
 396          o.attr('src', opts.src);
 397          o.css('backgroundColor', o.bgColor);
 398      }
 399      else if ($.browser.msie) {
 400          var a = ['<object width="' + opts.width + '" height="' + opts.height + '" '];
 401          for (var key in opts.attrs)
 402              a.push(key + '="'+opts.attrs[key]+'" ');
 403          for (var key in o.ieAttrs || {}) {
 404              var v = o.ieAttrs[key];
 405              if (key == 'codebase' && window.location.protocol == 'https:')
 406                  v = v.replace('http','https');
 407              a.push(key + '="'+v+'" ');
 408          }
 409          a.push('></ob'+'ject'+'>');
 410          var p = ['<param name="' + (o.oUrl || 'src') +'" value="' + opts.src + '">'];
 411          for (var key in opts.params)
 412              p.push('<param name="'+ key +'" value="' + opts.params[key] + '">');
 413          var o = document.createElement(a.join(''));
 414          for (var i=0; i < p.length; i++)
 415              o.appendChild(document.createElement(p[i]));
 416      }
 417      else if (o.standards) {
 418          // Rewritten to be standards compliant by Richard Connamacher

 419          var a = ['<object type="' + o.mimetype +'" width="' + opts.width + '" height="' + opts.height +'"'];
 420          if (opts.src) a.push(' data="' + opts.src + '" ');
 421          a.push('>');
 422          a.push('<param name="' + (o.oUrl || 'src') +'" value="' + opts.src + '">');
 423          for (var key in opts.params) {
 424              if (key == 'wmode' && player != 'flash') // FF3/Quicktime borks on wmode
 425                  continue;
 426              a.push('<param name="'+ key +'" value="' + opts.params[key] + '">');
 427          }
 428          // Alternate HTML

 429          a.push('<div><p><strong>'+o.title+' Required</strong></p><p>'+o.title+' is required to view this media. <a href="'+o.pluginspage+'">Download Here</a>.</p></div>');
 430          a.push('</ob'+'ject'+'>');
 431      }
 432       else {
 433              var a = ['<embed width="' + opts.width + '" height="' + opts.height + '" style="display:block"'];
 434              if (opts.src) a.push(' src="' + opts.src + '" ');
 435              for (var key in opts.attrs)
 436                  a.push(key + '="'+opts.attrs[key]+'" ');
 437              for (var key in o.eAttrs || {})
 438                  a.push(key + '="'+o.eAttrs[key]+'" ');
 439              for (var key in opts.params) {
 440                  if (key == 'wmode' && player != 'flash') // FF3/Quicktime borks on wmode
 441                      continue;
 442                  a.push(key + '="'+opts.params[key]+'" ');
 443              }
 444              a.push('></em'+'bed'+'>');
 445          }    
 446      // convert element to div

 447      var id = el.id ? (' id="'+el.id+'"') : '';
 448      var cls = opts.cls ? (' class="' + opts.cls + '"') : '';
 449      var $div = $('<div' + id + cls + '>');
 450      $el.after($div).remove();
 451      ($.browser.msie || player == 'iframe') ? $div.append(o) : $div.html(a.join(''));
 452      if (opts.caption) $('<div>').appendTo($div).html(opts.caption);
 453      return $div;
 454  };
 455  
 456  
 457  })(jQuery);


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7