define(["ember","text!./image-loader-tmpl.hbs","mm","../../utilities/path.helper"],function(a,b,c,d){var e=function(a){var b=a;return/^\d+$/.test(a)&&(b=a+"px"),b};return a.Component.extend({tagName:"div",layout:a.Handlebars.compile(b),attributeBindings:["style"],style:function(){var a="";return this.get("maxWidth")?a+="width:"+e(this.get("maxWidth"))+";":this.get("width")&&(a+="width:"+e(this.get("width"))+";"),this.get("maxeight")?a+="height:"+e(this.get("maxHeight"))+";":this.get("height")&&(a+="height:"+e(this.get("height"))+";"),a}.property("width","height","maxWidth","maxHeight"),temp:"",resource:function(){return!this.get("thumbq")||!this.get("src")||this.get("src")instanceof Blob?this.get("src")||this.get("default"):"%@@%@w_%@h_%@q.jpg".fmt(this.get("src").replace(/@\d+w_\d+h_\d+q.jpg$/,""),this.get("maxWidth"),this.get("maxHeight"),this.get("thumbq"))}.property("src"),percent:0,status:"loading",mode:function(){var a={};return a[this.get("status")]=!0,a}.property("status"),setDetect:function(){var b=this,c=new Image;return c.onerror=function(){console.log("image-loader ---- error"),a.run.later(function(){b._checkFile(0)},500),a.run.later(function(){b._checkFile(1)},2e3)},c.onload=function(){b.set("status","loaded")},b.set("detect",c),c},_checkFile:function(a){if(!this.get("isDestroying")&&!this.get("isDestroyed")){var b=this;c.request({name:d.getFileNameWithoutExtension(b.get("resource").replace(/@\d+w_\d+h_\d+q.jpg$/,""))},{module:"fileManager",action:"uploadStatus"}).then(function(c){c&&"uploaded"==c.status?b.set("status",a>=1?"loaded":"loading"):!c||"pending"!=c.status&&"uploading"!=c.status?b.set("status","notfound"):(b.set("status","waiting"),a>=1&&b.set("temp",c.tmp))})}},setProcess:function(){var a=this,b=function(b,c){d.getFileNameWithoutExtension(a.get("resource").replace(/@\d+w_\d+h_\d+q.jpg$/,""))==c.name&&("process"==c.status&&(a.set("temp",c.src),0!=c.percent&&a.setProperties({status:"upload",percent:c.percent})),"terminal"==c.status&&a.fetchImg())};a.set("processHandle",b),c.listener.on("upload",b)},disposeDetect:function(){var a=this.get("detect");a&&(a.onload=null,a.onerror=null,a.src="",this.set("detect",null))},disposeProcess:function(){var a=this.get("processHandle");a&&(this.set("processHandle",null),c.listener.off("upload",a))},domReady:function(){this.setDetect(),this.setProcess(),this.fetchImg()}.on("didInsertElement"),fetchImg:function(){var a=this.get("resource");a&&0==a.indexOf("http://")?this.get("detect")&&(this.get("detect").src=a):this.set("status","loaded")},watchRes:function(){this.setProperties({status:"loading",percent:0,temp:""}),this.fetchImg()}.observes("resource"),recycleImg:function(){this.disposeDetect(),this.disposeProcess()}.on("willDestroyElement")})});