use wgPageName
[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 var urlparts = getRemoteEmbedPath();
6 var mwEmbedHostPath = urlparts[0];
7 var reqAguments = urlparts[1];
8
9 addOnloadHook( function(){
10 //only do rewrites if MV_EMBED / js2 is "off"
11 if( typeof MV_EMBED_VERSION == 'undefined' ) {
12 doPageSpecificRewrite();
13 }
14 });
15
16 function doPageSpecificRewrite() {
17 // Add media wizard
18 if( wgAction == 'edit' || wgAction == 'submit' ) {
19 load_mv_embed( function() {
20 loadExternalJs( mwEmbedHostPath + '/editPage.js' + reqAguments );
21 } );
22 }
23
24 //timed text dispaly:
25 if(wgPageName.indexOf("TimedText") === 0){
26 load_mv_embed( function() {
27 loadExternalJs( mwEmbedHostPath + '/mwEmbed/libTimedText/mvTimeTextEdit.js' + reqAguments );
28 } );
29 }
30
31 // Firefogg integration
32 if( wgPageName == "Special:Upload" ){
33 load_mv_embed( function() {
34 loadExternalJs( mwEmbedHostPath + '/uploadPage.js' + reqAguments );
35 } );
36 }
37
38 // Special api proxy page
39 if( wgPageName == 'MediaWiki:ApiProxy' ){
40 var wgEnableIframeApiProxy = true;
41 load_mv_embed( function() {
42 js_log("Wiki:ApiProxy::");
43 loadExternalJs( mwEmbedHostPath + '/apiProxyPage.js' + reqAguments );
44 });
45 }
46
47 // OggHandler rewrite for view pages:
48 var vidIdList = [];
49 var divs = document.getElementsByTagName( 'div' );
50 for( var i = 0; i < divs.length; i++ ) {
51 if( divs[i].id && divs[i].id.substring( 0, 11 ) == 'ogg_player_' ) {
52 vidIdList.push( divs[i].getAttribute( "id" ) );
53 }
54 }
55 if( vidIdList.length > 0 ) {
56 load_mv_embed( function() {
57 mvJsLoader.embedVideoCheck( function() {
58 // Do utility rewrite of OggHandler content:
59 rewrite_for_OggHandler( vidIdList );
60 } );
61 } );
62 }
63 }
64 // will be depreciated in favor of updates to OggHandler
65 function rewrite_for_OggHandler( vidIdList ){
66 function procVidId( vidId ){
67 //don't process empty vids
68 if(!vidId)
69 return ;
70 js_log('vidIdList on: ' + vidId +' length: ' + vidIdList.length + ' left in the set: ' + vidIdList );
71
72 // Grab the thumbnail and src of the video
73 var pimg = $j( '#' + vidId + ' img' );
74 var poster_attr = 'poster = "' + pimg.attr( 'src' ) + '" ';
75 var pwidth = $j( '#' + vidId).width();
76 var pheight = $j( '#' + vidId ).height();
77
78 var tag_type = 'video';
79
80 // Check for audio
81 if( pheight == '22' || pheight == '52') {
82 //set width to parent width:
83 tag_type = 'audio';
84 poster_attr = '';
85 }
86
87 // Parsed values:
88 var src = '';
89 var duration_attr = '';
90 var wikiTitleKey = $j( '#'+vidId + ' img').filter(':first').attr('src').split('/');
91 wikiTitleKey = unescape( wikiTitleKey[ wikiTitleKey.length - 2 ] );
92 var re = new RegExp( /videoUrl(&quot;:?\s*)*([^&]*)/ );
93 src = re.exec( $j( '#'+vidId ).html() )[2];
94
95 var re = new RegExp( /length(&quot;:?\s*)*([^,]*)/ );
96 var dv = re.exec( $j( '#'+vidId ).html() )[2];
97 if( dv ){
98 duration_attr = 'durationHint="'+ dv +'" ';
99 }
100
101 var re = new RegExp( /offset(&quot;:?\s*)*([^&]*)/ );
102 offset = re.exec( $j( '#'+vidId ).html() )[2];
103 var offset_attr = offset ? 'startOffset="' + offset + '"' : '';
104
105 if( src ) {
106 var html_out = '';
107
108 var common_attr = ' id="mwe_' + vidId +'" '+
109 'wikiTitleKey="' + wikiTitleKey + '" ' +
110 'src="' + src + '" ' +
111 duration_attr +
112 offset_attr + ' ';
113
114 if( tag_type == 'audio' ){
115 html_out='<audio' + common_attr + ' style="width:' + pwidth + 'px;"></audio>';
116 }else{
117 html_out='<video' + common_attr +
118 poster_attr + ' ' +
119 'style="width:' + pwidth + 'px;height:' + pheight + 'px;">' +
120 '</video>';
121 }
122 //set the video tag inner html and update the height
123 $j( '#' + vidId ).html( html_out )
124 .css('height', pheight + 30);
125
126 }
127 rewrite_by_id( 'mwe_' + vidId, function(){
128 if( vidIdList.length != 0 ){
129 setTimeout( function(){
130 procVidId( vidIdList.pop() )
131 }, 1);
132 }
133 });
134 };
135 //process each item in the vidIdList (with setTimeout to avoid locking)
136 procVidId( vidIdList.pop() );
137 }
138 function getRemoteEmbedPath() {
139 for( var i = 0; i < document.getElementsByTagName( 'script' ).length; i++ ) {
140 var s = document.getElementsByTagName( 'script' )[i];
141 if( s.src.indexOf( '/remoteMwEmbed.js' ) != -1 ) {
142 var reqStr = '';
143 var scriptPath = '';
144 if( s.src.indexOf( '?' ) != -1) {
145 reqStr = s.src.substr( s.src.indexOf( '?' ) );
146 scriptPath = s.src.substr( 0, s.src.indexOf( '?' ) ).replace( '/remoteMwEmbed.js', '' );
147 } else {
148 scriptPath = s.src.replace( '/remoteMwEmbed.js', '' )
149 }
150 // Use the external_media_wizard path:
151 return [scriptPath, reqStr];
152 }
153 }
154 }
155
156 function load_mv_embed( callback ) {
157 // Inject mv_embed if needed
158 if( typeof $mw == 'undefined' ) {
159 var mvurl = mwEmbedHostPath + '/mwEmbed/mv_embed.js' + reqAguments ;
160 importScriptURI( mvurl );
161 }
162 check_for_mv_embed( callback );
163 }
164
165 function check_for_mv_embed( callback ) {
166 if( typeof $mw == 'undefined' ) {
167 setTimeout( function(){
168 check_for_mv_embed( callback );
169 }, 25 );
170 } else {
171 callback();
172 }
173 }