* some updates to the api_proxy examples and code
[lhc/web/wiklou.git] / js2 / remoteMwEmbed.js
1 /*
2 * this file exposes some of the functionality of mwEmbed to wikis
3 * that do not yet have js2 enabled
4 */
5
6 var urlparts = getRemoteEmbedPath();
7 var mwEmbedHostPath = urlparts[0];
8 var reqAguments = urlparts[1];
9
10 addOnloadHook( function(){
11 //only do rewrites if MV_EMBED / js2 is "off"
12 if( typeof MV_EMBED_VERSION == 'undefined' ) {
13 doPageSpecificRewrite();
14 }
15 });
16
17 function doPageSpecificRewrite() {
18 // Add media wizard
19 if( wgAction == 'edit' || wgAction == 'submit' ) {
20 load_mv_embed( function() {
21 loadExternalJs( mwEmbedHostPath + '/editPage.js' + reqAguments );
22 } );
23 }
24
25 // Firefogg integration
26 if( wgPageName == "Special:Upload" ){
27 load_mv_embed( function() {
28 loadExternalJs( mwEmbedHostPath + '/uploadPage.js' + reqAguments );
29 } );
30 }
31
32 // Special api proxy page
33 if( wgPageName == 'MediaWiki:ApiProxy' ){
34 var wgEnableIframeApiProxy = true;
35 load_mv_embed( function() {
36 js_log("Wiki:ApiProxy::");
37 loadExternalJs( mwEmbedHostPath + '/apiProxyPage.js' + reqAguments );
38 });
39 }
40
41
42 // OggHandler rewrite for view pages:
43 var vidIdList = [];
44 var divs = document.getElementsByTagName( 'div' );
45 for( var i = 0; i < divs.length; i++ ) {
46 if( divs[i].id && divs[i].id.substring( 0, 11 ) == 'ogg_player_' ) {
47 vidIdList.push( divs[i].getAttribute( "id" ) );
48 }
49 }
50 if( vidIdList.length > 0 ) {
51 load_mv_embed( function() {
52 mvJsLoader.embedVideoCheck( function() {
53 // Do utility rewrite of OggHandler content:
54 rewrite_for_OggHandler( vidIdList );
55 } );
56 } );
57 }
58 }
59 // will be deprecated in favor of updates to OggHandler
60 function rewrite_for_OggHandler( vidIdList ){
61 for( var i = 0; i < vidIdList.length; i++ ) {
62 var vidId = vidIdList[i];
63 // Grab the thumbnail and src of the video
64 var pimg = $j( '#' + vidId + ' img' );
65 var poster_attr = 'poster = "' + pimg.attr( 'src' ) + '" ';
66 var pwidth = pimg.attr( 'width' );
67 var pheight = pimg.attr( 'height' );
68
69 var type_attr = '';
70 // Check for audio
71 if( pwidth == '22' && pheight == '22' ) {
72 //set width to parent width:
73 pwidth = $j( '#' + vidId ).width();
74 pheight = '100';
75 type_attr = 'type="audio/ogg"';
76 poster_attr = '';
77 }
78
79 // Parsed values:
80 var src = '';
81 var duration = '';
82
83 var re = new RegExp( /videoUrl(&quot;:?\s*)*([^&]*)/ );
84 src = re.exec( $j( '#'+vidId).html() )[2];
85
86 var re = new RegExp( /length(&quot;:?\s*)*([^&]*)/ );
87 duration = re.exec( $j( '#'+vidId).html() )[2];
88
89 var re = new RegExp( /offset(&quot;:?\s*)*([^&]*)/ );
90 offset = re.exec( $j( '#'+vidId).html() )[2];
91 var offset_attr = offset ? 'startOffset="' + offset + '"' : '';
92
93 // Rewrite that video id (do async calls to avoid locking)
94 if( src ) {
95 // Replace the top div with the mv_embed based player:
96 var vid_html = '<video id="vid_' + i +'" '+
97 'src="' + src + '" ' +
98 poster_attr + ' ' +
99 type_attr + ' ' +
100 offset_attr + ' ' +
101 'duration="' + duration + '" ' +
102 'style="width:' + pwidth + 'px;height:' +
103 pheight + 'px;"></video>';
104 //set the video tag inner html and update the height
105 $j( '#' + vidId ).html( vid_html )
106 .css('height', pheight + 30);
107
108 }
109
110 rewrite_by_id( 'vid_' + i );
111 }
112 }
113
114 function getRemoteEmbedPath() {
115 for( var i = 0; i < document.getElementsByTagName( 'script' ).length; i++ ) {
116 var s = document.getElementsByTagName( 'script' )[i];
117 if( s.src.indexOf( '/remoteMwEmbed.js' ) != -1 ) {
118 var reqStr = '';
119 var scriptPath = '';
120 if( s.src.indexOf( '?' ) != -1) {
121 reqStr = s.src.substr( s.src.indexOf( '?' ) );
122 scriptPath = s.src.substr( 0, s.src.indexOf( '?' ) ).replace( '/remoteMwEmbed.js', '' );
123 } else {
124 scriptPath = s.src.replace( '/remoteMwEmbed.js', '' )
125 }
126 // Use the external_media_wizard path:
127 return [scriptPath, reqStr];
128 }
129 }
130 }
131
132 function load_mv_embed( callback ) {
133 if(console.log)
134 console.log( 'load_mv_embed');
135 // Inject mv_embed if needed
136 if( typeof mvEmbed == 'undefined' ) {
137 importScriptURI( mwEmbedHostPath + '/mwEmbed/mv_embed.js' + reqAguments );
138 check_for_mv_embed( callback );
139 } else {
140 check_for_mv_embed( callback );
141 }
142 }
143
144 function check_for_mv_embed( callback ) {
145 if( typeof MV_EMBED_VERSION == 'undefined' ) {
146 setTimeout( function(){
147 check_for_mv_embed( callback );
148 }, 25 );
149 } else {
150 callback();
151 }
152 }