Changed duplication function
[mediawiki.git] / js2 / remoteMwEmbed.js
blobe7a2612875fece8edfe872809424af48aee3682d
1 /*
2  * this file exposes some of the functionality of mwEmbed to wikis
3  * that do not yet have js2 enabled
4  */
6 var urlparts = getRemoteEmbedPath();
7 var mwEmbedHostPath = urlparts[0];
8 var reqAguments = urlparts[1];
10 addOnloadHook( function(){
11         //only do rewrites if MV_EMBED / js2 is "off"
12         if( typeof MV_EMBED_VERSION == 'undefined' ) {
13                 doPageSpecificRewrite();
14         }
15 });
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         }
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         // OggHandler rewrite for view pages:
42         var vidIdList = [];
43         var divs = document.getElementsByTagName( 'div' );
44         for( var i = 0; i < divs.length; i++ ) {
45                 if( divs[i].id && divs[i].id.substring( 0, 11 ) == 'ogg_player_' ) {
46                         vidIdList.push( divs[i].getAttribute( "id" ) );
47                 }
48         }
49         if( vidIdList.length > 0 ) {
50                 load_mv_embed( function() {
51                         mvJsLoader.embedVideoCheck( function() {
52                                 // Do utility rewrite of OggHandler content:
53                                 rewrite_for_OggHandler( vidIdList );
54                         } );
55                 } );
56         }
58 // will be deprecated in favor of updates to OggHandler
59 function rewrite_for_OggHandler( vidIdList ){
60         for( var i = 0; i < vidIdList.length; i++ ) {
61                 var vidId = vidIdList[i];
62                 // Grab the thumbnail and src of the video
63                 var pimg = $j( '#' + vidId + ' img' );
64                 var poster_attr = 'poster = "' + pimg.attr( 'src' ) + '" ';
65                 var pwidth = pimg.attr( 'width' );
66                 var pheight = pimg.attr( 'height' );
68                 var type_attr = '';
69                 // Check for audio
70                 if( pwidth == '22' && pheight == '22' ) {
71                         //set width to parent width:
72                         pwidth = $j( '#' + vidId ).width();
73                         pheight = '100';
74                         type_attr = 'type="audio/ogg"';
75                         poster_attr = '';
76                 }
78                 // Parsed values:
79                 var src = '';
80                 var duration = '';
82                 var re = new RegExp( /videoUrl(&quot;:?\s*)*([^&]*)/ );
83                 src = re.exec( $j( '#'+vidId).html() )[2];
85                 var re = new RegExp( /length(&quot;:?\s*)*([^&]*)/ );
86                 duration = re.exec( $j( '#'+vidId).html() )[2];
88                 var re = new RegExp( /offset(&quot;:?\s*)*([^&]*)/ );
89                 offset = re.exec( $j( '#'+vidId).html() )[2];
90                 var offset_attr = offset ? 'startOffset="' + offset + '"' : '';
92                 // Rewrite that video id (do async calls to avoid locking)
93                 if( src ) {
94                         // Replace the top div with the mv_embed based player:
95                         var vid_html = '<video id="vid_' + i +'" '+
96                                         'src="' + src + '" ' +
97                                         poster_attr + ' ' +
98                                         type_attr + ' ' +
99                                         offset_attr + ' ' +
100                                         'duration="' + duration + '" ' +
101                                         'style="width:' + pwidth + 'px;height:' +
102                                                 pheight + 'px;"></video>';
103                         //set the video tag inner html and update the height
104                         $j( '#' + vidId ).html( vid_html )
105                                 .css('height', pheight + 30);
107                 }
109                 rewrite_by_id( 'vid_' + i );
110         }
113 function getRemoteEmbedPath() {
114         for( var i = 0; i < document.getElementsByTagName( 'script' ).length; i++ ) {
115                 var s = document.getElementsByTagName( 'script' )[i];
116                 if( s.src.indexOf( '/remoteMwEmbed.js' ) != -1 ) {
117                         var reqStr = '';
118                         var scriptPath = '';
119                         if( s.src.indexOf( '?' ) != -1) {
120                                 reqStr = s.src.substr( s.src.indexOf( '?' ) );
121                                 scriptPath = s.src.substr( 0,  s.src.indexOf( '?' ) ).replace( '/remoteMwEmbed.js', '' );
122                         } else {
123                                 scriptPath = s.src.replace( '/remoteMwEmbed.js', '' )
124                         }
125                         // Use the external_media_wizard path:
126                         return [scriptPath, reqStr];
127                 }
128         }
131 function load_mv_embed( callback ) {    
132         // Inject mv_embed if needed
133         if( typeof $mw == 'undefined' ) {
134                 var mvurl = mwEmbedHostPath + '/mwEmbed/mv_embed.js' + reqAguments ;
135                 importScriptURI( mvurl );               
136         }
137         check_for_mv_embed( callback );
140 function check_for_mv_embed( callback ) {
141         if( typeof $mw == 'undefined' ) {
142                 setTimeout( function(){
143                         check_for_mv_embed( callback );
144                 }, 25 );
145         } else {
146                 callback();
147         }