UnitWithType.js 12.1 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
MWF.xApplication.Selector = MWF.xApplication.Selector || {};
MWF.xDesktop.requireApp("Selector", "Unit", null, false);
MWF.xApplication.Selector.UnitWithType = new Class({
	Extends: MWF.xApplication.Selector.Unit,
    options: {
        "style": "default",
        "count": 0,
        "title": MWF.xApplication.Selector.LP.selectUnit,
        "units": [],
        "unitType": "",
        "values": [],
        "zIndex": 1000,
        "expand": true,
        "exclude" : [],
R
roo00 已提交
15 16
        "expandSubEnable" : true, //是否允许展开下一层
        "selectAllEnable" : true //分类是否允许全选下一层
R
roo00 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    },

    loadSelectItems: function(addToNext){
        var data = {};
        data.type = this.options.unitType;
        data.unitList = [];
        this.options.units.each(function(unit){
            if (typeOf(unit)==="string"){
                data.unitList.push(unit);
            }else{
                data.unitList.push(unit.distinguishedName);
            }
        }.bind(this));

        //data.unitList = this.options.units;
        this.orgAction.listUnitByType(function(json){
            if (json.data.length){
                json.data.each(function(data){
                    if( !this.isExcluded( data ) ) {
                        if ( !this.options.expandSubEnable || (!this.options.unitType) || data.typeList.indexOf(this.options.unitType)!==-1){
                            var unit = this._newItem(data, this, this.itemAreaNode, 1);
R
roo00 已提交
38
                            this.subItems.push(unit);
R
roo00 已提交
39
                        }else{
R
roo00 已提交
40 41 42 43
                            if (data.woSubDirectUnitList.length){
                                var category = this._newItemCategory("ItemCategory", data, this, this.itemAreaNode);
                                this.subCategorys.push(category);
                            }
R
roo00 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
                        }
                    }
                }.bind(this));
            }
        }.bind(this), null, data);
    },

    _scrollEvent: function(y){
        return true;
    },
    _getChildrenItemIds: function(){
        return null;
    },
R
roo00 已提交
57 58
    _newItemCategory: function(type, data, selector, item, level, category){
        return new MWF.xApplication.Selector.UnitWithType[type](data, selector, item, level, category)
R
roo00 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    },

    _listItemByKey: function(callback, failure, key){
        key = {"key": key};
        if (this.options.units.length){
            var units = [];
            this.options.units.each(function(u){
                if (typeOf(u)==="string"){
                    units.push(u);
                }
                if (typeOf(u)==="object"){
                    units.push(u.distinguishedName);
                }
            });
            key.unitList = units;
        }
        if (this.options.unitType) key.type = this.options.unitType;
        this.orgAction.listUnitByKey(function(json){
            if (callback) callback.apply(this, [json]);
        }.bind(this), failure, key);
    },
    _getItem: function(callback, failure, id, async){
        this.orgAction.getUnit(function(json){
            if (callback) callback.apply(this, [json]);
        }.bind(this), failure, ((typeOf(id)==="string") ? id : id.distinguishedName), async);
    },

    _newItemSelected: function(data, selector, item){
        return new MWF.xApplication.Selector.UnitWithType.ItemSelected(data, selector, item)
    },
    _listItemByPinyin: function(callback, failure, key){
        key = {"key": key};
        if (this.options.units.length){
            var units = [];
            this.options.units.each(function(u){
                if (typeOf(u)==="string"){
                    units.push(u);
                }
                if (typeOf(u)==="object"){
                    units.push(u.distinguishedName);
                }
            });
            key.unitList = units;
        }
        if (this.options.unitType) key.type = this.options.unitType;
        this.orgAction.listUnitByPinyininitial(function(json){
            if (callback) callback.apply(this, [json]);
        }.bind(this), failure, key);
    },
R
roo00 已提交
108 109
    _newItem: function(data, selector, container, level, category){
        return new MWF.xApplication.Selector.UnitWithType.Item(data, selector, container, level, category);
R
roo00 已提交
110 111 112 113 114 115 116 117 118 119
    },
    _newItemSearch: function(data, selector, container, level){
        return new MWF.xApplication.Selector.UnitWithType.SearchItem(data, selector, container, level);
    }
});
MWF.xApplication.Selector.UnitWithType.Item = new Class({
	Extends: MWF.xApplication.Selector.Unit.Item,
    _getShowName: function(){
        return this.data.name;
    },
R
roo00 已提交
120 121 122
    _getTtiteText: function(){
        return this.data.levelName || this.data.name;
    },
R
roo00 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    _setIcon: function(){
        this.iconNode.setStyle("background-image", "url("+"/x_component_Selector/$Selector/default/icon/departmenticon.png)");
    },
    loadSubItem: function(){
        if( !this.selector.options.expandSubEnable )return;
        this.isExpand = (this.selector.options.expand);
        if (this.data.subDirectUnitCount){
            if (this.selector.options.expand){
                if (this.level===1){
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
                    this.loadSubItems();
                }else{
                    this.isExpand = false;
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
                }
            }else{
                this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
            }
            this.levelNode.addEvent("click", function(e){
                if (this.isExpand){
                    this.children.setStyle("display", "none");
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
                    this.isExpand = false;
                }else{
                    this.loadSubItems();
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
                    this.isExpand = true;
                }
                e.stopPropagation();
            }.bind(this));
R
roo00 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

            if( !this.selectAllNode ){
                this.selectAllNode = new Element("div", {
                    "styles": this.selector.css.selectorItemCategoryActionNode_selectAll,
                    "title" : "全选下级"
                }).inject(this.textNode, "before");
                //this.selectAllNode.addEvent( "click", function(ev){
                //    this.selectAll(ev);
                //    ev.stopPropagation();
                //}.bind(this))
                if( this.isSelectedAll ){
                    this.unselectAll(ev);
                    this.selector.fireEvent("unselectCatgory",[this])
                }else{
                    this.selectAll(ev);
                    this.selector.fireEvent("selectCatgory",[this])
                }
                ev.stopPropagation();
            }
R
roo00 已提交
172 173
        }
    },
R
roo00 已提交
174
    loadSubItems: function(callback){
R
roo00 已提交
175 176 177 178 179 180 181 182 183 184
        if (!this.loaded){
            if (!this.children){
                this.children = new Element("div", {
                    "styles": this.selector.css.selectorItemCategoryChildrenNode
                }).inject(this.node, "after");
            }
            this.children.setStyle("display", "block");
            this.data.woSubDirectUnitList.each(function(subData){
                if( !this.selector.isExcluded( subData ) ) {
                    if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType) !== -1) {
R
roo00 已提交
185 186 187
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level + 1, this);
                        if( !this.subItems )this.subItems = [];
                        this.subItems.push( unit );
R
roo00 已提交
188
                    } else {
R
roo00 已提交
189 190 191 192
                        if (data.woSubDirectUnitList.length){
                            var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1, this);
                            this.subCategorys.push( category );
                        }
R
roo00 已提交
193 194
                    }
                }
R
roo00 已提交
195
                if(callback)callback()
R
roo00 已提交
196 197 198 199 200
            }.bind(this));
            this.loaded = true;
        }else{
            this.children.setStyle("display", "block");
        }
R
roo00 已提交
201 202
    },
    postLoad : function(){
R
roo00 已提交
203 204
    }
});
R
roo00 已提交
205

R
roo00 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
MWF.xApplication.Selector.UnitWithType.SearchItem = new Class({
    Extends: MWF.xApplication.Selector.Unit.Item,
    _getShowName: function(){
        return this.data.levelName || this.data.name;
    }
});

MWF.xApplication.Selector.UnitWithType.ItemSelected = new Class({
	Extends: MWF.xApplication.Selector.Unit.ItemSelected,
    // _getShowName: function(){
    //     return this.data.levelName || this.data.name;
    // },
    _getTtiteText: function(){
        return this.data.levelName || this.data.name;
    },
    _getShowName: function(){
        return this.data.name+((this.data.levelName) ? "("+this.data.levelName+")" : "");
    },
    _setIcon: function(){
        this.iconNode.setStyle("background-image", "url("+"/x_component_Selector/$Selector/default/icon/departmenticon.png)");
    }
});

MWF.xApplication.Selector.UnitWithType.ItemCategory = new Class({
    Extends: MWF.xApplication.Selector.Unit.ItemCategory,

    loadSub: function(callback){
        if (!this.loaded){
            this.data.woSubDirectUnitList.each(function(subData){
                if( !this.selector.isExcluded( subData ) ) {
                    if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType)!==-1){
R
roo00 已提交
237 238
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level+1, this);
                        if(this.subItems)this.subItems.push( unit );
R
roo00 已提交
239
                    }else{
R
roo00 已提交
240 241 242 243 244 245 246 247 248
                        if (subData.woSubDirectUnitList.length){
                            var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1, this);
                            this.subCategorys.push(category);
                        }
                    }
                }
                if( !this.subItems || !this.subItems.length ){
                    if( this.selectAllNode ){
                        this.selectAllNode.destroy();
R
roo00 已提交
249 250 251 252 253 254 255 256 257
                    }
                }
            }.bind(this));
            this.loaded = true;
            if (callback) callback();
        }else{
            if (callback) callback();
        }
    },
R
roo00 已提交
258 259 260 261 262 263 264 265 266 267 268 269
    afterLoad: function(){
        if (this.level===1) this.clickItem();
        var flag = false;
        this.data.woSubDirectUnitList.each(function(subData) {
            if (!this.selector.isExcluded(subData)) {
                if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType) !== -1) {
                    flag = true;
                }
            }
            if (!flag && this.selectAllNode)this.selectAllNode.destroy();
        }.bind(this));
    },
R
roo00 已提交
270 271
    _hasChild: function(){
        return this.data.woSubDirectUnitList.length;
R
roo00 已提交
272 273 274
    },
    _hasChildItem: function(){
        return this.data.woSubDirectUnitList.length;
R
roo00 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    }
});

MWF.xApplication.Selector.UnitWithType.Filter = new Class({
    Implements: [Options, Events],
    options: {
        "style": "default",
        "units": []
    },
    initialize: function(value, options){
        this.setOptions(options);
        this.value = value;
        this.orgAction = MWF.Actions.get("x_organization_assemble_control");
    },
    filter: function(value, callback){
        this.value = value;
        var key = this.value;
        key = {"key": key};

        if (this.options.units.length){
            var units = [];
            this.options.units.each(function(u){
                if (typeOf(u)==="string"){
                    units.push(u);
                }
                if (typeOf(u)==="object"){
                    units.push(u.distinguishedName);
                }
            });
            key.unitList = units;
        }
        if (this.options.unitType) key.type = this.options.unitType;
        this.orgAction.listUnitByKey(function(json){
            data = json.data;
            if (callback) callback(data)
        }.bind(this), failure, key);

    }
});