!function(t){"function"==typeof define&&define.amd?define(["jquery"],t):t("object"==typeof exports?require("jquery"):jQuery)}(function(t){"use strict";function a(a){this.$container=a,this.$avatarView=this.$container.find(".avatar-view"),this.$avatar=this.$avatarView.find("img"),this.$avatarModal=t("body").find("#avatar-modal"),this.$loading=t("#page-wrapper").find(".loading"),this.$avatarForm=this.$avatarModal.find(".avatar-form"),this.$avatarUpload=this.$avatarForm.find(".avatar-upload"),this.$avatarSrc=this.$avatarForm.find(".avatar-src"),this.$avatarData=this.$avatarForm.find(".avatar-data"),this.$avatarInput=this.$avatarForm.find(".avatar-input"),this.$avatarSave=this.$avatarForm.find(".avatar-save"),this.$avatarBtns=this.$avatarForm.find(".avatar-btns"),this.$avatarWrapper=this.$avatarModal.find(".avatar-wrapper"),this.$avatarPreview=this.$avatarModal.find(".avatar-preview"),this.init()}var i=window.console||{log:function(){}};a.prototype={constructor:a,support:{fileList:!!t('<input type="file">').prop("files"),blobURLs:!!window.URL&&URL.createObjectURL,formData:!!window.FormData},init:function(){this.support.datauri=this.support.fileList&&this.support.blobURLs,this.support.formData||this.initIframe(),this.initTooltip(),this.initModal(),this.addListener()},addListener:function(){this.$avatarView.on("click",t.proxy(this.click,this)),this.$avatarInput.on("change",t.proxy(this.change,this)),this.$avatarForm.on("submit",t.proxy(this.submit,this)),this.$avatarBtns.on("click",t.proxy(this.rotate,this))},initTooltip:function(){this.$avatarView.tooltip({placement:"bottom"})},initModal:function(){this.$avatarModal.modal({show:!1})},initPreview:function(){var t=this.$avatar.attr("src");this.$avatarPreview.empty().html('<img src="'+t+'">')},initIframe:function(){var a="upload-iframe-"+(new Date).getTime(),r=t("<iframe>").attr({name:a,src:""}),s=this;r.one("load",function(){r.on("load",function(){var a;try{a=t(this).contents().find("body").text()}catch(t){i.log(t.message)}if(a){try{a=t.parseJSON(a)}catch(t){i.log(t.message)}s.submitDone(a)}else s.submitFail("Image upload failed!");s.submitEnd()})}),this.$iframe=r,this.$avatarForm.attr("target",a).after(r.hide())},click:function(){this.$avatarModal.modal("show"),this.initPreview()},change:function(){var t,a;this.support.datauri?(t=this.$avatarInput.prop("files"),t.length>0&&(a=t[0],this.isImageFile(a)&&(this.url&&URL.revokeObjectURL(this.url),this.url=URL.createObjectURL(a),this.startCropper()))):(a=this.$avatarInput.val(),this.isImageFile(a)&&this.syncUpload())},submit:function(){return!(!this.$avatarSrc.val()&&!this.$avatarInput.val())&&(this.support.formData?(this.ajaxUpload(),!1):void 0)},rotate:function(a){var i;this.active&&(i=t(a.target).data(),i.method&&this.$img.cropper(i.method,i.option))},isImageFile:function(t){return t.type?/^image\/\w+$/.test(t.type):/\.(jpg|jpeg|png|gif)$/.test(t)},startCropper:function(){var a=this;this.active?this.$img.cropper("replace",this.url):(this.$img=t('<img src="'+this.url+'">'),this.$avatarWrapper.empty().html(this.$img),this.$img.cropper({aspectRatio:1,preview:this.$avatarPreview.selector,strict:!1,crop:function(t){var i=['{"x":'+t.x,'"y":'+t.y,'"height":'+t.height,'"width":'+t.width,'"rotate":'+t.rotate+"}"].join();a.$avatarData.val(i)}}),this.active=!0)},stopCropper:function(){this.active&&(this.$img.cropper("destroy"),this.$img.remove(),this.active=!1)},ajaxUpload:function(){var a=this.$avatarForm.attr("action"),i=new FormData(this.$avatarForm[0]),r=this;t.ajax(a,{headers:{"X-XSRF-TOKEN":t('meta[name="csrf-token"]').attr("content")},type:"post",data:i,dataType:"json",processData:!1,contentType:!1,beforeSend:function(){r.submitStart()},success:function(t){r.submitDone(t)},error:function(t,a,i){r.submitFail(a||i)},complete:function(){r.submitEnd()}})},syncUpload:function(){this.$avatarSave.click()},submitStart:function(){this.$loading.fadeIn()},submitDone:function(a){t.isPlainObject(a)?a.result?(this.url=a.result,this.support.datauri||this.uploaded?(this.uploaded=!1,this.cropDone()):(this.uploaded=!0,this.$avatarSrc.val(this.url),this.startCropper()),this.$avatarInput.val("")):a.message&&this.alert(a.message):this.alert("Failed to response")},submitFail:function(t){this.alert(t)},submitEnd:function(){this.$loading.fadeOut()},cropDone:function(){this.$avatarForm.get(0).reset(),this.$avatar.attr("src",this.url),this.stopCropper(),this.$avatarModal.modal("hide")},alert:function(t){var a=['<div class="alert alert-danger avater-alert">','<button type="button" class="close" data-dismiss="alert">&times;</button>',t,"</div>"].join("");this.$avatarUpload.after(a)}},t(function(){return new a(t("#crop-avatar"))})});