contentEdit.js 9.82 KB
var $ = require('jquery'),
    common = require('../common/common');
    // module;
var ENUM={};
// common.util.__ajax({
// 	url:'/guang/article/getEnumList',
// 	async:false
// },function(res){
// 	ENUM=res.data;
// });
// console.log(ENUM);

var edit=new common.edit(".modal-body",{bucket:"goodsimg"});

var Bll={
	Brands:[],
	Brands1:{},
	Brdata:[],
	getBrands:function(){
		var Brand={};
		$.get("/ajax/yohosearch",function(res){
			res.data.forEach(function(item,index){
				var brandAlif=(item.brand_alif||"").toUpperCase();
				if(/^[0-9]$/.test(item.brand_alif)){
					brandAlif="0-9";
				}
				if(/^\W$/.test(item.brand_alif)){
					brandAlif="#";
				}
				Brand[brandAlif]=Brand[brandAlif]||[];
				Brand[brandAlif].push(item);
				Bll.Brands1[item.id]=item;
			});
			for(var i in Brand){
				Bll.Brands.push({name:i,items:Brand[i]});
			}
		});
	},
	renderBrandPic:function(Brdata){
		var Brands2=[];
		Brdata.forEach(function(item,index){
			var a=Bll.Brands1[item.id]
			//图片的绝对路径是多少
			a.brand_ico=common.util.__joinImg("brandLogo",a.brand_ico)
			Brands2.push(a);
		});
		$("#addBrands").parent("div").parent("li").prevAll().remove();
		$("#addPic").prepend(common.util.__template2($("#template3").html(),{Brands:Brands2}));
	},
	contentDatas:[],
	module:null,
	toast:function(index,module){
		var d=new common.dialog({
			title:(!!~index?"修改":"添加")+module.template_intro,
			content:common.util.__template2($("#"+module.dialog).html(),module),
			width:'70%',
			button:[
				{
		            value: "保存",
		            callback: function(){
		            	if(edit.validate()){
		            		//TODO
		            		!!~index?Bll.contentDatas[index]=module:Bll.contentDatas.push(module);
			            	Bll.rendContent();
			            	console.log(Bll.contentDatas);

		            		d.close();
		            	}
		            	
		            	return false;
		            },
		            css: "btn-primary"
				}
			]
		});
		edit.init();
		edit.on("callback", function(obj) {
		if(/^file_onComplete/.test(obj.key)){
			//(!!~index?index:0)
			var _field=obj.key.replace(/^file_onComplete_file__/,'').split('-');
			Bll.module.data[_field[1]].src=obj.data;
		}
	});
	},
	rendContent:function(){
		$("#add-content").html(common.util.__template2($("#template_content").html(),{modules:Bll.contentDatas}));
	},
	__render:function(selecter,templater,data){
		$(selecter).html(common.util.__template2($("#"+templater).html(),data) );
	},
	Tags:[]
}
//获取品牌
Bll.getBrands();

$("#panel-body").html(common.util.__template2($("#tempalte1").html(),ENUM));

new common.dropDown({el:"#authorId",ajax:"guangGetAuthorList"});
new common.dropDown({el:"#maxSortId",ajax:"guangGetSortList",params:function(){
	return {pid:0};
}});
new common.dropDown({el:"#minSortId",ajax:"guangGetSortList",params:function(){
	return {pid:$("#maxSortId").val()};
}});

var g= new common.grid({
	el:"#gird",
	columns:[
		{display:"选择","type":"checkbox"},
		{display:"标签名",name:"tagName"}
	]
});

$(document).on("click","#addUrl",function(){
	common.dialog.confirm("添加链接",common.util.__template2($("#template2").html(),{}),function(){
		$("#url").val(JSON.stringify({
			action:'go.'+$("#goTo").val(),
			url:$("#goToUrl").val()
		}));
	});
});

$(document).on("click","#TagsSelectBtn",function(){
	common.dialog.confirm("选择标签","<div id='gird'></div>",function(){

		$("#taglist").html(common.util.__template2($("#template4").html(),{tags:g.selected}));
	});
	g.init('/guang/tags/getList');
});
$(document).on("click",".tag a",function(){
	g.selected.splice($(this).data("index"),1);
	
	$("#taglist").html(common.util.__template2($("#template4").html(),{tags:g.selected}));
	return false;
});

$(document).on("click","#addBrands",function(){
	///goods/brands/queryBrandsByStatus
	var e=new common.edit("#brandForm");
	common.dialog.confirm("选择品牌",common.util.__template2($("#template5").html(),{Brands:Bll.Brands,Brdata:Bll.Brdata.join('|')}),function(){
		//todo
		Bll.Brdata=$("#brandCheckBox").val().split('|');
		Bll.Brdata=Bll.Brdata.map(function(item,index){
			return {
				 "brandCategory": "2",
				  "id": item
			}
		});
		Bll.renderBrandPic(Bll.Brdata);
	});
	e.init();
});
$(document).on("click",".remove1",function(){
	Bll.Brdata.splice($(this).data("index"),1);
	Bll.renderBrandPic(Bll.Brdata);
});



var Button=[
	{
		template_intro:"文本",
		dialog:"template_dialog_text",
		data:{text:""},
		template_name:"text"
	},
	{
		template_intro:"一张大图",
		dialog:"template_dialog_grid",
		template_name:"singleImage",
		data:[{
	            "url": "",
	            "alt": "",
	            "src": "",
	            "imgId": ""
	    }]
	},
	{
		template_intro:"两张小图",
		dialog:"template_dialog_grid",
		template_name:"smallPic",
		data:[{
	            "url": "",
	            "alt": "",
	            "src": "",
	            "imgId": ""
	    },{
	            "url": "",
	            "alt": "",
	            "src": "",
	            "imgId": ""
	    }]
	},
	{
		template_intro:"添加商品",
		dialog:"template_dialog_goods",
		template_name:"goods",
		data:[
			// {
			// 	src:"",
			// 	id:""
			// }
		]
	},
	{
		template_intro:"推荐商品组",
		dialog:"template_dialog_remgoods",
		template_name:"goodsGroup",
		data:[
			{
				cover:{cover:"",maxSortId:""},
				list:[
					// {
					// 	src:"",
					// 	id:"",
					// 	maxSortId:""
					// }
				]
			}
		]
	},
	{
		template_intro:"一个连接",
		dialog:"template_dialog_link",
		template_name:"link",
		data:[
			{"url":{"action":"","url":""}}
		]
	}
];

$("#content_btns").html(common.util.__template2($("#template_content_btns").html(),{btns:Button}));


$(document).on("click",".add_btn",function(){
	var item=Button[$(this).data("index")];
	Bll.module=$.extend(true,{},item);
	Bll.toast(-1,Bll.module);
});
$(document).on("click",".del",function(){
	Bll.contentDatas.splice($(this).data("index"),1);
	Bll.rendContent();
});

$(document).on("change",".observe",function(){
	var $this=$(this);
	var name=$this.data("field"),index=$this.data("index");
	var module=(index==undefined)?Bll.module.data:Bll.module.data[index];
	common.util.__buildobj(name,'.',module,function(obj,name){
		obj[name]=$this.val();
	});
});
$(document).on("click",".edit",function(){
	var index=$(this).data("index");
	var item=Bll.contentDatas[index];
	console.log(Bll.contentDatas);
	Bll.module=$.extend(true,{},item);
	Bll.toast(index,Bll.module);
});

/*添加商品*/
var goodsgird=new common.grid({
	el:'#goodsgird',
	parms:function(){
		return {};
	},
	columns:[
		{display:"选择",type:"checkbox"},
		{display:"产品图片",render:function(item){
			item.default_images=common.util.__joinImg("goodsimg",item.default_images)
			return "<img width=120 height=60 src='"+item.default_images+"'/>";
		}},
		{display:"产品名称",name:"product_name"},
		{display:"品牌",name:"brand_name"},
		{display:"现价",name:"sales_price"},
		{display:"牌价",name:"market_price"},
		{display:"预售",name:"stock_number"},
		{display:"库存",name:"storage_num"}
	]
});
$(document).on("click","#goodsSelectBtn",function(){
	new common.dialog({
		title:"选择商品",
		content:$("#template_dialog_goodsgird").html(),
		width:'80%',
		button:[
				{
		            value: "确定",
		            callback: function(){
		            	Bll.module.data=goodsgird.selected.map(function(item,index){
		            		return {
		            			src:item.default_images,
		            			id:item.product_id
		            		}
		            	});
		            	// Bll.module.data=goodsgird.selected;
		            	Bll.__render("#goodspic","template_dialog_goodsimgs",{datas:Bll.module.data});
		            },
		            css: "btn-primary"
				}
			]
	});
	goodsgird.init('/yohosearch/search');
});
$(document).on("click",".removepic",function(){
	Bll.module.data.splice($(this).data("index"),1);
	$("#goodspic").html(common.util.__template2($("#template_dialog_goodsimgs").html(),{datas:Bll.module.data}) );
});

/*添加商品组*/
$(document).on("click","#goodsaddBtn",function(){
	Bll.module.data.push(Button[4].data[0]);
	console.log(Bll.module.data);
	Bll.__render("#groupsgoods","template_dialog_remgoodsgroup",Bll.module);
});

$(document).on("click",".goodsSelectBtn",function(){
	var index=$(this).data("index");
	new common.dialog({
		title:"选择商品",
		content:$("#template_dialog_goodsgird").html(),
		width:'80%',
		button:[
				{
		            value: "确定",
		            callback: function(){
		            	goodsgird.selected.forEach(function(item,i){
		            		Bll.module.data[index].list[i]={
		            			src:item.default_images,
		            			id:item.product_id,
		            			maxSortId:1
		            		}
		            		if(!i){
		            			Bll.module.data[index].cover={
			            			cover:item.default_images,
			            			maxSortId:item.product_id
			            		};
		            		}
		            	});
		            	Bll.__render("#groupsgoods","template_dialog_remgoodsgroup",Bll.module);
		            },
		            css: "btn-primary"
				}
			]
	});
	goodsgird.init('/yohosearch/search');
});






/********************************/
var e=new common.edit("#panel-body",{bucket:"article"});
e.init();

$(document).on("click","#submit",function(){
	e.submit($("#panel-body").attr("action"),function(option){
		
		option.data.contentData=[];
		Bll.contentDatas.forEach(function(item,index){
			var data=common.util.__ArrayToObj({
				data:item.data,
				template_name:item.template_name,
				template_intro:item.template_intro
			});
			
			option.data.contentData.push({
				"templateKey":item.template_name,
				"contentData":data
			});

		});
		console.log("Bll.contentDatas",Bll.Brdata);
		option.data.contentData=JSON.stringify(option.data.contentData);
		option.data.brands=JSON.stringify(Bll.Brdata);
		g.selected.forEach(function(item,index){
			Bll.Tags.push(item.id);
		});
		option.data.tag=Bll.Tags.join(',');

		console.log("submit",option.data);
	});
});