123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- /*
- SMOKE.JS - 0.1.3
- (c) 2011-2013 Jonathan Youngblood
- demos / documentation: http://smoke-js.com/
- */
- ;(function(window, document) {
- /*jslint browser: true, onevar: true, undef: true, nomen: false, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */
-
- var smoke = {
- smoketimeout: [],
- init: false,
- zindex: 40000,
- i: 0,
-
- bodyload: function(id) {
- var ff = document.createElement('div');
- ff.setAttribute('id','smoke-out-'+id);
- ff.className = 'smoke-base';
- ff.style.zIndex = smoke.zindex;
- smoke.zindex++;
- document.body.appendChild(ff);
- },
-
- newdialog: function() {
- var newid = new Date().getTime();
- newid = Math.random(1,99) + newid;
-
- if (!smoke.init) {
- smoke.listen(window,"load", function() {
- smoke.bodyload(newid);
- });
- } else {
- smoke.bodyload(newid);
- }
-
- return newid;
- },
-
- forceload: function() {},
-
- build: function (e, f) {
- smoke.i++;
-
- f.stack = smoke.i;
-
- e = e.replace(/\n/g,'<br />');
- e = e.replace(/\r/g,'<br />');
-
- var prompt = '',
- ok = 'OK',
- cancel = 'Cancel',
- classname = '',
- buttons = '',
- box;
-
- if (f.type === 'prompt') {
- prompt =
- '<div class="smoke-dialog-prompt">'+
- '<input class="input" id="smoke-dialog-input-'+f.newid+'" type="text" ' + (f.params.value ? 'value="' + f.params.value + '"' : '') + ' />'+
- '</div>';
- }
-
- if (f.params.ok) {
- ok = f.params.ok;
- }
-
- if (f.params.cancel) {
- cancel = f.params.cancel;
- }
-
- if (f.params.classname) {
- classname = f.params.classname;
- }
-
- if (f.type !== 'signal') {
- buttons = '<div class="smoke-dialog-buttons">';
- if (f.type === 'alert') {
- buttons +=
- '<button class="btn btn-md btn-round" id="alert-ok-'+f.newid+'">'+ok+'</button>';
- }
- else if (f.type === 'quiz') {
-
- if (f.params.button_1) {
- buttons +=
- '<button class="btn btn-md btn-round quiz-button" id="'+f.type+'-ok1-'+f.newid+'">'+f.params.button_1+'</button>';
- }
-
- if (f.params.button_2) {
- buttons +=
- '<button class="btn btn-md btn-round quiz-button" id="'+f.type+'-ok2-'+f.newid+'">'+f.params.button_2+'</button>';
- }
-
- if (f.params.button_3) {
- buttons +=
- '<button class="btn btn-md btn-round quiz-button" id="'+f.type+'-ok3-'+f.newid+'">'+f.params.button_3+'</button>';
- }
- if (f.params.button_cancel) {
- buttons +=
- '<button id="'+f.type+'-cancel-'+f.newid+'" class="btn btn-md btn-round cancel">'+f.params.button_cancel+'</button>';
- }
-
- }
-
- else if (f.type === 'prompt' || f.type === 'confirm') {
- if (f.params.reverseButtons) {
- buttons +=
- '<button class="btn btn-md btn-round btn-primary" id="'+f.type+'-ok-'+f.newid+'">'+ok+'</button>' +
- '<button class="btn btn-md btn-round cancel" id="'+f.type+'-cancel-'+f.newid+'">'+cancel+'</button>';
- } else {
- buttons +=
- '<button class="btn btn-md btn-round cancel" id="'+f.type+'-cancel-'+f.newid+'">'+cancel+'</button>'+
- '<button class="btn btn-md btn-round btn-primary" id="'+f.type+'-ok-'+f.newid+'">'+ok+'</button>';
- }
- }
- buttons += '</div>';
- }
-
-
- box =
- '<div class="smoke-dialog smoke '+classname+'">'+
- '<div class="smoke-dialog-inner">'+
- e+
- prompt+
- buttons+
- '</div>'+
- '</div>';
-
- if (!smoke.init) {
- smoke.listen(window,"load", function() {
- smoke.finishbuild(e,f,box);
- });
- } else{
- smoke.finishbuild(e,f,box);
- }
-
- },
-
- finishbuild: function(e, f, box) {
-
- var ff = document.getElementById('smoke-out-'+f.newid);
-
- ff.className = 'smoke-base smoke-visible smoke-' + f.type;
- ff.innerHTML = box;
-
- while (ff.innerHTML === "") {
- ff.innerHTML = box;
- }
-
- if (smoke.smoketimeout[f.newid]) {
- clearTimeout(smoke.smoketimeout[f.newid]);
- }
-
- // smoke.listen(
- // document.getElementById('smoke-bg-'+f.newid),
- // "click",
- // function () {
- // smoke.destroy(f.type, f.newid);
- // if (f.type === 'prompt' || f.type === 'confirm' || f.type === 'quiz') {
- // f.callback(false);
- // } else if (f.type === 'alert' && typeof f.callback !== 'undefined') {
- // f.callback();
- // }
- // }
- // );
-
-
- switch (f.type) {
- case 'alert':
- smoke.finishbuildAlert(e, f, box);
- break;
- case 'confirm':
- smoke.finishbuildConfirm(e, f, box);
- break;
- case 'quiz':
- smoke.finishbuildQuiz(e, f, box);
- break;
- case 'prompt':
- smoke.finishbuildPrompt(e, f, box);
- break;
- case 'signal':
- smoke.finishbuildSignal(e, f, box);
- break;
- default:
- throw "Unknown type: " + f.type;
- }
- },
-
- finishbuildAlert: function (e, f, box) {
- smoke.listen(
- document.getElementById('alert-ok-'+f.newid),
- "click",
- function () {
- smoke.destroy(f.type, f.newid);
- if (typeof f.callback !== 'undefined') {
- f.callback();
- }
- }
- );
-
- document.onkeyup = function (e) {
- if (!e) {
- e = window.event;
- }
- if (e.keyCode === 13 || e.keyCode === 32 || e.keyCode === 27) {
- smoke.destroy(f.type, f.newid);
- if (typeof f.callback !== 'undefined') {
- f.callback();
- }
- }
- };
- },
-
- finishbuildConfirm: function (e, f, box) {
- smoke.listen(
- document.getElementById('confirm-cancel-' + f.newid),
- "click",
- function ()
- {
- smoke.destroy(f.type, f.newid);
- f.callback(false);
- }
- );
-
- smoke.listen(
- document.getElementById('confirm-ok-' + f.newid),
- "click",
- function ()
- {
- smoke.destroy(f.type, f.newid);
- f.callback(true);
- }
- );
-
- document.onkeyup = function (e) {
- if (!e) {
- e = window.event;
- }
- if (e.keyCode === 13 || e.keyCode === 32) {
- smoke.destroy(f.type, f.newid);
- f.callback(true);
- } else if (e.keyCode === 27) {
- smoke.destroy(f.type, f.newid);
- f.callback(false);
- }
- };
- },
-
- finishbuildQuiz: function (e, f, box) {
- var a, b, c;
-
- smoke.listen(
- document.getElementById('quiz-cancel-' + f.newid),
- "click",
- function ()
- {
- smoke.destroy(f.type, f.newid);
- f.callback(false);
- }
- );
-
-
- if (a = document.getElementById('quiz-ok1-'+f.newid))
- smoke.listen(
- a,
- "click",
- function () {
- smoke.destroy(f.type, f.newid);
- f.callback(a.innerHTML);
- }
- );
-
-
- if (b = document.getElementById('quiz-ok2-'+f.newid))
- smoke.listen(
- b,
- "click",
- function () {
- smoke.destroy(f.type, f.newid);
- f.callback(b.innerHTML);
- }
- );
-
-
- if (c = document.getElementById('quiz-ok3-'+f.newid))
- smoke.listen(
- c,
- "click",
- function () {
- smoke.destroy(f.type, f.newid);
- f.callback(c.innerHTML);
- }
- );
-
- document.onkeyup = function (e) {
- if (!e) {
- e = window.event;
- }
- if (e.keyCode === 27) {
- smoke.destroy(f.type, f.newid);
- f.callback(false);
- }
- };
-
- },
-
- finishbuildPrompt: function (e, f, box) {
- var pi = document.getElementById('smoke-dialog-input-'+f.newid);
-
- setTimeout(function () {
- pi.focus();
- pi.select();
- }, 100);
-
- smoke.listen(
- document.getElementById('prompt-cancel-'+f.newid),
- "click",
- function () {
- smoke.destroy(f.type, f.newid);
- f.callback(false);
- }
- );
-
- smoke.listen(
- document.getElementById('prompt-ok-'+f.newid),
- "click",
- function () {
- smoke.destroy(f.type, f.newid);
- f.callback(pi.value);
- }
- );
-
- document.onkeyup = function (e) {
- if (!e) {
- e = window.event;
- }
-
- if (e.keyCode === 13) {
- smoke.destroy(f.type, f.newid);
- f.callback(pi.value);
- } else if (e.keyCode === 27) {
- smoke.destroy(f.type, f.newid);
- f.callback(false);
- }
- };
- },
-
- finishbuildSignal: function (e, f, box) {
-
-
- document.onkeyup = function (e) {
- if (!e) {
- e = window.event;
- }
- if (e.keyCode === 27) {
- smoke.destroy(f.type, f.newid);
- if (typeof f.callback !== 'undefined') {
- f.callback();
- }
- }
- };
-
- smoke.smoketimeout[f.newid] = setTimeout(function () {
- smoke.destroy(f.type, f.newid);
- if (typeof f.callback !== 'undefined') {
- f.callback();
- }
- }, f.timeout);
- },
-
-
- destroy: function (type,id) {
-
- var box = document.getElementById('smoke-out-'+id);
-
- if (type !== 'quiz') {
- var okButton = document.getElementById(type+'-ok-'+id);
- }
-
- var cancelButton = document.getElementById(type+'-cancel-'+id);
- box.className = 'smoke-base';
-
- if (okButton) {
- smoke.stoplistening(okButton, "click", function() {});
- document.onkeyup = null;
- }
-
- if (type === 'quiz') {
- var quiz_buttons = document.getElementsByClassName("quiz-button");
- for (var i = 0; i < quiz_buttons.length; i++) {
- smoke.stoplistening(quiz_buttons[i], "click", function() {});
- document.onkeyup = null;
- }
- }
-
- if (cancelButton) {
- smoke.stoplistening(cancelButton, "click", function() {});
- }
-
- smoke.i = 0;
- box.innerHTML = '';
- },
-
- alert: function (e, f, g) {
- if (typeof g !== 'object') {
- g = false;
- }
-
- var id = smoke.newdialog();
-
- smoke.build(e, {
- type: 'alert',
- callback: f,
- params: g,
- newid: id
- });
- },
-
- signal: function (e, f, g) {
- if (typeof g !== 'object') {
- g = false;
- }
- var duration = 5000;
- if (g.duration !== 'undefined'){
- duration = g.duration;
- }
-
- var id = smoke.newdialog();
- smoke.build(e, {
- type: 'signal',
- callback: f,
- timeout: duration,
- params: g,
- newid: id
- });
- },
-
- confirm: function (e, f, g) {
- if (typeof g !== 'object') {
- g = false;
- }
-
- var id = smoke.newdialog();
- smoke.build(e, {
- type: 'confirm',
- callback: f,
- params: g,
- newid: id
- });
- },
-
- quiz: function (e, f, g) {
- if (typeof g !== 'object') {
- g = false;
- }
-
- var id = smoke.newdialog();
- smoke.build(e, {
- type: 'quiz',
- callback: f,
- params: g,
- newid: id
- });
- },
-
- prompt: function (e, f, g) {
- if (typeof g !== 'object') {
- g = false;
- }
-
- var id = smoke.newdialog();
- return smoke.build(e,{type:'prompt',callback:f,params:g,newid:id});
- },
-
- listen: function (e, f, g) {
- if (e.addEventListener) {
- return e.addEventListener(f, g, false);
- }
-
- if (e.attachEvent) {
- return e.attachEvent('on'+f, g);
- }
-
- return false;
- },
-
- stoplistening: function (e, f, g) {
- if (e.removeEventListener) {
- return e.removeEventListener(f, g, false);
- }
-
- if (e.detachEvent) {
- return e.detachEvent('on'+f, g);
- }
-
- return false;
- }
- };
-
-
- smoke.init = true;
- if (typeof module != 'undefined' && module.exports) {
- module.exports = smoke;
- }
- else if (typeof define === 'function' && define.amd) {
- define('smoke', [], function() {
- return smoke;
- });
- }
- else {
- this.smoke = smoke;
- }
- })(window, document);
|