UnitWithType.js 19.4 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
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,
        "units": [],
        "unitType": "",
        "values": [],
        "zIndex": 1000,
        "expand": true,
        "exclude" : [],
14
        "selectType" : "unit",
R
roo00 已提交
15 16
        "expandSubEnable" : true, //是否允许展开下一层
        "selectAllEnable" : true //分类是否允许全选下一层
R
roo00 已提交
17
    },
18 19 20
    setInitTitle: function(){
        this.setOptions({"title": MWF.xApplication.Selector.LP.selectUnit});
    },
U
unknown 已提交
21 22 23 24
    _init : function(){
        this.selectType = "unit";
        this.className = "UnitWithType";
    },
R
roo00 已提交
25
    loadSelectItems: function(addToNext){
U
unknown 已提交
26 27 28 29 30 31

        if( this.options.disabled ){
            this.afterLoadSelectItem();
            return;
        }

32 33 34
        this._loadSelectItems();
    },
    _loadSelectItems: function(){
35 36
        var afterLoadSelectItemFun = this.afterLoadSelectItem.bind(this);

R
roo00 已提交
37 38 39 40 41 42 43 44 45 46 47
        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));

48 49 50 51 52
        if( this.isCheckStatusOrCount() ) {
            this.loadingCount = "wait";
            this.loadCount( data.unitList );
        }

R
roo00 已提交
53 54 55 56 57 58 59
        //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);
60
                            this.items.push(unit);
R
roo00 已提交
61
                            this.subItems.push(unit);
R
roo00 已提交
62
                        }else{
R
roo00 已提交
63 64 65
                            if (data.woSubDirectUnitList.length){
                                var category = this._newItemCategory("ItemCategory", data, this, this.itemAreaNode);
                                this.subCategorys.push(category);
66
                                this.subCategoryMap[data.levelName] = category;
R
roo00 已提交
67
                            }
R
roo00 已提交
68 69 70 71
                        }
                    }
                }.bind(this));
            }
72 73
            afterLoadSelectItemFun();
        }.bind(this), afterLoadSelectItemFun, data);
R
roo00 已提交
74 75 76 77 78 79 80 81
    },

    _scrollEvent: function(y){
        return true;
    },
    _getChildrenItemIds: function(){
        return null;
    },
U
unknown 已提交
82 83
    _newItemCategory: function(type, data, selector, item, level, category, delay){
        return new MWF.xApplication.Selector.UnitWithType[type](data, selector, item, level, category, delay)
R
roo00 已提交
84 85 86
    },

    _listItemByKey: function(callback, failure, key){
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        if( this.options.expandSubEnable ){
            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);
        }else{
            if (key){
                this.initSearchArea(true);
                this.searchInItems(key);
            }else{
                this.initSearchArea(false);
            }
R
roo00 已提交
112 113 114 115 116 117 118 119
        }
    },
    _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);
    },

120 121
    _newItemSelected: function(data, selector, item, selectedNode){
        return new MWF.xApplication.Selector.UnitWithType.ItemSelected(data, selector, item, selectedNode)
R
roo00 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    },
    _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 已提交
142 143
    _newItem: function(data, selector, container, level, category, delay){
        return new MWF.xApplication.Selector.UnitWithType.Item(data, selector, container, level, category, delay);
R
roo00 已提交
144 145 146 147 148 149 150 151
    },
    _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 已提交
152
        return (this.isShowLevelName && this.data.levelName) ? this.data.levelName : this.data.name;
R
roo00 已提交
153
    },
R
roo00 已提交
154 155 156
    _getTtiteText: function(){
        return this.data.levelName || this.data.name;
    },
R
roo00 已提交
157
    _setIcon: function(){
NoSubject's avatar
NoSubject 已提交
158
        var style = this.selector.options.style;
159
        this.iconNode.setStyle("background-image", "url("+"../x_component_Selector/$Selector/"+style+"/icon/departmenticon.png)");
R
roo00 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    },
    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 已提交
178
                    this.selector.fireEvent("collapse", [this] );
R
roo00 已提交
179 180 181 182
                    this.children.setStyle("display", "none");
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_collapse);
                    this.isExpand = false;
                }else{
U
unknown 已提交
183
                    this.selector.fireEvent("expand", [this] );
R
roo00 已提交
184 185 186 187 188 189
                    this.loadSubItems();
                    this.levelNode.setStyles(this.selector.css.selectorItemLevelNode_expand);
                    this.isExpand = true;
                }
                e.stopPropagation();
            }.bind(this));
R
roo00 已提交
190

NoSubject's avatar
NoSubject 已提交
191
            if( !this.selectAllNode && !this.selector.isFlatCategory ){
R
roo00 已提交
192 193
                this.selectAllNode = new Element("div", {
                    "styles": this.selector.css.selectorItemCategoryActionNode_selectAll,
194
                    "title" : MWF.SelectorLP.selectChildren
R
roo00 已提交
195 196 197 198 199
                }).inject(this.textNode, "before");
                //this.selectAllNode.addEvent( "click", function(ev){
                //    this.selectAll(ev);
                //    ev.stopPropagation();
                //}.bind(this))
NoSubject's avatar
NoSubject 已提交
200 201
                this.selectAllNode.addEvent( "click", function(ev) {
                    if (this.isSelectedAll) {
202
                        // this.unselectAll(ev);
203
                        this.selector.options.selectAllRange === "all" ? this.unselectAllNested(ev, null, true) : this.unselectAll(ev, null, true);
U
jsdoc  
unknown 已提交
204 205
                        this.selector.fireEvent("unselectCatgory", [this]);
                        this.selector.fireEvent("unselectCategory", [this])
NoSubject's avatar
NoSubject 已提交
206
                    } else {
207
                        // this.selectAll(ev);
208
                        this.selector.options.selectAllRange === "all" ? this.selectAllNested(ev, true) : this.selectAll(ev, true);
U
jsdoc  
unknown 已提交
209 210
                        this.selector.fireEvent("selectCatgory", [this]);
                        this.selector.fireEvent("selectCategory", [this])
NoSubject's avatar
NoSubject 已提交
211 212 213
                    }
                    ev.stopPropagation();
                });
R
roo00 已提交
214
            }
R
roo00 已提交
215 216
        }
    },
R
roo00 已提交
217
    loadSubItems: function(callback){
218 219
        if (!this.loaded && !this.loading){
            this.loading = true;
R
roo00 已提交
220 221 222 223 224 225
            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 已提交
226
            ( this.data.woSubDirectUnitList || [] ).each(function(subData){
R
roo00 已提交
227 228
                if( !this.selector.isExcluded( subData ) ) {
                    if ((!this.selector.options.unitType) || subData.typeList.indexOf(this.selector.options.unitType) !== -1) {
R
roo00 已提交
229
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level + 1, this);
230
                        this.selector.items.push( unit );
R
roo00 已提交
231 232
                        if( !this.subItems )this.subItems = [];
                        this.subItems.push( unit );
R
roo00 已提交
233
                    } else {
NoSubject's avatar
NoSubject 已提交
234
                        if (subData.woSubDirectUnitList.length){ // ? 原来是data.woSubDirectUnitList.length 2020-3-1
R
roo00 已提交
235 236
                            var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1, this);
                            this.subCategorys.push( category );
237
                            this.subCategoryMap[subData.levelName] = category;
R
roo00 已提交
238
                        }
R
roo00 已提交
239 240
                    }
                }
R
roo00 已提交
241
                if(callback)callback()
R
roo00 已提交
242 243
            }.bind(this));
            this.loaded = true;
244
            this.loading = false;
R
roo00 已提交
245 246 247
        }else{
            this.children.setStyle("display", "block");
        }
R
roo00 已提交
248 249
    },
    postLoad : function(){
NoSubject's avatar
NoSubject 已提交
250 251 252 253 254 255 256 257 258 259 260 261
    },

    //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 );
262
                            this.subCategoryMap[subData.levelName] = category;
NoSubject's avatar
NoSubject 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
                        }
                    }
                }
                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);
285 286 287
                        unit.justItem = true;
                        unit.load();
                        this.selector.items.push( unit );
NoSubject's avatar
NoSubject 已提交
288 289 290 291 292 293 294 295 296 297 298
                        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 已提交
299
    }
NoSubject's avatar
NoSubject 已提交
300
    //for flat category end
R
roo00 已提交
301
});
R
roo00 已提交
302

R
roo00 已提交
303
MWF.xApplication.Selector.UnitWithType.SearchItem = new Class({
NoSubject's avatar
NoSubject 已提交
304 305 306 307 308
    //Extends: MWF.xApplication.Selector.Unit.Item,
    Extends: MWF.xApplication.Selector.UnitWithType.Item,
    load : function(){
        this.loadForNormal();
    },
R
roo00 已提交
309 310
    _getShowName: function(){
        return this.data.levelName || this.data.name;
NoSubject's avatar
NoSubject 已提交
311 312
    },
    loadSubItems: function( callback ){
R
roo00 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    }
});

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 已提交
328
        var style = this.selector.options.style;
329
        this.iconNode.setStyle("background-image", "url("+"../x_component_Selector/$Selector/"+style+"/icon/departmenticon.png)");
R
roo00 已提交
330 331 332 333 334 335 336 337 338 339 340
    }
});

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 已提交
341
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level+1, this);
342
                        this.selector.items.push( unit );
R
roo00 已提交
343
                        if(this.subItems)this.subItems.push( unit );
R
roo00 已提交
344
                    }else{
R
roo00 已提交
345 346 347
                        if (subData.woSubDirectUnitList.length){
                            var category = this.selector._newItemCategory("ItemCategory", subData, this.selector, this.children, this.level+1, this);
                            this.subCategorys.push(category);
348
                            this.subCategoryMap[subData.levelName] = category;
R
roo00 已提交
349 350 351 352 353 354
                        }
                    }
                }
                if( !this.subItems || !this.subItems.length ){
                    if( this.selectAllNode ){
                        this.selectAllNode.destroy();
R
roo00 已提交
355 356 357 358 359 360 361 362 363
                    }
                }
            }.bind(this));
            this.loaded = true;
            if (callback) callback();
        }else{
            if (callback) callback();
        }
    },
R
roo00 已提交
364 365 366 367 368 369 370 371 372 373 374 375
    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 已提交
376 377
    _hasChild: function(){
        return this.data.woSubDirectUnitList.length;
R
roo00 已提交
378
    },
NoSubject's avatar
NoSubject 已提交
379
    _hasChildCategory: function(){
R
roo00 已提交
380
        return this.data.woSubDirectUnitList.length;
NoSubject's avatar
NoSubject 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    },
    _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);
405
                            this.subCategoryMap[subData.levelName] = category;
NoSubject's avatar
NoSubject 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
                        }
                    }
                }
            }.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);
422
                        this.selector.items.push( unit );
NoSubject's avatar
NoSubject 已提交
423 424 425 426 427 428 429 430 431 432
                        if(this.subItems)this.subItems.push( unit );
                    }
                }
                if( !this.subItems || !this.subItems.length ){
                    if( this.selectAllNode ){
                        this.selectAllNode.destroy();
                    }
                }
            }.bind(this));
            this.itemLoaded = true;
U
unknown 已提交
433
            if (callback) callback();
NoSubject's avatar
NoSubject 已提交
434 435 436
        }else{
            if (callback) callback( );
        }
R
roo00 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    }
});

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

    }
475
});