Project

General

Profile

1
// (c) dynarch.com 2003-2004
2
// Distributed under the same terms as HTMLArea itself.
3

    
4
function PopupWin(editor, title, handler, initFunction) {
5
	this.editor = editor;
6
	this.handler = handler;
7
	var dlg = window.open("", "__ha_dialog",
8
			      "toolbar=no,menubar=no,personalbar=no,width=600,height=600,left=20,top=40" +
9
			      "scrollbars=no,resizable=no");
10
	this.window = dlg;
11
	var doc = dlg.document;
12
	this.doc = doc;
13
	var self = this;
14

    
15
	var base = document.baseURI || document.URL;
16
	if (base && base.match(/(.*)\/([^\/]+)/)) {
17
		base = RegExp.$1 + "/";
18
	}
19
	if (typeof _editor_url != "undefined" && !/^\//.test(_editor_url)) {
20
		// _editor_url doesn't start with '/' which means it's relative
21
		// FIXME: there's a problem here, it could be http:// which
22
		// doesn't start with slash but it's not relative either.
23
		base += _editor_url;
24
	} else
25
		base = _editor_url;
26
	if (!/\/$/.test(base)) {
27
		// base does not end in slash, add it now
28
		base += '/';
29
	}
30
	this.baseURL = base;
31

    
32
	doc.open();
33
	var html = "<html><head><title>" + title + "</title>\n";
34
	// html += "<base href='" + base + "htmlarea.js' />\n";
35
	html += "<style type='text/css'>@import url(" + base + "htmlarea.css);</style></head>\n";
36
	html += "<body class='dialog popupwin' id='--HA-body'></body></html>";
37
	doc.write(html);
38
	doc.close();
39

    
40
	// sometimes I Hate Mozilla... ;-(
41
	function init2() {
42
		var body = doc.body;
43
		if (!body) {
44
			setTimeout(init2, 25);
45
			return false;
46
		}
47
		dlg.title = title;
48
		doc.documentElement.style.padding = "0px";
49
		doc.documentElement.style.margin = "0px";
50
		var content = doc.createElement("div");
51
		content.className = "content";
52
		self.content = content;
53
		body.appendChild(content);
54
		self.element = body;
55
		initFunction(self);
56
		dlg.focus();
57
	};
58
	init2();
59
};
60

    
61
PopupWin.prototype.callHandler = function() {
62
	var tags = ["input", "textarea", "select"];
63
	var params = new Object();
64
	for (var ti in tags) {
65
		var tag = tags[ti];
66
		var els = this.content.getElementsByTagName(tag);
67
		for (var j = 0; j < els.length; ++j) {
68
			var el = els[j];
69
			var val = el.value;
70
			if (el.tagName.toLowerCase() == "input") {
71
				if (el.type == "checkbox") {
72
					val = el.checked;
73
				}
74
			}
75
			params[el.name] = val;
76
		}
77
	}
78
	this.handler(this, params);
79
	return false;
80
};
81

    
82
PopupWin.prototype.close = function() {
83
	this.window.close();
84
};
85

    
86
PopupWin.prototype.addButtons = function() {
87
	var self = this;
88
	var div = this.doc.createElement("div");
89
	this.content.appendChild(div);
90
	div.className = "buttons";
91
	for (var i = 0; i < arguments.length; ++i) {
92
		var btn = arguments[i];
93
		var button = this.doc.createElement("button");
94
		div.appendChild(button);
95
		button.innerHTML = HTMLArea.I18N.buttons[btn];
96
		switch (btn) {
97
		    case "ok":
98
			button.onclick = function() {
99
				self.callHandler();
100
				self.close();
101
				return false;
102
			};
103
			break;
104
		    case "cancel":
105
			button.onclick = function() {
106
				self.close();
107
				return false;
108
			};
109
			break;
110
		}
111
	}
112
};
113

    
114
PopupWin.prototype.showAtElement = function() {
115
	var self = this;
116
	// Mozilla needs some time to realize what's goin' on..
117
	setTimeout(function() {
118
		var w = self.content.offsetWidth + 4;
119
		var h = self.content.offsetHeight + 4;
120
		// size to content -- that's fuckin' buggy in all fuckin' browsers!!!
121
		// so that we set a larger size for the dialog window and then center
122
		// the element inside... phuck!
123

    
124
		// center...
125
		var el = self.content;
126
		var s = el.style;
127
		// s.width = el.offsetWidth + "px";
128
		// s.height = el.offsetHeight + "px";
129
		s.position = "absolute";
130
		s.left = (w - el.offsetWidth) / 2 + "px";
131
		s.top = (h - el.offsetHeight) / 2 + "px";
132
		if (HTMLArea.is_gecko) {
133
			self.window.innerWidth = w;
134
			self.window.innerHeight = h;
135
		} else {
136
			self.window.resizeTo(w + 8, h + 35);
137
		}
138
	}, 25);
139
};
(7-7/9)