ACMS.Dispatch.Admin._media=function(elm){var objDropArea=elm;var mediaData=new Array;var objDispArea=document.getElementById("js-media_disp_area");var compiled=_.template($("#js-media_tmpl").html());var dragging=0;if(window.File&&window.FileReader){$("#js-media_browser").remove();objDropArea.addEventListener("drop",function(event){readFiles(event.dataTransfer.files);dragging=0;$(this).removeClass("drag-n-drop-hover");event.stopPropagation();event.preventDefault();return false},false);objDropArea.addEventListener("dragover",function(event){$(this).addClass("drag-n-drop-hover");event.stopPropagation();event.preventDefault();return false},false);objDropArea.addEventListener("dragenter",function(event){dragging++;$(this).addClass("drag-n-drop-hover");event.stopPropagation();event.preventDefault();return false},false);objDropArea.addEventListener("dragleave",function(event){dragging--;if(dragging===0){$(this).removeClass("drag-n-drop-hover")}event.stopPropagation();event.preventDefault();return false},false);document.getElementById("js-media_files").addEventListener("change",function(event){readFiles(event.target.files)},false)}else{$("#js-media_multiple").remove()}$("#js-media_upload").click(function(event){if(mediaData.length>0){_.each(mediaData,function(file,el){var obj=file.media_obj;var size=$("[name=image_size]").val();var fd=new FormData;var tag=$("[name=media_tag_text]").val();if(!tag)tag=$("[name=media_tag_select]").val();if(!tag)tag="";fd.append("file",file);fd.append("size",size);fd.append("tags",tag);fd.append("multi","on");fd.append("ACMS_POST_Media_Upload",true);progressbar(obj,"start");$.ajax({type:"POST",data:fd,processData:false,contentType:false,success:function(html){if(html=="SUCCESS"){progressbar(obj,"success")}else{progressbar(obj,"error")}},error:function(xhr,statusText){progressbar(obj,"error")},complete:function(xhr,statusText){delete mediaData[el]}})});$("[name=media_tag_select]").val("");$("[name=media_tag_text]").val("")}});function readFiles(files){for(var i=0;i<files.length;i++){var f=files[i];if(!f.type.match("image.*")){}var objFileReader=new FileReader;objFileReader.onerror=function(evt){return};if(f.type.match("image.*")){objFileReader.onload=function(theFile){return function(e){theFile.filetype="image";theFile.preview=this.result;rebuild(theFile)}}(f);objFileReader.readAsDataURL(f)}else if(f.type.match("video.*")){return}else if(f.type.match("application.*")||!f.type){objFileReader.onload=function(theFile){return function(e){theFile.filetype="file";rebuild(theFile)}}(f);objFileReader.readAsDataURL(f)}else{}}}function rebuild(data){var mediaHtml=compiled({data:data});var mediaHash=(data.name+data.size+data.lastModifiedDate).replace(/\s+/g,"");var mediaHtml=$(mediaHtml).attr("data-media_id",mediaHash).get(0);$("#js-media_disp_area tbody").append(mediaHtml);data.media_data=mediaHash;data.media_obj=$(mediaHtml);mediaData.push(data);$(".js-media_cancel_btn").unbind("click");$(".js-media_cancel_btn").click(function(event){var self=$(this);var parent=self.parents("tr");var media_id=parent.data("media_id");_.each(mediaData,function(index,el){if(index.media_data==media_id){mediaData.splice(el,1)}});parent.remove()})}function progressbar($obj,$on){var $bar=$obj.find(".acms-admin-progress");var $mess=$obj.find(".acms-admin-progress-mess");if($on=="start"){$bar.show();$mess.text("送信中")}else if($on=="success"){setTimeout(function(){$obj.remove()},500)}else if($on=="error"){setTimeout(function(){$bar.removeClass("acms-admin-active").find(".acms-admin-progress-bar").removeClass("acms-admin-progress-bar-info").addClass("acms-admin-progress-bar-danger");$mess.text("アップロードに失敗しました。ファイルサイズ、パーミッションを確認して下さい。")},500)}}};