resourceContentManage.js 6.26 KB
var $ = require('jquery'),
    common = require('../common/common');

var Button=require('./partials/Button1');

/*获取数据*/
var ViewModel={};
var param = location.href.substring(location.href.lastIndexOf("/") + 1);
common.util.__ajax({
    url: "/resource/content/resContentIndex",
    data: {id: param},
    async:false
},function(res) {
    ViewModel = res.data;
},true);

/*配置模块*/
var edit = new common.edit(".modal-body", {
    bucket: "goodsimg"
});

var Bll={
    __render:function(selecter,templater,data){
        $(selecter).html(common.util.__template2($("#"+templater).html(),data) );
    },
    contentDatas:[],
    module: null,
    toast: function(index, module) {
        var d = new common.dialog({
            title: (!!~index ? "修改" : "添加") + module.contentData.template_intro,
            content: common.util.__template2($("#" + module.contentData.dialog).html(), module),
            width: '70%',
            button: [{
                value: "保存",
                callback: function() {
                    if (edit.validate()) {
                        //TODO
                        !!~index ? Bll.contentDatas[index] = module : Bll.contentDatas.push(module);
                        Bll.__render("#add-content","template_content",{modules:Bll.contentDatas});
                        console.log(Bll.contentDatas);

                        d.close();
                    }

                    return false;
                },
                css: "btn-primary"
            }]
        });
        Bll.__editRender();
    },
    renderDialog:function(templater){
        Bll.__render(".modal-body",templater,Bll.module);
        Bll.__editRender();
    },
    __editRender:function(){
        edit.init();
        edit.on("callback", function(obj) {
            if (/^file_onComplete/.test(obj.key)) {
                var names=obj.field;
                Bll.module.contentData.data=common.util.__buildobj(names, '.', Bll.module.contentData.data, function(o, name) {
                    o[name] = obj.data;
                });
                console.log(Bll.module.contentData.data);

            }
        });
    }
}



/*第一步,基础模板*/
Bll.__render("#content-list","content-template",ViewModel);
Bll.__render(".contents","template_content_btns",{btns:Button});

/*第二部,把楼层数据转化成数组*/
ViewModel.contentData.forEach(function(item,index){
    item.contentData=common.util.__ObjToArray(JSON.parse(item.contentData));
    Bll.contentDatas.push(item);
});
console.log("Bll.contentDatas",Bll.contentDatas);
//console.log(Bll.contentDatas);

/*第三部解析楼层*/
Bll.__render("#add-content","template_content",{modules:Bll.contentDatas});


/*第四部 操作按钮  添加 删除 修改*/
$(document).on("click", ".add_btn", function() {
    var item = Button[$(this).data("index")];
    Bll.module={};
    Bll.module.contentData = $.extend(true, {}, item);
    Bll.toast(-1, Bll.module);
});
//$(document).on("click", ".del", function() {
//    Bll.contentDatas.splice($(this).data("index"), 1);
//    Bll.__render("#add-content","template_content",{modules:Bll.contentDatas});
//});
$(document).on("click", ".del", function () {//删除
    var index = $(this).data("index");
    console.log(Bll.contentDatas);
    common.dialog.confirm("警告",
        common.util.__template2("是否确认删除?", {}),
        function() {
            if(Bll.contentDatas[index].id) {
                common.util.__ajax({
                    url: "/resource/content/delResContent",
                    data: {id: Bll.contentDatas[index].id}
                });
            }
            Bll.contentDatas.splice(index, 1);
            Bll.__render("#add-content","template_content",{modules:Bll.contentDatas});
        });

    //$("#index_" + index).remove();
});
$(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);
});

/*第五步 绑定监听事件*/
$(document).on("change", ".observe", function() {
    var $this = $(this);
    var name = $this.data("field");
    Bll.module.contentData.data=common.util.__buildobj(name, '.', Bll.module.contentData.data, function(obj, name) {
        obj[name] = $this.val();
    });
});

$(document).on("click", '#multiLabelImage-addImage', function() {
    Bll.module.contentData.data.image.push({
        "src": "",
        "url": {
            "action": "",
            "url": ""
        }
    });
    Bll.renderDialog("multiLabelImage-template");
});

$(document).on("click", '#multiLabelImage-addOne', function() {
    Bll.module.contentData.data.label_list.push({
        "src": "",
        "url": {
            "action": "",
            "url": ""
        }
    });
    Bll.renderDialog("multiLabelImage-template");
});

$(document).on("click", '#matchImage-addOne', function() {
    Bll.module.contentData.data.list.push({
        "title": "",
        "url": {
            "action": "",
            "url": ""
        }
    });
    Bll.renderDialog("matchImage-template");
});
//添加一个图标
$(document).on("click", '#icon-addOne', function() {
    Bll.module.contentData.data.push({
        //"title": "",
        "url": {
            "action": "",
            "url": ""
        }
    });
    Bll.renderDialog("icon-template");
});
//删除一个图标
$(document).on("click", '#icon-delOne', function() {
    var index = $(this).data("index");
    Bll.module.contentData.data.splice(index,1);
    Bll.renderDialog("icon-template");
});

$(document).on("click", "#sub_btn", function() {
    var data = {
        "content": {},
        "data_id": {},
        "rId": ""
    };
    for(var i = 0; i < Bll.contentDatas.length; i++) {
        data.content[i]=JSON.stringify(common.util.__ArrayToObj(Bll.contentDatas[i].contentData));
        if(Bll.contentDatas[i].id) {
            data.data_id[i] = "id_"+Bll.contentDatas[i].id;
        }
    }
    data.rId = param;
    data.content = JSON.stringify(data.content);
    data.data_id = JSON.stringify(data.data_id);
    console.log(data);
    common.util.__ajax({
        url: "/resource/content/addResContent",
        data: data
    },function(res) {
        console.log(res.data);
        window.location.href=window.location.href;
    });
});