Wordpress/wp-includes/js/plupload/handlers.js
2012-02-24 02:00:58 +00:00

1 line
12 KiB
JavaScript

var topWin=window.dialogArguments||opener||parent||top,uploader,uploader_init;function fileDialogStart(){jQuery("#media-upload-error").empty()}function fileQueued(b){jQuery(".media-blank").remove();var a=jQuery("#media-items").children(),c=post_id||0;if(a.length==1){a.removeClass("open").find(".slidetoggle").slideUp(200)}jQuery("#media-items").append('<div id="media-item-'+b.id+'" class="media-item child-of-'+c+'"><div class="progress"><div class="percent">0%</div><div class="bar"></div></div><div class="filename original"> '+b.name+"</div></div>");jQuery("#insert-gallery").prop("disabled",true)}function uploadStart(){try{if(typeof topWin.tb_remove!="undefined"){topWin.jQuery("#TB_overlay").unbind("click",topWin.tb_remove)}}catch(a){}return true}function uploadProgress(a,b){var c=jQuery("#media-item-"+b.id);jQuery(".bar",c).width((200*b.loaded)/b.size);jQuery(".percent",c).html(b.percent+"%")}function fileUploading(c,d){var b=100*1024*1024,a=parseInt(c.settings.max_file_size,10);if(a>b&&d.size>b){setTimeout(function(){if(d.status==2&&d.loaded==0){wpFileError(d,pluploadL10n.big_upload_failed.replace("%1$s",'<a class="uploader-html" href="#">').replace("%2$s","</a>"));if(c.current&&c.current.file.id==d.id&&c.current.xhr.abort){c.current.xhr.abort()}}},10000)}}function updateMediaForm(){var a=jQuery("#media-items").children();if(a.length==1){a.addClass("open").find(".slidetoggle").show();jQuery(".insert-gallery").hide()}else{if(a.length>1){a.removeClass("open");jQuery(".insert-gallery").show()}}if(a.not(".media-blank").length>0){jQuery(".savebutton").show()}else{jQuery(".savebutton").hide()}}function uploadSuccess(c,a){var b=jQuery("#media-item-"+c.id);a=a.replace(/^<pre>(\d+)<\/pre>$/,"$1");if(a.match("media-upload-error")){b.html(a);return}else{jQuery(".percent",b).html(pluploadL10n.crunching)}prepareMediaItem(c,a);updateMediaForm();if(post_id&&b.hasClass("child-of-"+post_id)){jQuery("#attachments-count").text(1*jQuery("#attachments-count").text()+1)}}function setResize(a){if(a){if(uploader.features.jpgresize){uploader.settings.resize={width:resize_width,height:resize_height,quality:100}}else{uploader.settings.multipart_params.image_resize=true}}else{delete (uploader.settings.resize);delete (uploader.settings.multipart_params.image_resize)}}function prepareMediaItem(c,a){var d=(typeof shortform=="undefined")?1:2,b=jQuery("#media-item-"+c.id);try{if(typeof topWin.tb_remove!="undefined"){topWin.jQuery("#TB_overlay").click(topWin.tb_remove)}}catch(g){}if(isNaN(a)||!a){b.append(a);prepareMediaItemInit(c)}else{b.load("async-upload.php",{attachment_id:a,fetch:d},function(){prepareMediaItemInit(c);updateMediaForm()})}}function prepareMediaItemInit(b){var a=jQuery("#media-item-"+b.id);jQuery(".thumbnail",a).clone().attr("class","pinkynail toggle").prependTo(a);jQuery(".filename.original",a).replaceWith(jQuery(".filename.new",a));jQuery("a.delete",a).click(function(){jQuery.ajax({url:"admin-ajax.php",type:"post",success:deleteSuccess,error:deleteError,id:b.id,data:{id:this.id.replace(/[^0-9]/g,""),action:"trash-post",_ajax_nonce:this.href.replace(/^.*wpnonce=/,"")}});return false});jQuery("a.undo",a).click(function(){jQuery.ajax({url:"admin-ajax.php",type:"post",id:b.id,data:{id:this.id.replace(/[^0-9]/g,""),action:"untrash-post",_ajax_nonce:this.href.replace(/^.*wpnonce=/,"")},success:function(d,e){var c=jQuery("#media-item-"+b.id);if(type=jQuery("#type-of-"+b.id).val()){jQuery("#"+type+"-counter").text(jQuery("#"+type+"-counter").text()-0+1)}if(post_id&&c.hasClass("child-of-"+post_id)){jQuery("#attachments-count").text(jQuery("#attachments-count").text()-0+1)}jQuery(".filename .trashnotice",c).remove();jQuery(".filename .title",c).css("font-weight","normal");jQuery("a.undo",c).addClass("hidden");jQuery(".menu_order_input",c).show();c.css({backgroundColor:"#ceb"}).animate({backgroundColor:"#fff"},{queue:false,duration:500,complete:function(){jQuery(this).css({backgroundColor:""})}}).removeClass("undo")}});return false});jQuery("#media-item-"+b.id+".startopen").removeClass("startopen").addClass("open").find("slidetoggle").fadeIn()}function wpQueueError(a){jQuery("#media-upload-error").show().html('<div class="error"><p>'+a+"</p></div>")}function wpFileError(b,a){itemAjaxError(b.id,a)}function itemAjaxError(e,c){var b=jQuery("#media-item-"+e),a=b.find(".filename").text(),d=b.data("last-err");if(d==e){return}b.html('<div class="error-div"><a class="dismiss" href="#">'+pluploadL10n.dismiss+"</a><strong>"+pluploadL10n.error_uploading.replace("%s",jQuery.trim(a))+"</strong> "+c+"</div>").data("last-err",e)}function deleteSuccess(b,d){if(b=="-1"){return itemAjaxError(this.id,"You do not have permission. Has your session expired?")}if(b=="0"){return itemAjaxError(this.id,"Could not be deleted. Has it been deleted already?")}var c=this.id,a=jQuery("#media-item-"+c);if(type=jQuery("#type-of-"+c).val()){jQuery("#"+type+"-counter").text(jQuery("#"+type+"-counter").text()-1)}if(post_id&&a.hasClass("child-of-"+post_id)){jQuery("#attachments-count").text(jQuery("#attachments-count").text()-1)}if(jQuery("form.type-form #media-items").children().length==1&&jQuery(".hidden","#media-items").length>0){jQuery(".toggle").toggle();jQuery(".slidetoggle").slideUp(200).siblings().removeClass("hidden")}jQuery(".toggle",a).toggle();jQuery(".slidetoggle",a).slideUp(200).siblings().removeClass("hidden");a.css({backgroundColor:"#faa"}).animate({backgroundColor:"#f4f4f4"},{queue:false,duration:500}).addClass("undo");jQuery(".filename:empty",a).remove();jQuery(".filename .title",a).css("font-weight","bold");jQuery(".filename",a).append('<span class="trashnotice"> '+pluploadL10n.deleted+" </span>").siblings("a.toggle").hide();jQuery(".filename",a).append(jQuery("a.undo",a).removeClass("hidden"));jQuery(".menu_order_input",a).hide();return}function deleteError(c,b,a){}function uploadComplete(){jQuery("#insert-gallery").prop("disabled",false)}function switchUploader(a){if(a){deleteUserSetting("uploader");jQuery(".media-upload-form").removeClass("html-uploader");if(typeof(uploader)=="object"){uploader.refresh()}}else{setUserSetting("uploader","1");jQuery(".media-upload-form").addClass("html-uploader")}}function dndHelper(a){var b=document.getElementById("dnd-helper");if(a){b.style.display="block"}else{b.style.display="none"}}function uploadError(d,f,c,e){var b=100*1024*1024,a;switch(f){case plupload.FAILED:wpFileError(d,pluploadL10n.upload_failed);break;case plupload.FILE_EXTENSION_ERROR:wpFileError(d,pluploadL10n.invalid_filetype);break;case plupload.FILE_SIZE_ERROR:uploadSizeError(e,d);break;case plupload.IMAGE_FORMAT_ERROR:wpFileError(d,pluploadL10n.not_an_image);break;case plupload.IMAGE_MEMORY_ERROR:wpFileError(d,pluploadL10n.image_memory_exceeded);break;case plupload.IMAGE_DIMENSIONS_ERROR:wpFileError(d,pluploadL10n.image_dimensions_exceeded);break;case plupload.GENERIC_ERROR:wpQueueError(pluploadL10n.upload_failed);break;case plupload.IO_ERROR:a=parseInt(e.settings.max_file_size,10);if(a>b&&d.size>b){wpFileError(d,pluploadL10n.big_upload_failed.replace("%1$s",'<a class="uploader-html" href="#">').replace("%2$s","</a>"))}else{wpQueueError(pluploadL10n.io_error)}break;case plupload.HTTP_ERROR:wpQueueError(pluploadL10n.http_error);break;case plupload.INIT_ERROR:jQuery(".media-upload-form").addClass("html-uploader");break;case plupload.SECURITY_ERROR:wpQueueError(pluploadL10n.security_error);break;default:wpFileError(d,pluploadL10n.default_error)}}function uploadSizeError(a,b,d){var c;if(d){c=pluploadL10n.big_upload_queued.replace("%s",b.name)+" "+pluploadL10n.big_upload_failed.replace("%1$s",'<a class="uploader-html" href="#">').replace("%2$s","</a>")}else{c=pluploadL10n.file_exceeds_size_limit.replace("%s",b.name)}jQuery("#media-items").append('<div id="media-item-'+b.id+'" class="media-item error"><p>'+c+"</p></div>");a.removeFile(b)}jQuery(document).ready(function(c){var b,a;c(".media-upload-form").bind("click.uploader",function(i){var j=c(i.target),l,k,g,m,f,n,h,d;if(j.hasClass("caption-insert-link")){g=j.siblings("div.caption-insert-link-wrap"),m=j.parent().siblings("textarea").get(0);if(document.selection){m.focus();f=document.selection.createRange();a=f.getBookmark();if(f.text){g.find(".caption-insert-link-text").val(f.text)}}else{if(m.selectionStart||m.selectionStart=="0"){n=m.value;h=m.selectionStart;d=m.selectionEnd;if(h!=d){g.find(".caption-insert-link-text").val(n.substring(h,d))}}}j.hide();g.show();g.find(".caption-insert-link-url").focus()}else{if(j.hasClass("caption-cancel")||j.hasClass("caption-save")){g=j.closest("div.caption-insert-link-wrap");if(j.hasClass("caption-save")){b(g.closest(".edit-caption-controls").siblings("textarea"),g)}g.hide();g.siblings(".caption-insert-link").show()}else{if(j.is('input[type="radio"]')){l=j.closest("tr");if(c(l).hasClass("align")){setUserSetting("align",j.val())}else{if(c(l).hasClass("image-size")){setUserSetting("imgsize",j.val())}}}else{if(j.is("button.button")){k=i.target.className||"";k=k.match(/url([^ '"]+)/);if(k&&k[1]){setUserSetting("urlbutton",k[1]);j.siblings(".urlfield").val(j.data("link-url"))}}else{if(j.is("a.dismiss")){j.parents(".media-item").fadeOut(200,function(){c(this).remove()})}else{if(j.is(".upload-flash-bypass a")||j.is("a.uploader-html")){c("#media-items, p.submit, span.big-file-warning").css("display","none");switchUploader(0);i.preventDefault()}else{if(j.is(".upload-html-bypass a")){c("#media-items, p.submit, span.big-file-warning").css("display","");switchUploader(1);i.preventDefault()}else{if(j.is("a.describe-toggle-on")){j.parent().addClass("open");j.siblings(".slidetoggle").fadeIn(250,function(){var p=c(window).scrollTop(),o=c(window).height(),r=c(this).offset().top,q=c(this).height(),e,s;if(o&&r&&q){e=r+q;s=p+o;if(e>s){if(e-s<r-p){window.scrollBy(0,(e-s)+10)}else{window.scrollBy(0,r-p-40)}}}});i.preventDefault()}else{if(j.is("a.describe-toggle-off")){j.siblings(".slidetoggle").fadeOut(250,function(){j.parent().removeClass("open")});i.preventDefault()}}}}}}}}}});b=function(l,k){var h,i,j,f,g,m,e=k.find(".caption-insert-link-url"),d=k.find(".caption-insert-link-text");if(!e.length||!d.length){return}l=l.get(0);i="<a href='"+e.val()+"'>"+d.val()+"</a>";if(document.selection){l.focus();h=document.selection.createRange();if(a){h.moveToBookmark(a);a=""}h.text=i;l.focus()}else{if(l.selectionStart||l.selectionStart=="0"){m=l.value;j=l.selectionStart;f=l.selectionEnd;g=l.scrollTop;l.value=m.substring(0,j)+i+m.substring(f,m.length);l.focus();l.selectionStart=j+i.length;l.selectionEnd=j+i.length;l.scrollTop=g}}e.val("");d.val("")};uploader_init=function(){uploader=new plupload.Uploader(wpUploaderInit);c("#image_resize").bind("change",function(){var d=c(this).prop("checked");setResize(d);if(d){setUserSetting("upload_resize","1")}else{deleteUserSetting("upload_resize")}});uploader.bind("Init",function(d){var e=c("#plupload-upload-ui");setResize(getUserSetting("upload_resize",false));if(d.features.dragdrop){e.addClass("drag-drop");c("#drag-drop-area").bind("dragover.wp-uploader",function(){e.addClass("drag-over")}).bind("dragleave.wp-uploader, drop.wp-uploader",function(){e.removeClass("drag-over")})}else{e.removeClass("drag-drop");c("#drag-drop-area").unbind(".wp-uploader")}});uploader.init();uploader.bind("FilesAdded",function(f,g){var e=100*1024*1024,d=parseInt(f.settings.max_file_size,10);c("#media-upload-error").html("");uploadStart();plupload.each(g,function(h){if(d>e&&h.size>e&&f.runtime!="html5"){uploadSizeError(f,h,true)}else{fileQueued(h)}});f.refresh();f.start()});uploader.bind("BeforeUpload",function(d,e){});uploader.bind("UploadFile",function(d,e){fileUploading(d,e)});uploader.bind("UploadProgress",function(d,e){uploadProgress(d,e)});uploader.bind("Error",function(d,e){uploadError(e.file,e.code,e.message,d);d.refresh()});uploader.bind("FileUploaded",function(d,f,e){uploadSuccess(f,e.response)});uploader.bind("UploadComplete",function(d,e){uploadComplete()})};if(typeof(wpUploaderInit)=="object"){uploader_init()}});