ActivityUserSelectDialog.js 7.54 KB
/// <reference path="../jquery-1.11.1.js" />
/// <reference path="../jquery-easyui-1.4.1/jquery.easyui.min.js" />
/// <reference path="../jquery-easyui-ext.js" />
/// <reference path="../global.js" />
/// <reference path="../global.modelctls.js" />
/// <reference path="../global.enums.js" />
/// <reference path="../global.extension.js" />

global = global || {};
global.ui = global.ui || {};

function ActivityUserSelectDialog(url, queryParams, callback, options) {
    this._options = options || {};
    this._url = url || this._options.url || '';
    this._queryParams = queryParams || this._options.queryParams || {};
    this._callback = callback || this._options.callback;
    this._task = this._options.task || {};
    this._sendType = this._options.sendType || global.enums.sendType.SActivitySUser;

    this._dialog = null;
    this._tree = null;

    this.showDialog = function (data) {
        var thispd = this;
        if (data) {
            thispd._showDialog(data);
        } else if (url) {
            $.post(url, thispd._queryParams, function (result) {
                var d = thispd.buildData(result);
                thispd._showDialog(d);
            });
        }
    };

    this.buildData = function (data) {
        data = data || [];
        result = [];
        for (var i = 0; i < data.length; i++) {
            var activity = data[i].activity;
            activity.flowObjectType = activity.flowObjectType || '';
            var text = activity.flowObjectName;
            if (!text && activity.flowObjectType.toLowerCase() == 'endevent') {
                text = '结束';
            }
            var activityNode = {
                id: activity.flowObjectId, text: text, type: 'activity', flowRevisionId: activity.flowRevisionId,
                activityId: activity.id, flowObjectName: activity.flowObjectName, flowObjectType: activity.flowObjectType
            };
            result.push(activityNode);

            var users = data[i].users;
            if (!users || users.length <= 0) continue;
            activityNode.children = [];
            for (var j = 0; j < users.length; j++) {
                var user = users[j];
                var userNode = { id: user.id, text: user.realname, type: 'user', parentId: activityNode.id, loginname: user.loginname };
                activityNode.children.push(userNode);
            }
        }
        return result;
    };

    this.checkedUsers = function () {
        var data = $(this._tree).tree('getRoots');
        var result = [];
        for (var i = 0; i < data.length; i++) {
            var node = data[i];
            var select = { activityId: node.activityId, flowObjectId: node.id, flowObjectName: node.flowObjectName, users: '' };
            var childs = $(this._tree).tree('getChildren', node.target);
            for (var j = 0; j < childs.length; j++) {
                var child = childs[j];
                if (child.checked) {
                    select.users += child.loginname + ',';
                }
            }
            if (select.users || node.checked) {
                result.push(select);
            }
        }
        return result;
    };

    this._showDialog = function (data) {
        var thispd = this;
        var tree = thispd._tree = $('<ul/>');
        var dialog = this._dialog = $('<div style="padding-left:0px;padding-top:0px;"/>').append(tree).dialog({
            title: '选择发送人员', width: 300, height: 400, modal: true, onOpen: function () {
                var sendType = global.enums.sendType;
                $(tree).tree({
                    data: data, checkbox: true, onBeforeCheck: function (node, checked) {
                        if (!checked) return true;
                        if (node.type == 'activity') {
                            if (node.flowObjectType.toLowerCase() == 'endevent') {
                                thispd.uncheckTree($(this), function (fn) { return fn.parentId == node.id; });
                            } else {
                                thispd.uncheckEndNode($(this));
                                if (thispd._sendType == sendType.SActivitySUser) {
                                    return false;
                                } else if (thispd._sendType == sendType.SActivityMUser) {
                                    thispd.uncheckTree($(this), function (fn) { return fn.parentId == node.id; });
                                } else if (thispd._sendType == sendType.MActivitySUser) {
                                    return false;
                                } else if (thispd._sendType == sendType.MActivityMUser) {

                                }
                            }
                        } else if (node.type == 'user') {
                            thispd.uncheckEndNode($(this));
                            if (thispd._sendType == sendType.SActivitySUser) {
                                thispd.uncheckTree($(this), function (fn) { return fn.id == node.id; });
                            } else if (thispd._sendType == sendType.SActivityMUser) {
                                thispd.uncheckTree($(this), function (fn) { return fn.parentId == node.parentId; });
                            } else if (thispd._sendType == sendType.MActivitySUser) {
                                thispd.uncheckTree($(this), function (fn) { return (fn.parentId != node.parentId || fn.id == node.id); });
                            } else if (thispd._sendType == sendType.MActivityMUser) {

                            }
                        }
                    }, onClick: function (node) {
                        if (node.checked) {
                            $(tree).tree('uncheck', node.target);
                        } else {
                            $(tree).tree('check', node.target);
                        }
                    }
                });
            }, buttons: [{
                text: '发送', iconCls: 'icon-ok', handler: function () {
                    var checkedUsers = thispd.checkedUsers();
                    if (checkedUsers.length == 0) {
                        $.messager.alert('提示', '请选择人员!');
                        return;
                    } else {
                        if (thispd._callback) {
                            thispd._callback(checkedUsers);
                        }
                        $(dialog).dialog('close');
                    }
                }
            }, {
                text: '取消', iconCls: 'icon-cancel', handler: function () {
                    $(dialog).dialog('close');
                }
            }],
            onClose: function () {
                thispd._tree = null;
                thispd._dialog = null;
                $(this).dialog('destroy');
            }
        });
    };

    this.uncheckTree = function (tree, filter) {
        var roots = $(tree).tree('getRoots');
        for (var i = 0; i < roots.length; i++) {
            var root = roots[i];
            var chidren = $(tree).tree('getChildren', root.target);
            for (var j = 0; j < chidren.length; j++) {
                var child = chidren[j];
                if (filter && filter(child)) continue;
                if (child.checked)
                    $(tree).tree('uncheck', child.target);
            }
        }
    };

    this.uncheckEndNode = function (tree) {
        var roots = $(tree).tree('getRoots');
        for (var i = 0; i < roots.length; i++) {
            var root = roots[i];
            if (root.flowObjectType.toLowerCase() == 'endevent') {
                $(tree).tree('uncheck', root.target);
                break;
            }
        }
    };

};