upload.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. function licenseSelectorCheck() {
  2. var selector = document.getElementById( "wpLicense" );
  3. var selection = selector.options[selector.selectedIndex].value;
  4. if( selector.selectedIndex > 0 ) {
  5. if( selection == "" ) {
  6. // Option disabled, but browser is broken and doesn't respect this
  7. selector.selectedIndex = 0;
  8. }
  9. }
  10. // We might show a preview
  11. wgUploadLicenseObj.fetchPreview( selection );
  12. }
  13. function licenseSelectorFixup() {
  14. // for MSIE/Mac; non-breaking spaces cause the <option> not to render
  15. // but, for some reason, setting the text to itself works
  16. var selector = document.getElementById("wpLicense");
  17. if (selector) {
  18. var ua = navigator.userAgent;
  19. var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
  20. if (isMacIe) {
  21. for (var i = 0; i < selector.options.length; i++) {
  22. selector.options[i].text = selector.options[i].text;
  23. }
  24. }
  25. }
  26. }
  27. var wgUploadWarningObj = {
  28. 'responseCache' : { '' : '&nbsp;' },
  29. 'nameToCheck' : '',
  30. 'typing': false,
  31. 'delay': 500, // ms
  32. 'timeoutID': false,
  33. 'keypress': function () {
  34. if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
  35. // Find file to upload
  36. var destFile = document.getElementById('wpDestFile');
  37. var warningElt = document.getElementById( 'wpDestFile-warning' );
  38. if ( !destFile || !warningElt ) return ;
  39. this.nameToCheck = destFile.value ;
  40. // Clear timer
  41. if ( this.timeoutID ) {
  42. window.clearTimeout( this.timeoutID );
  43. }
  44. // Check response cache
  45. for (cached in this.responseCache) {
  46. if (this.nameToCheck == cached) {
  47. this.setWarning(this.responseCache[this.nameToCheck]);
  48. return;
  49. }
  50. }
  51. this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
  52. },
  53. 'checkNow': function (fname) {
  54. if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
  55. if ( this.timeoutID ) {
  56. window.clearTimeout( this.timeoutID );
  57. }
  58. this.nameToCheck = fname;
  59. this.timeout();
  60. },
  61. 'timeout' : function() {
  62. if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
  63. injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
  64. // Get variables into local scope so that they will be preserved for the
  65. // anonymous callback. fileName is copied so that multiple overlapping
  66. // ajax requests can be supported.
  67. var obj = this;
  68. var fileName = this.nameToCheck;
  69. sajax_do_call( 'UploadForm::ajaxGetExistsWarning', [this.nameToCheck],
  70. function (result) {
  71. obj.processResult(result, fileName)
  72. }
  73. );
  74. },
  75. 'processResult' : function (result, fileName) {
  76. removeSpinner( 'destcheck' );
  77. this.setWarning(result.responseText);
  78. this.responseCache[fileName] = result.responseText;
  79. },
  80. 'setWarning' : function (warning) {
  81. var warningElt = document.getElementById( 'wpDestFile-warning' );
  82. var ackElt = document.getElementById( 'wpDestFileWarningAck' );
  83. this.setInnerHTML(warningElt, warning);
  84. // Set a value in the form indicating that the warning is acknowledged and
  85. // doesn't need to be redisplayed post-upload
  86. if ( warning == '' || warning == '&nbsp;' ) {
  87. ackElt.value = '';
  88. } else {
  89. ackElt.value = '1';
  90. }
  91. },
  92. 'setInnerHTML' : function (element, text) {
  93. // Check for no change to avoid flicker in IE 7
  94. if (element.innerHTML != text) {
  95. element.innerHTML = text;
  96. }
  97. }
  98. }
  99. function fillDestFilename(id) {
  100. if (!wgUploadAutoFill) {
  101. return;
  102. }
  103. if (!document.getElementById) {
  104. return;
  105. }
  106. var path = document.getElementById(id).value;
  107. // Find trailing part
  108. var slash = path.lastIndexOf('/');
  109. var backslash = path.lastIndexOf('\\');
  110. var fname;
  111. if (slash == -1 && backslash == -1) {
  112. fname = path;
  113. } else if (slash > backslash) {
  114. fname = path.substring(slash+1, 10000);
  115. } else {
  116. fname = path.substring(backslash+1, 10000);
  117. }
  118. // Capitalise first letter and replace spaces by underscores
  119. fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
  120. // Output result
  121. var destFile = document.getElementById('wpDestFile');
  122. if (destFile) {
  123. destFile.value = fname;
  124. wgUploadWarningObj.checkNow(fname) ;
  125. }
  126. }
  127. function toggleFilenameFiller() {
  128. if(!document.getElementById) return;
  129. var upfield = document.getElementById('wpUploadFile');
  130. var destName = document.getElementById('wpDestFile').value;
  131. if (destName=='' || destName==' ') {
  132. wgUploadAutoFill = true;
  133. } else {
  134. wgUploadAutoFill = false;
  135. }
  136. }
  137. var wgUploadLicenseObj = {
  138. 'responseCache' : { '' : '' },
  139. 'fetchPreview': function( license ) {
  140. if( !wgAjaxLicensePreview || !sajax_init_object() ) return;
  141. for (cached in this.responseCache) {
  142. if (cached == license) {
  143. this.showPreview( this.responseCache[license] );
  144. return;
  145. }
  146. }
  147. injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
  148. sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license],
  149. function( result ) {
  150. wgUploadLicenseObj.processResult( result, license );
  151. }
  152. );
  153. },
  154. 'processResult' : function( result, license ) {
  155. removeSpinner( 'license' );
  156. this.showPreview( result.responseText );
  157. this.responseCache[license] = result.responseText;
  158. },
  159. 'showPreview' : function( preview ) {
  160. var previewPanel = document.getElementById( 'mw-license-preview' );
  161. if( previewPanel.innerHTML != preview )
  162. previewPanel.innerHTML = preview;
  163. }
  164. }
  165. addOnloadHook( licenseSelectorFixup );