3 (c) 2011-2013 Jonathan Youngblood
4 demos / documentation: http://smoke-js.com/
8(function (window, document) {
10 /*jslint browser: true, onevar: true, undef: true, nomen: false, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
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;
24 document.body.appendChild(ff);
27 newdialog: function () {
28 var newid = new Date().getTime();
29 newid = Math.random(1, 99) + newid;
32 smoke.listen(window, "load", function () {
33 smoke.bodyload(newid);
36 smoke.bodyload(newid);
42 forceload: function () {},
44 build: function (e, f) {
49 e = e.replace(/\n/g, '<br />');
50 e = e.replace(/\r/g, '<br />');
60 if (f.type === 'prompt') {
62 '<div class="dialog-prompt">' +
63 '<input id="dialog-input-' + f.newid + '" type="text" ' + (f.params.value ? 'value="' + f.params.value + '"' : '') + ' />' +
69 if ( f.params.title) {
70 sTitle='<div class="dialog-title">'+f.params.title+'</div>';
76 if (f.params.cancel) {
77 cancel = f.params.cancel;
80 if (f.params.classname) {
81 classname = f.params.classname;
84 if (f.type !== 'signal') {
85 buttons = '<div class="dialog-buttons">';
86 if (f.type === 'alert') {
88 '<button id="alert-ok-' + f.newid + '">' + ok + '</button>';
89 } else if (f.type === 'quiz') {
91 if (f.params.button_1) {
93 '<button class="quiz-button" id="' + f.type + '-ok1-' + f.newid + '">' + f.params.button_1 + '</button>';
96 if (f.params.button_2) {
98 '<button class="quiz-button" id="' + f.type + '-ok2-' + f.newid + '">' + f.params.button_2 + '</button>';
101 if (f.params.button_3) {
103 '<button class="quiz-button" id="' + f.type + '-ok3-' + f.newid + '">' + f.params.button_3 + '</button>';
105 if (f.params.button_cancel) {
107 '<button id="' + f.type + '-cancel-' + f.newid + '" class="cancel">' + f.params.button_cancel + '</button>';
110 } else if (f.type === 'prompt' || f.type === 'confirm') {
111 if (f.params.reverseButtons) {
113 '<button id="' + f.type + '-ok-' + f.newid + '">' + ok + '</button>' +
114 '<button id="' + f.type + '-cancel-' + f.newid + '" class="cancel">' + cancel + '</button>';
117 '<button id="' + f.type + '-cancel-' + f.newid + '" class="cancel">' + cancel + '</button>' +
118 '<button id="' + f.type + '-ok-' + f.newid + '">' + ok + '</button>';
126 '<div id="smoke-bg-' + f.newid + '" class="smokebg"></div>' +
127 '<div class="dialog smoke ' + classname + '">' +
129 '<div class="dialog-inner">' +
137 smoke.listen(window, "load", function () {
138 smoke.finishbuild(e, f, box);
141 smoke.finishbuild(e, f, box);
146 finishbuild: function (e, f, box) {
148 var ff = document.getElementById('smoke-out-' + f.newid);
150 ff.className = 'smoke-base smoke-visible smoke-' + f.type;
153 while (ff.innerHTML === "") {
157 if (smoke.smoketimeout[f.newid]) {
158 clearTimeout(smoke.smoketimeout[f.newid]);
162 document.getElementById('smoke-bg-' + f.newid),
165 smoke.destroy(f.type, f.newid);
166 if (f.type === 'prompt' || f.type === 'confirm' || f.type === 'quiz') {
168 } else if (f.type === 'alert' && typeof f.callback !== 'undefined') {
177 smoke.finishbuildAlert(e, f, box);
180 smoke.finishbuildConfirm(e, f, box);
183 smoke.finishbuildQuiz(e, f, box);
186 smoke.finishbuildPrompt(e, f, box);
189 smoke.finishbuildSignal(e, f, box);
192 throw "Unknown type: " + f.type;
196 finishbuildAlert: function (e, f, box) {
198 document.getElementById('alert-ok-' + f.newid),
201 smoke.destroy(f.type, f.newid);
202 if (typeof f.callback !== 'undefined' && f.callback) {
208 document.onkeyup = function (e) {
212 if (e.keyCode === 13 || e.keyCode === 32 || e.keyCode === 27) {
213 smoke.destroy(f.type, f.newid);
214 if (typeof f.callback !== 'undefined') {
221 finishbuildConfirm: function (e, f, box) {
223 document.getElementById('confirm-cancel-' + f.newid),
227 smoke.destroy(f.type, f.newid);
233 document.getElementById('confirm-ok-' + f.newid),
237 smoke.destroy(f.type, f.newid);
242 document.onkeyup = function (e) {
246 /*if (e.keyCode === 13 || e.keyCode === 32) {
247 smoke.destroy(f.type, f.newid);
250 if (e.keyCode === 27) {
251 smoke.destroy(f.type, f.newid);
257 finishbuildQuiz: function (e, f, box) {
261 document.getElementById('quiz-cancel-' + f.newid),
265 smoke.destroy(f.type, f.newid);
271 if (a = document.getElementById('quiz-ok1-' + f.newid))
276 smoke.destroy(f.type, f.newid);
277 f.callback(a.innerHTML);
282 if (b = document.getElementById('quiz-ok2-' + f.newid))
287 smoke.destroy(f.type, f.newid);
288 f.callback(b.innerHTML);
293 if (c = document.getElementById('quiz-ok3-' + f.newid))
298 smoke.destroy(f.type, f.newid);
299 f.callback(c.innerHTML);
303 document.onkeyup = function (e) {
307 if (e.keyCode === 27) {
308 smoke.destroy(f.type, f.newid);
315 finishbuildPrompt: function (e, f, box) {
316 var pi = document.getElementById('dialog-input-' + f.newid);
318 setTimeout(function () {
324 document.getElementById('prompt-cancel-' + f.newid),
327 smoke.destroy(f.type, f.newid);
333 document.getElementById('prompt-ok-' + f.newid),
336 smoke.destroy(f.type, f.newid);
337 f.callback(pi.value);
341 document.onkeyup = function (e) {
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);
356 finishbuildSignal: function (e, f, box) {
359 document.onkeyup = function (e) {
363 if (e.keyCode === 27) {
364 smoke.destroy(f.type, f.newid);
365 if (typeof f.callback !== 'undefined') {
371 smoke.smoketimeout[f.newid] = setTimeout(function () {
372 smoke.destroy(f.type, f.newid);
373 if (typeof f.callback !== 'undefined') {
379 destroy: function (type, id) {
381 var box = document.getElementById('smoke-out-' + id);
383 if (type !== 'quiz') {
384 var okButton = document.getElementById(type + '-ok-' + id);
387 var cancelButton = document.getElementById(type + '-cancel-' + id);
388 box.className = 'smoke-base';
391 smoke.stoplistening(okButton, "click", function () {});
392 document.onkeyup = null;
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;
404 smoke.stoplistening(cancelButton, "click", function () {});
408 document.body.removeChild(box);
411 alert: function (e, f, g) {
412 if (typeof g !== 'object') {
416 var id = smoke.newdialog();
426 signal: function (e, f, g) {
427 if (typeof g !== 'object') {
432 if (g.duration !== 'undefined') {
433 duration = g.duration;
435 var id = smoke.newdialog();
445 confirm: function (e, f, g) {
446 if (typeof g !== 'object') {
450 var id = smoke.newdialog();
459 quiz: function (e, f, g) {
460 if (typeof g !== 'object') {
464 var id = smoke.newdialog();
473 prompt: function (e, f, g) {
474 if (typeof g !== 'object') {
478 var id = smoke.newdialog();
479 return smoke.build(e, {type: 'prompt', callback: f, params: g, newid: id});
482 listen: function (e, f, g) {
483 if (e.addEventListener) {
484 return e.addEventListener(f, g, false);
488 return e.attachEvent('on' + f, g);
494 stoplistening: function (e, f, g) {
495 if (e.removeEventListener) {
496 return e.removeEventListener(f, g, false);
500 return e.detachEvent('on' + f, g);
510 if (typeof module != 'undefined' && module.exports) {
511 module.exports = smoke;
512 } else if (typeof define === 'function' && define.amd) {
513 define('smoke', [], function () {