template.js 9.7 KB
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dinghorn/template/index' + location.search,
                    add_url: 'dinghorn/template/add',
                    edit_url: 'dinghorn/template/edit',
                    del_url: 'dinghorn/template/del',
                    multi_url: 'dinghorn/template/multi',
                    table: 'dinghorn_template',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'code', title: __('Code')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', placeholder: '模糊查找'},
                        {field: 'robot_names', title: __('Robot_ids'), formatter: Controller.api.formatter.robot_names, operate: false},
                        {field: 'typelist', title: __('Typelist'), searchList: {"text": __('Typelist text'), "link": __('Typelist link'), "markdown": __('Typelist markdown'), "actionCard": __('Typelist actioncard'), "actionCard_whole": __('Typelist actioncard_whole'), "feedCard": __('Typelist feedcard')}, formatter: Table.api.formatter.normal},
                        {field: 'content', title: __('Content'), operate: 'LIKE', placeholder: '模糊查找', formatter: Controller.api.formatter.content_names},
                        {field: 'title', title: __('Title'), operate: 'LIKE', placeholder: '模糊查找'},
                        // {field: 'msg_url', title: __('Msg_url'), formatter: Table.api.formatter.url},
                        // {field: 'picurl_image', title: __('Picurl_image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'single_title', title: __('Single_title')},
                        // {field: 'btn_orientation', title: __('Btn_orientation'), searchList: {"0":__('Btn_orientation 0'),"1":__('Btn_orientation 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'hide_avatar', title: __('Hide_avatar'), searchList: {"0":__('Hide_avatar 0'),"1":__('Hide_avatar 1')}, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'btns', title: __('Btns')},
                        {field: 'at_mobiles', title: __('At_mobiles'), operate: 'LIKE', placeholder: '模糊查找'},
                        {field: 'is_atall', title: __('Is_atall'), searchList: {"0": __('Is_atall 0'), "1": __('Is_atall 1')}, formatter: Table.api.formatter.normal},
                        {field: 'openswitch', title: __('Openswitch'), searchList: {"0": __('Openswitch 0'), "1": __('Openswitch 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'test_msg',
                                    title: __('发送通知消息'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-send-o',
                                    url: 'dinghorn/template/msg_test',
                                    confirm: '确认发送?',
                                    success: function (data, ret) {
                                        Fast.api.msg(ret.msg);
                                    },
                                    error: function (data, ret) {
                                        Fast.api.msg(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'dinghorn/template/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', placeholder: '模糊查找'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'dinghorn/template/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'dinghorn/template/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function (e) {
                Form.api.bindevent($("form[role=form]"));

                function need_show(value) {

                    $('.msg_group').hide();

                    if (value == 'text') {
                        $('.msg_content,.msg_at').show(200);
                    }
                    else if (value == 'link') {
                        $('.msg_title,.msg_content,.msg_picurl_image,.msg_url').show(200);
                    }
                    else if (value == 'markdown') {
                        $('.msg_title,.msg_content,.msg_at,.markdown_help').show(200);
                    }
                    else if (value == 'actionCard') {
                        $('.msg_title,.msg_content,.markdown_help,.msg_btns,.msg_btn_orientation,.msg_hide_avatar').show(200);
                    }
                    else if (value == 'actionCard_whole') {
                        $('.msg_title,.msg_content,.markdown_help,.msg_single_title,.msg_url,.msg_btn_orientation,.msg_hide_avatar').show(200);
                    }
                    else if (value == 'feedCard') {
                        $('.msg_links').show(200);
                    }
                }

                need_show($('#c-typelist')[0].value);

                // 切换消息类型时,显示不同的输入框,变化太多,直接每项都来个class
                $('#c-typelist').on('change', function () {
                    need_show(this.value);
                })

                $('#show_grammar').on('click', function () {
                    $('.markdown_grammar').show(200);
                    $('.markdown_help').hide();
                })
            },
            formatter: {
                robot_names: function (value, row) {
                    var value = value.split(',');

                    var btn_html = '';

                    for (let i in value) {
                        btn_html += ' <a href="javascript:;" title="' + value[i] + '" class="btn btn-xs btn-info robot_names">' + value[i] + '</a> ';
                    }

                    return btn_html;
                },
                content_names: function (value, row) {
                    if (value.length > 30) {
                        return value.substring(0, 30) + '...';
                    }

                    return value;
                }
            }
        }
    };
    return Controller;
});