"use strict";function getData(){$.ajax({url:"/admin/getCategories",type:"Post",dataType:"json",success:function(t){$("#cate-list").empty(),$.each(t,function(t,a){$('<li class="list-group-item" data-uniqueid="'+a._id+'" data-catename="'+a.CateName+'" data-alias="'+a.Alias+'" data-img="'+a.Img+'" data-link="'+a.Link+'"><div class="row"><div class="col-md-2"><i class="fa fa-arrows"></i> <span class="fileinput-button"><img src="'+a.Img+'"/><input type="file" class="fileupload" name="file"></span></div><div class="col-md-2"><input class="form-control txtName" type="text" value="'+a.CateName+'" placeholder="分类名称"/></div><div class="col-md-2"><input class="form-control txtAlias" type="text" value="'+a.Alias+'" placeholder="分类alias"/></div><div class="col-md-5"><div class="input-group cate-link"><span class="input-group-addon"><label><input type="checkbox" '+(""!=a.Link?'checked="checked"':"")+'/> 链接</label></span><input type="text" class="form-control txtLink" value="'+a.Link+'" '+(""==a.Link?'disabled="disabled"':"")+'></div></div><div class="col-md-1"><button class="btn btn-link btn-del-cate" title="移除分类"><i class="fa fa-times"></i></button></div></div></li>').appendTo($("#cate-list"))})},complete:function(){bindFileUpload()}})}function bindFileUpload(){$(".fileupload").fileupload({url:"/admin/uploadimg",dataType:"text",done:function(t,a){$(this).prev().attr("src","/static/images/"+a.files[0].name),$(this).parent().parent().parent().parent().data("img","/static/images/"+a.files[0].name)}})}function isValidData(){for(var t=!0,a=$("#cate-list li"),i=0;i<a.length;i++){var e=$(a[i]).data("catename"),s=$(a[i]).data("alias");if(""===e||""===s){swal({title:"分类名称、分类alias都不能为空！",type:"warning",showConfirmButton:!1,timer:2e3}),t=!1;break}if(1<$("#cate-list li[data-catename='"+e+"']").length){swal({title:'分类名称 "'+e+'" 不唯一！',type:"warning",showConfirmButton:!1,timer:2e3}),t=!1;break}if(1<$("#cate-list li[data-alias='"+s+"']").length){swal({title:'分类alias "'+s+'" 不唯一！',type:"warning",showConfirmButton:!1,timer:2e3}),t=!1;break}}return t}$(function(){var l;getData();var e=$("#cate-list").sortable({group:"cate-list",handle:"i.fa-arrows",pullPlaceholder:!1,onDrop:function(t,a,i){var e=$("<li/>").css({height:0});t.before(e),e.animate({height:t.height()}),t.animate(e.position(),function(){e.detach(),i(t,a)})},onDragStart:function(t,a,i){var e=t.offset(),s=a.rootGroup.pointer;l={left:s.left-e.left,top:s.top-e.top},i(t,a)},onDrag:function(t,a){t.css({left:a.left-l.left,top:a.top-l.top})}});$(document).on({change:function(){var t=$(this).parent().parent().next("input");$(this).prop("checked")?(t.removeAttr("disabled"),$(this).parents(".list-group-item").data("link",t.val())):(t.attr("disabled","disabled"),$(this).parents(".list-group-item").data("link",""))}},"input:checkbox"),$(document).on({change:function(t){var a=$(this).val();$(t.target).hasClass("txtName")?($(this).parents(".list-group-item").data("catename",a),$(this).parents(".list-group-item").attr("data-catename",a)):$(t.target).hasClass("txtAlias")?($(this).parents(".list-group-item").data("alias",a),$(this).parents(".list-group-item").attr("data-alias",a)):($(this).parents(".list-group-item").data("link",a),$(this).parents(".list-group-item").attr("data-link",a))}},".txtName, .txtAlias, .txtLink"),$(document).on({click:function(){$(this).parents(".list-group-item").slideUp("normal",function(){$(this).remove()})}},".btn-del-cate"),$(document).on({click:function(){$('<li class="list-group-item" data-uniqueid="" data-catename="" data-alias="" data-img="" data-link=""><div class="row"><div class="col-md-2"><i class="fa fa-arrows"></i> <span class="fileinput-button"><img src=""/><input type="file" class="fileupload" name="file"></span></div><div class="col-md-2"><input class="form-control txtName" type="text" value="" placeholder="分类名称"/></div><div class="col-md-2"><input class="form-control txtAlias" type="text" value="" placeholder="分类alias"/></div><div class="col-md-5"><div class="input-group cate-link"><span class="input-group-addon"><label><input type="checkbox"/> 链接</label></span><input type="text" class="form-control txtLink" value="" disabled="disabled"></div></div><div class="col-md-1"><button class="btn btn-link btn-del-cate" title="移除分类"><i class="fa fa-times"></i></button></div></div></li>').appendTo($("#cate-list")),bindFileUpload()}},"#btnNew"),$(document).on({click:function(){if(isValidData()){var t=$(this);t.attr("disabled","disabled"),t.find(".fa").removeClass("fa-cloud-upload").addClass("fa-circle-o-notch fa-spin");var a=e.sortable("serialize").get(),i=JSON.stringify(a);$.ajax({url:"/admin/saveCategories",type:"Post",data:{json:i},success:function(){swal({title:"保存成功！",type:"success",showConfirmButton:!1,timer:2e3})},error:function(){swal({title:"操作失败！",type:"error",showConfirmButton:!1,timer:2e3})},complete:function(){t.removeAttr("disabled"),t.find(".fa").removeClass("fa-circle-o-notch fa-spin").addClass("fa-cloud-upload"),getData()}})}}},"#btnSave")});