global.practice.js 11.6 KB
/// <reference path="jquery-1.11.1.min.js" />
/// <reference path="jquery-easyui-1.4.1/jquery.easyui.min.js" />
/// <reference path="global.modelctls.js" />
/// <reference path="global.js" />

function PracticeDialog(options) {
    this._options = options || {};
    this._callback = this._options.callback;
    this._userId = this._options.userId;
    this._pagination = false;

    this._dgPractice = null;
    this._menu = null;
    this._dialog = null;

    this._initialize = function (dgPractice, menu) {
        var thispd = this;

        $(menu).menu({
            onClick: function (item) {
                if (item.text == '添加') {
                    thispd.addPractice();
                } else if (item.text == '编辑') {
                    thispd.editPractice();
                } else if (item.text == '删除') {
                    thispd.removePractice();
                }
            }
        });
        var data_json = new Array();
    	$.ajax({
    		type: "POST",
    		url: CONF_BACK_SERVERURL + global.modelctls.practice.list,
    		headers:{
        		"token":$.cookie('ftoken')
    		},
    		data:{userId: thispd._userId },
    		dataType: 'json',
    		async:"false",
    		success:function (result) {
    			for(var i=0;i<result.length;i++){
    				var data_row = {content:result[i].content,id:result[i].id};
    				data_json = {rows:data_row}
    			}
    			console.log(result);
    		}
    	});
		
        $(dgPractice).datagrid({
            idField: 'id', fit: true, fitColumns: true, showHeader: true, singleSelect: true, nowrap: false,
            border: false, checkOnSelect: false, selectOnCheck: false, scrollbarSize: 0, rownumbers: true,
            pagination: thispd._pagination, pageNumber: 1, pageSize: 20, pageList: [10, 20, 50], striped: true,
            method:"GET",
            url: CONF_BACK_SERVERURL + global.modelctls.practice.list,
            header:[{key:'token',value:$.cookie('token')}],
            queryParams: {userId:thispd._userId},
            columns: [[{
                field: 'content', title: '内容', width: 100, formatter: function (value, row, index) {
                    var text = row.content;
                    return '<span title="' + text + '">' + text + '</span>';
                }
            }, {
                field: 'id', title: '操作', width: 80, fixed: true, formatter: function (value, row, index) {
                    var tmp = '<a onclick="{0}(\'{1}\',\'{2}\')" href="#">{3}</a>';
                    var deleteStr = global.strfmt(tmp, 'window._practiceDialog.removePractice', row.id, index, '删除');
                    var editStr = global.strfmt(tmp, 'window._practiceDialog.editPractice', row.id, index, '编辑');
                    return editStr + '&nbsp;' + deleteStr;
                }
            }]],
            onDblClickRow: function (rowIndex, rowData) {
                $(thispd._dialog).dialog('close');
                if (thispd._callback) {
                    thispd._callback(rowData);
                }
            },
            onHeaderContextMenu: function (e, field) {
                e.preventDefault();
                var item = $(menu).menu('findItem', '删除');
                $(menu).menu('hideItem', item.target);
                item = $(menu).menu('findItem', '编辑');
                $(menu).menu('hideItem', item.target);
                $(menu).menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onRowContextMenu: function (e, rowIndex, rowData) {
                e.preventDefault();
                var item = $(menu).menu('findItem', '删除');
                $(menu).menu('showItem', item.target);
                item = $(menu).menu('findItem', '编辑');
                $(menu).menu('showItem', item.target);
                $(this).datagrid('selectRow', rowIndex);
                $(menu).menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }
        });
    };

    this.showDialog = function () {
        var thispd = this;
        var dgPractice = $('<table/>');
        var menu = $('<div></div>').append('<div data-options="name:\'add\',iconCls:\'icon-add\'">添加</div>')
            .append('<div data-options="name:\'add\',iconCls:\'icon-edit\'">编辑</div>')
            .append('<div data-options="name:\'add\',iconCls:\'icon-remove\'">删除</div>');
        var tools = $('<div/>');
        var btnAdd = $('<a href="javascript:void(0)" style="float:left"></a>').appendTo(tools);
        var btnOk = $('<a href="javascript:void(0)"></a>').appendTo(tools);
        var btnCancel = $('<a href="javascript:void(0)"></a>').appendTo(tools);
        thispd._dgPractice = dgPractice;
        thispd._menu = menu;
        window._practiceDialog = thispd;

        var dialog = thispd._dialog = $('<div style="padding-left:0px;padding-top:0px;"/>').append(dgPractice).append(menu).dialog({
            buttons: tools,
            title: '选择习惯用语', width: 450, height: 300, modal: true, onOpen: function () {
                thispd._initialize(dgPractice, menu);
                $(btnAdd).linkbutton({
                    iconCls: 'icon-add', text: '添加',
                    onClick: function () { thispd.addPractice(); }
                });
                $(btnCancel).linkbutton({
                    iconCls: 'icon-cancel', text: '取消', 
                    onClick: function () { $(dialog).dialog('close'); }
                });
                $(btnOk).linkbutton({
                    iconCls: 'icon-ok', text: '确定',
                    onClick: function () {
                        var row = $(dgPractice).datagrid('getSelected');
                        if (!row) {
                            $.messager.alert('提示', '请选择习惯用语');
                        } else {
                            $(dialog).dialog('close');
                            if (thispd._callback) {
                                thispd._callback(row);
                            }
                        }
                    }
                });
            },
            onClose: function () {
                thispd._dialog = null;
                delete window._practiceDialog;
                $(this).dialog('destroy');
            }
        });
    };

    this.removePractice = function (id, index) {
        var thispd = this;
        var datagrid = thispd._dgPractice;
        if (id) $(datagrid).datagrid('selectRecord', id);
        var row = $(datagrid).datagrid('getSelected');
        $.messager.confirm('确认', '您确认想要删除记录吗?', function (r) {
            if (!r) return;
            $.post(global.contextPath + global.modelctls.practice.remove, { id: row.id }, function (result) {
                if (result.status == 'ok') {
                    thispd.loadPractice();
                } else {
                    console.error(result);
                    $.messager.alert('错误', '删除失败!');
                }
            }).error(function (e) {
                console.error(e);
                $.messager.alert('错误', '发送Http请求失败!');
            });
        });
    };

    this.editPractice = function (id, index) {
        var thispd = this;
        var datagrid = thispd._dgPractice;
        if (id) $(datagrid).datagrid('selectRecord', id);
        var row = $(datagrid).datagrid('getSelected');
        var input = $('<input />');
        var dialog = $('<div style="padding-left:20px;padding-top:20px;"/>').append(input).dialog({
            title: '编辑习惯用语', width: 330, height: 230, modal: true,
            onOpen: function () {
                $(input).textbox({
                    height: 120,
                    width: 280,
                    multiline: true,
                    value: row.content,
                });
            }, onClose: function () { $(this).dialog('destroy'); },
            buttons: [{
                text: '保存', iconCls: 'icon-ok', handler: function () {
                    row.content = $(input).textbox('getValue');
                    if (row.content.trim() == '') {
                        $.messager.alert('提示', '请输入内容!');
                        return;
                    }
                    $.post(global.contextPath + global.modelctls.practice.save, row, function (result) {
                        if (result.status == 'ok') {
                            thispd.loadPractice();
                            $(dialog).dialog('close');
                        } else {
                            console.error(result);
                            $.messager.alert('错误', '保存失败!');
                        }
                    }).error(function (e) {
                        console.error(e);
                        $.messager.alert('错误', '发送Http请求失败!');
                    });
                }
            }, {
                text: '取消', iconCls: 'icon-cancel', handler: function () { $(dialog).dialog('close'); }
            }]
        });
    };

    this.addPractice = function addPractice() {
        var thispd = this;
        var input = $('<input />');
        var dialog = $('<div style="padding-left:20px;padding-top:20px;"/>').append(input).dialog({
            title: '添加习惯用语', width: 330, height: 230, modal: true,
            onOpen: function () {
                $(input).textbox({
                    height: 120,
                    width: 280,
                    multiline: true
                });
            }, onClose: function () { $(this).dialog('destroy'); },
            buttons: [{
                text: '添加', iconCls: 'icon-ok', handler: function () {
                    var content = $(input).textbox('getValue');
                    if (content.trim() == '') {
                        $.messager.alert('提示', '请输入内容!');
                        return;
                    }
                    /*$.post(global.contextPath + global.modelctls.practice.save, { content: content, userId: thispd._userId }, function (result) {
                        if (result.status == 'ok') {
                            thispd.loadPractice();
                            $(dialog).dialog('close');
                        } else {
                            console.error(result);
                            $.messager.alert('错误', '保存失败!');
                        }
                    }).error(function (e) {
                        console.error(e);
                        $.messager.alert('错误', '发送Http请求失败!');
                    });*/
                	$.ajax({
                		type: "POST",
                		url: CONF_BACK_SERVERURL + global.modelctls.practice.save,
                		headers:{
                    		"token":$.cookie('ftoken')
                		},
                		data:{ content: content, userId: thispd._userId },
                		dataType: 'json',
                		success:function (result) {
                            if (result.status == 'ok') {
                                thispd.loadPractice();
                                $(dialog).dialog('close');
                            } else {
                                console.error(result);
                                $.messager.alert('错误', '保存失败!');
                            }      
                		}
                	});					
                }
            }, {
                text: '取消', iconCls: 'icon-cancel', handler: function () { $(dialog).dialog('close'); }
            }]
        });
    };

    this.loadPractice = function () {
        var thispd = this;
        var datagrid = thispd._dgPractice;
        $(datagrid).datagrid('reload');
    };
}