resourceContentManage.js 4.5 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 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"
            }]
        });
        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.contentData.data[_field[1]].src = obj.data;
            }
        });
    }
}

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

/*第一步,基础模板*/
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("1",ViewModel.contentData);
//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"),
        index = $this.data("index");
    var module = (index == undefined) ? Bll.module.contentData.data : Bll.module.contentData.data[index];
    console.log(module);
    common.util.__buildobj(name, '.', module, function(obj, name) {
        obj[name] = $this.val();
        console.log(module);
    });
});

$(document).on("click", '#matchImage-addOne', function() {
    var length = Bll.module.contentData.data.list.length;//获得长度
    Bll.module.contentData.data.list.push({
        "src": "",
        "url": {
            "action": "",
            "url": ""
        }
    });
    Bll.__render("#add-content","template_content",{modules:Bll.contentDatas});
});