Orgfield.js 20.9 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
        var v = this._getBusinessData();
        if (!v || !v.length){
            if (this.json.description){
                var size = this.node.getFirst().getSize();
                var w = size.x-3;
U
unknown 已提交
14 15 16
                if( this.json.showIcon!='no' && !this.form.json.hideModuleIcon ) {
                    if (COMMON.Browser.safari) w = w - 20;
                }
R
roo00 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29
                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({
30
                "mousedown": function( ev ){
R
roo00 已提交
31
                    this.descriptionNode.setStyle("display", "none");
32 33
                    this.clickSelect( ev );
                }.bind(this)
R
roo00 已提交
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 84 85
            });
        }
    },

	_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 已提交
86
            var flag = item.data.distinguishedName.substr(item.data.distinguishedName.length-2, 2);
R
roo00 已提交
87
            switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
88
                case "@i":
R
roo00 已提交
89 90
                    text = item.data.name+"("+item.data.unitName+")";
                    break;
NoSubject's avatar
NoSubject 已提交
91
                case "@p":
R
roo00 已提交
92 93
                    text = item.data.name+ (item.data.employee ? "("+item.data.employee+")" : "");
                    break;
NoSubject's avatar
NoSubject 已提交
94
                case "@u":
R
roo00 已提交
95 96
                    text = item.data.levelName;
                    break;
NoSubject's avatar
NoSubject 已提交
97
                case "@g":
R
roo00 已提交
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 135 136
                    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 已提交
137
                var flag = d.distinguishedName.substr(d.distinguishedName.length-2, 2);
R
roo00 已提交
138
                switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
139
                    case "@i":
R
roo00 已提交
140 141
                        d.text = d.name+"("+d.unitName+")";
                        break;
NoSubject's avatar
NoSubject 已提交
142
                    case "@p":
R
roo00 已提交
143 144
                        d.text = d.name+(d.employee ? "("+d.employee+")" : "");
                        break;
NoSubject's avatar
NoSubject 已提交
145
                    case "@u":
R
roo00 已提交
146 147
                        d.text = d.name;
                        break;
NoSubject's avatar
NoSubject 已提交
148
                    case "@g":
R
roo00 已提交
149 150 151 152 153 154 155 156 157
                        d.text = d.name;
                        break;
                    default:
                        d.text = d.name;
                }
            });
            if (callback) callback(data);
        });
    },
158 159 160 161 162 163 164 165 166 167 168 169

    _resetNodeInputEdit: function(){
        var node = new Element("div", {
            "styles": {
                "overflow": "hidden",
                //"position": "relative",
                "margin-right": "20px"
            }
        }).inject(this.node, "after");
        this.node.destroy();
        this.node = node;
    },
R
roo00 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    _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);

194
        if (!this.json.preprocessing) this._resetNodeInputEdit();
R
roo00 已提交
195

196 197
        this.node.empty();
        input.inject(this.node);
R
roo00 已提交
198 199 200 201
        this.node.set({
            "id": this.json.id,
            "MWFType": this.json.type
        });
R
roo00 已提交
202 203 204 205
        if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
            this.iconNode = new Element("div", {
                "styles": this.form.css[this.iconStyle],
                "events": {
206
                    "click": function (ev) {
207
                        this.clickSelect( ev );
208 209
                    }.bind(this)
                    //this.clickSelect.bind(this)
R
roo00 已提交
210 211 212 213 214
                }
            }).inject(this.node, "before");
        }else if( this.form.json.nodeStyleWithhideModuleIcon ){
            this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
        }
R
roo00 已提交
215 216 217 218 219 220 221

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

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    _resetNodeEdit: function(){
        var input = new Element("div", {
            "styles": {
                "background": "transparent",
                "border": "0px",
                "min-height": "24px"
            }
        });
        var node = new Element("div", {"styles": {
                "overflow": "hidden",
                "position": "relative",
                "margin-right": "20px",
                "min-height": "24px"
            }}).inject(this.node, "after");
        input.inject(node);
        this.node.destroy();
        this.node = node;
    },
R
roo00 已提交
240
	_loadNodeEdit : function(){
241 242 243
        if (!this.json.preprocessing) this._resetNodeEdit();
        var input = this.node.getFirst();
        input.set(this.json.properties);
R
roo00 已提交
244 245 246 247 248 249 250 251
		this.node.set({
			"id": this.json.id,
			"MWFType": this.json.type,
			"readonly": true
		});
		if( !this.readonly ) {
			this.node.setStyle("cursor" , "pointer");
			this.node.addEvents({
252
				"click": function (ev) {
253
                    this.clickSelect( ev );
254 255
                }.bind(this)
                //this.clickSelect.bind(this)
R
roo00 已提交
256
			});
R
roo00 已提交
257 258 259
            if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
                this.iconNode = new Element("div", {  //this.form.css[this.iconStyle],
                    "styles": {
260
                        "background": "url(" + "../x_component_process_Xform/$Form/default/icon/selectorg.png) center center no-repeat",
R
roo00 已提交
261 262 263 264 265 266 267 268
                        "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 已提交
269 270 271
            if (this.iconNode){
                this.iconNode.setStyle("cursor" , "pointer");
                this.iconNode.addEvents({
272
                    "click": function (ev) {
273
                        this.clickSelect( ev );
274 275
                    }.bind(this)
                    //this.clickSelect.bind(this)
R
roo00 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288
                });
			}

		}
	},
    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 已提交
289 290 291 292 293 294
    },
    isEmpty: function(){
        var data = this.getData();
        if( typeOf(data) !== "array" )return true;
        if( data.length === 0 )return true;
        return false;
R
roo00 已提交
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 369 370 371 372 373 374 375 376 377 378
    },
	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 已提交
379
            var flag = data.value.distinguishedName.substr(data.value.distinguishedName.length-2, 2);
R
roo00 已提交
380
            switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
381
                case "@i":
R
roo00 已提交
382 383
                    text = data.value.name+"("+data.value.unitName+")";
                    break;
NoSubject's avatar
NoSubject 已提交
384
                case "@p":
R
roo00 已提交
385 386
                    text = data.value.name+ (data.value.employee ? "("+data.value.employee+")" : "");
                    break;
NoSubject's avatar
NoSubject 已提交
387
                case "@u":
R
roo00 已提交
388 389
                    text = data.value.levelName;
                    break;
NoSubject's avatar
NoSubject 已提交
390
                case "@g":
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 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
                    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 已提交
448
                this.combox.clear();
R
roo00 已提交
449 450 451 452
                this.combox.addNewValues(comboxValues);
            }else{
                var node = this.node.getFirst();
                if (node){
453
                    node.empty();
R
roo00 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
                    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){
472 473
        var disableInfor = layout.mobile ? true : false;
		var options = {"style": "xform", "canRemove":false , "onRemove" : this.removeItem,"disableInfor" : disableInfor};
R
roo00 已提交
474 475
		value.each(function(data){
			if( data.distinguishedName ){
NoSubject's avatar
NoSubject 已提交
476
				var flag = data.distinguishedName.substr(data.distinguishedName.length-2, 2);
R
roo00 已提交
477
				switch (flag.toLowerCase()){
NoSubject's avatar
NoSubject 已提交
478
					case "@i":
R
roo00 已提交
479 480
						var widget = new MWF.widget.O2Identity(data, node, options );
						break;
NoSubject's avatar
NoSubject 已提交
481
					case "@p":
R
roo00 已提交
482 483
						var widget = new MWF.widget.O2Person(data, node, options);
						break;
NoSubject's avatar
NoSubject 已提交
484
					case "@u":
R
roo00 已提交
485 486
						var widget = new MWF.widget.O2Unit(data, node, options);
						break;
NoSubject's avatar
NoSubject 已提交
487
					case "@g":
R
roo00 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
						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());
	},
521 522 523
	clickSelect: function( ev ){
        debugger;
        this.validationMode();
R
roo00 已提交
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 560 561 562 563 564 565 566 567 568 569 570 571
		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 已提交
572 573
				this.validation();
                this.fireEvent("select");
R
roo00 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587
			}.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 已提交
588
        if( this.form.json.selectorStyle )options = Object.merge( options, this.form.json.selectorStyle );
R
roo00 已提交
589

590 591 592 593 594
        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 已提交
595 596 597 598 599 600 601
	},
    _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);
602
            if (this.iconNode && this.iconNode.offsetParent !== null ){
R
roo00 已提交
603 604 605 606 607 608
                var size = this.node.getSize();
                this.iconNode.setStyle("height", ""+size.y+"px");
            }
        }
    }
});