1 /** This file is derived from PopupDiv, developed by Mihai Bazon for
2 * SamWare.net. Modifications were needed to make it usable in HTMLArea.
3 * HTMLArea is a free WYSIWYG online HTML editor from InteractiveTools.com.
5 * This file does not function standalone. It is dependent of global functions
6 * defined in HTMLArea-3.0 (htmlarea.js).
8 * Please see file htmlarea.js for further details.
11 var is_ie = ( (navigator.userAgent.toLowerCase().indexOf("msie") != -1) &&
12 (navigator.userAgent.toLowerCase().indexOf("opera") == -1) );
13 var is_compat = (document.compatMode == "BackCompat");
15 function PopupDiv(editor, titleText, handler, initFunction) {
19 this.doc = editor._mdoc;
20 this.handler = handler;
22 var el = this.doc.createElement("div");
23 el.className = "content";
25 var popup = this.doc.createElement("div");
26 popup.className = "dialog popupdiv";
29 s.position = "absolute";
33 var title = this.doc.createElement("div");
34 title.className = "title";
36 popup.appendChild(title);
38 HTMLArea._addEvent(title, "mousedown", function(ev) {
39 self._dragStart(is_ie ? window.event : ev);
42 var button = this.doc.createElement("div");
43 button.className = "button";
44 title.appendChild(button);
45 button.innerHTML = "×";
46 title.appendChild(this.doc.createTextNode(titleText));
47 this.titleText = titleText;
49 button.onmouseover = function() {
50 this.className += " button-hilite";
52 button.onmouseout = function() {
53 this.className = this.className.replace(/\s*button-hilite\s*/g, " ");
55 button.onclick = function() {
56 this.className = this.className.replace(/\s*button-hilite\s*/g, " ");
60 popup.appendChild(el);
63 this.doc.body.appendChild(popup);
65 this.dragging = false;
73 PopupDiv.currentPopup = null;
75 PopupDiv.prototype.showAtElement = function(el, mode) {
78 var popup = this.element;
79 popup.style.display = "block";
80 var w = popup.offsetWidth;
81 var h = popup.offsetHeight;
82 popup.style.display = "none";
84 pos = PopupDiv.getAbsolutePos(el);
89 var size = PopupDiv.getWindowSize();
93 var FX = false, FY = false;
94 if (mode.indexOf("l") != -1) {
98 if (mode.indexOf("r") != -1) {
102 if (mode.indexOf("t") != -1) {
106 if (mode.indexOf("b") != -1) {
110 if (mode.indexOf("c") != -1) {
111 FX || (pos.x += Math.round((ew - w) / 2));
112 FY || (pos.y += Math.round((eh - h) / 2));
114 this.showAt(pos.x, pos.y);
117 PopupDiv.prototype.defaultSize = function() {
118 var s = this.element.style;
119 var cs = this.element.currentStyle;
120 var addX = (is_ie && is_compat) ? (parseInt(cs.borderLeftWidth) +
121 parseInt(cs.borderRightWidth) +
122 parseInt(cs.paddingLeft) +
123 parseInt(cs.paddingRight)) : 0;
124 var addY = (is_ie && is_compat) ? (parseInt(cs.borderTopWidth) +
125 parseInt(cs.borderBottomWidth) +
126 parseInt(cs.paddingTop) +
127 parseInt(cs.paddingBottom)) : 0;
129 s.width = (this.content.offsetWidth + addX) + "px";
130 s.height = (this.content.offsetHeight + this.title.offsetHeight) + "px";
134 PopupDiv.prototype.showAt = function(x, y) {
136 var s = this.element.style;
140 this.hideShowCovered();
142 PopupDiv.currentPopup = this;
143 HTMLArea._addEvents(this.doc.body, ["mousedown", "click"], PopupDiv.checkPopup);
144 HTMLArea._addEvents(this.editor._doc.body, ["mousedown", "click"], PopupDiv.checkPopup);
145 if (is_ie && this.modal) {
146 this.doc.body.setCapture(false);
147 this.doc.body.onlosecapture = function() {
148 (PopupDiv.currentPopup) && (this.doc.body.setCapture(false));
151 window.event && HTMLArea._stopEvent(window.event);
153 if (typeof this.onShow == "function") {
155 } else if (typeof this.onShow == "string") {
159 var field = this.element.getElementsByTagName("input")[0];
161 field = this.element.getElementsByTagName("select")[0];
164 field = this.element.getElementsByTagName("textarea")[0];
171 PopupDiv.prototype.close = function() {
172 this.element.style.display = "none";
173 PopupDiv.currentPopup = null;
174 this.hideShowCovered();
175 HTMLArea._removeEvents(this.doc.body, ["mousedown", "click"], PopupDiv.checkPopup);
176 HTMLArea._removeEvents(this.editor._doc.body, ["mousedown", "click"], PopupDiv.checkPopup);
177 is_ie && this.modal && this.doc.body.releaseCapture();
178 if (typeof this.onClose == "function") {
180 } else if (typeof this.onClose == "string") {
183 this.element.parentNode.removeChild(this.element);
186 PopupDiv.prototype.getForm = function() {
187 var forms = this.content.getElementsByTagName("form");
188 return (forms.length > 0) ? forms[0] : null;
191 PopupDiv.prototype.callHandler = function() {
192 var tags = ["input", "textarea", "select"];
193 var params = new Object();
194 for (var ti in tags) {
196 var els = this.content.getElementsByTagName(tag);
197 for (var j = 0; j < els.length; ++j) {
199 params[el.name] = el.value;
202 this.handler(this, params);
206 PopupDiv.getAbsolutePos = function(el) {
207 var r = { x: el.offsetLeft, y: el.offsetTop };
208 if (el.offsetParent) {
209 var tmp = PopupDiv.getAbsolutePos(el.offsetParent);
216 PopupDiv.getWindowSize = function() {
217 if (window.innerHeight) {
218 return { y: window.innerHeight, x: window.innerWidth };
220 if (this.doc.body.clientHeight) {
221 return { y: this.doc.body.clientHeight, x: this.doc.body.clientWidth };
223 return { y: this.doc.documentElement.clientHeight, x: this.doc.documentElement.clientWidth };
226 PopupDiv.prototype.hideShowCovered = function () {
228 function isContained(el) {
230 if (el == self.element) {
237 var tags = new Array("applet", "select");
238 var el = this.element;
240 var p = PopupDiv.getAbsolutePos(el);
242 var EX2 = el.offsetWidth + EX1;
244 var EY2 = el.offsetHeight + EY1;
246 if (el.style.display == "none") {
247 EX1 = EX2 = EY1 = EY2 = 0;
250 for (var k = tags.length; k > 0; ) {
251 var ar = this.doc.getElementsByTagName(tags[--k]);
254 for (var i = ar.length; i > 0;) {
256 if (isContained(cc)) {
257 cc.style.visibility = "visible";
261 p = PopupDiv.getAbsolutePos(cc);
263 var CX2 = cc.offsetWidth + CX1;
265 var CY2 = cc.offsetHeight + CY1;
267 if ((CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
268 cc.style.visibility = "visible";
270 cc.style.visibility = "hidden";
276 PopupDiv.prototype._dragStart = function (ev) {
280 this.dragging = true;
281 PopupDiv.currentPopup = this;
282 var posX = ev.clientX;
283 var posY = ev.clientY;
285 posY += this.doc.body.scrollTop;
286 posX += this.doc.body.scrollLeft;
288 posY += window.scrollY;
289 posX += window.scrollX;
291 var st = this.element.style;
292 this.xOffs = posX - parseInt(st.left);
293 this.yOffs = posY - parseInt(st.top);
294 HTMLArea._addEvent(this.doc, "mousemove", PopupDiv.dragIt);
295 HTMLArea._addEvent(this.doc, "mouseover", HTMLArea._stopEvent);
296 HTMLArea._addEvent(this.doc, "mouseup", PopupDiv.dragEnd);
297 HTMLArea._stopEvent(ev);
300 PopupDiv.dragIt = function (ev) {
301 var popup = PopupDiv.currentPopup;
302 if (!(popup && popup.dragging)) {
305 is_ie && (ev = window.event);
306 var posX = ev.clientX;
307 var posY = ev.clientY;
309 posY += this.doc.body.scrollTop;
310 posX += this.doc.body.scrollLeft;
312 posY += window.scrollY;
313 posX += window.scrollX;
315 popup.hideShowCovered();
316 var st = popup.element.style;
317 st.left = (posX - popup.xOffs) + "px";
318 st.top = (posY - popup.yOffs) + "px";
319 HTMLArea._stopEvent(ev);
322 PopupDiv.dragEnd = function () {
323 var popup = PopupDiv.currentPopup;
327 popup.dragging = false;
328 HTMLArea._removeEvent(popup.doc, "mouseup", PopupDiv.dragEnd);
329 HTMLArea._removeEvent(popup.doc, "mouseover", HTMLArea._stopEvent);
330 HTMLArea._removeEvent(popup.doc, "mousemove", PopupDiv.dragIt);
331 popup.hideShowCovered();
334 PopupDiv.checkPopup = function (ev) {
335 is_ie && (ev = window.event);
336 var el = is_ie ? ev.srcElement : ev.target;
337 var cp = PopupDiv.currentPopup;
338 for (; (el != null) && (el != cp.element); el = el.parentNode);
340 cp.modal || ev.type == "mouseover" || cp.close();
341 HTMLArea._stopEvent(ev);
345 PopupDiv.prototype.addButtons = function() {
347 var div = this.doc.createElement("div");
348 this.content.appendChild(div);
349 div.className = "buttons";
350 for (var i = 0; i < arguments.length; ++i) {
351 var btn = arguments[i];
352 var button = this.doc.createElement("button");
353 div.appendChild(button);
354 button.innerHTML = HTMLArea.I18N.buttons[btn];
357 button.onclick = function() {
363 button.onclick = function() {