UnitWithType.js 17.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);
    },
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 125
        var style = this.selector.options.style;
        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 144 145 146 147 148 149 150 151 152 153
    },
    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 已提交
154

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

    //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 已提交
253
    }
NoSubject's avatar
NoSubject 已提交
254
    //for flat category end
R
roo00 已提交
255
});
R
roo00 已提交
256

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

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 已提交
282 283
        var style = this.selector.options.style;
        this.iconNode.setStyle("background-image", "url("+"/x_component_Selector/$Selector/"+style+"/icon/departmenticon.png)");
R
roo00 已提交
284 285 286 287 288 289 290 291 292 293 294
    }
});

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 已提交
295 296
                        var unit = this.selector._newItem(subData, this.selector, this.children, this.level+1, this);
                        if(this.subItems)this.subItems.push( unit );
R
roo00 已提交
297
                    }else{
R
roo00 已提交
298 299 300 301 302 303 304 305 306
                        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 已提交
307 308 309 310 311 312 313 314 315
                    }
                }
            }.bind(this));
            this.loaded = true;
            if (callback) callback();
        }else{
            if (callback) callback();
        }
    },
R
roo00 已提交
316 317 318 319 320 321 322 323 324 325 326 327
    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 已提交
328 329
    _hasChild: function(){
        return this.data.woSubDirectUnitList.length;
R
roo00 已提交
330
    },
NoSubject's avatar
NoSubject 已提交
331
    _hasChildCategory: function(){
R
roo00 已提交
332
        return this.data.woSubDirectUnitList.length;
NoSubject's avatar
NoSubject 已提交
333 334 335 336 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
    },
    _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 已提交
387 388 389 390 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
    }
});

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

    }
});