xonecas.io
186
Ft

fractal-tree / files

fractal-tree / smoke / smoke.js

6.9 KB
1var smoke = {
2  smoketimeout: [],
3  init: false,
4  zindex: 1000,
5  i: 0,
6
7	bodyload: function(id){
8		var ff = document.createElement('div');
9				ff.setAttribute('id','smoke-out-'+id);
10/* 				ff.setAttribute('class','smoke-base'); */
11				ff.className = 'smoke-base';
12				ff.style.zIndex = smoke.zindex;
13				smoke.zindex++;
14				document.body.appendChild(ff);
15	},
16
17	newdialog: function(){
18		var newid = new Date().getTime();
19				newid = Math.random(1,99) + newid;	
20
21		// make a new container while you're at it
22		if (!smoke.init){		
23	    smoke.listen(window,"load", function(){
24		    smoke.bodyload(newid);
25			});
26		}else{
27	    smoke.bodyload(newid);		
28		}
29
30		return newid;
31	},
32
33	forceload: function(){},
34
35	build: function(e,f){
36		smoke.i++;
37		
38		// determine stacking order
39		f.stack = smoke.i;
40
41		e = e.replace(/\n/g,'<br />');
42		e = e.replace(/\r/g,'<br />');
43
44		var prompt = '';
45
46		if (f.type == 'prompt'){
47			prompt = 
48				'<div class="dialog-prompt">'+
49						'<input id="dialog-input-'+f.newid+'" type="text" />'+
50					'</div>';
51		}
52
53		
54		var ok = 'OK';
55		var cancel = 'Cancel';
56		var classname = '';
57		
58		if (f.params.ok){
59			ok = f.params.ok;
60		}
61		
62		if (f.params.cancel){
63			cancel = f.params.cancel;
64		}
65		
66		if (f.params.classname){
67			classname = f.params.classname;
68		}
69
70
71		var buttons = '';
72		if (f.type != 'signal'){
73			buttons = '<div class="dialog-buttons">';
74			if (f.type == 'alert'){
75				buttons +=
76					'<button id="alert-ok-'+f.newid+'">'+ok+'</button>';
77			}
78			if (f.type == 'prompt' || f.type == 'confirm'){
79				buttons +=
80					'<button id="'+f.type+'-cancel-'+f.newid+'" class="cancel">'+cancel+'</button>'+
81					'<button id="'+f.type+'-ok-'+f.newid+'">'+ok+'</button>';
82			}
83			buttons += '</div>';
84		}
85
86
87		var box = 
88		'<div id="smoke-bg-'+f.newid+'" class="smokebg"></div>'+
89		'<div class="dialog smoke '+classname+'">'+
90			'<div class="dialog-inner">'+
91					e+
92					prompt+
93					buttons+			
94			'</div>'+
95		'</div>';
96
97	
98
99
100
101		if (!smoke.init){		
102	    smoke.listen(window,"load", function(){
103	    	smoke.finishbuild(e,f,box);
104			});
105		}else{
106	    	smoke.finishbuild(e,f,box);
107		}
108
109	},
110
111
112
113	finishbuild: function(e,f,box){
114	
115		var ff = document.getElementById('smoke-out-'+f.newid+'');
116				ff.className = 'smoke-base smoke-visible';		
117				ff.innerHTML = box;
118				
119		while (ff.innerHTML == ""){
120			ff.innerHTML = box;
121		}
122		
123		// clear the timeout if it's already been activated
124		if (smoke.smoketimeout[f.newid]){
125				clearTimeout(smoke.smoketimeout[f.newid]);
126		}
127		
128		// close on background click
129		// buberdds got rid of this for ie support?
130		var g = document.getElementById('smoke-bg-'+f.newid+'');
131	      smoke.listen(g,"click", function(){
132					smoke.destroy(f.type, f.newid);
133					if (f.type == 'prompt' || f.type == 'confirm'){
134						f.callback(false);
135					}
136				});
137	
138	
139		// listeners for button actions
140	
141		if (f.type == 'alert'){
142			// return true
143			var h = document.getElementById('alert-ok-'+f.newid+'');
144		      smoke.listen(h,"click", function(){
145						smoke.destroy(f.type, f.newid);
146					});
147	
148	
149			// listen for enter key or space, close it
150			document.onkeyup = function(e){
151	  		if (!e) e = window.event;
152				if (e.keyCode == 13 || e.keyCode == 32 || e.keyCode == 27){
153					smoke.destroy(f.type, f.newid);
154				}
155			};
156		}
157		
158		if (f.type == 'confirm'){
159			// return false
160			var h = document.getElementById('confirm-cancel-'+f.newid+'');            
161		      smoke.listen(h,"click", function(){
162							smoke.destroy(f.type, f.newid);
163							f.callback(false);
164					});
165	
166	
167			
168			// return true
169			var i = document.getElementById('confirm-ok-'+f.newid+'');  
170		      smoke.listen(i,"click", function(){
171							smoke.destroy(f.type, f.newid);
172							f.callback(true);
173					});
174					
175			// listen for enter key or space, close it & return true, esc will close with false
176			document.onkeyup = function(e){
177	  		if (!e) e = window.event;
178				if (e.keyCode == 13 || e.keyCode == 32){
179					smoke.destroy(f.type, f.newid);
180					f.callback(true);
181				} else if (e.keyCode == 27){
182					smoke.destroy(f.type, f.newid);
183					f.callback(false);
184				}
185	
186			};
187	
188		}
189		
190		if (f.type == 'prompt'){
191			// focus on input
192			var pi = document.getElementById('dialog-input-'+f.newid+'');
193				
194				setTimeout(function(){
195					pi.focus();
196					pi.select();
197				},100);
198	
199			// return false
200			var h = document.getElementById('prompt-cancel-'+f.newid+'');
201		      smoke.listen(h,"click", function(){
202						smoke.destroy(f.type, f.newid);
203						f.callback(false);
204					});
205	
206			// return	contents of input box
207			var j = document.getElementById('dialog-input-'+f.newid+'');
208			var i = document.getElementById('prompt-ok-'+f.newid+'');
209		      smoke.listen(i,"click", function(){
210							smoke.destroy(f.type, f.newid);
211							f.callback(j.value);
212					});
213					
214			// listen for enter
215			document.onkeyup = function(e){
216	  		if (!e) e = window.event;
217				if (e.keyCode == 13){
218					smoke.destroy(f.type, f.newid);
219					f.callback(j.value);
220				} else if (e.keyCode == 27){
221					smoke.destroy(f.type, f.newid);
222					f.callback(false);
223				}
224			};
225		}
226	
227	
228		// close after f.timeout ms
229		if (f.type == 'signal'){
230			smoke.smoketimeout[f.newid] = setTimeout(function(){
231				smoke.destroy(f.type, f.newid);
232			},f.timeout);
233		}
234
235	},
236	
237	
238		
239	destroy: function(type,id){
240		var box = document.getElementById('smoke-out-'+id);
241/* 				box.setAttribute('class','smoke-base'); */
242				box.className = 'smoke-base';
243
244			
245			// confirm/alert/prompt remove click listener
246			if (g = document.getElementById(type+'-ok-'+id)){
247				smoke.stoplistening(g,"click",function(){});
248				document.onkeyup = null;
249			}
250			
251			// confirm/prompt remove click listener
252			if (h = document.getElementById(type+'-cancel-'+id)){
253				smoke.stoplistening(h,"click",function(){});
254			}
255			
256			smoke.i = 0;
257			box.innerHTML = '';
258
259	},
260
261	alert: function(e,f){
262		if (typeof(f) != 'object'){
263			f = false;
264		}
265		
266		var id = smoke.newdialog();
267		smoke.build(e,{type:'alert',params:f,newid:id});
268	},
269	
270	signal: function(e,f){
271		if (typeof(f) == 'undefined'){
272			f = 5000;
273		}
274		
275		var id = smoke.newdialog();
276		smoke.build(e,{type:'signal',timeout:f,params:false,newid:id});
277	},
278	
279	confirm: function(e,f,g){
280		if (typeof(g) != 'object'){
281			g = false;
282		}
283		
284		var id = smoke.newdialog();
285		smoke.build(e,{type:'confirm',callback:f,params:g,newid:id});
286		
287	},
288	
289	prompt: function(e,f,g){
290		if (typeof(g) != 'object'){
291			g = false;
292		}
293		
294		var id = smoke.newdialog();
295		return smoke.build(e,{type:'prompt',callback:f,params:g,newid:id});
296	},
297	
298	listen: function(e,f,g){
299	
300    if (e.addEventListener) {
301      e.addEventListener(f, g, false);
302    } else if (e.attachEvent){
303      var r = e.attachEvent('on'+f, g);
304      return r;
305    }else{
306    	return false;
307    }
308	},
309	
310	stoplistening: function(e,f,g){	
311    if (e.removeEventListener) {
312      e.removeEventListener("click", g, false);
313    } else if (e.detachEvent){
314      var r = e.detachEvent('on'+f, g);
315      return r;
316    }else{
317    	return false;
318    }
319	}
320};
321
322		if (!smoke.init){		
323	    smoke.listen(window,"load", function(){
324		    smoke.init = true;
325			});
326		}