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.81 (06/05/2008) 12 * @requires jQuery v1.1.2 or later 13 * $Id: jquery.media.js,v 1.1.4.1 2008/06/30 14:26:55 aaron Exp $ 14 * 15 * Supported Media Players: 16 * - Flash 17 * - Quicktime 18 * - Real Player 19 * - Silverlight 20 * - Windows Media Player 21 * - iframe 22 * 23 * Supported Media Formats: 24 * Any types supported by the above players, such as: 25 * Video: asf, avi, flv, mov, mpg, mpeg, mp4, qt, smil, swf, wmv, 3g2, 3gp 26 * Audio: aif, aac, au, gsm, mid, midi, mov, mp3, m4a, snd, rm, wav, wma 27 * Other: bmp, html, pdf, psd, qif, qtif, qti, tif, tiff, xaml 28 * 29 * Thanks to Mark Hicken and Brent Pedersen for helping me debug this on the Mac! 30 * Thanks to Dan Rossi for numerous bug reports and code bits! 31 */ 32;(function($) { 33 34/** 35 * Chainable method for converting elements into rich media. 36 * 37 * @param options 38 * @param callback fn invoked for each matched element before conversion 39 * @param callback fn invoked for each matched element after conversion 40 */ 41$.fn.media = function(options, f1, f2) { 42 return this.each(function() { 43 if (typeof options == 'function') { 44 f2 = f1; 45 f1 = options; 46 options = {}; 47 } 48 var o = getSettings(this, options); 49 // pre-conversion callback, passes original element and fully populated options 50 if (typeof f1 == 'function') f1(this, o); 51 52 var r = getTypesRegExp(); 53 var m = r.exec(o.src) || ['']; 54 o.type ? m[0] = o.type : m.shift(); 55 for (var i=0; i < m.length; i++) { 56 fn = m[i].toLowerCase(); 57 if (isDigit(fn[0])) fn = 'fn' + fn; // fns can't begin with numbers 58 if (!$.fn.media[fn]) 59 continue; // unrecognized media type 60 // normalize autoplay settings 61 var player = $.fn.media[fn+'_player']; 62 if (!o.params) o.params = {}; 63 if (player) { 64 var num = player.autoplayAttr == 'autostart'; 65 o.params[player.autoplayAttr || 'autoplay'] = num ? (o.autoplay ? 1 : 0) : o.autoplay ? true : false; 66 } 67 var $div = $.fn.media[fn](this, o); 68 69 $div.css('backgroundColor', o.bgColor).width(o.width); 70 // post-conversion callback, passes original element, new div element and fully populated options 71 if (typeof f2 == 'function') f2(this, $div[0], o, player.name); 72 break; 73 } 74 }); 75}; 76 77/** 78 * Non-chainable method for adding or changing file format / player mapping 79 * @name mapFormat 80 * @param String format File format extension (ie: mov, wav, mp3) 81 * @param String player Player name to use for the format (one of: flash, quicktime, realplayer, winmedia, silverlight or iframe 82 */ 83$.fn.media.mapFormat = function(format, player) { 84 if (!format || !player || !$.fn.media.defaults.players[player]) return; // invalid 85 format = format.toLowerCase(); 86 if (isDigit(format[0])) format = 'fn' + format; 87 $.fn.media[format] = $.fn.media[player]; 88 $.fn.media[format+'_player'] = $.fn.media.defaults.players[player]; 89}; 90 91// global defautls; override as needed 92$.fn.media.defaults = { 93 width: 400, 94 height: 400, 95 autoplay: 0, // normalized cross-player setting 96 bgColor: '#ffffff', // background color 97 params: { wmode: 'transparent'}, // added to object element as param elements; added to embed element as attrs 98 attrs: {}, // added to object and embed elements as attrs 99 flashvars: {}, // added to flash content as flashvars param/attr 100 flashVersion: '7', // required flash version 101 expressInstaller: null, // src for express installer 102 103 // default flash video and mp3 player (@see: http://jeroenwijering.com/?item=Flash_Media_Player) 104 flvPlayer: 'mediaplayer.swf', 105 mp3Player: 'mediaplayer.swf', 106
107 // @see http://msdn2.microsoft.com/en-us/library/bb412401.aspx 108 silverlight: { 109 inplaceInstallPrompt: 'true', // display in-place install prompt? 110 isWindowless: 'true', // windowless mode (false for wrapping markup) 111 framerate: '24', // maximum framerate 112 version: '0.9', // Silverlight version 113 onError: null, // onError callback 114 onLoad: null, // onLoad callback 115 initParams: null, // object init params 116 userContext: null // callback arg passed to the load callback 117 } 118}; 119 120// Media Players; think twice before overriding 121$.fn.media.defaults.players = { 122 flash: { 123 name: 'flash', 124 types: 'flv,mp3,swf', 125 oAttrs: { 126 classid: 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000', 127 type: 'application/x-oleobject', 128 codebase: 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + $.fn.media.defaults.flashVersion 129 }, 130 eAttrs: { 131 type: 'application/x-shockwave-flash', 132 pluginspage: 'http://www.adobe.com/go/getflashplayer' 133 } 134 }, 135 quicktime: { 136 name: 'quicktime', 137 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', 138 oAttrs: { 139 classid: 'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B', 140 codebase: 'http://www.apple.com/qtactivex/qtplugin.cab' 141 }, 142 eAttrs: { 143 pluginspage: 'http://www.apple.com/quicktime/download/' 144 } 145 }, 146 realplayer: { 147 name: 'real', 148 types: 'ra,ram,rm,rpm,rv,smi,smil', 149 autoplayAttr: 'autostart', 150 oAttrs: { 151 classid: 'clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' 152 }, 153 eAttrs: { 154 type: 'audio/x-pn-realaudio-plugin', 155 pluginspage: 'http://www.real.com/player/' 156 } 157 }, 158 winmedia: { 159 name: 'winmedia', 160 types: 'asf,avi,wma,wmv', 161 autoplayAttr: 'autostart', 162 oUrl: 'url', 163 oAttrs: { 164 classid: 'clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6', 165 type: 'application/x-oleobject' 166 }, 167 eAttrs: { 168 type: $.browser.mozilla && isFirefoxWMPPluginInstalled() ? 'application/x-ms-wmp' : 'application/x-mplayer2', 169 pluginspage: 'http://www.microsoft.com/Windows/MediaPlayer/' 170 } 171 }, 172 // special cases 173 /* 174 iframe: { 175 name: 'iframe', 176 types: 'html,pdf' 177 },*/ 178 silverlight: { 179 name: 'silverlight', 180 types: 'xaml' 181 } 182}; 183 184// 185// everything below here is private 186// 187 188 189// detection script for FF WMP plugin (http://www.therossman.org/experiments/wmp_play.html) 190// (hat tip to Mark Ross for this script) 191function isFirefoxWMPPluginInstalled() { 192 var plugs = navigator.plugins; 193 for (i = 0; i < plugs.length; i++) { 194 var plugin = plugs[i]; 195 if (plugin['filename'] == 'np-mswmp.dll') 196 return true; 197 } 198 return false; 199} 200 201var counter = 1; 202 203for (var player in $.fn.media.defaults.players) { 204 var types = $.fn.media.defaults.players[player].types; 205 $.each(types.split(','), function(i,o) { 206 if (isDigit(o[0])) o = 'fn' + o; 207 $.fn.media[o] = $.fn.media[player] = getGenerator(player); 208 $.fn.media[o+'_player'] = $.fn.media.defaults.players[player]; 209 }); 210}; 211 212function getTypesRegExp() { 213 var types = ''; 214 for (var player in $.fn.media.defaults.players) { 215 if (types.length) types += ','; 216 types += $.fn.media.defaults.players[player].types; 217 }; 218 return new RegExp('\\.(' + types.replace(/,/g,'|') + ')\\b'); 219}; 220 221function getGenerator(player) { 222 return function(el, options) { 223 return generate(el, options, player); 224 }; 225}; 226 227function isDigit(c) { 228 return '0123456789'.indexOf(c) > -1; 229}; 230 231// flatten all possible options: global defaults, meta, option obj 232function getSettings(el, options) { 233 options = options || {}; 234 var $el = $(el); 235 var cls = el.className || ''; 236 // support metadata plugin (v1.0 and v2.0) 237 var meta = $.metadata ? $el.metadata() : $.meta ? $el.data() : {}; 238 meta = meta || {}; 239 var w = meta.width || parseInt(((cls.match(/w:(\d+)/)||[])[1]||0)); 240 var h = meta.height || parseInt(((cls.match(/h:(\d+)/)||[])[1]||0)); 241 242 if (w) meta.width = w; 243 if (h) meta.height = h; 244 if (cls) meta.cls = cls; 245 246 var a = $.fn.media.defaults; 247 var b = options; 248 var c = meta; 249 250 var p = { params: { bgColor: options.bgColor || $.fn.media.defaults.bgColor } }; 251 var opts = $.extend({}, a, b, c); 252 $.each(['attrs','params','flashvars','silverlight'], function(i,o) { 253 opts[o] = $.extend({}, p[o] || {}, a[o] || {}, b[o] || {}, c[o] || {}); 254 }); 255 256 if (typeof opts.caption == 'undefined') opts.caption = $el.text(); 257 258 // make sure we have a source! 259 opts.src = opts.src || $el.attr('href') || $el.attr('src') || 'unknown'; 260 return opts; 261}; 262 263// 264// Flash Player 265// 266 267// generate flash using SWFObject library if possible 268$.fn.media.swf = function(el, opts) { 269 if (!window.SWFObject && !window.swfobject) { 270 // roll our own 271 if (opts.flashvars) { 272 var a = []; 273 for (var f in opts.flashvars) 274 a.push(f + '=' + opts.flashvars[f]); 275 if (!opts.params) opts.params = {}; 276 opts.params.flashvars = a.join('&'); 277 } 278 return generate(el, opts, 'flash'); 279 } 280 281 var id = el.id ? (' id="'+el.id+'"') : ''; 282 var cls = opts.cls ? (' class="' + opts.cls + '"') : '';
283 var $div = $('<div' + id + cls + '>'); 284 285 // swfobject v2+ 286 if (window.swfobject) { 287 $(el).after($div).appendTo($div); 288 if (!el.id) el.id = 'movie_player_' + counter++; 289 290 // replace el with swfobject content 291 swfobject.embedSWF(opts.src, el.id, opts.width, opts.height, opts.flashVersion, 292 opts.expressInstaller, opts.flashvars, opts.params, opts.attrs); 293 } 294 // swfobject < v2 295 else { 296 $(el).after($div).remove(); 297 var so = new SWFObject(opts.src, 'movie_player_' + counter++, opts.width, opts.height, opts.flashVersion, opts.bgColor); 298 if (opts.expressInstaller) so.useExpressInstall(opts.expressInstaller); 299 300 for (var p in opts.params) 301 if (p != 'bgColor') so.addParam(p, opts.params[p]); 302 for (var f in opts.flashvars) 303 so.addVariable(f, opts.flashvars[f]); 304 so.write($div[0]); 305 } 306 307 if (opts.caption) $('<div>').appendTo($div).html(opts.caption); 308 return $div; 309}; 310 311// map flv and mp3 files to the swf player by default 312$.fn.media.flv = $.fn.media.mp3 = function(el, opts) { 313 var src = opts.src; 314 var player = /\.mp3\b/i.test(src) ? $.fn.media.defaults.mp3Player : $.fn.media.defaults.flvPlayer; 315 opts.src = player; 316 opts.src = opts.src + '?file=' + src; 317 opts.flashvars = $.extend({}, { file: src }, opts.flashvars ); 318 return $.fn.media.swf(el, opts); 319}; 320 321// 322// Silverlight 323// 324$.fn.media.xaml = function(el, opts) { 325 if (!window.Sys || !window.Sys.Silverlight) { 326 if ($.fn.media.xaml.warning) return; 327 $.fn.media.xaml.warning = 1; 328 alert('You must include the Silverlight.js script.'); 329 return; 330 } 331 332 var props = { 333 width: opts.width, 334 height: opts.height, 335 background: opts.bgColor, 336 inplaceInstallPrompt: opts.silverlight.inplaceInstallPrompt, 337 isWindowless: opts.silverlight.isWindowless, 338 framerate: opts.silverlight.framerate, 339 version: opts.silverlight.version 340 }; 341 var events = { 342 onError: opts.silverlight.onError, 343 onLoad: opts.silverlight.onLoad 344 }; 345 346 var id1 = el.id ? (' id="'+el.id+'"') : ''; 347 var id2 = opts.id || 'AG' + counter++; 348 // convert element to div 349 var cls = opts.cls ? (' class="' + opts.cls + '"') : ''; 350 var $div = $('<div' + id1 + cls + '>'); 351 $(el).after($div).remove(); 352 353 Sys.Silverlight.createObjectEx({ 354 source: opts.src, 355 initParams: opts.silverlight.initParams, 356 userContext: opts.silverlight.userContext, 357 id: id2, 358 parentElement: $div[0], 359 properties: props, 360 events: events 361 }); 362 363 if (opts.caption) $('<div>').appendTo($div).html(opts.caption); 364 return $div; 365}; 366 367// 368// generate object/embed markup 369// 370function generate(el, opts, player) { 371 var $el = $(el); 372 var o = $.fn.media.defaults.players[player]; 373 374 if (player == 'iframe') { 375 var o = $('<iframe' + ' width="' + opts.width + '" height="' + opts.height + '" >'); 376 o.attr('src', opts.src); 377 o.css('backgroundColor', o.bgColor); 378 } 379 else if ($.browser.msie) { 380 var a = ['<object width="' + opts.width + '" height="' + opts.height + '" ']; 381 for (var key in opts.attrs) 382 a.push(key + '="'+opts.attrs[key]+'" '); 383 for (var key in o.oAttrs || {}) 384 a.push(key + '="'+o.oAttrs[key]+'" '); 385 a.push('></ob'+'ject'+'>'); 386 var p = ['<param name="' + (o.oUrl || 'src') +'" value="' + opts.src + '">']; 387 for (var key in opts.params) 388 p.push('<param name="'+ key +'" value="' + opts.params[key] + '">'); 389 var o = document.createElement(a.join('')); 390 for (var i=0; i < p.length; i++) 391 o.appendChild(document.createElement(p[i])); 392 } 393 else { 394 var a = ['<embed width="' + opts.width + '" height="' + opts.height + '" style="display:block"']; 395 if (opts.src) a.push(' src="' + opts.src + '" '); 396 for (var key in opts.attrs) 397 a.push(key + '="'+opts.attrs[key]+'" '); 398 for (var key in o.eAttrs || {}) 399 a.push(key + '="'+o.eAttrs[key]+'" '); 400 for (var key in opts.params) 401 a.push(key + '="'+opts.params[key]+'" '); 402 a.push('></em'+'bed'+'>'); 403 } 404 // convert element to div 405 var id = el.id ? (' id="'+el.id+'"') : ''; 406 var cls = opts.cls ? (' class="' + opts.cls + '"') : '';
407 var $div = $('<div' + id + cls + '>'); 408 $el.after($div).remove(); 409 ($.browser.msie || player == 'iframe') ? $div.append(o) : $div.html(a.join('')); 410 if (opts.caption) $('<div>').appendTo($div).html(opts.caption); 411 return $div; 412}; 413 414 415})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.