Dialog.js 17.9 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9
o2.widget = o2.widget || {};
o2.widget.Dialog = o2.DL = new Class({
	Implements: [Options, Events],
    Extends: o2.widget.Common,
	options: {
		"style": "default",
		"title": "dialog",
		"width": "300",
		"height": "150",
NoSubject's avatar
NoSubject 已提交
10 11
		"contentWidth": null,
		"contentHeight": null,
R
roo00 已提交
12 13 14 15 16 17 18 19 20 21 22 23
		"top": "0",
		"left": "0",
		"fromTop": "0",
		"fromLeft": "0",
		"mark": true,

		"html": "",
		"text": "",
		"url": "",
		"content": null,

		"isMax": false,
NoSubject's avatar
NoSubject 已提交
24
		"isClose": false,
R
roo00 已提交
25 26
		"isResize": true,
		"isMove": true,
NoSubject's avatar
NoSubject 已提交
27
        "isTitle": true,
R
roo00 已提交
28 29 30 31 32
		
		"buttons": null,
		"buttonList": null,
        "maskNode" : null,

NoSubject's avatar
NoSubject 已提交
33 34 35
		"transition": null,
		"duration": 200,

R
roo00 已提交
36 37 38
        "container": null
	},
	initialize: function(options){
39
		debugger;
R
roo00 已提交
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 86 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		this.setOptions(options);

		this.path = o2.session.path+"/widget/$Dialog/";
		this.cssPath = o2.session.path+"/widget/$Dialog/"+this.options.style+"/css.wcss";

		this._loadCss();
		
		this.reStyle();
//		this.css.to.height = this.options.height;
//		this.css.to.width = this.options.width;
//		this.css.to.top = this.options.top;
//		this.css.to.left = this.options.left;
//		this.css.to.top = this.options.top;
//		this.css.from.top = this.options.fromTop;
//		this.css.from.left = this.options.fromLeft;
		
		this.fireEvent("queryLoad");

		this.getContentUrl();
		var request = new Request.HTML({
			url: this.contentUrl,
			method: "GET",
			async: false,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
				this.node = responseTree[0];
				this.getDialogNode();
				this.fireEvent("postLoad");
			}.bind(this),
			onFailure: function(xhr){
				alert(xhr);
			}
		});
		request.send();
	},
	getContentUrl: function(){
		this.contentUrl = o2.session.path+"/widget/$Dialog/"+this.options.style+"/dialog.html";
	},
	reStyle: function(options){
		if (options) this.setOptions(options);
		this.css.to.height = this.options.height+"px";
		this.css.to.width = this.options.width+"px";
		this.css.to.top = this.options.top+"px";
		this.css.to.left = this.options.left+"px";
		//this.css.to.top = this.options.top+"px";
		this.css.from.top = this.options.fromTop+"px";
		this.css.from.left = this.options.fromLeft+"px";

		if (this.node) this.node.set("styles", this.css.from);
	},

    getParentSelect: function(node){
        var select = "";
        var pnode = node.getParent();
        while (!select && pnode){
            select = pnode.getStyle("-webkit-user-select");
            var pnode = pnode.getParent();
        }
        return select;
    },
	getDialogNode: function(){
		this.node.set("styles", this.css.from);
		this.node.inject(this.options.container || $(document.body));
		this.node.addEvent("selectstart", function(e){
			var select = e.target.getStyle("-webkit-user-select");
            if (!select) select = this.getParentSelect(e.target);
			if (!select){
				select = "none";
			}else{
				select = select.toString().toLowerCase();
			}
			var tag = e.target.tagName.toString().toLowerCase();
			if (select!="text" && select!="auto" && ["input", "textarea"].indexOf(tag)==-1) e.preventDefault();
			
        }.bind(this));

		this.title = this.node.getElement(".MWF_dialod_title");
		this.titleCenter = this.node.getElement(".MWF_dialod_title_center");
        this.titleRefresh = this.node.getElement(".MWF_dialod_title_refresh");
		this.titleText = this.node.getElement(".MWF_dialod_title_text");
		this.titleAction = this.node.getElement(".MWF_dialod_title_action");
        this.under = this.node.getElement(".MWF_dialod_under");
		this.content = this.node.getElement(".MWF_dialod_content");
		this.bottom = this.node.getElement(".MWF_dialod_bottom");
		this.resizeNode = this.node.getElement(".MWF_dialod_bottom_resize");
		this.button = this.node.getElement(".MWF_dialod_button");

NoSubject's avatar
NoSubject 已提交
126 127 128 129 130 131 132 133 134
		if (!this.options.isTitle) {
            this.title.destroy();
            this.title = null;
            this.titleCenter = null;
            this.titleRefresh = null;
            this.titleText = null;
            this.titleAction = null;
        }

R
roo00 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		if (this.title) this.title.setStyles(this.css.MWF_dialod_title);
        if (this.titleCenter) this.titleCenter.setStyles(this.css.MWF_dialod_title_center);
        if (this.titleRefresh) this.titleRefresh.setStyles(this.css.MWF_dialod_title_refresh);
        if (this.titleText) this.titleText.setStyles(this.css.MWF_dialod_title_text);
        if (this.titleAction) this.titleAction.setStyles(this.css.MWF_dialod_title_action);
        if (this.under) this.under.setStyles(this.css.MWF_dialod_under);
        if (this.content) this.content.setStyles(this.css.MWF_dialod_content);
        if (this.bottom) this.bottom.setStyles(this.css.MWF_dialod_bottom);
        if (this.resizeNode) this.resizeNode.setStyles(this.css.MWF_dialod_bottom_resize);
        if (this.button) this.button.setStyles(this.css.MWF_dialod_button);


		if (this.title) this.setTitleEvent();
        if (this.titleRefresh) this.setTitleRefreshNode();
	//	if (this.titleText) this.getTitle();
		if (this.content) this.getContent();
		if (this.titleAction) this.getAction();
		if (this.resizeNode) this.setResizeNode();
	//	if (this.button) this.getButton();

		if (this.content) this.setContentSize();
	},
    setTitleRefreshNode: function(){
        this.titleRefresh.setStyles(this.css.titleRefresh);
        this.titleRefresh.set("title", o2.LP.widget.refresh);
    },
	setTitleEvent: function(){
R
roo00 已提交
162 163 164 165 166
		var content;
		if (layout.app) content = layout.app.content;
		if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
		this.containerDrag = new Drag.Move(this.node, {
			"handle": this.title,
NoSubject's avatar
NoSubject 已提交
167
			"container": this.options.container || this.markNode || content,
R
roo00 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
			"snap": 5
		});

		// this.title.addEvent("mousedown", function(e){
        //     var content;
        //     if (layout.app) content = layout.app.content;
        //     if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
		// 	this.containerDrag = new Drag.Move(this.node, {
        //         "container": content
        //     });
		// 	this.containerDrag.start();
		// }.bind(this));
		// this.title.addEvent("mouseup", function(){
		// 	this.node.removeEvents("mousedown");
		// 	this.title.addEvent("mousedown", function(){
		// 		var content;
		// 		if (layout.app) content = layout.app.content;
		// 		if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
        //         this.containerDrag = new Drag.Move(this.node, {
        //             "container": content
        //         });
		// 		this.containerDrag.start();
		// 	}.bind(this));
		// }.bind(this));
R
roo00 已提交
192 193 194
	},
	setResizeNode: function(){
		//未实现................................
NoSubject's avatar
NoSubject 已提交
195 196 197 198 199 200 201 202 203
        if (!this.options.isResize){
            if (this.resizeNode) this.resizeNode.hide();
        }else{
            if (this.resizeNode){
                this.node.makeResizable({
                    "handle": this.resizeNode || this.bottom,
                    "limit": {x:[200, null], y:[150, null]},
                    "onDrag": function(){
                        var size = this.node.getComputedSize();
NoSubject's avatar
NoSubject 已提交
204 205 206 207
                        // this.css.to.width = size.totalWidth;
                        // this.css.to.height = size.totalHeight;
                        this.css.to.width = size.width;
                        this.css.to.height = size.height;
NoSubject's avatar
NoSubject 已提交
208

NoSubject's avatar
NoSubject 已提交
209
                        this.setContentSize(size.height, size.width);
NoSubject's avatar
NoSubject 已提交
210 211 212 213 214 215 216 217 218

                        this.fireEvent("resize");
                    }.bind(this),
                    "onComplete": function(){
                        this.fireEvent("resizeCompleted");
                    }.bind(this)
                });
			}
		}
R
roo00 已提交
219 220
	},
	getAction: function(){
NoSubject's avatar
NoSubject 已提交
221 222 223 224 225
		//未完成................................
		if (this.options.isClose){
			this.closeAction = new Element("div", {"styles": this.css.closeAction}).inject(this.titleAction);
            this.closeAction.addEvent("click", this.close.bind(this));
		}
R
roo00 已提交
226 227 228 229 230 231
	},
	getButton: function(){
		for (i in this.options.buttons){
			var button = new Element("input", {
				"type": "button",
				"value": i,
232
				"class": "mainColor_bg",
R
roo00 已提交
233 234 235 236 237 238
				"styles": this.css.button,
				"events": {
					"click": this.options.buttons[i].bind(this)
				}
			}).inject(this.button);
		}
239

R
roo00 已提交
240 241
		if (this.options.buttonList){
			this.options.buttonList.each(function(bt){
NoSubject's avatar
NoSubject 已提交
242 243 244 245
				var styles = this.css.button;
				if( bt.type === "ok" && this.css.okButton )styles = this.css.okButton;
				if( bt.type === "cancel" && this.css.cancelButton )styles = this.css.cancelButton;
				if( bt.styles )styles = bt.styles;
R
roo00 已提交
246 247 248
				var button = new Element("input", {
					"type": "button",
					"value": bt.text,
NoSubject's avatar
NoSubject 已提交
249
					"title": bt.title,
250
					"class": (bt.type!=="cancel") ? "mainColor_bg" : "",
NoSubject's avatar
NoSubject 已提交
251
					"styles": styles,
R
roo00 已提交
252
					"events": {
NoSubject's avatar
NoSubject 已提交
253
						"click": function(e){bt.action.call(this, this, e)}.bind(this)
R
roo00 已提交
254 255 256 257 258 259
					}
				}).inject(this.button);
			}.bind(this));
		}
	},
	getContentSize: function(height, width){
NoSubject's avatar
NoSubject 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        var nodeHeight, nodeWidth;
		if (!height){
			if (this.options.contentHeight){
                nodeHeight = height = this.options.contentHeight.toFloat();
			}else{
                height = this.options.height.toFloat();
			}
		}
        if (!width){
            if (this.options.contentWidth){
                nodeWidth = width = this.options.contentWidth.toFloat();
            }else{
                width = this.options.width.toFloat();
            }
        }
R
roo00 已提交
275

NoSubject's avatar
NoSubject 已提交
276 277
        var offsetHeight = 0;
        var offsetWidth = 0;
R
roo00 已提交
278 279 280 281 282 283
		if (this.title){
			var h1 = this.title.getSize().y;
			var ptop1 = this.title.getStyle("padding-top").toFloat();
			var pbottom1 = this.title.getStyle("padding-bottom").toFloat();
			var mtop1 = this.title.getStyle("margin-top").toFloat();
			var mbottom1 = this.title.getStyle("margin-bottom").toFloat();
NoSubject's avatar
NoSubject 已提交
284
            offsetHeight += h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
R
roo00 已提交
285 286 287 288 289 290 291
		}
		if (this.bottom){
			var h2 = this.bottom.getSize().y;
			var ptop2 = this.bottom.getStyle("padding-top").toFloat();
			var pbottom2 = this.bottom.getStyle("padding-bottom").toFloat();
			var mtop2 = this.bottom.getStyle("margin-top").toFloat();
			var mbottom2 = this.bottom.getStyle("margin-bottom").toFloat();
NoSubject's avatar
NoSubject 已提交
292 293

            offsetHeight += h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
R
roo00 已提交
294 295 296 297 298 299 300
		}
		if (this.button){
			var h3 = this.button.getSize().y;
			var ptop3 = this.button.getStyle("padding-top").toFloat();
			var pbottom3 = this.button.getStyle("padding-bottom").toFloat();
			var mtop3 = this.button.getStyle("margin-top").toFloat();
			var mbottom3 = this.button.getStyle("margin-bottom").toFloat();
NoSubject's avatar
NoSubject 已提交
301 302

            offsetHeight += h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
R
roo00 已提交
303 304 305 306 307 308
		}
				
		var ptop4 = this.content.getStyle("padding-top").toFloat();
		var pbottom4 = this.content.getStyle("padding-bottom").toFloat();
		var mtop4 = this.content.getStyle("margin-top").toFloat();
		var mbottom4 = this.content.getStyle("margin-bottom").toFloat();
NoSubject's avatar
NoSubject 已提交
309 310 311 312 313 314 315
        offsetHeight += ptop4 + pbottom4 + mtop4 + mbottom4;

        if (nodeHeight){
            nodeHeight = nodeHeight + offsetHeight+2;
        }else {
            height = height - offsetHeight;
        }
R
roo00 已提交
316 317 318 319 320 321 322

        //if (this.content.getParent().getStyle("overflow-x")!="hidden" ) height = height-18;
		
		var pleft = this.content.getStyle("padding-left").toFloat();
		var pright = this.content.getStyle("padding-right").toFloat();
		var mleft = this.content.getStyle("margin-left").toFloat();
		var mright = this.content.getStyle("margin-right").toFloat();
NoSubject's avatar
NoSubject 已提交
323 324
        offsetWidth = pleft+pright+mleft+mright;
		//width = width-pleft-pright-mleft-mright;
R
roo00 已提交
325
        //if (this.content.getParent().getStyle("overflow-y")!="hidden" ) width = width-18;
NoSubject's avatar
NoSubject 已提交
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
        if (nodeWidth){
            nodeWidth = nodeWidth+offsetWidth;
        }else{
            width = width-offsetWidth;
		}


		if (nodeHeight) {
            this.options.height = nodeHeight;
            this.options.contentHeight = null;
            this.options.fromTop = this.options.fromTop.toFloat()-offsetHeight/2;
            this.options.top = this.options.top.toFloat()-offsetHeight/2;
            this.css.to.height = nodeHeight+"px";
            this.css.to.top = this.options.top+"px";
            this.css.from.top = this.options.fromTop+"px";
        }
        if (nodeWidth){
            this.options.width = nodeWidth;
            this.options.contentWidth = null;
            this.options.fromLeft = this.options.fromLeft.toFloat()-offsetWidth/2;
            this.options.left = this.options.left.toFloat()-offsetWidth/2;
            this.css.to.width = nodeWidth+"px";
            this.css.to.left = this.options.left+"px";
            this.css.from.left = this.options.fromLeft+"px";
        }
R
roo00 已提交
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

        if (!height || height<0){
            this.content.setStyles({"overflow": "hidden", "height": "auto", "width": ""+width+"px"});
            height = this.content.getSize().y;
            var h = height + h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
            h = h + h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
            h = h + h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
            h = h + ptop4 + pbottom4 + mtop4 + mbottom4;
            this.css.to.height = h;
        }

//		var ptop5 = this.node.getStyle("padding-top").toFloat();
//		var pbottom5 = this.node.getStyle("padding-bottom").toFloat();
//		height = height - ptop5 - pbottom5;
		
		return {"height": height+"px", "width": width+"px"};
	},
	setContentSize: function(height, width){
		//this.content.setStyle("height", this.getContentSize(height));
		// if (!this.options.height && !height){
         //    this.content.setStyle("height", "auto");
         //    this.content.setStyle("overflow", "hidden");
         //    this.content.setStyle("width", "auto");
		// }else{
            this.content.setStyles(this.getContentSize(height, width));
            this.content.setStyle("width", "auto");
		//}
	},
NoSubject's avatar
NoSubject 已提交
379 380
	reCenter: function(){
		var size = this.node.getSize();
NoSubject's avatar
NoSubject 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
		var container = $(document.body);
		if (layout.desktop.currentApp){
			container = layout.desktop.currentApp.content;
		}else{
			if (this.options.container){
				if (this.options.container.getSize().y<$(document.body).getSize().y){
					container = this.options.container;
				}
			}
		}

		// if (this.options.container){
		// 	if (this.options.container.getSize().y<$(document.body).getSize().y){
		// 		container = this.options.container;
		// 	}
		// }

NoSubject's avatar
NoSubject 已提交
398
        var p = o2.getCenter(size, container, container);
NoSubject's avatar
NoSubject 已提交
399
        if (p.y<0) p.y = 0;
NoSubject's avatar
NoSubject 已提交
400 401 402 403 404 405 406 407 408 409
        this.options.top = p.y;
        this.options.left = p.x;
        this.css.to.top = this.options.top+"px";
        this.css.to.left = this.options.left+"px";
        this.node.setStyles({
			"top": this.css.to.top,
            "left": this.css.to.left
		});
	},

R
roo00 已提交
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
	getTitle: function(){
		this.titleText.set("text", this.options.title);
	},
	getContent: function(){
		this.content.setStyles(this.css.content);
		if (this.options.content){
			this.options.content.inject(this.content);
		}else if (this.options.url){
			this.content.set("load", {"method": "get", "async": false});
			$(this.content).load(this.options.url);
/*
			var request = new Request.HTML({
				url: this.options.url,
				method: "GET",
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
					alert(responseHTML);
					this.content.set("html", responseHTML);
				}.bind(this),
				onFailure: function(xhr){
					alert("回退出现错误:"+xhr.status+" "+xhr.statusText);
					window.close();
				}
			});*/
		}else if (this.options.html){
			this.content.set("html", this.options.html);
		}else if (this.options.text){
			this.content.set("text", this.options.text);
		}
//		this.content.addEvent("selectstart", function(e){
//			e.preventDefault();
//		});
	},
	show: function(){
		if (this.options.mark) this._markShow();
		if (!this.morph){
NoSubject's avatar
NoSubject 已提交
445
			this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
R
roo00 已提交
446 447 448 449 450 451 452 453 454 455
		}
		if (this.fireEvent("queryShow")){
			this.node.setStyle("display", "block");

            // this.node.setStyles(t);
            // if (this.titleText) this.getTitle();
            // if (this.button) this.getButton();
            // //	this.content.setStyle("display", "block");
            // //this.fireEvent("postShow");

NoSubject's avatar
NoSubject 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469
			var pn = this.node.getOffsetParent();
			var p = pn.getPosition();

			var h = this.css.to.height.toInt();
			var y = this.css.to.top.toInt();
			y = y+p.y;

			var ih = window.innerHeight.toInt();
			if (h+y> ih){
				y = ih-p.y-h-20;
				if (y<0) y=0;
				this.css.to.top = ""+y+"px";
			}

R
roo00 已提交
470 471 472 473 474 475 476 477 478 479
			this.morph.start(this.css.to).chain(function(){
				if (this.titleText) this.getTitle();
				if (this.button) this.getButton();
			//	this.content.setStyle("display", "block");
				this.fireEvent("postShow");
			}.bind(this));
		}
	},
	hide: function() {
		if (!this.morph){
NoSubject's avatar
NoSubject 已提交
480
			this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
R
roo00 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494
		}
		if (this.fireEvent("queryHide")){
			if (this.titleText) this.titleText.set("text", "");
			if (this.button) this.button.set("html", "");
			
			this.morph.start(this.css.from).chain(function(){
				this._markHide();
				this.node.setStyle("display", "none");
				this.fireEvent("postHide");
			}.bind(this));
		}
	},
	close: function(){
		if (!this.morph){
NoSubject's avatar
NoSubject 已提交
495
			this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
R
roo00 已提交
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 521 522 523 524 525 526
		}
		
		if (this.fireEvent("queryClose")){
			this.morph.start(this.css.from).chain(function(){
				this._markHide();
				this.node.destroy();
				this.node = null;
				this.fireEvent("postClose");
			}.bind(this));
		}
	},
	_markShow: function(){

		if (this.options.mark){
			if (!this.markNode){
				var size = o2.getMarkSize(this.options.maskNode);
				this.markNode = new Element("div", {
					styles: this.css.mark
				}).inject(this.options.container || $(document.body));
				this.markNode.set("styles", {
					"height": size.y,
					"width": size.x
				});
			}
			this.markNode.setStyle("display", "block");
		}
	},
	
	_markHide: function(){
		if (this.markNode){
			this.markNode.setStyle("display", "none");
R
roo00 已提交
527 528 529 530 531 532 533
			this.markNode.destroy();
			this.markNode = null;
		}
		if (this.markNode_up){
			this.markNode_up.setStyle("display", "none");
			this.markNode_up.destroy();
			this.markNode_up = null;
R
roo00 已提交
534 535
		}
	}
NoSubject's avatar
NoSubject 已提交
536
});