noalyss Version-9
smoke.js
Go to the documentation of this file.
1/*
2 SMOKE.JS - 0.1.3
3 (c) 2011-2013 Jonathan Youngblood
4 demos / documentation: http://smoke-js.com/
5 */
6
7;
8(function (window, document) {
9
10 /*jslint browser: true, onevar: true, undef: true, nomen: false, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
11
12 var smoke = {
13 smoketimeout: [],
14 init: false,
15 zindex: 1000,
16 i: 0,
17
18 bodyload: function (id) {
19 var ff = document.createElement('div');
20 ff.setAttribute('id', 'smoke-out-' + id);
21 ff.className = 'smoke-base';
22 ff.style.zIndex = smoke.zindex;
23 smoke.zindex++;
24 document.body.appendChild(ff);
25 },
26
27 newdialog: function () {
28 var newid = new Date().getTime();
29 newid = Math.random(1, 99) + newid;
30
31 if (!smoke.init) {
32 smoke.listen(window, "load", function () {
33 smoke.bodyload(newid);
34 });
35 } else {
36 smoke.bodyload(newid);
37 }
38
39 return newid;
40 },
41
42 forceload: function () {},
43
44 build: function (e, f) {
45 smoke.i++;
46
47 f.stack = smoke.i;
48
49 e = e.replace(/\n/g, '<br />');
50 e = e.replace(/\r/g, '<br />');
51
52 var prompt = '',
53 ok = 'OK',
54 cancel = 'Cancel',
55 classname = '',
56 buttons = '',
57 sTitle = '',
58 box;
59
60 if (f.type === 'prompt') {
61 prompt =
62 '<div class="dialog-prompt">' +
63 '<input id="dialog-input-' + f.newid + '" type="text" ' + (f.params.value ? 'value="' + f.params.value + '"' : '') + ' />' +
64 '</div>';
65 }
66 /**
67 * Add a title
68 */
69 if ( f.params.title) {
70 sTitle='<div class="dialog-title">'+f.params.title+'</div>';
71 }
72 if (f.params.ok) {
73 ok = f.params.ok;
74 }
75
76 if (f.params.cancel) {
77 cancel = f.params.cancel;
78 }
79
80 if (f.params.classname) {
81 classname = f.params.classname;
82 }
83
84 if (f.type !== 'signal') {
85 buttons = '<div class="dialog-buttons">';
86 if (f.type === 'alert') {
87 buttons +=
88 '<button id="alert-ok-' + f.newid + '">' + ok + '</button>';
89 } else if (f.type === 'quiz') {
90
91 if (f.params.button_1) {
92 buttons +=
93 '<button class="quiz-button" id="' + f.type + '-ok1-' + f.newid + '">' + f.params.button_1 + '</button>';
94 }
95
96 if (f.params.button_2) {
97 buttons +=
98 '<button class="quiz-button" id="' + f.type + '-ok2-' + f.newid + '">' + f.params.button_2 + '</button>';
99 }
100
101 if (f.params.button_3) {
102 buttons +=
103 '<button class="quiz-button" id="' + f.type + '-ok3-' + f.newid + '">' + f.params.button_3 + '</button>';
104 }
105 if (f.params.button_cancel) {
106 buttons +=
107 '<button id="' + f.type + '-cancel-' + f.newid + '" class="cancel">' + f.params.button_cancel + '</button>';
108 }
109
110 } else if (f.type === 'prompt' || f.type === 'confirm') {
111 if (f.params.reverseButtons) {
112 buttons +=
113 '<button id="' + f.type + '-ok-' + f.newid + '">' + ok + '</button>' +
114 '<button id="' + f.type + '-cancel-' + f.newid + '" class="cancel">' + cancel + '</button>';
115 } else {
116 buttons +=
117 '<button id="' + f.type + '-cancel-' + f.newid + '" class="cancel">' + cancel + '</button>' +
118 '<button id="' + f.type + '-ok-' + f.newid + '">' + ok + '</button>';
119 }
120 }
121 buttons += '</div>';
122 }
123
124
125 box =
126 '<div id="smoke-bg-' + f.newid + '" class="smokebg"></div>' +
127 '<div class="dialog smoke ' + classname + '">' +
128 sTitle +
129 '<div class="dialog-inner">' +
130 e +
131 prompt +
132 buttons +
133 '</div>' +
134 '</div>';
135
136 if (!smoke.init) {
137 smoke.listen(window, "load", function () {
138 smoke.finishbuild(e, f, box);
139 });
140 } else {
141 smoke.finishbuild(e, f, box);
142 }
143
144 },
145
146 finishbuild: function (e, f, box) {
147
148 var ff = document.getElementById('smoke-out-' + f.newid);
149
150 ff.className = 'smoke-base smoke-visible smoke-' + f.type;
151 ff.innerHTML = box;
152
153 while (ff.innerHTML === "") {
154 ff.innerHTML = box;
155 }
156
157 if (smoke.smoketimeout[f.newid]) {
158 clearTimeout(smoke.smoketimeout[f.newid]);
159 }
160
161 smoke.listen(
162 document.getElementById('smoke-bg-' + f.newid),
163 "click",
164 function () {
165 smoke.destroy(f.type, f.newid);
166 if (f.type === 'prompt' || f.type === 'confirm' || f.type === 'quiz') {
167 f.callback(false);
168 } else if (f.type === 'alert' && typeof f.callback !== 'undefined') {
169 f.callback();
170 }
171 }
172 );
173
174
175 switch (f.type) {
176 case 'alert':
177 smoke.finishbuildAlert(e, f, box);
178 break;
179 case 'confirm':
180 smoke.finishbuildConfirm(e, f, box);
181 break;
182 case 'quiz':
183 smoke.finishbuildQuiz(e, f, box);
184 break;
185 case 'prompt':
186 smoke.finishbuildPrompt(e, f, box);
187 break;
188 case 'signal':
189 smoke.finishbuildSignal(e, f, box);
190 break;
191 default:
192 throw "Unknown type: " + f.type;
193 }
194 },
195
196 finishbuildAlert: function (e, f, box) {
197 smoke.listen(
198 document.getElementById('alert-ok-' + f.newid),
199 "click",
200 function () {
201 smoke.destroy(f.type, f.newid);
202 if (typeof f.callback !== 'undefined' && f.callback) {
203 f.callback();
204 }
205 }
206 );
207
208 document.onkeyup = function (e) {
209 if (!e) {
210 e = window.event;
211 }
212 if (e.keyCode === 13 || e.keyCode === 32 || e.keyCode === 27) {
213 smoke.destroy(f.type, f.newid);
214 if (typeof f.callback !== 'undefined') {
215 f.callback();
216 }
217 }
218 };
219 },
220
221 finishbuildConfirm: function (e, f, box) {
222 smoke.listen(
223 document.getElementById('confirm-cancel-' + f.newid),
224 "click",
225 function ()
226 {
227 smoke.destroy(f.type, f.newid);
228 f.callback(false);
229 }
230 );
231
232 smoke.listen(
233 document.getElementById('confirm-ok-' + f.newid),
234 "click",
235 function ()
236 {
237 smoke.destroy(f.type, f.newid);
238 f.callback(true);
239 }
240 );
241
242 document.onkeyup = function (e) {
243 if (!e) {
244 e = window.event;
245 }
246 /*if (e.keyCode === 13 || e.keyCode === 32) {
247 smoke.destroy(f.type, f.newid);
248 f.callback(true);
249 } else*/
250 if (e.keyCode === 27) {
251 smoke.destroy(f.type, f.newid);
252 f.callback(false);
253 }
254 };
255 },
256
257 finishbuildQuiz: function (e, f, box) {
258 var a, b, c;
259
260 smoke.listen(
261 document.getElementById('quiz-cancel-' + f.newid),
262 "click",
263 function ()
264 {
265 smoke.destroy(f.type, f.newid);
266 f.callback(false);
267 }
268 );
269
270
271 if (a = document.getElementById('quiz-ok1-' + f.newid))
272 smoke.listen(
273 a,
274 "click",
275 function () {
276 smoke.destroy(f.type, f.newid);
277 f.callback(a.innerHTML);
278 }
279 );
280
281
282 if (b = document.getElementById('quiz-ok2-' + f.newid))
283 smoke.listen(
284 b,
285 "click",
286 function () {
287 smoke.destroy(f.type, f.newid);
288 f.callback(b.innerHTML);
289 }
290 );
291
292
293 if (c = document.getElementById('quiz-ok3-' + f.newid))
294 smoke.listen(
295 c,
296 "click",
297 function () {
298 smoke.destroy(f.type, f.newid);
299 f.callback(c.innerHTML);
300 }
301 );
302
303 document.onkeyup = function (e) {
304 if (!e) {
305 e = window.event;
306 }
307 if (e.keyCode === 27) {
308 smoke.destroy(f.type, f.newid);
309 f.callback(false);
310 }
311 };
312
313 },
314
315 finishbuildPrompt: function (e, f, box) {
316 var pi = document.getElementById('dialog-input-' + f.newid);
317
318 setTimeout(function () {
319 pi.focus();
320 pi.select();
321 }, 100);
322
323 smoke.listen(
324 document.getElementById('prompt-cancel-' + f.newid),
325 "click",
326 function () {
327 smoke.destroy(f.type, f.newid);
328 f.callback(false);
329 }
330 );
331
332 smoke.listen(
333 document.getElementById('prompt-ok-' + f.newid),
334 "click",
335 function () {
336 smoke.destroy(f.type, f.newid);
337 f.callback(pi.value);
338 }
339 );
340
341 document.onkeyup = function (e) {
342 if (!e) {
343 e = window.event;
344 }
345
346 if (e.keyCode === 13) {
347 smoke.destroy(f.type, f.newid);
348 f.callback(pi.value);
349 } else if (e.keyCode === 27) {
350 smoke.destroy(f.type, f.newid);
351 f.callback(false);
352 }
353 };
354 },
355
356 finishbuildSignal: function (e, f, box) {
357
358
359 document.onkeyup = function (e) {
360 if (!e) {
361 e = window.event;
362 }
363 if (e.keyCode === 27) {
364 smoke.destroy(f.type, f.newid);
365 if (typeof f.callback !== 'undefined') {
366 f.callback();
367 }
368 }
369 };
370
371 smoke.smoketimeout[f.newid] = setTimeout(function () {
372 smoke.destroy(f.type, f.newid);
373 if (typeof f.callback !== 'undefined') {
374 f.callback();
375 }
376 }, f.timeout);
377 },
378
379 destroy: function (type, id) {
380
381 var box = document.getElementById('smoke-out-' + id);
382
383 if (type !== 'quiz') {
384 var okButton = document.getElementById(type + '-ok-' + id);
385 }
386
387 var cancelButton = document.getElementById(type + '-cancel-' + id);
388 box.className = 'smoke-base';
389
390 if (okButton) {
391 smoke.stoplistening(okButton, "click", function () {});
392 document.onkeyup = null;
393 }
394
395 if (type === 'quiz') {
396 var quiz_buttons = document.getElementsByClassName("quiz-button");
397 for (var i = 0; i < quiz_buttons.length; i++) {
398 smoke.stoplistening(quiz_buttons[i], "click", function () {});
399 document.onkeyup = null;
400 }
401 }
402
403 if (cancelButton) {
404 smoke.stoplistening(cancelButton, "click", function () {});
405 }
406
407 smoke.i = 0;
408 document.body.removeChild(box);
409 },
410
411 alert: function (e, f, g) {
412 if (typeof g !== 'object') {
413 g = false;
414 }
415
416 var id = smoke.newdialog();
417
418 smoke.build(e, {
419 type: 'alert',
420 callback: f,
421 params: g,
422 newid: id
423 });
424 },
425
426 signal: function (e, f, g) {
427 if (typeof g !== 'object') {
428 g = false;
429 }
430
431 var duration = 5000;
432 if (g.duration !== 'undefined') {
433 duration = g.duration;
434 }
435 var id = smoke.newdialog();
436 smoke.build(e, {
437 type: 'signal',
438 callback: f,
439 timeout: duration,
440 params: g,
441 newid: id
442 });
443 },
444
445 confirm: function (e, f, g) {
446 if (typeof g !== 'object') {
447 g = false;
448 }
449
450 var id = smoke.newdialog();
451 smoke.build(e, {
452 type: 'confirm',
453 callback: f,
454 params: g,
455 newid: id
456 });
457 },
458
459 quiz: function (e, f, g) {
460 if (typeof g !== 'object') {
461 g = false;
462 }
463
464 var id = smoke.newdialog();
465 smoke.build(e, {
466 type: 'quiz',
467 callback: f,
468 params: g,
469 newid: id
470 });
471 },
472
473 prompt: function (e, f, g) {
474 if (typeof g !== 'object') {
475 g = false;
476 }
477
478 var id = smoke.newdialog();
479 return smoke.build(e, {type: 'prompt', callback: f, params: g, newid: id});
480 },
481
482 listen: function (e, f, g) {
483 if (e.addEventListener) {
484 return e.addEventListener(f, g, false);
485 }
486
487 if (e.attachEvent) {
488 return e.attachEvent('on' + f, g);
489 }
490
491 return false;
492 },
493
494 stoplistening: function (e, f, g) {
495 if (e.removeEventListener) {
496 return e.removeEventListener(f, g, false);
497 }
498
499 if (e.detachEvent) {
500 return e.detachEvent('on' + f, g);
501 }
502
503 return false;
504 }
505 };
506
507
508 smoke.init = true;
509
510 if (typeof module != 'undefined' && module.exports) {
511 module.exports = smoke;
512 } else if (typeof define === 'function' && define.amd) {
513 define('smoke', [], function () {
514 return smoke;
515 });
516 } else {
517 this.smoke = smoke;
518 }
519
520})(window, document);