upload.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. ( function () {
  2. var ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
  3. fileExtensions = mw.config.get( 'wgFileExtensions' );
  4. window.licenseSelectorCheck = function() {
  5. var selector = document.getElementById( "wpLicense" );
  6. var selection = selector.options[selector.selectedIndex].value;
  7. if( selector.selectedIndex > 0 ) {
  8. if( selection == "" ) {
  9. // Option disabled, but browser is broken and doesn't respect this
  10. selector.selectedIndex = 0;
  11. }
  12. }
  13. // We might show a preview
  14. wgUploadLicenseObj.fetchPreview( selection );
  15. };
  16. function uploadSetup() {
  17. // Disable URL box if the URL copy upload source type is not selected
  18. var e = document.getElementById( 'wpSourceTypeurl' );
  19. if( e ) {
  20. if( !e.checked ) {
  21. var ein = document.getElementById( 'wpUploadFileURL' );
  22. if(ein)
  23. ein.setAttribute( 'disabled', 'disabled' );
  24. }
  25. }
  26. // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
  27. // But for some reason, setting the text to itself works
  28. var selector = document.getElementById("wpLicense");
  29. if (selector) {
  30. var ua = navigator.userAgent;
  31. var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
  32. if (isMacIe) {
  33. for (var i = 0; i < selector.options.length; i++) {
  34. selector.options[i].text = selector.options[i].text;
  35. }
  36. }
  37. }
  38. // AJAX wpDestFile warnings
  39. if ( ajaxUploadDestCheck ) {
  40. // Insert an event handler that fetches upload warnings when wpDestFile
  41. // has been changed
  42. document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
  43. wgUploadWarningObj.checkNow(this.value);
  44. };
  45. // Insert a row where the warnings will be displayed just below the
  46. // wpDestFile row
  47. var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
  48. var row = optionsTable.insertRow( 1 );
  49. var td = document.createElement( 'td' );
  50. td.id = 'wpDestFile-warning';
  51. td.colSpan = 2;
  52. row.appendChild( td );
  53. }
  54. var wpLicense = document.getElementById( 'wpLicense' );
  55. if ( mw.config.get( 'wgAjaxLicensePreview' ) && wpLicense ) {
  56. // License selector check
  57. wpLicense.onchange = licenseSelectorCheck;
  58. // License selector table row
  59. var wpLicenseRow = wpLicense.parentNode.parentNode;
  60. var wpLicenseTbody = wpLicenseRow.parentNode;
  61. var row = document.createElement( 'tr' );
  62. var td = document.createElement( 'td' );
  63. row.appendChild( td );
  64. td = document.createElement( 'td' );
  65. td.id = 'mw-license-preview';
  66. row.appendChild( td );
  67. wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
  68. }
  69. // fillDestFile setup
  70. var i,
  71. uploadSourceIds = mw.config.get( 'wgUploadSourceIds' ),
  72. len = uploadSourceIds.length;
  73. for ( i = 0; i < len; i += 1 )
  74. document.getElementById( uploadSourceIds[i] ).onchange = function (e) {
  75. fillDestFilename( this.id );
  76. };
  77. };
  78. window.wgUploadWarningObj = {
  79. 'responseCache' : { '' : '&nbsp;' },
  80. 'nameToCheck' : '',
  81. 'typing': false,
  82. 'delay': 500, // ms
  83. 'timeoutID': false,
  84. 'keypress': function () {
  85. if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
  86. // Find file to upload
  87. var destFile = document.getElementById('wpDestFile');
  88. var warningElt = document.getElementById( 'wpDestFile-warning' );
  89. if ( !destFile || !warningElt ) return ;
  90. this.nameToCheck = destFile.value ;
  91. // Clear timer
  92. if ( this.timeoutID ) {
  93. window.clearTimeout( this.timeoutID );
  94. }
  95. // Check response cache
  96. for (cached in this.responseCache) {
  97. if (this.nameToCheck == cached) {
  98. this.setWarning(this.responseCache[this.nameToCheck]);
  99. return;
  100. }
  101. }
  102. this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
  103. },
  104. 'checkNow': function (fname) {
  105. if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
  106. if ( this.timeoutID ) {
  107. window.clearTimeout( this.timeoutID );
  108. }
  109. this.nameToCheck = fname;
  110. this.timeout();
  111. },
  112. 'timeout' : function() {
  113. if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
  114. injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
  115. // Get variables into local scope so that they will be preserved for the
  116. // anonymous callback. fileName is copied so that multiple overlapping
  117. // ajax requests can be supported.
  118. var obj = this;
  119. var fileName = this.nameToCheck;
  120. sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
  121. function (result) {
  122. obj.processResult(result, fileName)
  123. }
  124. );
  125. },
  126. 'processResult' : function (result, fileName) {
  127. removeSpinner( 'destcheck' );
  128. this.setWarning(result.responseText);
  129. this.responseCache[fileName] = result.responseText;
  130. },
  131. 'setWarning' : function (warning) {
  132. var warningElt = document.getElementById( 'wpDestFile-warning' );
  133. var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
  134. this.setInnerHTML(warningElt, warning);
  135. // Set a value in the form indicating that the warning is acknowledged and
  136. // doesn't need to be redisplayed post-upload
  137. if ( warning == '' || warning == '&nbsp;' ) {
  138. ackElt[0].value = '';
  139. } else {
  140. ackElt[0].value = '1';
  141. }
  142. },
  143. 'setInnerHTML' : function (element, text) {
  144. // Check for no change to avoid flicker in IE 7
  145. if (element.innerHTML != text) {
  146. element.innerHTML = text;
  147. }
  148. }
  149. };
  150. window.fillDestFilename = function(id) {
  151. if ( !mw.config.get( 'wgUploadAutoFill' ) ) {
  152. return;
  153. }
  154. if (!document.getElementById) {
  155. return;
  156. }
  157. // Remove any previously flagged errors
  158. var e = document.getElementById( 'mw-upload-permitted' );
  159. if( e ) e.className = '';
  160. var e = document.getElementById( 'mw-upload-prohibited' );
  161. if( e ) e.className = '';
  162. var path = document.getElementById(id).value;
  163. // Find trailing part
  164. var slash = path.lastIndexOf('/');
  165. var backslash = path.lastIndexOf('\\');
  166. var fname;
  167. if (slash == -1 && backslash == -1) {
  168. fname = path;
  169. } else if (slash > backslash) {
  170. fname = path.substring(slash+1, 10000);
  171. } else {
  172. fname = path.substring(backslash+1, 10000);
  173. }
  174. // Clear the filename if it does not have a valid extension.
  175. // URLs are less likely to have a useful extension, so don't include them in the
  176. // extension check.
  177. if ( mw.config.get( 'wgStrictFileExtensions' ) && fileExtensions && id !== 'wpUploadFileURL' ) {
  178. var found = false;
  179. if ( fname.lastIndexOf( '.' ) !== -1 ) {
  180. var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
  181. for ( var i = 0; i < fileExtensions.length; i += 1 ) {
  182. if ( fileExtensions[i].toLowerCase() === ext.toLowerCase() ) {
  183. found = true;
  184. break;
  185. }
  186. }
  187. }
  188. if( !found ) {
  189. // Not a valid extension
  190. // Clear the upload and set mw-upload-permitted to error
  191. document.getElementById(id).value = '';
  192. var e = document.getElementById( 'mw-upload-permitted' );
  193. if( e ) e.className = 'error';
  194. var e = document.getElementById( 'mw-upload-prohibited' );
  195. if( e ) e.className = 'error';
  196. // Clear wpDestFile as well
  197. var e = document.getElementById( 'wpDestFile' );
  198. if( e ) e.value = '';
  199. return false;
  200. }
  201. }
  202. // Replace spaces by underscores
  203. fname = fname.replace( / /g, '_' );
  204. // Capitalise first letter if needed
  205. if ( mw.config.get( 'wgCapitalizeUploads' ) ) {
  206. fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) );
  207. }
  208. // Output result
  209. var destFile = document.getElementById('wpDestFile');
  210. if (destFile) {
  211. destFile.value = fname;
  212. wgUploadWarningObj.checkNow(fname) ;
  213. }
  214. };
  215. window.toggleFilenameFiller = function() {
  216. if(!document.getElementById) return;
  217. var upfield = document.getElementById('wpUploadFile');
  218. var destName = document.getElementById('wpDestFile').value;
  219. wgUploadAutoFill = ( destName == '' || destName == ' ' );
  220. };
  221. window.wgUploadLicenseObj = {
  222. 'responseCache' : { '' : '' },
  223. 'fetchPreview': function( license ) {
  224. if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) return;
  225. for (cached in this.responseCache) {
  226. if (cached == license) {
  227. this.showPreview( this.responseCache[license] );
  228. return;
  229. }
  230. }
  231. injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
  232. var title = document.getElementById('wpDestFile').value;
  233. if ( !title ) title = 'File:Sample.jpg';
  234. var url = mw.util.wikiScript( 'api' )
  235. + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
  236. + '&title=' + encodeURIComponent( title )
  237. + '&prop=text&pst&format=json';
  238. var req = sajax_init_object();
  239. req.onreadystatechange = function() {
  240. if ( req.readyState == 4 && req.status == 200 )
  241. wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
  242. };
  243. req.open( 'GET', url, true );
  244. req.send( '' );
  245. },
  246. 'processResult' : function( result, license ) {
  247. removeSpinner( 'license' );
  248. this.responseCache[license] = result['parse']['text']['*'];
  249. this.showPreview( this.responseCache[license] );
  250. },
  251. 'showPreview' : function( preview ) {
  252. var previewPanel = document.getElementById( 'mw-license-preview' );
  253. if( previewPanel.innerHTML != preview )
  254. previewPanel.innerHTML = preview;
  255. }
  256. };
  257. $( document ).ready( uploadSetup );
  258. }() );