initial commit
[JIRC.git] / public / windows_js_1.3 / javascripts / window.js
CommitLineData
39c8b14f 1// Copyright (c) 2006 Sébastien Gruhier (http://xilinus.com, http://itseb.com)
2//
3// Permission is hereby granted, free of charge, to any person obtaining
4// a copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to
8// permit persons to whom the Software is furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be
12// included in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21//
22// VERSION 1.3
23
24var Window = Class.create();
25
26Window.keepMultiModalWindow = false;
27Window.hasEffectLib = (typeof Effect != 'undefined');
28Window.resizeEffectDuration = 0.4;
29
30Window.prototype = {
31 // Constructor
32 // Available parameters : className, blurClassName, title, minWidth, minHeight, maxWidth, maxHeight, width, height, top, left, bottom, right, resizable, zIndex, opacity, recenterAuto, wiredDrag
33 // hideEffect, showEffect, showEffectOptions, hideEffectOptions, effectOptions, url, draggable, closable, minimizable, maximizable, parent, onload
34 // add all callbacks (if you do not use an observer)
35 // onDestroy onStartResize onStartMove onResize onMove onEndResize onEndMove onFocus onBlur onBeforeShow onShow onHide onMinimize onMaximize onClose
36
37 initialize: function() {
38 var id;
39 var optionIndex = 0;
40 // For backward compatibility like win= new Window("id", {...}) instead of win = new Window({id: "id", ...})
41 if (arguments.length > 0) {
42 if (typeof arguments[0] == "string" ) {
43 id = arguments[0];
44 optionIndex = 1;
45 }
46 else
47 id = arguments[0] ? arguments[0].id : null;
48 }
49
50 // Generate unique ID if not specified
51 if (!id)
52 id = "window_" + new Date().getTime();
53
54 if ($(id))
55 alert("Window " + id + " is already registered in the DOM! Make sure you use setDestroyOnClose() or destroyOnClose: true in the constructor");
56
57 this.options = Object.extend({
58 className: "dialog",
59 blurClassName: null,
60 minWidth: 100,
61 minHeight: 20,
62 resizable: true,
63 closable: true,
64 minimizable: true,
65 maximizable: true,
66 draggable: true,
67 userData: null,
68 showEffect: (Window.hasEffectLib ? Effect.Appear : Element.show),
69 hideEffect: (Window.hasEffectLib ? Effect.Fade : Element.hide),
70 showEffectOptions: {},
71 hideEffectOptions: {},
72 effectOptions: null,
73 parent: document.body,
74 title: " ",
75 url: null,
76 onload: Prototype.emptyFunction,
77 width: 200,
78 height: 300,
79 opacity: 1,
80 recenterAuto: true,
81 wiredDrag: false,
82 closeCallback: null,
83 destroyOnClose: false,
84 gridX: 1,
85 gridY: 1
86 }, arguments[optionIndex] || {});
87 if (this.options.blurClassName)
88 this.options.focusClassName = this.options.className;
89
90 if (typeof this.options.top == "undefined" && typeof this.options.bottom == "undefined")
91 this.options.top = this._round(Math.random()*500, this.options.gridY);
92 if (typeof this.options.left == "undefined" && typeof this.options.right == "undefined")
93 this.options.left = this._round(Math.random()*500, this.options.gridX);
94
95 if (this.options.effectOptions) {
96 Object.extend(this.options.hideEffectOptions, this.options.effectOptions);
97 Object.extend(this.options.showEffectOptions, this.options.effectOptions);
98 if (this.options.showEffect == Element.Appear)
99 this.options.showEffectOptions.to = this.options.opacity;
100 }
101 if (Window.hasEffectLib) {
102 if (this.options.showEffect == Effect.Appear)
103 this.options.showEffectOptions.to = this.options.opacity;
104
105 if (this.options.hideEffect == Effect.Fade)
106 this.options.hideEffectOptions.from = this.options.opacity;
107 }
108 if (this.options.hideEffect == Element.hide)
109 this.options.hideEffect = function(){ Element.hide(this.element); if (this.options.destroyOnClose) this.destroy(); }.bind(this)
110
111 if (this.options.parent != document.body)
112 this.options.parent = $(this.options.parent);
113
114 this.element = this._createWindow(id);
115 this.element.win = this;
116
117 // Bind event listener
118 this.eventMouseDown = this._initDrag.bindAsEventListener(this);
119 this.eventMouseUp = this._endDrag.bindAsEventListener(this);
120 this.eventMouseMove = this._updateDrag.bindAsEventListener(this);
121 this.eventOnLoad = this._getWindowBorderSize.bindAsEventListener(this);
122 this.eventMouseDownContent = this.toFront.bindAsEventListener(this);
123 this.eventResize = this._recenter.bindAsEventListener(this);
124
125 this.topbar = $(this.element.id + "_top");
126 this.bottombar = $(this.element.id + "_bottom");
127 this.content = $(this.element.id + "_content");
128
129 Event.observe(this.topbar, "mousedown", this.eventMouseDown);
130 Event.observe(this.bottombar, "mousedown", this.eventMouseDown);
131 Event.observe(this.content, "mousedown", this.eventMouseDownContent);
132 Event.observe(window, "load", this.eventOnLoad);
133 Event.observe(window, "resize", this.eventResize);
134 Event.observe(window, "scroll", this.eventResize);
135 Event.observe(this.options.parent, "scroll", this.eventResize);
136
137 if (this.options.draggable) {
138 var that = this;
139 [this.topbar, this.topbar.up().previous(), this.topbar.up().next()].each(function(element) {
140 element.observe("mousedown", that.eventMouseDown);
141 element.addClassName("top_draggable");
142 });
143 [this.bottombar.up(), this.bottombar.up().previous(), this.bottombar.up().next()].each(function(element) {
144 element.observe("mousedown", that.eventMouseDown);
145 element.addClassName("bottom_draggable");
146 });
147
148 }
149
150 if (this.options.resizable) {
151 this.sizer = $(this.element.id + "_sizer");
152 Event.observe(this.sizer, "mousedown", this.eventMouseDown);
153 }
154
155 this.useLeft = null;
156 this.useTop = null;
157 if (typeof this.options.left != "undefined") {
158 this.element.setStyle({left: parseFloat(this.options.left) + 'px'});
159 this.useLeft = true;
160 }
161 else {
162 this.element.setStyle({right: parseFloat(this.options.right) + 'px'});
163 this.useLeft = false;
164 }
165
166 if (typeof this.options.top != "undefined") {
167 this.element.setStyle({top: parseFloat(this.options.top) + 'px'});
168 this.useTop = true;
169 }
170 else {
171 this.element.setStyle({bottom: parseFloat(this.options.bottom) + 'px'});
172 this.useTop = false;
173 }
174
175 this.storedLocation = null;
176
177 this.setOpacity(this.options.opacity);
178 if (this.options.zIndex)
179 this.setZIndex(this.options.zIndex)
180
181 if (this.options.destroyOnClose)
182 this.setDestroyOnClose(true);
183
184 this._getWindowBorderSize();
185 this.width = this.options.width;
186 this.height = this.options.height;
187 this.visible = false;
188
189 this.constraint = false;
190 this.constraintPad = {top: 0, left:0, bottom:0, right:0};
191
192 if (this.width && this.height)
193 this.setSize(this.options.width, this.options.height);
194 this.setTitle(this.options.title)
195 Windows.register(this);
196 },
197
198 // Destructor
199 destroy: function() {
200 this._notify("onDestroy");
201 Event.stopObserving(this.topbar, "mousedown", this.eventMouseDown);
202 Event.stopObserving(this.bottombar, "mousedown", this.eventMouseDown);
203 Event.stopObserving(this.content, "mousedown", this.eventMouseDownContent);
204
205 Event.stopObserving(window, "load", this.eventOnLoad);
206 Event.stopObserving(window, "resize", this.eventResize);
207 Event.stopObserving(window, "scroll", this.eventResize);
208
209 Event.stopObserving(this.content, "load", this.options.onload);
210
211 if (this._oldParent) {
212 var content = this.getContent();
213 var originalContent = null;
214 for(var i = 0; i < content.childNodes.length; i++) {
215 originalContent = content.childNodes[i];
216 if (originalContent.nodeType == 1)
217 break;
218 originalContent = null;
219 }
220 if (originalContent)
221 this._oldParent.appendChild(originalContent);
222 this._oldParent = null;
223 }
224
225 if (this.sizer)
226 Event.stopObserving(this.sizer, "mousedown", this.eventMouseDown);
227
228 if (this.options.url)
229 this.content.src = null
230
231 if(this.iefix)
232 Element.remove(this.iefix);
233
234 Element.remove(this.element);
235 Windows.unregister(this);
236 },
237
238 // Sets close callback, if it sets, it should return true to be able to close the window.
239 setCloseCallback: function(callback) {
240 this.options.closeCallback = callback;
241 },
242
243 // Gets window content
244 getContent: function () {
245 return this.content;
246 },
247
248 // Sets the content with an element id
249 setContent: function(id, autoresize, autoposition) {
250 var element = $(id);
251 if (null == element) throw "Unable to find element '" + id + "' in DOM";
252 this._oldParent = element.parentNode;
253
254 var d = null;
255 var p = null;
256
257 if (autoresize)
258 d = Element.getDimensions(element);
259 if (autoposition)
260 p = Position.cumulativeOffset(element);
261
262 var content = this.getContent();
263 // Clear HTML (and even iframe)
264 this.setHTMLContent("");
265 content = this.getContent();
266
267 content.appendChild(element);
268 element.show();
269 if (autoresize)
270 this.setSize(d.width, d.height);
271 if (autoposition)
272 this.setLocation(p[1] - this.heightN, p[0] - this.widthW);
273 },
274
275 setHTMLContent: function(html) {
276 // It was an url (iframe), recreate a div content instead of iframe content
277 if (this.options.url) {
278 this.content.src = null;
279 this.options.url = null;
280
281 var content ="<div id=\"" + this.getId() + "_content\" class=\"" + this.options.className + "_content\"> </div>";
282 $(this.getId() +"_table_content").innerHTML = content;
283
284 this.content = $(this.element.id + "_content");
285 }
286
287 this.getContent().innerHTML = html;
288 },
289
290 setAjaxContent: function(url, options, showCentered, showModal) {
291 this.showFunction = showCentered ? "showCenter" : "show";
292 this.showModal = showModal || false;
293
294 options = options || {};
295
296 // Clear HTML (and even iframe)
297 this.setHTMLContent("");
298
299 this.onComplete = options.onComplete;
300 if (! this._onCompleteHandler)
301 this._onCompleteHandler = this._setAjaxContent.bind(this);
302 options.onComplete = this._onCompleteHandler;
303
304 new Ajax.Request(url, options);
305 options.onComplete = this.onComplete;
306 },
307
308 _setAjaxContent: function(originalRequest) {
309 Element.update(this.getContent(), originalRequest.responseText);
310 if (this.onComplete)
311 this.onComplete(originalRequest);
312 this.onComplete = null;
313 this[this.showFunction](this.showModal)
314 },
315
316 setURL: function(url) {
317 // Not an url content, change div to iframe
318 if (this.options.url)
319 this.content.src = null;
320 this.options.url = url;
321 var content= "<iframe frameborder='0' name='" + this.getId() + "_content' id='" + this.getId() + "_content' src='" + url + "' width='" + this.width + "' height='" + this.height + "'> </iframe>";
322 $(this.getId() +"_table_content").innerHTML = content;
323
324 this.content = $(this.element.id + "_content");
325 },
326
327 getURL: function() {
328 return this.options.url ? this.options.url : null;
329 },
330
331 refresh: function() {
332 if (this.options.url)
333 $(this.element.getAttribute('id') + '_content').src = this.options.url;
334 },
335
336 // Stores position/size in a cookie, by default named with window id
337 setCookie: function(name, expires, path, domain, secure) {
338 name = name || this.element.id;
339 this.cookie = [name, expires, path, domain, secure];
340
341 // Get cookie
342 var value = WindowUtilities.getCookie(name)
343 // If exists
344 if (value) {
345 var values = value.split(',');
346 var x = values[0].split(':');
347 var y = values[1].split(':');
348
349 var w = parseFloat(values[2]), h = parseFloat(values[3]);
350 var mini = values[4];
351 var maxi = values[5];
352
353 this.setSize(w, h);
354 if (mini == "true")
355 this.doMinimize = true; // Minimize will be done at onload window event
356 else if (maxi == "true")
357 this.doMaximize = true; // Maximize will be done at onload window event
358
359 this.useLeft = x[0] == "l";
360 this.useTop = y[0] == "t";
361
362 this.element.setStyle(this.useLeft ? {left: x[1]} : {right: x[1]});
363 this.element.setStyle(this.useTop ? {top: y[1]} : {bottom: y[1]});
364 }
365 },
366
367 // Gets window ID
368 getId: function() {
369 return this.element.id;
370 },
371
372 // Detroys itself when closing
373 setDestroyOnClose: function() {
374 this.options.destroyOnClose = true;
375 },
376
377 setConstraint: function(bool, padding) {
378 this.constraint = bool;
379 this.constraintPad = Object.extend(this.constraintPad, padding || {});
380 // Reset location to apply constraint
381 if (this.useTop && this.useLeft)
382 this.setLocation(parseFloat(this.element.style.top), parseFloat(this.element.style.left));
383 },
384
385 // initDrag event
386
387 _initDrag: function(event) {
388 // No resize on minimized window
389 if (Event.element(event) == this.sizer && this.isMinimized())
390 return;
391
392 // No move on maximzed window
393 if (Event.element(event) != this.sizer && this.isMaximized())
394 return;
395
396 if (Prototype.Browser.IE && this.heightN == 0)
397 this._getWindowBorderSize();
398
399 // Get pointer X,Y
400 this.pointer = [this._round(Event.pointerX(event), this.options.gridX), this._round(Event.pointerY(event), this.options.gridY)];
401 if (this.options.wiredDrag)
402 this.currentDrag = this._createWiredElement();
403 else
404 this.currentDrag = this.element;
405
406 // Resize
407 if (Event.element(event) == this.sizer) {
408 this.doResize = true;
409 this.widthOrg = this.width;
410 this.heightOrg = this.height;
411 this.bottomOrg = parseFloat(this.element.getStyle('bottom'));
412 this.rightOrg = parseFloat(this.element.getStyle('right'));
413 this._notify("onStartResize");
414 }
415 else {
416 this.doResize = false;
417
418 // Check if click on close button,
419 var closeButton = $(this.getId() + '_close');
420 if (closeButton && Position.within(closeButton, this.pointer[0], this.pointer[1])) {
421 this.currentDrag = null;
422 return;
423 }
424
425 this.toFront();
426
427 if (! this.options.draggable)
428 return;
429 this._notify("onStartMove");
430 }
431 // Register global event to capture mouseUp and mouseMove
432 Event.observe(document, "mouseup", this.eventMouseUp, false);
433 Event.observe(document, "mousemove", this.eventMouseMove, false);
434
435 // Add an invisible div to keep catching mouse event over iframes
436 WindowUtilities.disableScreen('__invisible__', '__invisible__', this.overlayOpacity);
437
438 // Stop selection while dragging
439 document.body.ondrag = function () { return false; };
440 document.body.onselectstart = function () { return false; };
441
442 this.currentDrag.show();
443 Event.stop(event);
444 },
445
446 _round: function(val, round) {
447 return round == 1 ? val : val = Math.floor(val / round) * round;
448 },
449
450 // updateDrag event
451 _updateDrag: function(event) {
452 var pointer = [this._round(Event.pointerX(event), this.options.gridX), this._round(Event.pointerY(event), this.options.gridY)];
453 var dx = pointer[0] - this.pointer[0];
454 var dy = pointer[1] - this.pointer[1];
455
456 // Resize case, update width/height
457 if (this.doResize) {
458 var w = this.widthOrg + dx;
459 var h = this.heightOrg + dy;
460
461 dx = this.width - this.widthOrg
462 dy = this.height - this.heightOrg
463
464 // Check if it's a right position, update it to keep upper-left corner at the same position
465 if (this.useLeft)
466 w = this._updateWidthConstraint(w)
467 else
468 this.currentDrag.setStyle({right: (this.rightOrg -dx) + 'px'});
469 // Check if it's a bottom position, update it to keep upper-left corner at the same position
470 if (this.useTop)
471 h = this._updateHeightConstraint(h)
472 else
473 this.currentDrag.setStyle({bottom: (this.bottomOrg -dy) + 'px'});
474
475 this.setSize(w , h);
476 this._notify("onResize");
477 }
478 // Move case, update top/left
479 else {
480 this.pointer = pointer;
481
482 if (this.useLeft) {
483 var left = parseFloat(this.currentDrag.getStyle('left')) + dx;
484 var newLeft = this._updateLeftConstraint(left);
485 // Keep mouse pointer correct
486 this.pointer[0] += newLeft-left;
487 this.currentDrag.setStyle({left: newLeft + 'px'});
488 }
489 else
490 this.currentDrag.setStyle({right: parseFloat(this.currentDrag.getStyle('right')) - dx + 'px'});
491
492 if (this.useTop) {
493 var top = parseFloat(this.currentDrag.getStyle('top')) + dy;
494 var newTop = this._updateTopConstraint(top);
495 // Keep mouse pointer correct
496 this.pointer[1] += newTop - top;
497 this.currentDrag.setStyle({top: newTop + 'px'});
498 }
499 else
500 this.currentDrag.setStyle({bottom: parseFloat(this.currentDrag.getStyle('bottom')) - dy + 'px'});
501
502 this._notify("onMove");
503 }
504 if (this.iefix)
505 this._fixIEOverlapping();
506
507 this._removeStoreLocation();
508 Event.stop(event);
509 },
510
511 // endDrag callback
512 _endDrag: function(event) {
513 // Remove temporary div over iframes
514 WindowUtilities.enableScreen('__invisible__');
515
516 if (this.doResize)
517 this._notify("onEndResize");
518 else
519 this._notify("onEndMove");
520
521 // Release event observing
522 Event.stopObserving(document, "mouseup", this.eventMouseUp,false);
523 Event.stopObserving(document, "mousemove", this.eventMouseMove, false);
524
525 Event.stop(event);
526
527 this._hideWiredElement();
528
529 // Store new location/size if need be
530 this._saveCookie()
531
532 // Restore selection
533 document.body.ondrag = null;
534 document.body.onselectstart = null;
535 },
536
537 _updateLeftConstraint: function(left) {
538 if (this.constraint && this.useLeft && this.useTop) {
539 var width = this.options.parent == document.body ? WindowUtilities.getPageSize().windowWidth : this.options.parent.getDimensions().width;
540
541 if (left < this.constraintPad.left)
542 left = this.constraintPad.left;
543 if (left + this.width + this.widthE + this.widthW > width - this.constraintPad.right)
544 left = width - this.constraintPad.right - this.width - this.widthE - this.widthW;
545 }
546 return left;
547 },
548
549 _updateTopConstraint: function(top) {
550 if (this.constraint && this.useLeft && this.useTop) {
551 var height = this.options.parent == document.body ? WindowUtilities.getPageSize().windowHeight : this.options.parent.getDimensions().height;
552
553 var h = this.height + this.heightN + this.heightS;
554
555 if (top < this.constraintPad.top)
556 top = this.constraintPad.top;
557 if (top + h > height - this.constraintPad.bottom)
558 top = height - this.constraintPad.bottom - h;
559 }
560 return top;
561 },
562
563 _updateWidthConstraint: function(w) {
564 if (this.constraint && this.useLeft && this.useTop) {
565 var width = this.options.parent == document.body ? WindowUtilities.getPageSize().windowWidth : this.options.parent.getDimensions().width;
566 var left = parseFloat(this.element.getStyle("left"));
567
568 if (left + w + this.widthE + this.widthW > width - this.constraintPad.right)
569 w = width - this.constraintPad.right - left - this.widthE - this.widthW;
570 }
571 return w;
572 },
573
574 _updateHeightConstraint: function(h) {
575 if (this.constraint && this.useLeft && this.useTop) {
576 var height = this.options.parent == document.body ? WindowUtilities.getPageSize().windowHeight : this.options.parent.getDimensions().height;
577 var top = parseFloat(this.element.getStyle("top"));
578
579 if (top + h + this.heightN + this.heightS > height - this.constraintPad.bottom)
580 h = height - this.constraintPad.bottom - top - this.heightN - this.heightS;
581 }
582 return h;
583 },
584
585
586 // Creates HTML window code
587 _createWindow: function(id) {
588 var className = this.options.className;
589 var win = document.createElement("div");
590 win.setAttribute('id', id);
591 win.className = "dialog";
592
593 var content;
594 if (this.options.url)
595 content= "<iframe frameborder=\"0\" name=\"" + id + "_content\" id=\"" + id + "_content\" src=\"" + this.options.url + "\"> </iframe>";
596 else
597 content ="<div id=\"" + id + "_content\" class=\"" +className + "_content\"> </div>";
598
599 var closeDiv = this.options.closable ? "<div class='"+ className +"_close' id='"+ id +"_close' onclick='Windows.close(\""+ id +"\", event)'> </div>" : "";
600 var minDiv = this.options.minimizable ? "<div class='"+ className + "_minimize' id='"+ id +"_minimize' onclick='Windows.minimize(\""+ id +"\", event)'> </div>" : "";
601 var maxDiv = this.options.maximizable ? "<div class='"+ className + "_maximize' id='"+ id +"_maximize' onclick='Windows.maximize(\""+ id +"\", event)'> </div>" : "";
602 var seAttributes = this.options.resizable ? "class='" + className + "_sizer' id='" + id + "_sizer'" : "class='" + className + "_se'";
603 var blank = "../themes/default/blank.gif";
604
605 win.innerHTML = closeDiv + minDiv + maxDiv + "\
606 <table id='"+ id +"_row1' class=\"top table_window\">\
607 <tr>\
608 <td class='"+ className +"_nw'></td>\
609 <td class='"+ className +"_n'><div id='"+ id +"_top' class='"+ className +"_title title_window'>"+ this.options.title +"</div></td>\
610 <td class='"+ className +"_ne'></td>\
611 </tr>\
612 </table>\
613 <table id='"+ id +"_row2' class=\"mid table_window\">\
614 <tr>\
615 <td class='"+ className +"_w'></td>\
616 <td id='"+ id +"_table_content' class='"+ className +"_content' valign='top'>" + content + "</td>\
617 <td class='"+ className +"_e'></td>\
618 </tr>\
619 </table>\
620 <table id='"+ id +"_row3' class=\"bot table_window\">\
621 <tr>\
622 <td class='"+ className +"_sw'></td>\
623 <td class='"+ className +"_s'><div id='"+ id +"_bottom' class='status_bar'><span style='float:left; width:1px; height:1px'></span></div></td>\
624 <td " + seAttributes + "></td>\
625 </tr>\
626 </table>\
627 ";
628 Element.hide(win);
629 this.options.parent.insertBefore(win, this.options.parent.firstChild);
630 Event.observe($(id + "_content"), "load", this.options.onload);
631 return win;
632 },
633
634
635 changeClassName: function(newClassName) {
636 var className = this.options.className;
637 var id = this.getId();
638 $A(["_close", "_minimize", "_maximize", "_sizer", "_content"]).each(function(value) { this._toggleClassName($(id + value), className + value, newClassName + value) }.bind(this));
639 this._toggleClassName($(id + "_top"), className + "_title", newClassName + "_title");
640 $$("#" + id + " td").each(function(td) {td.className = td.className.sub(className,newClassName); });
641 this.options.className = newClassName;
642 },
643
644 _toggleClassName: function(element, oldClassName, newClassName) {
645 if (element) {
646 element.removeClassName(oldClassName);
647 element.addClassName(newClassName);
648 }
649 },
650
651 // Sets window location
652 setLocation: function(top, left) {
653 top = this._updateTopConstraint(top);
654 left = this._updateLeftConstraint(left);
655
656 var e = this.currentDrag || this.element;
657 e.setStyle({top: top + 'px'});
658 e.setStyle({left: left + 'px'});
659
660 this.useLeft = true;
661 this.useTop = true;
662 },
663
664 getLocation: function() {
665 var location = {};
666 if (this.useTop)
667 location = Object.extend(location, {top: this.element.getStyle("top")});
668 else
669 location = Object.extend(location, {bottom: this.element.getStyle("bottom")});
670 if (this.useLeft)
671 location = Object.extend(location, {left: this.element.getStyle("left")});
672 else
673 location = Object.extend(location, {right: this.element.getStyle("right")});
674
675 return location;
676 },
677
678 // Gets window size
679 getSize: function() {
680 return {width: this.width, height: this.height};
681 },
682
683 // Sets window size
684 setSize: function(width, height, useEffect) {
685 width = parseFloat(width);
686 height = parseFloat(height);
687
688 // Check min and max size
689 if (!this.minimized && width < this.options.minWidth)
690 width = this.options.minWidth;
691
692 if (!this.minimized && height < this.options.minHeight)
693 height = this.options.minHeight;
694
695 if (this.options. maxHeight && height > this.options. maxHeight)
696 height = this.options. maxHeight;
697
698 if (this.options. maxWidth && width > this.options. maxWidth)
699 width = this.options. maxWidth;
700
701
702 if (this.useTop && this.useLeft && Window.hasEffectLib && Effect.ResizeWindow && useEffect) {
703 new Effect.ResizeWindow(this, null, null, width, height, {duration: Window.resizeEffectDuration});
704 } else {
705 this.width = width;
706 this.height = height;
707 var e = this.currentDrag ? this.currentDrag : this.element;
708
709 e.setStyle({width: width + this.widthW + this.widthE + "px"})
710 e.setStyle({height: height + this.heightN + this.heightS + "px"})
711
712 // Update content size
713 if (!this.currentDrag || this.currentDrag == this.element) {
714 var content = $(this.element.id + '_content');
715 content.setStyle({height: height + 'px'});
716 content.setStyle({width: width + 'px'});
717 }
718 }
719 },
720
721 updateHeight: function() {
722 this.setSize(this.width, this.content.scrollHeight, true);
723 },
724
725 updateWidth: function() {
726 this.setSize(this.content.scrollWidth, this.height, true);
727 },
728
729 // Brings window to front
730 toFront: function() {
731 if (this.element.style.zIndex < Windows.maxZIndex)
732 this.setZIndex(Windows.maxZIndex + 1);
733 if (this.iefix)
734 this._fixIEOverlapping();
735 },
736
737 getBounds: function(insideOnly) {
738 if (! this.width || !this.height || !this.visible)
739 this.computeBounds();
740 var w = this.width;
741 var h = this.height;
742
743 if (!insideOnly) {
744 w += this.widthW + this.widthE;
745 h += this.heightN + this.heightS;
746 }
747 var bounds = Object.extend(this.getLocation(), {width: w + "px", height: h + "px"});
748 return bounds;
749 },
750
751 computeBounds: function() {
752 if (! this.width || !this.height) {
753 var size = WindowUtilities._computeSize(this.content.innerHTML, this.content.id, this.width, this.height, 0, this.options.className)
754 if (this.height)
755 this.width = size + 5
756 else
757 this.height = size + 5
758 }
759
760 this.setSize(this.width, this.height);
761 if (this.centered)
762 this._center(this.centerTop, this.centerLeft);
763 },
764
765 // Displays window modal state or not
766 show: function(modal) {
767 this.visible = true;
768 if (modal) {
769 // Hack for Safari !!
770 if (typeof this.overlayOpacity == "undefined") {
771 var that = this;
772 setTimeout(function() {that.show(modal)}, 10);
773 return;
774 }
775 Windows.addModalWindow(this);
776
777 this.modal = true;
778 this.setZIndex(Windows.maxZIndex + 1);
779 Windows.unsetOverflow(this);
780 }
781 else
782 if (!this.element.style.zIndex)
783 this.setZIndex(Windows.maxZIndex + 1);
784
785 // To restore overflow if need be
786 if (this.oldStyle)
787 this.getContent().setStyle({overflow: this.oldStyle});
788
789 this.computeBounds();
790
791 this._notify("onBeforeShow");
792 if (this.options.showEffect != Element.show && this.options.showEffectOptions)
793 this.options.showEffect(this.element, this.options.showEffectOptions);
794 else
795 this.options.showEffect(this.element);
796
797 this._checkIEOverlapping();
798 WindowUtilities.focusedWindow = this
799 this._notify("onShow");
800 },
801
802 // Displays window modal state or not at the center of the page
803 showCenter: function(modal, top, left) {
804 this.centered = true;
805 this.centerTop = top;
806 this.centerLeft = left;
807
808 this.show(modal);
809 },
810
811 isVisible: function() {
812 return this.visible;
813 },
814
815 _center: function(top, left) {
816 var windowScroll = WindowUtilities.getWindowScroll(this.options.parent);
817 var pageSize = WindowUtilities.getPageSize(this.options.parent);
818 if (typeof top == "undefined")
819 top = (pageSize.windowHeight - (this.height + this.heightN + this.heightS))/2;
820 top += windowScroll.top
821
822 if (typeof left == "undefined")
823 left = (pageSize.windowWidth - (this.width + this.widthW + this.widthE))/2;
824 left += windowScroll.left
825 this.setLocation(top, left);
826 this.toFront();
827 },
828
829 _recenter: function(event) {
830 if (this.centered) {
831 var pageSize = WindowUtilities.getPageSize(this.options.parent);
832 var windowScroll = WindowUtilities.getWindowScroll(this.options.parent);
833
834 // Check for this stupid IE that sends dumb events
835 if (this.pageSize && this.pageSize.windowWidth == pageSize.windowWidth && this.pageSize.windowHeight == pageSize.windowHeight &&
836 this.windowScroll.left == windowScroll.left && this.windowScroll.top == windowScroll.top)
837 return;
838 this.pageSize = pageSize;
839 this.windowScroll = windowScroll;
840 // set height of Overlay to take up whole page and show
841 if ($('overlay_modal'))
842 $('overlay_modal').setStyle({height: (pageSize.pageHeight + 'px')});
843
844 if (this.options.recenterAuto)
845 this._center(this.centerTop, this.centerLeft);
846 }
847 },
848
849 // Hides window
850 hide: function() {
851 this.visible = false;
852 if (this.modal) {
853 Windows.removeModalWindow(this);
854 Windows.resetOverflow();
855 }
856 // To avoid bug on scrolling bar
857 this.oldStyle = this.getContent().getStyle('overflow') || "auto"
858 this.getContent().setStyle({overflow: "hidden"});
859
860 this.options.hideEffect(this.element, this.options.hideEffectOptions);
861
862 if(this.iefix)
863 this.iefix.hide();
864
865 if (!this.doNotNotifyHide)
866 this._notify("onHide");
867 },
868
869 close: function() {
870 // Asks closeCallback if exists
871 if (this.visible) {
872 if (this.options.closeCallback && ! this.options.closeCallback(this))
873 return;
874
875 if (this.options.destroyOnClose) {
876 var destroyFunc = this.destroy.bind(this);
877 if (this.options.hideEffectOptions.afterFinish) {
878 var func = this.options.hideEffectOptions.afterFinish;
879 this.options.hideEffectOptions.afterFinish = function() {func();destroyFunc() }
880 }
881 else
882 this.options.hideEffectOptions.afterFinish = function() {destroyFunc() }
883 }
884 Windows.updateFocusedWindow();
885
886 this.doNotNotifyHide = true;
887 this.hide();
888 this.doNotNotifyHide = false;
889 this._notify("onClose");
890 }
891 },
892
893 minimize: function() {
894 if (this.resizing)
895 return;
896
897 var r2 = $(this.getId() + "_row2");
898
899 if (!this.minimized) {
900 this.minimized = true;
901
902 var dh = r2.getDimensions().height;
903 this.r2Height = dh;
904 var h = this.element.getHeight() - dh;
905
906 if (this.useLeft && this.useTop && Window.hasEffectLib && Effect.ResizeWindow) {
907 new Effect.ResizeWindow(this, null, null, null, this.height -dh, {duration: Window.resizeEffectDuration});
908 } else {
909 this.height -= dh;
910 this.element.setStyle({height: h + "px"});
911 r2.hide();
912 }
913
914 if (! this.useTop) {
915 var bottom = parseFloat(this.element.getStyle('bottom'));
916 this.element.setStyle({bottom: (bottom + dh) + 'px'});
917 }
918 }
919 else {
920 this.minimized = false;
921
922 var dh = this.r2Height;
923 this.r2Height = null;
924 if (this.useLeft && this.useTop && Window.hasEffectLib && Effect.ResizeWindow) {
925 new Effect.ResizeWindow(this, null, null, null, this.height + dh, {duration: Window.resizeEffectDuration});
926 }
927 else {
928 var h = this.element.getHeight() + dh;
929 this.height += dh;
930 this.element.setStyle({height: h + "px"})
931 r2.show();
932 }
933 if (! this.useTop) {
934 var bottom = parseFloat(this.element.getStyle('bottom'));
935 this.element.setStyle({bottom: (bottom - dh) + 'px'});
936 }
937 this.toFront();
938 }
939 this._notify("onMinimize");
940
941 // Store new location/size if need be
942 this._saveCookie()
943 },
944
945 maximize: function() {
946 if (this.isMinimized() || this.resizing)
947 return;
948
949 if (Prototype.Browser.IE && this.heightN == 0)
950 this._getWindowBorderSize();
951
952 if (this.storedLocation != null) {
953 this._restoreLocation();
954 if(this.iefix)
955 this.iefix.hide();
956 }
957 else {
958 this._storeLocation();
959 Windows.unsetOverflow(this);
960
961 var windowScroll = WindowUtilities.getWindowScroll(this.options.parent);
962 var pageSize = WindowUtilities.getPageSize(this.options.parent);
963 var left = windowScroll.left;
964 var top = windowScroll.top;
965
966 if (this.options.parent != document.body) {
967 windowScroll = {top:0, left:0, bottom:0, right:0};
968 var dim = this.options.parent.getDimensions();
969 pageSize.windowWidth = dim.width;
970 pageSize.windowHeight = dim.height;
971 top = 0;
972 left = 0;
973 }
974
975 if (this.constraint) {
976 pageSize.windowWidth -= Math.max(0, this.constraintPad.left) + Math.max(0, this.constraintPad.right);
977 pageSize.windowHeight -= Math.max(0, this.constraintPad.top) + Math.max(0, this.constraintPad.bottom);
978 left += Math.max(0, this.constraintPad.left);
979 top += Math.max(0, this.constraintPad.top);
980 }
981
982 var width = pageSize.windowWidth - this.widthW - this.widthE;
983 var height= pageSize.windowHeight - this.heightN - this.heightS;
984
985 if (this.useLeft && this.useTop && Window.hasEffectLib && Effect.ResizeWindow) {
986 new Effect.ResizeWindow(this, top, left, width, height, {duration: Window.resizeEffectDuration});
987 }
988 else {
989 this.setSize(width, height);
990 this.element.setStyle(this.useLeft ? {left: left} : {right: left});
991 this.element.setStyle(this.useTop ? {top: top} : {bottom: top});
992 }
993
994 this.toFront();
995 if (this.iefix)
996 this._fixIEOverlapping();
997 }
998 this._notify("onMaximize");
999
1000 // Store new location/size if need be
1001 this._saveCookie()
1002 },
1003
1004 isMinimized: function() {
1005 return this.minimized;
1006 },
1007
1008 isMaximized: function() {
1009 return (this.storedLocation != null);
1010 },
1011
1012 setOpacity: function(opacity) {
1013 if (Element.setOpacity)
1014 Element.setOpacity(this.element, opacity);
1015 },
1016
1017 setZIndex: function(zindex) {
1018 this.element.setStyle({zIndex: zindex});
1019 Windows.updateZindex(zindex, this);
1020 },
1021
1022 setTitle: function(newTitle) {
1023 if (!newTitle || newTitle == "")
1024 newTitle = "&nbsp;";
1025
1026 Element.update(this.element.id + '_top', newTitle);
1027 },
1028
1029 getTitle: function() {
1030 return $(this.element.id + '_top').innerHTML;
1031 },
1032
1033 setStatusBar: function(element) {
1034 var statusBar = $(this.getId() + "_bottom");
1035
1036 if (typeof(element) == "object") {
1037 if (this.bottombar.firstChild)
1038 this.bottombar.replaceChild(element, this.bottombar.firstChild);
1039 else
1040 this.bottombar.appendChild(element);
1041 }
1042 else
1043 this.bottombar.innerHTML = element;
1044 },
1045
1046 _checkIEOverlapping: function() {
1047 if(!this.iefix && (navigator.appVersion.indexOf('MSIE')>0) && (navigator.userAgent.indexOf('Opera')<0) && (this.element.getStyle('position')=='absolute')) {
1048 new Insertion.After(this.element.id, '<iframe id="' + this.element.id + '_iefix" '+ 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
1049 this.iefix = $(this.element.id+'_iefix');
1050 }
1051 if(this.iefix)
1052 setTimeout(this._fixIEOverlapping.bind(this), 50);
1053 },
1054
1055 _fixIEOverlapping: function() {
1056 Position.clone(this.element, this.iefix);
1057 this.iefix.style.zIndex = this.element.style.zIndex - 1;
1058 this.iefix.show();
1059 },
1060
1061 _getWindowBorderSize: function(event) {
1062 // Hack to get real window border size!!
1063 var div = this._createHiddenDiv(this.options.className + "_n")
1064 this.heightN = Element.getDimensions(div).height;
1065 div.parentNode.removeChild(div)
1066
1067 var div = this._createHiddenDiv(this.options.className + "_s")
1068 this.heightS = Element.getDimensions(div).height;
1069 div.parentNode.removeChild(div)
1070
1071 var div = this._createHiddenDiv(this.options.className + "_e")
1072 this.widthE = Element.getDimensions(div).width;
1073 div.parentNode.removeChild(div)
1074
1075 var div = this._createHiddenDiv(this.options.className + "_w")
1076 this.widthW = Element.getDimensions(div).width;
1077 div.parentNode.removeChild(div);
1078
1079 var div = document.createElement("div");
1080 div.className = "overlay_" + this.options.className ;
1081 document.body.appendChild(div);
1082 //alert("no timeout:\nopacity: " + div.getStyle("opacity") + "\nwidth: " + document.defaultView.getComputedStyle(div, null).width);
1083 var that = this;
1084
1085 // Workaround for Safari!!
1086 setTimeout(function() {that.overlayOpacity = ($(div).getStyle("opacity")); div.parentNode.removeChild(div);}, 10);
1087
1088 // Workaround for IE!!
1089 if (Prototype.Browser.IE) {
1090 this.heightS = $(this.getId() +"_row3").getDimensions().height;
1091 this.heightN = $(this.getId() +"_row1").getDimensions().height;
1092 }
1093
1094 // Safari size fix
1095 if (Prototype.Browser.WebKit && Prototype.Browser.WebKitVersion < 420)
1096 this.setSize(this.width, this.height);
1097 if (this.doMaximize)
1098 this.maximize();
1099 if (this.doMinimize)
1100 this.minimize();
1101 },
1102
1103 _createHiddenDiv: function(className) {
1104 var objBody = document.body;
1105 var win = document.createElement("div");
1106 win.setAttribute('id', this.element.id+ "_tmp");
1107 win.className = className;
1108 win.style.display = 'none';
1109 win.innerHTML = '';
1110 objBody.insertBefore(win, objBody.firstChild);
1111 return win;
1112 },
1113
1114 _storeLocation: function() {
1115 if (this.storedLocation == null) {
1116 this.storedLocation = {useTop: this.useTop, useLeft: this.useLeft,
1117 top: this.element.getStyle('top'), bottom: this.element.getStyle('bottom'),
1118 left: this.element.getStyle('left'), right: this.element.getStyle('right'),
1119 width: this.width, height: this.height };
1120 }
1121 },
1122
1123 _restoreLocation: function() {
1124 if (this.storedLocation != null) {
1125 this.useLeft = this.storedLocation.useLeft;
1126 this.useTop = this.storedLocation.useTop;
1127
1128 if (this.useLeft && this.useTop && Window.hasEffectLib && Effect.ResizeWindow)
1129 new Effect.ResizeWindow(this, this.storedLocation.top, this.storedLocation.left, this.storedLocation.width, this.storedLocation.height, {duration: Window.resizeEffectDuration});
1130 else {
1131 this.element.setStyle(this.useLeft ? {left: this.storedLocation.left} : {right: this.storedLocation.right});
1132 this.element.setStyle(this.useTop ? {top: this.storedLocation.top} : {bottom: this.storedLocation.bottom});
1133 this.setSize(this.storedLocation.width, this.storedLocation.height);
1134 }
1135
1136 Windows.resetOverflow();
1137 this._removeStoreLocation();
1138 }
1139 },
1140
1141 _removeStoreLocation: function() {
1142 this.storedLocation = null;
1143 },
1144
1145 _saveCookie: function() {
1146 if (this.cookie) {
1147 var value = "";
1148 if (this.useLeft)
1149 value += "l:" + (this.storedLocation ? this.storedLocation.left : this.element.getStyle('left'))
1150 else
1151 value += "r:" + (this.storedLocation ? this.storedLocation.right : this.element.getStyle('right'))
1152 if (this.useTop)
1153 value += ",t:" + (this.storedLocation ? this.storedLocation.top : this.element.getStyle('top'))
1154 else
1155 value += ",b:" + (this.storedLocation ? this.storedLocation.bottom :this.element.getStyle('bottom'))
1156
1157 value += "," + (this.storedLocation ? this.storedLocation.width : this.width);
1158 value += "," + (this.storedLocation ? this.storedLocation.height : this.height);
1159 value += "," + this.isMinimized();
1160 value += "," + this.isMaximized();
1161 WindowUtilities.setCookie(value, this.cookie)
1162 }
1163 },
1164
1165 _createWiredElement: function() {
1166 if (! this.wiredElement) {
1167 if (Prototype.Browser.IE)
1168 this._getWindowBorderSize();
1169 var div = document.createElement("div");
1170 div.className = "wired_frame " + this.options.className + "_wired_frame";
1171
1172 div.style.position = 'absolute';
1173 this.options.parent.insertBefore(div, this.options.parent.firstChild);
1174 this.wiredElement = $(div);
1175 }
1176 if (this.useLeft)
1177 this.wiredElement.setStyle({left: this.element.getStyle('left')});
1178 else
1179 this.wiredElement.setStyle({right: this.element.getStyle('right')});
1180
1181 if (this.useTop)
1182 this.wiredElement.setStyle({top: this.element.getStyle('top')});
1183 else
1184 this.wiredElement.setStyle({bottom: this.element.getStyle('bottom')});
1185
1186 var dim = this.element.getDimensions();
1187 this.wiredElement.setStyle({width: dim.width + "px", height: dim.height +"px"});
1188
1189 this.wiredElement.setStyle({zIndex: Windows.maxZIndex+30});
1190 return this.wiredElement;
1191 },
1192
1193 _hideWiredElement: function() {
1194 if (! this.wiredElement || ! this.currentDrag)
1195 return;
1196 if (this.currentDrag == this.element)
1197 this.currentDrag = null;
1198 else {
1199 if (this.useLeft)
1200 this.element.setStyle({left: this.currentDrag.getStyle('left')});
1201 else
1202 this.element.setStyle({right: this.currentDrag.getStyle('right')});
1203
1204 if (this.useTop)
1205 this.element.setStyle({top: this.currentDrag.getStyle('top')});
1206 else
1207 this.element.setStyle({bottom: this.currentDrag.getStyle('bottom')});
1208
1209 this.currentDrag.hide();
1210 this.currentDrag = null;
1211 if (this.doResize)
1212 this.setSize(this.width, this.height);
1213 }
1214 },
1215
1216 _notify: function(eventName) {
1217 if (this.options[eventName])
1218 this.options[eventName](this);
1219 else
1220 Windows.notify(eventName, this);
1221 }
1222};
1223
1224// Windows containers, register all page windows
1225var Windows = {
1226 windows: [],
1227 modalWindows: [],
1228 observers: [],
1229 focusedWindow: null,
1230 maxZIndex: 0,
1231 overlayShowEffectOptions: {duration: 0.5},
1232 overlayHideEffectOptions: {duration: 0.5},
1233
1234 addObserver: function(observer) {
1235 this.removeObserver(observer);
1236 this.observers.push(observer);
1237 },
1238
1239 removeObserver: function(observer) {
1240 this.observers = this.observers.reject( function(o) { return o==observer });
1241 },
1242
1243 // onDestroy onStartResize onStartMove onResize onMove onEndResize onEndMove onFocus onBlur onBeforeShow onShow onHide onMinimize onMaximize onClose
1244 notify: function(eventName, win) {
1245 this.observers.each( function(o) {if(o[eventName]) o[eventName](eventName, win);});
1246 },
1247
1248 // Gets window from its id
1249 getWindow: function(id) {
1250 return this.windows.detect(function(d) { return d.getId() ==id });
1251 },
1252
1253 // Gets the last focused window
1254 getFocusedWindow: function() {
1255 return this.focusedWindow;
1256 },
1257
1258 updateFocusedWindow: function() {
1259 this.focusedWindow = this.windows.length >=2 ? this.windows[this.windows.length-2] : null;
1260 },
1261
1262 // Registers a new window (called by Windows constructor)
1263 register: function(win) {
1264 this.windows.push(win);
1265 },
1266
1267 // Add a modal window in the stack
1268 addModalWindow: function(win) {
1269 // Disable screen if first modal window
1270 if (this.modalWindows.length == 0) {
1271 WindowUtilities.disableScreen(win.options.className, 'overlay_modal', win.overlayOpacity, win.getId(), win.options.parent);
1272 }
1273 else {
1274 // Move overlay over all windows
1275 if (Window.keepMultiModalWindow) {
1276 $('overlay_modal').style.zIndex = Windows.maxZIndex + 1;
1277 Windows.maxZIndex += 1;
1278 WindowUtilities._hideSelect(this.modalWindows.last().getId());
1279 }
1280 // Hide current modal window
1281 else
1282 this.modalWindows.last().element.hide();
1283 // Fucking IE select issue
1284 WindowUtilities._showSelect(win.getId());
1285 }
1286 this.modalWindows.push(win);
1287 },
1288
1289 removeModalWindow: function(win) {
1290 this.modalWindows.pop();
1291
1292 // No more modal windows
1293 if (this.modalWindows.length == 0)
1294 WindowUtilities.enableScreen();
1295 else {
1296 if (Window.keepMultiModalWindow) {
1297 this.modalWindows.last().toFront();
1298 WindowUtilities._showSelect(this.modalWindows.last().getId());
1299 }
1300 else
1301 this.modalWindows.last().element.show();
1302 }
1303 },
1304
1305 // Registers a new window (called by Windows constructor)
1306 register: function(win) {
1307 this.windows.push(win);
1308 },
1309
1310 // Unregisters a window (called by Windows destructor)
1311 unregister: function(win) {
1312 this.windows = this.windows.reject(function(d) { return d==win });
1313 },
1314
1315 // Closes all windows
1316 closeAll: function() {
1317 this.windows.each( function(w) {Windows.close(w.getId())} );
1318 },
1319
1320 closeAllModalWindows: function() {
1321 WindowUtilities.enableScreen();
1322 this.modalWindows.each( function(win) {if (win) win.close()});
1323 },
1324
1325 // Minimizes a window with its id
1326 minimize: function(id, event) {
1327 var win = this.getWindow(id)
1328 if (win && win.visible)
1329 win.minimize();
1330 Event.stop(event);
1331 },
1332
1333 // Maximizes a window with its id
1334 maximize: function(id, event) {
1335 var win = this.getWindow(id)
1336 if (win && win.visible)
1337 win.maximize();
1338 Event.stop(event);
1339 },
1340
1341 // Closes a window with its id
1342 close: function(id, event) {
1343 var win = this.getWindow(id);
1344 if (win)
1345 win.close();
1346 if (event)
1347 Event.stop(event);
1348 },
1349
1350 blur: function(id) {
1351 var win = this.getWindow(id);
1352 if (!win)
1353 return;
1354 if (win.options.blurClassName)
1355 win.changeClassName(win.options.blurClassName);
1356 if (this.focusedWindow == win)
1357 this.focusedWindow = null;
1358 win._notify("onBlur");
1359 },
1360
1361 focus: function(id) {
1362 var win = this.getWindow(id);
1363 if (!win)
1364 return;
1365 if (this.focusedWindow)
1366 this.blur(this.focusedWindow.getId())
1367
1368 if (win.options.focusClassName)
1369 win.changeClassName(win.options.focusClassName);
1370 this.focusedWindow = win;
1371 win._notify("onFocus");
1372 },
1373
1374 unsetOverflow: function(except) {
1375 this.windows.each(function(d) { d.oldOverflow = d.getContent().getStyle("overflow") || "auto" ; d.getContent().setStyle({overflow: "hidden"}) });
1376 if (except && except.oldOverflow)
1377 except.getContent().setStyle({overflow: except.oldOverflow});
1378 },
1379
1380 resetOverflow: function() {
1381 this.windows.each(function(d) { if (d.oldOverflow) d.getContent().setStyle({overflow: d.oldOverflow}) });
1382 },
1383
1384 updateZindex: function(zindex, win) {
1385 if (zindex > this.maxZIndex) {
1386 this.maxZIndex = zindex;
1387 if (this.focusedWindow)
1388 this.blur(this.focusedWindow.getId())
1389 }
1390 this.focusedWindow = win;
1391 if (this.focusedWindow)
1392 this.focus(this.focusedWindow.getId())
1393 }
1394};
1395
1396var Dialog = {
1397 dialogId: null,
1398 onCompleteFunc: null,
1399 callFunc: null,
1400 parameters: null,
1401
1402 confirm: function(content, parameters) {
1403 // Get Ajax return before
1404 if (content && typeof content != "string") {
1405 Dialog._runAjaxRequest(content, parameters, Dialog.confirm);
1406 return
1407 }
1408 content = content || "";
1409
1410 parameters = parameters || {};
1411 var okLabel = parameters.okLabel ? parameters.okLabel : "Ok";
1412 var cancelLabel = parameters.cancelLabel ? parameters.cancelLabel : "Cancel";
1413
1414 // Backward compatibility
1415 parameters = Object.extend(parameters, parameters.windowParameters || {});
1416 parameters.windowParameters = parameters.windowParameters || {};
1417
1418 parameters.className = parameters.className || "alert";
1419
1420 var okButtonClass = "class ='" + (parameters.buttonClass ? parameters.buttonClass + " " : "") + " ok_button'"
1421 var cancelButtonClass = "class ='" + (parameters.buttonClass ? parameters.buttonClass + " " : "") + " cancel_button'"
1422 var content = "\
1423 <div class='" + parameters.className + "_message'>" + content + "</div>\
1424 <div class='" + parameters.className + "_buttons'>\
1425 <input type='button' value='" + okLabel + "' onclick='Dialog.okCallback()' " + okButtonClass + "/>\
1426 <input type='button' value='" + cancelLabel + "' onclick='Dialog.cancelCallback()' " + cancelButtonClass + "/>\
1427 </div>\
1428 ";
1429 return this._openDialog(content, parameters)
1430 },
1431
1432 alert: function(content, parameters) {
1433 // Get Ajax return before
1434 if (content && typeof content != "string") {
1435 Dialog._runAjaxRequest(content, parameters, Dialog.alert);
1436 return
1437 }
1438 content = content || "";
1439
1440 parameters = parameters || {};
1441 var okLabel = parameters.okLabel ? parameters.okLabel : "Ok";
1442
1443 // Backward compatibility
1444 parameters = Object.extend(parameters, parameters.windowParameters || {});
1445 parameters.windowParameters = parameters.windowParameters || {};
1446
1447 parameters.className = parameters.className || "alert";
1448
1449 var okButtonClass = "class ='" + (parameters.buttonClass ? parameters.buttonClass + " " : "") + " ok_button'"
1450 var content = "\
1451 <div class='" + parameters.className + "_message'>" + content + "</div>\
1452 <div class='" + parameters.className + "_buttons'>\
1453 <input type='button' value='" + okLabel + "' onclick='Dialog.okCallback()' " + okButtonClass + "/>\
1454 </div>";
1455 return this._openDialog(content, parameters)
1456 },
1457
1458 info: function(content, parameters) {
1459 // Get Ajax return before
1460 if (content && typeof content != "string") {
1461 Dialog._runAjaxRequest(content, parameters, Dialog.info);
1462 return
1463 }
1464 content = content || "";
1465
1466 // Backward compatibility
1467 parameters = parameters || {};
1468 parameters = Object.extend(parameters, parameters.windowParameters || {});
1469 parameters.windowParameters = parameters.windowParameters || {};
1470
1471 parameters.className = parameters.className || "alert";
1472
1473 var content = "<div id='modal_dialog_message' class='" + parameters.className + "_message'>" + content + "</div>";
1474 if (parameters.showProgress)
1475 content += "<div id='modal_dialog_progress' class='" + parameters.className + "_progress'> </div>";
1476
1477 parameters.ok = null;
1478 parameters.cancel = null;
1479
1480 return this._openDialog(content, parameters)
1481 },
1482
1483 setInfoMessage: function(message) {
1484 $('modal_dialog_message').update(message);
1485 },
1486
1487 closeInfo: function() {
1488 Windows.close(this.dialogId);
1489 },
1490
1491 _openDialog: function(content, parameters) {
1492 var className = parameters.className;
1493
1494 if (! parameters.height && ! parameters.width) {
1495 parameters.width = WindowUtilities.getPageSize(parameters.options.parent || document.body).pageWidth / 2;
1496 }
1497 if (parameters.id)
1498 this.dialogId = parameters.id;
1499 else {
1500 var t = new Date();
1501 this.dialogId = 'modal_dialog_' + t.getTime();
1502 parameters.id = this.dialogId;
1503 }
1504
1505 // compute height or width if need be
1506 if (! parameters.height || ! parameters.width) {
1507 var size = WindowUtilities._computeSize(content, this.dialogId, parameters.width, parameters.height, 5, className)
1508 if (parameters.height)
1509 parameters.width = size + 5
1510 else
1511 parameters.height = size + 5
1512 }
1513 parameters.effectOptions = parameters.effectOptions ;
1514 parameters.resizable = parameters.resizable || false;
1515 parameters.minimizable = parameters.minimizable || false;
1516 parameters.maximizable = parameters.maximizable || false;
1517 parameters.draggable = parameters.draggable || false;
1518 parameters.closable = parameters.closable || false;
1519
1520 var win = new Window(parameters);
1521 win.getContent().innerHTML = content;
1522
1523 win.showCenter(true, parameters.top, parameters.left);
1524 win.setDestroyOnClose();
1525
1526 win.cancelCallback = parameters.onCancel || parameters.cancel;
1527 win.okCallback = parameters.onOk || parameters.ok;
1528
1529 return win;
1530 },
1531
1532 _getAjaxContent: function(originalRequest) {
1533 Dialog.callFunc(originalRequest.responseText, Dialog.parameters)
1534 },
1535
1536 _runAjaxRequest: function(message, parameters, callFunc) {
1537 if (message.options == null)
1538 message.options = {}
1539 Dialog.onCompleteFunc = message.options.onComplete;
1540 Dialog.parameters = parameters;
1541 Dialog.callFunc = callFunc;
1542
1543 message.options.onComplete = Dialog._getAjaxContent;
1544 new Ajax.Request(message.url, message.options);
1545 },
1546
1547 okCallback: function() {
1548 var win = Windows.focusedWindow;
1549 if (!win.okCallback || win.okCallback(win)) {
1550 // Remove onclick on button
1551 $$("#" + win.getId()+" input").each(function(element) {element.onclick=null;})
1552 win.close();
1553 }
1554 },
1555
1556 cancelCallback: function() {
1557 var win = Windows.focusedWindow;
1558 // Remove onclick on button
1559 $$("#" + win.getId()+" input").each(function(element) {element.onclick=null})
1560 win.close();
1561 if (win.cancelCallback)
1562 win.cancelCallback(win);
1563 }
1564}
1565/*
1566 Based on Lightbox JS: Fullsize Image Overlays
1567 by Lokesh Dhakar - http://www.huddletogether.com
1568
1569 For more information on this script, visit:
1570 http://huddletogether.com/projects/lightbox/
1571
1572 Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
1573 (basically, do anything you want, just leave my name and link)
1574*/
1575
1576if (Prototype.Browser.WebKit) {
1577 var array = navigator.userAgent.match(new RegExp(/AppleWebKit\/([\d\.\+]*)/));
1578 Prototype.Browser.WebKitVersion = parseFloat(array[1]);
1579}
1580
1581var WindowUtilities = {
1582 // From dragdrop.js
1583 getWindowScroll: function(parent) {
1584 var T, L, W, H;
1585 parent = parent || document.body;
1586 if (parent != document.body) {
1587 T = parent.scrollTop;
1588 L = parent.scrollLeft;
1589 W = parent.scrollWidth;
1590 H = parent.scrollHeight;
1591 }
1592 else {
1593 var w = window;
1594 with (w.document) {
1595 if (w.document.documentElement && documentElement.scrollTop) {
1596 T = documentElement.scrollTop;
1597 L = documentElement.scrollLeft;
1598 } else if (w.document.body) {
1599 T = body.scrollTop;
1600 L = body.scrollLeft;
1601 }
1602 if (w.innerWidth) {
1603 W = w.innerWidth;
1604 H = w.innerHeight;
1605 } else if (w.document.documentElement && documentElement.clientWidth) {
1606 W = documentElement.clientWidth;
1607 H = documentElement.clientHeight;
1608 } else {
1609 W = body.offsetWidth;
1610 H = body.offsetHeight
1611 }
1612 }
1613 }
1614 return { top: T, left: L, width: W, height: H };
1615 },
1616 //
1617 // getPageSize()
1618 // Returns array with page width, height and window width, height
1619 // Core code from - quirksmode.org
1620 // Edit for Firefox by pHaez
1621 //
1622 getPageSize: function(parent){
1623 parent = parent || document.body;
1624 var windowWidth, windowHeight;
1625 var pageHeight, pageWidth;
1626 if (parent != document.body) {
1627 windowWidth = parent.getWidth();
1628 windowHeight = parent.getHeight();
1629 pageWidth = parent.scrollWidth;
1630 pageHeight = parent.scrollHeight;
1631 }
1632 else {
1633 var xScroll, yScroll;
1634
1635 if (window.innerHeight && window.scrollMaxY) {
1636 xScroll = document.body.scrollWidth;
1637 yScroll = window.innerHeight + window.scrollMaxY;
1638 } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
1639 xScroll = document.body.scrollWidth;
1640 yScroll = document.body.scrollHeight;
1641 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
1642 xScroll = document.body.offsetWidth;
1643 yScroll = document.body.offsetHeight;
1644 }
1645
1646
1647 if (self.innerHeight) { // all except Explorer
1648 windowWidth = self.innerWidth;
1649 windowHeight = self.innerHeight;
1650 } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
1651 windowWidth = document.documentElement.clientWidth;
1652 windowHeight = document.documentElement.clientHeight;
1653 } else if (document.body) { // other Explorers
1654 windowWidth = document.body.clientWidth;
1655 windowHeight = document.body.clientHeight;
1656 }
1657
1658 // for small pages with total height less then height of the viewport
1659 if(yScroll < windowHeight){
1660 pageHeight = windowHeight;
1661 } else {
1662 pageHeight = yScroll;
1663 }
1664
1665 // for small pages with total width less then width of the viewport
1666 if(xScroll < windowWidth){
1667 pageWidth = windowWidth;
1668 } else {
1669 pageWidth = xScroll;
1670 }
1671 }
1672 return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
1673 },
1674
1675 disableScreen: function(className, overlayId, overlayOpacity, contentId, parent) {
1676 WindowUtilities.initLightbox(overlayId, className, function() {this._disableScreen(className, overlayId, overlayOpacity, contentId)}.bind(this), parent || document.body);
1677 },
1678
1679 _disableScreen: function(className, overlayId, overlayOpacity, contentId) {
1680 // prep objects
1681 var objOverlay = $(overlayId);
1682
1683 var pageSize = WindowUtilities.getPageSize(objOverlay.parentNode);
1684
1685 // Hide select boxes as they will 'peek' through the image in IE, store old value
1686 if (contentId && Prototype.Browser.IE) {
1687 WindowUtilities._hideSelect();
1688 WindowUtilities._showSelect(contentId);
1689 }
1690
1691 // set height of Overlay to take up whole page and show
1692 objOverlay.style.height = (pageSize.pageHeight + 'px');
1693 objOverlay.style.display = 'none';
1694 if (overlayId == "overlay_modal" && Window.hasEffectLib && Windows.overlayShowEffectOptions) {
1695 objOverlay.overlayOpacity = overlayOpacity;
1696 new Effect.Appear(objOverlay, Object.extend({from: 0, to: overlayOpacity}, Windows.overlayShowEffectOptions));
1697 }
1698 else
1699 objOverlay.style.display = "block";
1700 },
1701
1702 enableScreen: function(id) {
1703 id = id || 'overlay_modal';
1704 var objOverlay = $(id);
1705 if (objOverlay) {
1706 // hide lightbox and overlay
1707 if (id == "overlay_modal" && Window.hasEffectLib && Windows.overlayHideEffectOptions)
1708 new Effect.Fade(objOverlay, Object.extend({from: objOverlay.overlayOpacity, to:0}, Windows.overlayHideEffectOptions));
1709 else {
1710 objOverlay.style.display = 'none';
1711 objOverlay.parentNode.removeChild(objOverlay);
1712 }
1713
1714 // make select boxes visible using old value
1715 if (id != "__invisible__")
1716 WindowUtilities._showSelect();
1717 }
1718 },
1719
1720 _hideSelect: function(id) {
1721 if (Prototype.Browser.IE) {
1722 id = id == null ? "" : "#" + id + " ";
1723 $$(id + 'select').each(function(element) {
1724 if (! WindowUtilities.isDefined(element.oldVisibility)) {
1725 element.oldVisibility = element.style.visibility ? element.style.visibility : "visible";
1726 element.style.visibility = "hidden";
1727 }
1728 });
1729 }
1730 },
1731
1732 _showSelect: function(id) {
1733 if (Prototype.Browser.IE) {
1734 id = id == null ? "" : "#" + id + " ";
1735 $$(id + 'select').each(function(element) {
1736 if (WindowUtilities.isDefined(element.oldVisibility)) {
1737 // Why?? Ask IE
1738 try {
1739 element.style.visibility = element.oldVisibility;
1740 } catch(e) {
1741 element.style.visibility = "visible";
1742 }
1743 element.oldVisibility = null;
1744 }
1745 else {
1746 if (element.style.visibility)
1747 element.style.visibility = "visible";
1748 }
1749 });
1750 }
1751 },
1752
1753 isDefined: function(object) {
1754 return typeof(object) != "undefined" && object != null;
1755 },
1756
1757 // initLightbox()
1758 // Function runs on window load, going through link tags looking for rel="lightbox".
1759 // These links receive onclick events that enable the lightbox display for their targets.
1760 // The function also inserts html markup at the top of the page which will be used as a
1761 // container for the overlay pattern and the inline image.
1762 initLightbox: function(id, className, doneHandler, parent) {
1763 // Already done, just update zIndex
1764 if ($(id)) {
1765 Element.setStyle(id, {zIndex: Windows.maxZIndex + 1});
1766 Windows.maxZIndex++;
1767 doneHandler();
1768 }
1769 // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
1770 else {
1771 var objOverlay = document.createElement("div");
1772 objOverlay.setAttribute('id', id);
1773 objOverlay.className = "overlay_" + className
1774 objOverlay.style.display = 'none';
1775 objOverlay.style.position = 'absolute';
1776 objOverlay.style.top = '0';
1777 objOverlay.style.left = '0';
1778 objOverlay.style.zIndex = Windows.maxZIndex + 1;
1779 Windows.maxZIndex++;
1780 objOverlay.style.width = '100%';
1781 parent.insertBefore(objOverlay, parent.firstChild);
1782 if (Prototype.Browser.WebKit && id == "overlay_modal") {
1783 setTimeout(function() {doneHandler()}, 10);
1784 }
1785 else
1786 doneHandler();
1787 }
1788 },
1789
1790 setCookie: function(value, parameters) {
1791 document.cookie= parameters[0] + "=" + escape(value) +
1792 ((parameters[1]) ? "; expires=" + parameters[1].toGMTString() : "") +
1793 ((parameters[2]) ? "; path=" + parameters[2] : "") +
1794 ((parameters[3]) ? "; domain=" + parameters[3] : "") +
1795 ((parameters[4]) ? "; secure" : "");
1796 },
1797
1798 getCookie: function(name) {
1799 var dc = document.cookie;
1800 var prefix = name + "=";
1801 var begin = dc.indexOf("; " + prefix);
1802 if (begin == -1) {
1803 begin = dc.indexOf(prefix);
1804 if (begin != 0) return null;
1805 } else {
1806 begin += 2;
1807 }
1808 var end = document.cookie.indexOf(";", begin);
1809 if (end == -1) {
1810 end = dc.length;
1811 }
1812 return unescape(dc.substring(begin + prefix.length, end));
1813 },
1814
1815 _computeSize: function(content, id, width, height, margin, className) {
1816 var objBody = document.body;
1817 var tmpObj = document.createElement("div");
1818 tmpObj.setAttribute('id', id);
1819 tmpObj.className = className + "_content";
1820
1821 if (height)
1822 tmpObj.style.height = height + "px"
1823 else
1824 tmpObj.style.width = width + "px"
1825
1826 tmpObj.style.position = 'absolute';
1827 tmpObj.style.top = '0';
1828 tmpObj.style.left = '0';
1829 tmpObj.style.display = 'none';
1830
1831 tmpObj.innerHTML = content;
1832 objBody.insertBefore(tmpObj, objBody.firstChild);
1833
1834 var size;
1835 if (height)
1836 size = $(tmpObj).getDimensions().width + margin;
1837 else
1838 size = $(tmpObj).getDimensions().height + margin;
1839 objBody.removeChild(tmpObj);
1840 return size;
1841 }
1842}
1843