Javascript Dialog box based on Prototype and Scriptaculous
Use it thusly:
new Dialog.box([element ID]);
$([element ID]).show();
$([element ID]).hide();
Simple right?
1 2 var Dialog = {}; 3 Dialog.Box = Class.create(); 4 Object.extend(Dialog.Box.prototype, { 5 initialize: function(id) { 6 this.createOverlay(); 7 8 this.dialog_box = $(id); 9 this.dialog_box.show = this.show.bind(this); 10 this.dialog_box.hide = this.hide.bind(this); 11 12 this.parent_element = this.dialog_box.parentNode; 13 14 var e_dims = Element.getDimensions(this.dialog_box); 15 var b_dims = Element.getDimensions(this.overlay); 16 this.dialog_box.style.left = ((b_dims.width/2) - (e_dims.width/2)) + 'px'; 17 this.dialog_box.style.top = '12px'; 18 }, 19 20 createOverlay: function() { 21 if($('dialog_overlay')) { 22 this.overlay = $('dialog_overlay'); 23 } else { 24 this.overlay = document.createElement('div'); 25 this.overlay.id = 'dialog_overlay'; 26 Object.extend(this.overlay.style, { 27 position: 'absolute', 28 top: 0, 29 left: 0, 30 zIndex: 90, 31 width: '100%', 32 backgroundColor: '#000', 33 display: 'none' 34 }); 35 document.body.insertBefore(this.overlay, document.body.childNodes[0]); 36 } 37 }, 38 39 moveDialogBox: function(where) { 40 Element.remove(this.dialog_box); 41 if(where == 'back') 42 this.dialog_box = this.parent_element.appendChild(this.dialog_box); 43 else 44 this.dialog_box = this.overlay.parentNode.insertBefore(this.dialog_box, this.overlay); 45 }, 46 47 show: function() { 48 this.overlay.style.height = $('body').getHeight()+'px'; 49 this.moveDialogBox('out'); 50 this.overlay.onclick = this.hide.bind(this); 51 this.selectBoxes('hide'); 52 new Effect.Appear(this.overlay, {duration: 0.1, from: 0.0, to: 0.3}); 53 this.dialog_box.style.display = '' 54 }, 55 56 hide: function() { 57 this.selectBoxes('show'); 58 new Effect.Fade(this.overlay, {duration: 0.1}); 59 this.dialog_box.style.display = 'none'; 60 this.moveDialogBox('back'); 61 $A(this.dialog_box.getElementsByTagName('input')).each(function(e){if(e.type!='submit')e.value=''}); 62 }, 63 64 selectBoxes: function(what) { 65 $A(document.getElementsByTagName('select')).each(function(select) { 66 Element[what](select); 67 }); 68 69 if(what == 'hide') 70 $A(this.dialog_box.getElementsByTagName('select')).each(function(select){Element.show(select)}) 71 } 72 });