Dialog.js 16.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 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 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
        "container": null
	},
	initialize: function(options){
		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 已提交
125 126 127 128 129 130 131 132 133
		if (!this.options.isTitle) {
            this.title.destroy();
            this.title = null;
            this.titleCenter = null;
            this.titleRefresh = null;
            this.titleText = null;
            this.titleAction = null;
        }

R
roo00 已提交
134 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(){
		this.title.addEvent("mousedown", function(){
NoSubject's avatar
NoSubject 已提交
162 163 164
            var content;
            if (layout.app) content = layout.app.content;
            if (layout.desktop.currentApp) content = layout.desktop.currentApp.content;
NoSubject's avatar
NoSubject 已提交
165
			this.containerDrag = new Drag.Move(this.node, {
NoSubject's avatar
NoSubject 已提交
166
                "container": content
NoSubject's avatar
NoSubject 已提交
167
            });
R
roo00 已提交
168 169 170 171
		}.bind(this));
		this.title.addEvent("mouseup", function(){
			this.node.removeEvents("mousedown");
			this.title.addEvent("mousedown", function(){
NoSubject's avatar
NoSubject 已提交
172 173 174 175 176
				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
NoSubject's avatar
NoSubject 已提交
177
                });
R
roo00 已提交
178 179 180 181 182
			}.bind(this));
		}.bind(this));
	},
	setResizeNode: function(){
		//未实现................................
NoSubject's avatar
NoSubject 已提交
183 184 185 186 187 188 189 190 191
        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 已提交
192 193 194 195
                        // 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 已提交
196

NoSubject's avatar
NoSubject 已提交
197
                        this.setContentSize(size.height, size.width);
NoSubject's avatar
NoSubject 已提交
198 199 200 201 202 203 204 205 206

                        this.fireEvent("resize");
                    }.bind(this),
                    "onComplete": function(){
                        this.fireEvent("resizeCompleted");
                    }.bind(this)
                });
			}
		}
R
roo00 已提交
207 208
	},
	getAction: function(){
NoSubject's avatar
NoSubject 已提交
209 210 211 212 213
		//未完成................................
		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 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227
	},
	getButton: function(){
		for (i in this.options.buttons){
			var button = new Element("input", {
				"type": "button",
				"value": i,
				"styles": this.css.button,
				"events": {
					"click": this.options.buttons[i].bind(this)
				}
			}).inject(this.button);
		}
		if (this.options.buttonList){
			this.options.buttonList.each(function(bt){
NoSubject's avatar
NoSubject 已提交
228 229 230 231
				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 已提交
232 233 234
				var button = new Element("input", {
					"type": "button",
					"value": bt.text,
NoSubject's avatar
NoSubject 已提交
235
					"styles": styles,
R
roo00 已提交
236
					"events": {
NoSubject's avatar
NoSubject 已提交
237
						"click": function(e){bt.action.call(this, this, e)}.bind(this)
R
roo00 已提交
238 239 240 241 242 243
					}
				}).inject(this.button);
			}.bind(this));
		}
	},
	getContentSize: function(height, width){
NoSubject's avatar
NoSubject 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        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 已提交
259

NoSubject's avatar
NoSubject 已提交
260 261
        var offsetHeight = 0;
        var offsetWidth = 0;
R
roo00 已提交
262 263 264 265 266 267
		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 已提交
268
            offsetHeight += h1 + ptop1 + pbottom1 + mtop1 + mbottom1;
R
roo00 已提交
269 270 271 272 273 274 275
		}
		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 已提交
276 277

            offsetHeight += h2 + ptop2 + pbottom2 + mtop2 + mbottom2;
R
roo00 已提交
278 279 280 281 282 283 284
		}
		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 已提交
285 286

            offsetHeight += h3 + ptop3 + pbottom3 + mtop3 + mbottom3;
R
roo00 已提交
287 288 289 290 291 292
		}
				
		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 已提交
293 294 295 296 297 298 299
        offsetHeight += ptop4 + pbottom4 + mtop4 + mbottom4;

        if (nodeHeight){
            nodeHeight = nodeHeight + offsetHeight+2;
        }else {
            height = height - offsetHeight;
        }
R
roo00 已提交
300 301 302 303 304 305 306

        //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 已提交
307 308
        offsetWidth = pleft+pright+mleft+mright;
		//width = width-pleft-pright-mleft-mright;
R
roo00 已提交
309
        //if (this.content.getParent().getStyle("overflow-y")!="hidden" ) width = width-18;
NoSubject's avatar
NoSubject 已提交
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
        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 已提交
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

        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 已提交
363 364
	reCenter: function(){
		var size = this.node.getSize();
NoSubject's avatar
NoSubject 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
		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 已提交
382
        var p = o2.getCenter(size, container, container);
NoSubject's avatar
NoSubject 已提交
383
        if (p.y<0) p.y = 0;
NoSubject's avatar
NoSubject 已提交
384 385 386 387 388 389 390 391 392 393
        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 已提交
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
	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 已提交
429
			this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
R
roo00 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
		}
		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");

			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 已提交
450
			this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
R
roo00 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464
		}
		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 已提交
465
			this.morph = new Fx.Morph(this.node, {duration: this.options.duration, "transition": this.options.transition});
R
roo00 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
		}
		
		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");
		}
	}
});