Orgfield.js 20.3 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7
MWF.xDesktop.requireApp("process.Xform", "Personfield", null, false);
MWF.require("MWF.widget.O2Identity", null,false);
MWF.xDesktop.requireApp("Selector", "package", null, false);
MWF.xApplication.process.Xform.Orgfield = MWF.APPOrgfield =  new Class({
	Extends: MWF.APPPersonfield,
	iconStyle: "orgfieldIcon",
    loadDescription: function(){
NoSubject's avatar
NoSubject 已提交
8
        if (this.readonly || this.json.isReadonly)return;
R
roo00 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
        var v = this._getBusinessData();
        if (!v || !v.length){
            if (this.json.description){
                var size = this.node.getFirst().getSize();
                var w = size.x-3;
                if (COMMON.Browser.safari) w = w-20;
                this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
                this.descriptionNode.setStyles({
                    "width": ""+w+"px",
                    "height": ""+size.y+"px",
                    "line-height": ""+size.y+"px"
                });
                this.setDescriptionEvent();
            }
        }
    },
    setDescriptionEvent: function(){
        if (this.descriptionNode){
            this.descriptionNode.addEvents({
28
                "mousedown": function( ev ){
R
roo00 已提交
29
                    this.descriptionNode.setStyle("display", "none");
30 31
                    this.clickSelect( ev );
                }.bind(this)
R
roo00 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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
            });
        }
    },

	_loadUserInterface: function(){
		this.field = true;

		this._loadNode();

		if (this.json.compute == "show"){
			this._setValue(this._computeValue());
		}else{
			this._loadValue();
		}

	},
    _loadNode: function(){
        if (this.readonly || this.json.isReadonly){
            this._loadNodeRead();
        }else{
            this._getOrgOptions();
            if (this.json.isInput){
                this._loadNodeInputEdit();
            }else{
                this._loadNodeEdit();
            }

        }
    },
    _getOrgOptions: function(){
        var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
        if( selectType.contains("unit") || selectType.contains("identity")){
            this.selectUnits = this.getSelectRange();
            if (this.json.range!=="all"){
                if (!this.selectUnits.length){
                    this.form.notice(MWF.xApplication.process.Xform.LP.noSelectRange, "error", this.node);
                    return false;
                }
            }
        }else{
            this.selectUnits = [];
        }
    },
	_loadNodeRead: function(){
		this.node.empty();
		this.node.setStyle("overflow" , "hidden");
        var node = new Element("div").inject(this.node);
	},
    _searchConfirmPerson: function(item){
        var inforNode = item.inforNode || new Element("div");
        if (item.data){
            var text = "";
NoSubject's avatar
NoSubject 已提交
84
            var flag = item.data.distinguishedName.substr(item.data.distinguishedName.length-2, 2);
R
roo00 已提交
85
            switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
86
                case "@i":
R
roo00 已提交
87 88
                    text = item.data.name+"("+item.data.unitName+")";
                    break;
NoSubject's avatar
NoSubject 已提交
89
                case "@p":
R
roo00 已提交
90 91
                    text = item.data.name+ (item.data.employee ? "("+item.data.employee+")" : "");
                    break;
NoSubject's avatar
NoSubject 已提交
92
                case "@u":
R
roo00 已提交
93 94
                    text = item.data.levelName;
                    break;
NoSubject's avatar
NoSubject 已提交
95
                case "@g":
R
roo00 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
                    text = item.data.name;
                    break;
                default:
                    text = item.data.name;
            }
            inforNode.set({
                "styles": {"font-size": "14px", "color": ""},
                "text": text
            });
        }else{
            inforNode.set({
                "styles": {"font-size": "14px", "color": "#bd0000"},
                "text": MWF.xApplication.process.Xform.LP.noOrgObject
            });
        }
        if (!item.inforNode){
            new mBox.Tooltip({
                content: inforNode,
                setStyles: {content: {padding: 15, lineHeight: 20}},
                attach: item.node,
                transition: 'flyin'
            });
            item.inforNode = inforNode;
        }

    },
    _searchOptions: function(value, callback){
        var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
        var options = {
            "type" : "",
            "types": selectType,
            "units": this.selectUnits, //范围
            "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType,
        };
        if (!this.comboxFilter) this.comboxFilter = new MWF.O2SelectorFilter(value, options);
        this.comboxFilter.filter(value, function(data){
            data.map(function(d){
                var value = Object.clone(d);
                d.value = value;
NoSubject's avatar
NoSubject 已提交
135
                var flag = d.distinguishedName.substr(d.distinguishedName.length-2, 2);
R
roo00 已提交
136
                switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
137
                    case "@i":
R
roo00 已提交
138 139
                        d.text = d.name+"("+d.unitName+")";
                        break;
NoSubject's avatar
NoSubject 已提交
140
                    case "@p":
R
roo00 已提交
141 142
                        d.text = d.name+(d.employee ? "("+d.employee+")" : "");
                        break;
NoSubject's avatar
NoSubject 已提交
143
                    case "@u":
R
roo00 已提交
144 145
                        d.text = d.name;
                        break;
NoSubject's avatar
NoSubject 已提交
146
                    case "@g":
R
roo00 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
                        d.text = d.name;
                        break;
                    default:
                        d.text = d.name;
                }
            });
            if (callback) callback(data);
        });
    },
    _loadNodeInputEdit: function(){
        var input=null;
        MWF.require("MWF.widget.Combox", function(){
            this.combox = input = new MWF.widget.Combox({
                "count": this.json.count || 0,
                "splitShow": this.json.splitShow || ", ",
                "onCommitInput": function(item){
                    this._searchConfirmPerson(item);
                    //this.fireEvent("change");
                }.bind(this),
                "onChange": function(){
                    this._setBusinessData(this.getInputData());
                    this.fireEvent("change");
                }.bind(this),
                "optionsMethod": this._searchOptions.bind(this)

            });
        }.bind(this), false);
        input.setStyles({
            "background": "transparent",
            "border": "0px"
        });
        input.set(this.json.properties);

        var node = new Element("div", {"styles": {
            "overflow": "hidden",
            //"position": "relative",
            "margin-right": "20px"
        }}).inject(this.node, "after");
        input.inject(node);
        //this.combox = input;

        this.node.destroy();
        this.node = node;
        this.node.set({
            "id": this.json.id,
            "MWFType": this.json.type
        });
R
roo00 已提交
194 195 196 197
        if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
            this.iconNode = new Element("div", {
                "styles": this.form.css[this.iconStyle],
                "events": {
198
                    "click": function (ev) {
199
                        this.clickSelect( ev );
200 201
                    }.bind(this)
                    //this.clickSelect.bind(this)
R
roo00 已提交
202 203 204 205 206
                }
            }).inject(this.node, "before");
        }else if( this.form.json.nodeStyleWithhideModuleIcon ){
            this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
        }
R
roo00 已提交
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

        this.combox.addEvent("change", function(){
            this.validationMode();
            if (this.validation()) this._setBusinessData(this.getInputData("change"));
        }.bind(this));
    },

	_loadNodeEdit : function(){
		var input = this.input = new Element("div", {
			"styles": {
				"background": "transparent",
				"border": "0px",
                "min-height": "20px"
			}
		});
		input.set(this.json.properties);

		var node = new Element("div", {"styles": {
			"overflow": "hidden",
			"position": "relative",
			"min-height" : "20px",
			"margin-right": "20px"
		}}).inject(this.node, "after");
		input.inject(node);

		this.node.destroy();
		this.node = node;
		this.node.set({
			"id": this.json.id,
			"MWFType": this.json.type,
			"readonly": true
		});
		if( !this.readonly ) {
			this.node.setStyle("cursor" , "pointer");
			this.node.addEvents({
242
				"click": function (ev) {
243
                    this.clickSelect( ev );
244 245
                }.bind(this)
                //this.clickSelect.bind(this)
R
roo00 已提交
246
			});
R
roo00 已提交
247 248 249
            if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
                this.iconNode = new Element("div", {  //this.form.css[this.iconStyle],
                    "styles": {
250
                        "background": "url(" + "../x_component_process_Xform/$Form/default/icon/selectorg.png) center center no-repeat",
R
roo00 已提交
251 252 253 254 255 256 257 258
                        "width": "18px",
                        "height": "18px",
                        "float": "right"
                    }
                }).inject(this.node, "before");
            }else if( this.form.json.nodeStyleWithhideModuleIcon ){
                this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
            }
R
roo00 已提交
259 260 261
            if (this.iconNode){
                this.iconNode.setStyle("cursor" , "pointer");
                this.iconNode.addEvents({
262
                    "click": function (ev) {
263
                        this.clickSelect( ev );
264 265
                    }.bind(this)
                    //this.clickSelect.bind(this)
R
roo00 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278
                });
			}

		}
	},
    getValue: function(){
        var value = this._getBusinessData();
        if( typeOf( value ) === "array" ){
            if( value.length === 0 )value = this._computeValue();
        }else if (!value){
            value = this._computeValue();
        }
        return value || "";
U
unknown 已提交
279 280 281 282 283 284
    },
    isEmpty: function(){
        var data = this.getData();
        if( typeOf(data) !== "array" )return true;
        if( data.length === 0 )return true;
        return false;
R
roo00 已提交
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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 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
    },
	getData: function(when){
		if (this.json.compute == "save") this._setValue(this._computeValue());
		return this._getBusinessData();
	},
	getInputData: function(){
        if (this.json.isInput){
            if (this.combox) return this.combox.getData();
            return this._getBusinessData();
        }else{
            return this._getBusinessData();
        }
	},
    addData: function(value){
        if (!value) return false;
        value.each(function(v){
            var vtype = typeOf(v);
            if (vtype==="string"){
                var data;
                this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), null, v, false);
                if (data) this.combox.addNewValue(this.getDataText(data), data);
            }
            if (vtype==="object"){
                this.combox.addNewValue(this.getDataText(v), v);
            }
        }.bind(this));
    },
	setData: function(data) {
		this._setValue(data);
	},
	_computeValue: function(){
		var values = [];
		if (this.json.identityValue) {
			this.json.identityValue.each(function(v){ if (v) values.push(v)});
		}
		if (this.json.unitValue) {
			this.json.unitValue.each(function(v){ if (v) values.push(v)});
		}
		if (this.json.defaultValue && this.json.defaultValue.code){
            var fd = this.form.Macro.exec(this.json.defaultValue.code, this);
            if (typeOf(fd)!=="array") fd = (fd) ? [fd] : [];
            fd.each(function(fdd){
                if (fdd){
                    if (typeOf(fdd)==="string"){
                        var data;
                        this.getOrgAction()[this.getValueMethod(fdd)](function(json){ data = json.data }.bind(this), null, fdd, false);
                        values.push(data);
                    }else{
                        values.push(fdd);
                    }
                }
            }.bind(this));
		}
		if (this.json.count>0){
			return values.slice(0, this.json.count);
		}
		return values;
	},

	_getBusinessData: function(){
		if (this.json.section=="yes"){
			var data = this._getBusinessSectionData();
		}else {
			var data = this.form.businessData.data[this.json.id] || "";
		}
		if (typeOf( data ) != "array"){
            if (data) return [data];
            return [];
        }else{
		    return data;
        }
	},
    creteShowNode: function(data, islast){
        var nodeText = (data.text) ?  data.text : data;
        if (!islast) nodeText = nodeText + (this.json.splitStr || ", ");
        var node = new Element("div", {
            "styles": {
                "float": "left",
                "margin-right": "5px"
            },
            "text": nodeText
        });
        var text = "";
        if (data.value){
NoSubject's avatar
NoSubject 已提交
369
            var flag = data.value.distinguishedName.substr(data.value.distinguishedName.length-2, 2);
R
roo00 已提交
370
            switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
371
                case "@i":
R
roo00 已提交
372 373
                    text = data.value.name+"("+data.value.unitName+")";
                    break;
NoSubject's avatar
NoSubject 已提交
374
                case "@p":
R
roo00 已提交
375 376
                    text = data.value.name+ (data.value.employee ? "("+data.value.employee+")" : "");
                    break;
NoSubject's avatar
NoSubject 已提交
377
                case "@u":
R
roo00 已提交
378 379
                    text = data.value.levelName;
                    break;
NoSubject's avatar
NoSubject 已提交
380
                case "@g":
R
roo00 已提交
381 382 383 384 385 386 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 426 427 428 429 430 431 432 433 434 435 436 437
                    text = data.value.name;
                    break;
                default:
                    text = data.value.name;
            }
            var inforNode = new Element("div").set({
                "styles": {"font-size": "14px", "color": ""},
                "text": text
            });

            new mBox.Tooltip({
                content: inforNode,
                setStyles: {content: {padding: 15, lineHeight: 20}},
                attach: node,
                transition: 'flyin'
            });
        }
        return node;
    },
	_setValue: function(value){
        if (value.length==1 && !(value[0])) value=[];
        var values = [];
        var comboxValues = [];
        var type = typeOf(value);
        if (type==="array"){
            value.each(function(v){
                var data=null;
                var vtype = typeOf(v);
                if (vtype==="string"){
                    var error = (this.json.isInput) ? function(){ comboxValues.push(v); } : null;
                    this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), error, v, false);
                }
                if (vtype==="object") data = v;
                if (data){
                    values.push(data);
                    comboxValues.push({"text": this.getDataText(data),"value": data});
                }
            }.bind(this));
        }
        if (type==="string"){
            var vData;
            var error = (this.json.isInput) ? function(){ comboxValues.push(value); } : null;
            this.getOrgAction()[this.getValueMethod(value)](function(json){ vData = json.data }.bind(this), error, value, false);
            if (vData){
                values.push(vData);
                comboxValues.push({"text": this.getDataText(vData),"value": vData});
            }
        }
        if (type==="object"){
            values.push(value);
            comboxValues.push({"text": this.getDataText(value),"value": value});
        }

		this._setBusinessData(value);

        if (this.json.isInput){
            if (this.combox){
R
roo00 已提交
438
                this.combox.clear();
R
roo00 已提交
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
                this.combox.addNewValues(comboxValues);
            }else{
                var node = this.node.getFirst();
                if (node){
                    comboxValues.each(function(v, i){
                        this.creteShowNode(v, (i===comboxValues.length-1)).inject(node);
                    }.bind(this));
                }
            }

        }else {
            var input = this.node.getFirst();
            if (!input) {
                input = this.node;
            }
            input.empty();

            this.loadOrgWidget(values, input);
        }

	},
	loadOrgWidget: function(value, node){
		var options = {"style": "xform", "canRemove":false , "onRemove" : this.removeItem};
		value.each(function(data){
			if( data.distinguishedName ){
NoSubject's avatar
NoSubject 已提交
464
				var flag = data.distinguishedName.substr(data.distinguishedName.length-2, 2);
R
roo00 已提交
465
				switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
466
					case "@i":
R
roo00 已提交
467 468
						var widget = new MWF.widget.O2Identity(data, node, options );
						break;
NoSubject's avatar
NoSubject 已提交
469
					case "@p":
R
roo00 已提交
470 471
						var widget = new MWF.widget.O2Person(data, node, options);
						break;
NoSubject's avatar
NoSubject 已提交
472
					case "@u":
R
roo00 已提交
473 474
						var widget = new MWF.widget.O2Unit(data, node, options);
						break;
NoSubject's avatar
NoSubject 已提交
475
					case "@g":
R
roo00 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
						var widget = new MWF.widget.O2Group(data, node, options);
						break;
					default:
						var widget = new MWF.widget.O2Other(data, node, options);
				}
				widget.field = this;
				if( layout.mobile ){
					widget.node.setStyles({
						"float" : "none"
					})
				}
			}
		}.bind(this));
	},
	removeItem : function( widget, ev ){
		//this 是 MWF.widget.O2Identity 之类的对象
		var _self = this.field; //这个才是Readerfield
		var dn = this.data.distinguishedName;
		var data = _self._getBusinessData();
		var index;
		data.each( function ( d , i){
			if( d.distinguishedName == dn ){
				index = i
			}
		});
		data.splice( index, 1 );
		_self._setBusinessData( data );
		this.node.destroy();
		ev.stopPropagation();
	},
	_loadValue: function(){
		this._setValue(this.getValue());
	},
509 510 511
	clickSelect: function( ev ){
        debugger;
        this.validationMode();
R
roo00 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
		var count = (this.json.count) ? this.json.count : 0;

		var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
		// if( selectType.contains("unit") || selectType.contains("identity")){
		// 	var selectUnits = this.getSelectRange();
		// 	if (this.json.range!=="all"){
		// 		if (!selectUnits.length){
		// 			this.form.notice(MWF.xApplication.process.Xform.LP.noSelectRange, "error", this.node);
		// 			return false;
		// 		}
		// 	}
		// }else{
		// 	var selectUnits = [];
		// }
        var selectUnits = this.selectUnits;
		if( !selectType[0] ){
			this.form.notice(MWF.xApplication.process.Xform.LP.noSelectType, "error", this.node);
			return false;
		}

        var exclude = [];
        if( this.json.exclude ){
            var v = this.form.Macro.exec(this.json.exclude.code, this);
            exclude = typeOf(v)==="array" ? v : [v];
        }

        var options = {
			"type" : "",
			"types": selectType,
			"values" : (this.json.isInput) ? [] : this._getBusinessData(),
			"count": count,
			"units": selectUnits, //范围
            "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType,
            "exclude" : exclude,
            "expandSubEnable" : (this.json.expandSubEnable=="no") ? false : true,
			//"expand" : false,
			"onComplete": function(items, itemsObject){
				var values = [];
				items.each( function(it){
					values.push(MWF.org.parseOrgData(it.data));
				});
                if (this.json.isInput){
                    this.addData(values);
                }else{
                    this.setData(values);
                }

				//this.setData( values );
NoSubject's avatar
NoSubject 已提交
560 561
				this.validation();
                this.fireEvent("select");
R
roo00 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575
			}.bind(this),
			"onCancel": function(){
				this.validation();
			}.bind(this),
			"onLoad": function(){
				if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
			}.bind(this),
			"onClose": function(){
				//var v = this.node.getFirst().get("value");
				var v = this.getInputData();
				if (!v || !v.length) if (this.descriptionNode)  this.descriptionNode.setStyle("display", "block");

			}.bind(this)
		};
R
roo00 已提交
576
        if( this.form.json.selectorStyle )options = Object.merge( options, this.form.json.selectorStyle );
R
roo00 已提交
577

578 579 580 581 582
        if( this.selector && this.selector.loading ) {
        }else if( this.selector && this.selector.selector && this.selector.selector.active ){
        }else{
            this.selector = new MWF.O2Selector(this.form.app.content, options);
        }
R
roo00 已提交
583 584 585 586 587 588 589
	},
    _loadStyles: function(){
        if (this.readonly || this.json.isReadonly){
            if (this.json.styles) this.node.setStyles(this.json.styles);
        }else{
            if (this.json.styles) this.node.setStyles(this.json.styles);
            if (this.json.inputStyles) if (this.node.getFirst()) this.node.getFirst().setStyles(this.json.inputStyles);
590
            if (this.iconNode && this.iconNode.offsetParent !== null ){
R
roo00 已提交
591 592 593 594 595 596
                var size = this.node.getSize();
                this.iconNode.setStyle("height", ""+size.y+"px");
            }
        }
    }
});