您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. var topWin = window.dialogArguments || opener || parent || top;
  2. function fileDialogStart() {
  3. jQuery("#media-upload-error").empty();
  4. }
  5. // progress and success handlers for media multi uploads
  6. function fileQueued(fileObj) {
  7. // Get rid of unused form
  8. jQuery('.media-blank').remove();
  9. // Collapse a single item
  10. if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
  11. jQuery('.describe-toggle-on').show();
  12. jQuery('.describe-toggle-off').hide();
  13. jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
  14. }
  15. // Create a progress bar containing the filename
  16. jQuery('<div class="media-item">')
  17. .attr( 'id', 'media-item-' + fileObj.id )
  18. .addClass('child-of-' + post_id)
  19. .append('<div class="progress"><div class="bar"></div></div>',
  20. jQuery('<div class="filename original"><span class="percent"></span>').text( ' ' + fileObj.name ))
  21. .appendTo( jQuery('#media-items' ) );
  22. // Display the progress div
  23. jQuery('.progress', '#media-item-' + fileObj.id).show();
  24. // Disable submit and enable cancel
  25. jQuery('#insert-gallery').prop('disabled', true);
  26. jQuery('#cancel-upload').prop('disabled', false);
  27. }
  28. function uploadStart(fileObj) {
  29. try {
  30. if ( typeof topWin.tb_remove != 'undefined' )
  31. topWin.jQuery('#TB_overlay').unbind('click', topWin.tb_remove);
  32. } catch(e){}
  33. return true;
  34. }
  35. function uploadProgress(fileObj, bytesDone, bytesTotal) {
  36. // Lengthen the progress bar
  37. var w = jQuery('#media-items').width() - 2, item = jQuery('#media-item-' + fileObj.id);
  38. jQuery('.bar', item).width( w * bytesDone / bytesTotal );
  39. jQuery('.percent', item).html( Math.ceil(bytesDone / bytesTotal * 100) + '%' );
  40. if ( bytesDone == bytesTotal )
  41. jQuery('.bar', item).html('<strong class="crunching">' + swfuploadL10n.crunching + '</strong>');
  42. }
  43. function prepareMediaItem(fileObj, serverData) {
  44. var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
  45. // Move the progress bar to 100%
  46. jQuery('.bar', item).remove();
  47. jQuery('.progress', item).hide();
  48. try {
  49. if ( typeof topWin.tb_remove != 'undefined' )
  50. topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
  51. } catch(e){}
  52. // Old style: Append the HTML returned by the server -- thumbnail and form inputs
  53. if ( isNaN(serverData) || !serverData ) {
  54. item.append(serverData);
  55. prepareMediaItemInit(fileObj);
  56. }
  57. // New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
  58. else {
  59. item.load('async-upload.php', {attachment_id:serverData, fetch:f}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
  60. }
  61. }
  62. function prepareMediaItemInit(fileObj) {
  63. var item = jQuery('#media-item-' + fileObj.id);
  64. // Clone the thumbnail as a "pinkynail" -- a tiny image to the left of the filename
  65. jQuery('.thumbnail', item).clone().attr('class', 'pinkynail toggle').prependTo(item);
  66. // Replace the original filename with the new (unique) one assigned during upload
  67. jQuery('.filename.original', item).replaceWith( jQuery('.filename.new', item) );
  68. // Also bind toggle to the links
  69. jQuery('a.toggle', item).click(function(){
  70. jQuery(this).siblings('.slidetoggle').slideToggle(350, function(){
  71. var w = jQuery(window).height(), t = jQuery(this).offset().top, h = jQuery(this).height(), b;
  72. if ( w && t && h ) {
  73. b = t + h;
  74. if ( b > w && (h + 48) < w )
  75. window.scrollBy(0, b - w + 13);
  76. else if ( b > w )
  77. window.scrollTo(0, t - 36);
  78. }
  79. });
  80. jQuery(this).siblings('.toggle').andSelf().toggle();
  81. jQuery(this).siblings('a.toggle').focus();
  82. return false;
  83. });
  84. // Bind AJAX to the new Delete button
  85. jQuery('a.delete', item).click(function(){
  86. // Tell the server to delete it. TODO: handle exceptions
  87. jQuery.ajax({
  88. url: ajaxurl,
  89. type: 'post',
  90. success: deleteSuccess,
  91. error: deleteError,
  92. id: fileObj.id,
  93. data: {
  94. id : this.id.replace(/[^0-9]/g, ''),
  95. action : 'trash-post',
  96. _ajax_nonce : this.href.replace(/^.*wpnonce=/,'')
  97. }
  98. });
  99. return false;
  100. });
  101. // Bind AJAX to the new Undo button
  102. jQuery('a.undo', item).click(function(){
  103. // Tell the server to untrash it. TODO: handle exceptions
  104. jQuery.ajax({
  105. url: ajaxurl,
  106. type: 'post',
  107. id: fileObj.id,
  108. data: {
  109. id : this.id.replace(/[^0-9]/g,''),
  110. action: 'untrash-post',
  111. _ajax_nonce: this.href.replace(/^.*wpnonce=/,'')
  112. },
  113. success: function(data, textStatus){
  114. var item = jQuery('#media-item-' + fileObj.id);
  115. if ( type = jQuery('#type-of-' + fileObj.id).val() )
  116. jQuery('#' + type + '-counter').text(jQuery('#' + type + '-counter').text()-0+1);
  117. if ( item.hasClass('child-of-'+post_id) )
  118. jQuery('#attachments-count').text(jQuery('#attachments-count').text()-0+1);
  119. jQuery('.filename .trashnotice', item).remove();
  120. jQuery('.filename .title', item).css('font-weight','normal');
  121. jQuery('a.undo', item).addClass('hidden');
  122. jQuery('a.describe-toggle-on, .menu_order_input', item).show();
  123. item.css( {backgroundColor:'#ceb'} ).animate( {backgroundColor: '#fff'}, { queue: false, duration: 500, complete: function(){ jQuery(this).css({backgroundColor:''}); } }).removeClass('undo');
  124. }
  125. });
  126. return false;
  127. });
  128. // Open this item if it says to start open (e.g. to display an error)
  129. jQuery('#media-item-' + fileObj.id + '.startopen').removeClass('startopen').slideToggle(500).siblings('.toggle').toggle();
  130. }
  131. function itemAjaxError(id, html) {
  132. var item = jQuery('#media-item-' + id);
  133. var filename = jQuery('.filename', item).text();
  134. item.html('<div class="error-div">'
  135. + '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
  136. + '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
  137. + html
  138. + '</div>');
  139. item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
  140. }
  141. function deleteSuccess(data, textStatus) {
  142. if ( data == '-1' )
  143. return itemAjaxError(this.id, 'You do not have permission. Has your session expired?');
  144. if ( data == '0' )
  145. return itemAjaxError(this.id, 'Could not be deleted. Has it been deleted already?');
  146. var id = this.id, item = jQuery('#media-item-' + id);
  147. // Decrement the counters.
  148. if ( type = jQuery('#type-of-' + id).val() )
  149. jQuery('#' + type + '-counter').text( jQuery('#' + type + '-counter').text() - 1 );
  150. if ( item.hasClass('child-of-'+post_id) )
  151. jQuery('#attachments-count').text( jQuery('#attachments-count').text() - 1 );
  152. if ( jQuery('form.type-form #media-items').children().length == 1 && jQuery('.hidden', '#media-items').length > 0 ) {
  153. jQuery('.toggle').toggle();
  154. jQuery('.slidetoggle').slideUp(200).siblings().removeClass('hidden');
  155. }
  156. // Vanish it.
  157. jQuery('.toggle', item).toggle();
  158. jQuery('.slidetoggle', item).slideUp(200).siblings().removeClass('hidden');
  159. item.css( {backgroundColor:'#faa'} ).animate( {backgroundColor:'#f4f4f4'}, {queue:false, duration:500} ).addClass('undo');
  160. jQuery('.filename:empty', item).remove();
  161. jQuery('.filename .title', item).css('font-weight','bold');
  162. jQuery('.filename', item).append('<span class="trashnotice"> ' + swfuploadL10n.deleted + ' </span>').siblings('a.toggle').hide();
  163. jQuery('.filename', item).append( jQuery('a.undo', item).removeClass('hidden') );
  164. jQuery('.menu_order_input', item).hide();
  165. return;
  166. }
  167. function deleteError(X, textStatus, errorThrown) {
  168. // TODO
  169. }
  170. function updateMediaForm() {
  171. var one = jQuery('form.type-form #media-items').children(), items = jQuery('#media-items').children();
  172. // Just one file, no need for collapsible part
  173. if ( one.length == 1 ) {
  174. jQuery('.slidetoggle', one).slideDown(500).siblings().addClass('hidden').filter('.toggle').toggle();
  175. }
  176. // Only show Save buttons when there is at least one file.
  177. if ( items.not('.media-blank').length > 0 )
  178. jQuery('.savebutton').show();
  179. else
  180. jQuery('.savebutton').hide();
  181. // Only show Gallery buttons when there are at least two files.
  182. if ( items.length > 1 ) {
  183. jQuery('.insert-gallery').show();
  184. } else {
  185. jQuery('.insert-gallery').hide();
  186. }
  187. }
  188. function uploadSuccess(fileObj, serverData) {
  189. // if async-upload returned an error message, place it in the media item div and return
  190. if ( serverData.match('media-upload-error') ) {
  191. jQuery('#media-item-' + fileObj.id).html(serverData);
  192. return;
  193. }
  194. prepareMediaItem(fileObj, serverData);
  195. updateMediaForm();
  196. // Increment the counter.
  197. if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
  198. jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
  199. }
  200. function uploadComplete(fileObj) {
  201. // If no more uploads queued, enable the submit button
  202. if ( swfu.getStats().files_queued == 0 ) {
  203. jQuery('#cancel-upload').prop('disabled', true);
  204. jQuery('#insert-gallery').prop('disabled', false);
  205. }
  206. }
  207. // wp-specific error handlers
  208. // generic message
  209. function wpQueueError(message) {
  210. jQuery('#media-upload-error').show().text(message);
  211. }
  212. // file-specific message
  213. function wpFileError(fileObj, message) {
  214. var item = jQuery('#media-item-' + fileObj.id);
  215. var filename = jQuery('.filename', item).text();
  216. item.html('<div class="error-div">'
  217. + '<a class="dismiss" href="#">' + swfuploadL10n.dismiss + '</a>'
  218. + '<strong>' + swfuploadL10n.error_uploading.replace('%s', filename) + '</strong><br />'
  219. + message
  220. + '</div>');
  221. item.find('a.dismiss').click(function(){jQuery(this).parents('.media-item').slideUp(200, function(){jQuery(this).remove();})});
  222. }
  223. function fileQueueError(fileObj, error_code, message) {
  224. // Handle this error separately because we don't want to create a FileProgress element for it.
  225. if ( error_code == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED ) {
  226. wpQueueError(swfuploadL10n.queue_limit_exceeded);
  227. }
  228. else if ( error_code == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT ) {
  229. fileQueued(fileObj);
  230. wpFileError(fileObj, swfuploadL10n.file_exceeds_size_limit);
  231. }
  232. else if ( error_code == SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE ) {
  233. fileQueued(fileObj);
  234. wpFileError(fileObj, swfuploadL10n.zero_byte_file);
  235. }
  236. else if ( error_code == SWFUpload.QUEUE_ERROR.INVALID_FILETYPE ) {
  237. fileQueued(fileObj);
  238. wpFileError(fileObj, swfuploadL10n.invalid_filetype);
  239. }
  240. else {
  241. wpQueueError(swfuploadL10n.default_error);
  242. }
  243. }
  244. function fileDialogComplete(num_files_queued) {
  245. try {
  246. if (num_files_queued > 0) {
  247. this.startUpload();
  248. }
  249. } catch (ex) {
  250. this.debug(ex);
  251. }
  252. }
  253. function switchUploader(s) {
  254. var f = document.getElementById(swfu.customSettings.swfupload_element_id), h = document.getElementById(swfu.customSettings.degraded_element_id);
  255. if ( s ) {
  256. f.style.display = 'block';
  257. h.style.display = 'none';
  258. } else {
  259. f.style.display = 'none';
  260. h.style.display = 'block';
  261. }
  262. }
  263. function swfuploadPreLoad() {
  264. if ( !uploaderMode ) {
  265. switchUploader(1);
  266. } else {
  267. switchUploader(0);
  268. }
  269. }
  270. function swfuploadLoadFailed() {
  271. switchUploader(0);
  272. jQuery('.upload-html-bypass').hide();
  273. }
  274. function uploadError(fileObj, errorCode, message) {
  275. switch (errorCode) {
  276. case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
  277. wpFileError(fileObj, swfuploadL10n.missing_upload_url);
  278. break;
  279. case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
  280. wpFileError(fileObj, swfuploadL10n.upload_limit_exceeded);
  281. break;
  282. case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
  283. wpQueueError(swfuploadL10n.http_error);
  284. break;
  285. case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
  286. wpQueueError(swfuploadL10n.upload_failed);
  287. break;
  288. case SWFUpload.UPLOAD_ERROR.IO_ERROR:
  289. wpQueueError(swfuploadL10n.io_error);
  290. break;
  291. case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
  292. wpQueueError(swfuploadL10n.security_error);
  293. break;
  294. case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
  295. case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
  296. jQuery('#media-item-' + fileObj.id).remove();
  297. break;
  298. default:
  299. wpFileError(fileObj, swfuploadL10n.default_error);
  300. }
  301. }
  302. function cancelUpload() {
  303. swfu.cancelQueue();
  304. }
  305. // remember the last used image size, alignment and url
  306. jQuery(document).ready(function($){
  307. $('input[type="radio"]', '#media-items').live('click', function(){
  308. var tr = $(this).closest('tr');
  309. if ( $(tr).hasClass('align') )
  310. setUserSetting('align', $(this).val());
  311. else if ( $(tr).hasClass('image-size') )
  312. setUserSetting('imgsize', $(this).val());
  313. });
  314. $('button.button', '#media-items').live('click', function(){
  315. var c = this.className || '';
  316. c = c.match(/url([^ '"]+)/);
  317. if ( c && c[1] ) {
  318. setUserSetting('urlbutton', c[1]);
  319. $(this).siblings('.urlfield').val( $(this).attr('title') );
  320. }
  321. });
  322. });