use wgPageName
[lhc/web/wiklou.git] / js2 / remoteMwEmbed.js
index 836b971..bb9bcb9 100644 (file)
@@ -2,7 +2,6 @@
  * this file exposes some of the functionality of mwEmbed to wikis
  * that do not yet have js2 enabled
  */
-
 var urlparts = getRemoteEmbedPath();
 var mwEmbedHostPath = urlparts[0];
 var reqAguments = urlparts[1];
@@ -17,27 +16,34 @@ addOnloadHook( function(){
 function doPageSpecificRewrite() {
        // Add media wizard
        if( wgAction == 'edit' || wgAction == 'submit' ) {
+               load_mv_embed( function() {                     
+                       loadExternalJs( mwEmbedHostPath + '/editPage.js' + reqAguments );
+               } );
+       }
+       
+       //timed text dispaly:
+       if(wgPageName.indexOf("TimedText") === 0){
                load_mv_embed( function() {
-                       importScriptURI( mwEmbedHostPath + '/editPage.js' + reqAguments );
+                       loadExternalJs( mwEmbedHostPath + '/mwEmbed/libTimedText/mvTimeTextEdit.js' + reqAguments );
                } );
        }
-
+       
        // Firefogg integration
-       if( wgPageName == "Special:Upload" ){
+       if( wgPageName == "Special:Upload" ){           
                load_mv_embed( function() {
-                       importScriptURI( mwEmbedHostPath + '/uploadPage.js' + reqAguments );
+                       loadExternalJs( mwEmbedHostPath + '/uploadPage.js' + reqAguments );
                } );
        }
        
        // Special api proxy page
-       if( wgPageName == 'MediaWiki:ApiProxy' ){
+       if( wgPageName == 'MediaWiki:ApiProxy' ){               
                var wgEnableIframeApiProxy = true;
                load_mv_embed( function() {
-                       importScriptURI( mwEmbedHostPath + '/ApiProxyPage.js' + reqAguments );
+                       js_log("Wiki:ApiProxy::");
+                       loadExternalJs( mwEmbedHostPath + '/apiProxyPage.js' + reqAguments );
                });
        }
        
-
        // OggHandler rewrite for view pages:
        var vidIdList = [];
        var divs = document.getElementsByTagName( 'div' );
@@ -55,72 +61,91 @@ function doPageSpecificRewrite() {
                } );
        }
 }
-// will be deprecated in favor of updates to OggHandler
+// will be depreciated in favor of updates to OggHandler
 function rewrite_for_OggHandler( vidIdList ){
-       for( var i = 0; i < vidIdList.length; i++ ) {
-               var vidId = vidIdList[i];
+       function procVidId( vidId ){
+               //don't process empty vids
+               if(!vidId)
+                       return ;
+               js_log('vidIdList on: ' + vidId +' length: ' + vidIdList.length + ' left in the set: ' + vidIdList );
+               
                // Grab the thumbnail and src of the video
                var pimg = $j( '#' + vidId + ' img' );
                var poster_attr = 'poster = "' + pimg.attr( 'src' ) + '" ';
-               var pwidth = pimg.attr( 'width' );
-               var pheight = pimg.attr( 'height' );
+               var pwidth = $j( '#' + vidId).width();
+               var pheight = $j( '#' + vidId ).height();
 
-               var type_attr = '';
+               var tag_type = 'video';
+                               
                // Check for audio
-               if( pwidth == '22' && pheight == '22' ) {
-                       //set width to parent width:
-                       pwidth = $j( '#' + vidId ).width();
-                       pheight = '100';
-                       type_attr = 'type="audio/ogg"';
+               if( pheight == '22' || pheight == '52') {
+                       //set width to parent width:                    
+                       tag_type = 'audio';
                        poster_attr = '';
                }
 
                // Parsed values:
                var src = '';
-               var duration = '';
-
+               var duration_attr = ''; 
+               var wikiTitleKey = $j( '#'+vidId + ' img').filter(':first').attr('src').split('/');
+               wikiTitleKey = unescape( wikiTitleKey[ wikiTitleKey.length - 2 ] );
                var re = new RegExp( /videoUrl(&quot;:?\s*)*([^&]*)/ );
-               src = re.exec( $j( '#'+vidId).html() )[2];
+               src = re.exec( $j( '#'+vidId ).html() )[2];
 
-               var re = new RegExp( /length(&quot;:?\s*)*([^&]*)/ );
-               duration = re.exec( $j( '#'+vidId).html() )[2];
+               var re = new RegExp( /length(&quot;:?\s*)*([^,]*)/ );
+               var dv = re.exec( $j( '#'+vidId ).html() )[2];
+               if( dv ){
+                       duration_attr = 'durationHint="'+ dv +'" ';
+               }
 
                var re = new RegExp( /offset(&quot;:?\s*)*([^&]*)/ );
-               offset = re.exec( $j( '#'+vidId).html() )[2];
+               offset = re.exec( $j( '#'+vidId ).html() )[2];
                var offset_attr = offset ? 'startOffset="' + offset + '"' : '';
 
-               // Rewrite that video id (do async calls to avoid locking)
                if( src ) {
-                       // Replace the top div with the mv_embed based player:
-                       var vid_html = '<video id="vid_' + i +'" '+
+                       var html_out = '';
+                       
+                       var common_attr = ' id="mwe_' + vidId +'" '+
+                                       'wikiTitleKey="' + wikiTitleKey + '" ' +
                                        'src="' + src + '" ' +
-                                       poster_attr + ' ' +
-                                       type_attr + ' ' +
-                                       offset_attr + ' ' +
-                                       'duration="' + duration + '" ' +
-                                       'style="width:' + pwidth + 'px;height:' +
-                                               pheight + 'px;"></video>';
+                                       duration_attr +
+                                       offset_attr + ' ';
+                                       
+                       if( tag_type == 'audio' ){
+                               html_out='<audio' + common_attr + ' style="width:' + pwidth + 'px;"></audio>';
+                       }else{
+                               html_out='<video' + common_attr +
+                               poster_attr + ' ' +
+                               'style="width:' + pwidth + 'px;height:' + pheight + 'px;">' +
+                               '</video>';
+                       }       
                        //set the video tag inner html and update the height
-                       $j( '#' + vidId ).html( vid_html )
+                       $j( '#' + vidId ).html( html_out )
                                .css('height', pheight + 30);
 
-               }
-
-               rewrite_by_id( 'vid_' + i );
-       }
+               }                               
+               rewrite_by_id( 'mwe_' + vidId, function(){
+                       if( vidIdList.length != 0 ){
+                               setTimeout( function(){
+                                       procVidId( vidIdList.pop() )
+                               }, 1);
+                       }
+               });
+       };
+       //process each item in the vidIdList (with setTimeout to avoid locking)
+       procVidId( vidIdList.pop() );
 }
-
 function getRemoteEmbedPath() {
        for( var i = 0; i < document.getElementsByTagName( 'script' ).length; i++ ) {
                var s = document.getElementsByTagName( 'script' )[i];
-               if( s.src.indexOf( 'remoteMwEmbed.js' ) != -1 ) {
+               if( s.src.indexOf( '/remoteMwEmbed.js' ) != -1 ) {
                        var reqStr = '';
                        var scriptPath = '';
                        if( s.src.indexOf( '?' ) != -1) {
                                reqStr = s.src.substr( s.src.indexOf( '?' ) );
-                               scriptPath = s.src.substr( 0,  s.src.indexOf( '?' ) ).replace( 'remoteMwEmbed.js', '' );
+                               scriptPath = s.src.substr( 0,  s.src.indexOf( '?' ) ).replace( '/remoteMwEmbed.js', '' );
                        } else {
-                               scriptPath = s.src.replace( 'remoteMwEmbed.js', '' )
+                               scriptPath = s.src.replace( '/remoteMwEmbed.js', '' )
                        }
                        // Use the external_media_wizard path:
                        return [scriptPath, reqStr];
@@ -128,18 +153,17 @@ function getRemoteEmbedPath() {
        }
 }
 
-function load_mv_embed( callback ) {
+function load_mv_embed( callback ) {   
        // Inject mv_embed if needed
-       if( typeof mvEmbed == 'undefined' ) {
-               importScriptURI( mwEmbedHostPath + '/mwEmbed/mv_embed.js' + reqAguments );
-               check_for_mv_embed( callback );
-       } else {
-               check_for_mv_embed( callback );
+       if( typeof $mw == 'undefined' ) {
+               var mvurl = mwEmbedHostPath + '/mwEmbed/mv_embed.js' + reqAguments ;
+               importScriptURI( mvurl );               
        }
+       check_for_mv_embed( callback );
 }
 
 function check_for_mv_embed( callback ) {
-       if( typeof MV_EMBED_VERSION == 'undefined' ) {
+       if( typeof $mw == 'undefined' ) {
                setTimeout( function(){
                        check_for_mv_embed( callback );
                }, 25 );