1 | /* |
---|
2 | * Lert v1.0 |
---|
3 | * by Jeffrey Sambells - http://JeffreySambells.com |
---|
4 | * For more information on this script, visit http://JeffreySambells.com/openprojects/lert/ |
---|
5 | * Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/ |
---|
6 | * Icons from Tango Desktop Project http://tango.freedesktop.org/Tango_Desktop_Project |
---|
7 | */ |
---|
8 | function Lert(title, message, buttons, options) { |
---|
9 | this.title_ = title; |
---|
10 | this.message_ = message; |
---|
11 | this.buttons_ = buttons; |
---|
12 | this.defaultButton_ = options.defaultButton || this.buttons_[0]; |
---|
13 | this.icon_ = options.icon || null; |
---|
14 | } |
---|
15 | |
---|
16 | Lert.prototype.display = function() { |
---|
17 | var body = document.getElementsByTagName ('BODY')[0]; |
---|
18 | //create the overlay if necessary |
---|
19 | var overlay = document.getElementById('lertOverlay'); |
---|
20 | if(!overlay) { |
---|
21 | var overlay = document.createElement("div"); |
---|
22 | overlay.setAttribute('id','lertOverlay'); |
---|
23 | overlay.style.display = 'none'; |
---|
24 | body.appendChild(overlay); |
---|
25 | } |
---|
26 | |
---|
27 | //position and show the overlay |
---|
28 | overlay.style.height='100%'; |
---|
29 | overlay.style.display='block'; |
---|
30 | |
---|
31 | //create the container if necessary |
---|
32 | var container = document.getElementById('lertContainer'); |
---|
33 | if(!container) { |
---|
34 | var container = document.createElement("div"); |
---|
35 | container.setAttribute('id','lertContainer'); |
---|
36 | container.style.display = 'none'; |
---|
37 | body.appendChild(container); |
---|
38 | } |
---|
39 | |
---|
40 | //position and show the container |
---|
41 | container.style.top = '35%'; |
---|
42 | container.style.display = 'block'; |
---|
43 | |
---|
44 | //create the window |
---|
45 | var win = document.createElement('div'); |
---|
46 | win.setAttribute('id','lertWindow'); |
---|
47 | |
---|
48 | var topBorder = document.createElement('div'); |
---|
49 | topBorder.setAttribute('id','topBorder'); |
---|
50 | topBorder.innerHTML = this.title_; |
---|
51 | var iconClose = document.createElement('img'); |
---|
52 | iconClose.setAttribute('src', 'js/lert/images/close_button.gif'); |
---|
53 | iconClose.setAttribute('id','closeIcon'); |
---|
54 | iconClose.setAttribute('alt',''); |
---|
55 | iconClose.setAttribute('style','cursor: pointer;'); |
---|
56 | iconClose.setAttribute('onclick', "document.getElementById('lertOverlay').style.display='none';document.getElementById('lertContainer').style.display='none';document.getElementById('lertContainer').innerHTML = '';"); |
---|
57 | topBorder.appendChild(iconClose); |
---|
58 | win.appendChild(topBorder); |
---|
59 | //create the optional icon |
---|
60 | if(this.icon_ != null) { |
---|
61 | var icon = document.createElement('img'); |
---|
62 | icon.setAttribute('src',this.icon_); |
---|
63 | |
---|
64 | icon.setAttribute('id','lertIcon'); |
---|
65 | icon.setAttribute('alt',''); |
---|
66 | win.appendChild(icon); |
---|
67 | } |
---|
68 | |
---|
69 | //create the message space |
---|
70 | var message = document.createElement('p'); |
---|
71 | message.setAttribute('id','lertMessage'); |
---|
72 | message.innerHTML = this.message_; |
---|
73 | win.appendChild(message); |
---|
74 | |
---|
75 | //create the button space |
---|
76 | var buttons = document.createElement('div'); |
---|
77 | buttons.setAttribute('id','lertButtons'); |
---|
78 | |
---|
79 | var oldKeyDown = document.onkeydown; |
---|
80 | |
---|
81 | //add each button |
---|
82 | for(i in this.buttons_) { |
---|
83 | var button = this.buttons_[i]; |
---|
84 | if(button.getDom) { |
---|
85 | var domButton = button.getDom(function() { |
---|
86 | container.style.display = 'none'; |
---|
87 | overlay.style.display = 'none'; |
---|
88 | document.onkeydown=oldKeyDown; |
---|
89 | container.innerHTML = ''; |
---|
90 | button.onclick_; |
---|
91 | },this.defaultButton_); |
---|
92 | buttons.appendChild(domButton); |
---|
93 | } |
---|
94 | } |
---|
95 | win.appendChild(buttons); |
---|
96 | |
---|
97 | document.onkeydown = this.keyboardControls; |
---|
98 | |
---|
99 | //append the window |
---|
100 | container.appendChild(win); |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | Lert.prototype.keyboardControls = function(e) { |
---|
105 | if (e == null) { keycode = event.keyCode; } // ie |
---|
106 | else { keycode = e.which; } // mozilla |
---|
107 | if(keycode==13){ |
---|
108 | document.getElementById('lertDefaultButton').onclick(); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | function LertButton(label, event, options) { |
---|
113 | this.label_ = label; |
---|
114 | this.onclick_ = event; |
---|
115 | this.eventClick = function() {}; |
---|
116 | } |
---|
117 | |
---|
118 | LertButton.prototype.getDom = function(eventCleanup,defaultButton) { |
---|
119 | var button = document.createElement('a'); |
---|
120 | //button.setAttribute('href','javascript:void(0);'); |
---|
121 | button.className = 'lertButton'; |
---|
122 | if(this == defaultButton) button.setAttribute('id','lertDefaultButton'); |
---|
123 | button.innerHTML = this.label_; |
---|
124 | |
---|
125 | var eventOnclick = this.onclick_; |
---|
126 | button.onclick = function() { |
---|
127 | eventCleanup(); |
---|
128 | eventOnclick(); |
---|
129 | } |
---|
130 | this.eventClick = button.onclick; |
---|
131 | return button; |
---|
132 | } |
---|