UnitWithType.js 17.5 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);
    },
NoSubject's avatar
NoSubject 已提交
108 109
    _newItem: function(data, selector, container, level, category, delay){
        return new MWF.xApplication.Selector.UnitWithType.Item(data, selector, container, level, category, delay);
R
roo00 已提交
110 111 112 113 114 115 116 117
    },
    _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(){
NoSubject's avatar
NoSubject 已提交
118
        return (this.isShowLevelName && this.data.levelName) ? this.data.levelName : this.data.name;
R
roo00 已提交
119
    },
R
roo00 已提交
120 121 122
    _getTtiteText: function(){
        return this.data.levelName || this.data.name;
    },
R
roo00 已提交
123
    _setIcon: function(){
NoSubject's avatar
NoSubject 已提交
124
        var style = this.selector.options.style;
125
        this.iconNode.setStyle("background-image", "url("+"../x_component_Selector/$Selector/"+style+"/icon/departmenticon.png)");
R
roo00 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    },
    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){
U
unknown 已提交
144
                    this.selector.fireEvent("collapse", [this] );
R
roo00 已提交
145 146 147 148
                    this.children.setStyle("display", "none");
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
                    this.isExpand = false;
                }else{
U
unknown 已提交
149
                    this.selector.fireEvent("expand", [this] );
R
roo00 已提交
150 151 152 153 154 155
                    this.loadSubItems();
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
                    this.isExpand = true;
                }
                e.stopPropagation();
            }.bind(this));
R
roo00 已提交
156

NoSubject's avatar
NoSubject 已提交
157
            if( !this.selectAllNode && !this.selector.isFlatCategory ){
R
roo00 已提交
158 159 160 161 162 163 164 165
                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))
NoSubject's avatar
NoSubject 已提交
166 167
                this.selectAllNode.addEvent( "click", function(ev) {
                    if (this.isSelectedAll) {
168 169
                        // this.unselectAll(ev);
                        this.selector.options.selectAllRange === "all" ? this.unselectAllNested(ev) : this.unselectAll(ev);
NoSubject's avatar
NoSubject 已提交
170 171
                        this.selector.fireEvent("unselectCatgory", [this])
                    } else {
172 173
                        // this.selectAll(ev);
                        this.selector.options.selectAllRange === "all" ? this.selectAllNested(ev) : this.selectAll(ev);
NoSubject's avatar
NoSubject 已提交
174 175 176 177
                        this.selector.fireEvent("selectCatgory", [this])
                    }
                    ev.stopPropagation();
                });
R
roo00 已提交
178
            }
R
roo00 已提交
179 180
        }
    },
R
roo00 已提交
181
    loadSubItems: function(callback){
R
roo00 已提交
182 183 184 185 186 187 188
        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");
NoSubject's avatar
NoSubject 已提交
189
            ( this.data.woSubDirectUnitList || [] ).each(function(subData){
R
roo00 已提交
190 191
                if( !this.selector.isExcluded( subData ) ) {
                    if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType) !== -1) {
R
roo00 已提交
192 193 194
                        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 已提交
195
                    } else {
NoSubject's avatar
NoSubject 已提交
196
                        if (subData.woSubDirectUnitList.length){ // ? 原来是data.woSubDirectUnitList.length 2020-3-1
R
roo00 已提交
197 198 199
                            var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1, this);
                            this.subCategorys.push( category );
                        }
R
roo00 已提交
200 201
                    }
                }
R
roo00 已提交
202
                if(callback)callback()
R
roo00 已提交
203 204 205 206 207
            }.bind(this));
            this.loaded = true;
        }else{
            this.children.setStyle("display", "block");
        }
R
roo00 已提交
208 209
    },
    postLoad : function(){
NoSubject's avatar
NoSubject 已提交
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    },

    //for flat category start
    loadCategoryChildren : function( callback ){
        if (!this.categoryLoaded){
            this.data.woSubDirectUnitList.each(function(subData){
                if( !this.selector.isExcluded( subData ) ) {
                    if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType) !== -1) {
                    } else {
                        if (subData.woSubDirectUnitList.length){
                            var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1, this);
                            this.subCategorys.push( category );
                        }
                    }
                }
                if(callback)callback()
            }.bind(this));
            this.categoryLoaded = true;
        }else{
            //if(callback)callback();
        }
    },
    loadItemChildren : function( callback ){
        if (!this.itemLoaded){
            if (!this.children){
                this.children = new Element("div", {
                    "styles": this.selector.css.selectorItemCategoryChildrenNode
                }).inject(this.selector.itemAreaNode);
            }
            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) {
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level + 1, this, true);
                        category.justItem = true;
                        category.load();
                        if( !this.subItems )this.subItems = [];
                        this.subItems.push( unit );
                    }
                }
                if(callback)callback()
            }.bind(this));
            this.itemLoaded = true;
        }else{
            this.children.setStyle("display", "block");
            //if(callback)callback();
        }
R
roo00 已提交
257
    }
NoSubject's avatar
NoSubject 已提交
258
    //for flat category end
R
roo00 已提交
259
});
R
roo00 已提交
260

R
roo00 已提交
261
MWF.xApplication.Selector.UnitWithType.SearchItem = new Class({
NoSubject's avatar
NoSubject 已提交
262 263 264 265 266
    //Extends: MWF.xApplication.Selector.Unit.Item,
    Extends: MWF.xApplication.Selector.UnitWithType.Item,
    load : function(){
        this.loadForNormal();
    },
R
roo00 已提交
267 268
    _getShowName: function(){
        return this.data.levelName || this.data.name;
NoSubject's avatar
NoSubject 已提交
269 270
    },
    loadSubItems: function( callback ){
R
roo00 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    }
});

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(){
NoSubject's avatar
NoSubject 已提交
286
        var style = this.selector.options.style;
287
        this.iconNode.setStyle("background-image", "url("+"../x_component_Selector/$Selector/"+style+"/icon/departmenticon.png)");
R
roo00 已提交
288 289 290 291 292 293 294 295 296 297 298
    }
});

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 已提交
299 300
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level+1, this);
                        if(this.subItems)this.subItems.push( unit );
R
roo00 已提交
301
                    }else{
R
roo00 已提交
302 303 304 305 306 307 308 309 310
                        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 已提交
311 312 313 314 315 316 317 318 319
                    }
                }
            }.bind(this));
            this.loaded = true;
            if (callback) callback();
        }else{
            if (callback) callback();
        }
    },
R
roo00 已提交
320 321 322 323 324 325 326 327 328 329 330 331
    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 已提交
332 333
    _hasChild: function(){
        return this.data.woSubDirectUnitList.length;
R
roo00 已提交
334
    },
NoSubject's avatar
NoSubject 已提交
335
    _hasChildCategory: function(){
R
roo00 已提交
336
        return this.data.woSubDirectUnitList.length;
NoSubject's avatar
NoSubject 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    },
    _hasChildItem: function(){
        if( typeOf(this.isHasChildItem) === "boolean" )return this.isHasChildItem;
        this.isHasChildItem = false;
        var unitList = this.data.woSubDirectUnitList || [];
        for( var i=0; i<unitList.length; i++){
            if ((!this.selector.options.unitType) || unitList[i].typeList.indexOf(this.selector.options.unitType) !== -1) {
                this.isHasChildItem = true;
                break;
            }
        }
        return this.isHasChildItem;
    },

    //for flat category start
    loadCategoryChildren: function(callback){
        if (!this.categoryLoaded){
            this.data.woSubDirectUnitList.each(function(subData){
                if( !this.selector.isExcluded( subData ) ) {
                    if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType)!==-1){
                    }else{
                        if (subData.woSubDirectUnitList.length){
                            var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1, this);
                            this.subCategorys.push(category);
                        }
                    }
                }
            }.bind(this));
            this.categoryLoaded = true;
            if (callback) callback();
        }else{
            if (callback) callback( );
        }
    },
    loadItemChildren: function(callback){
        if (!this.itemLoaded){
            this.data.woSubDirectUnitList.each(function(subData){
                if( !this.selector.isExcluded( subData ) ) {
                    if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType)!==-1){
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level+1, this);
                        if(this.subItems)this.subItems.push( unit );
                    }
                }
                if( !this.subItems || !this.subItems.length ){
                    if( this.selectAllNode ){
                        this.selectAllNode.destroy();
                    }
                }
            }.bind(this));
            this.itemLoaded = true;
            if (callback) callback();
        }else{
            if (callback) callback( );
        }
R
roo00 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    }
});

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);

    }
});