entrance.js 4.54 KB
/**
 * Created by ty on 2016/3/22.
 */

var $ = require('jquery'),
    common = require('../common/common'),
    util = require('../common/util');

new common.dropDown({el: "#entrance-platform"});
new common.dropDown({el: "#entrance-status"});

var g = new common.grid({
    el: "#entrance-list",
    hash: false,
    parms: function () {
        return {
            platform: common.util.__input('entrance-platform'),
            status: common.util.__input('entrance-status')
        };
    },
    columns: [
        {display: "ID", name: "id"},
        {display: "入口名称", name: "entryName"},
        {display: "点击前的图片", name: "clickBeforeImg"},
        {display: "点击后的图片", name: "clickAfterImg"},
        {display: "ZIP图片包地址",name: "zipUrl"},
        {display: "类别",name: "platform", render: function(item) {
            if(item.platform == 1) {
                return "手机";
            } else if(item.platform == 2) {
                return "ipad";
            }
        }},
        {
            display: "状态",name: "", render: function (item) {
                if(item.status == 0) {
                    return "关闭";
                } else if(item.status == 1) {
                    return "开启";
                }
            }
        },
        {
            display: "操作",name: "", render: function (item) {
                var arr = [];
                arr.push('<a class="btn btn-primary btn-xs add2" data-index="' + item.__index + '">编辑</a>');
                if(item.status == 0) {
                    arr.push('<a class="btn btn-danger btn-xs change-status" data-index="' + item.__index + '">开启入口</a>');
                } else {
                    arr.push('<a class="btn btn-info btn-xs change-status" data-index="' + item.__index + '">关闭入口</a>');
                }
                return arr.join("");
            }
        }
    ]

});

g.init('/operations/entrance/selectEntranceList');

var Bll = {
    toast:function(url, item, hint) {
        var e = new common.edit("#base-form", {bucket:"yhb-img01"});

        e.on("validate", function() {
            if(!$("#intent").val()) {
                return "跳转目标不能为空";
            }
        });

        var dialog = common.dialog.confirm(hint,
            common.util.__template($("#template-add").html(), item),
            function() {
                e.submit(url, function (option) {
                    //提取url信息
                    if($("#intent").val()) {
                        option.data.url = '{"action":"go.' + $("#intent").val()
                            + '","url":"' + $("#url").val() + '"}';
                    }
                    //zipUrl    待修复
                    option.success=function(res) {
                        dialog.close();
                        if(res.data.code == 200) {
                            util.__tip(res.data.message, 'success');
                            g.reload();
                        } else {
                            util.__tip(res.data.message);
                        }

                    };
                    option.error=function(res){
                        dialog.close();
                        util.__tip(res.data.message);
                    };
                });
                return false;
            });

        e.init();

        new common.dropDown({el: "#platform"});
         new common.dropDown({el: "#gender"});

        if(item.url && item.url.indexOf('"action"') != -1) {
            var urlIndex = '","url":"';
            $("#intent").val(item.url.substring('{"action":"go.'.length, item.url.indexOf(urlIndex)));
            $("#url").val(item.url.substring(item.url.indexOf(urlIndex) + urlIndex.length, item.url.length - 2));
        }

    }
};

$(document).on('click', '.change-status', function() {
    var item = g.rows[$(this).data("index")];
    if(item.status == 1) {
        item.status = 0;
    } else {
        item.status = 1;
    }
    common.util.__ajax({
        url: '/operations/entrance/publishEntrance',
        data: {
            id: item.id,
            status: item.status
        }
    }, function() {
        g.reload();
    });
});

$(document).on('click', '#add-entry', function() {
    var item = {};
    Bll.toast("/operations/entrance/insertEntrance", item, "添加入口信息");
});

$(document).on('click', '.add2', function() {
    var item = g.rows[$(this).data("index")];
    Bll.toast("/operations/entrance/updateEntrance", item, "编辑入口信息");
});

$(document).on('click', '#filter-btn', function() {
    g.reload(1);
});