diff options
-rw-r--r-- | dist/demos/loan_calculator.html | 2 | ||||
-rw-r--r-- | dist/demos/purecss.html | 2 | ||||
-rw-r--r-- | dist/demos/select_filter.html | 10 | ||||
-rw-r--r-- | dist/demos/survey.html | 2 | ||||
-rw-r--r-- | dist/demos/twitter_bootstrap.html | 2 | ||||
-rw-r--r-- | dist/demos/user_manager.html | 2 | ||||
-rw-r--r-- | dist/index.html | 29 | ||||
-rw-r--r-- | dist/jquery-impromptu.js | 1143 | ||||
-rw-r--r-- | dist/jquery-impromptu.min.js | 2 | ||||
-rw-r--r-- | src/demos/loan_calculator.html | 2 | ||||
-rw-r--r-- | src/demos/purecss.html | 2 | ||||
-rw-r--r-- | src/demos/select_filter.html | 10 | ||||
-rw-r--r-- | src/demos/survey.html | 2 | ||||
-rw-r--r-- | src/demos/twitter_bootstrap.html | 2 | ||||
-rw-r--r-- | src/demos/user_manager.html | 2 | ||||
-rw-r--r-- | src/index.html | 29 | ||||
-rw-r--r-- | src/jquery-impromptu.js | 1141 | ||||
-rw-r--r-- | test/jquery-impromptu_spec.js | 542 |
18 files changed, 1520 insertions, 1406 deletions
diff --git a/dist/demos/loan_calculator.html b/dist/demos/loan_calculator.html index 3fbbfbb..c4909b2 100644 --- a/dist/demos/loan_calculator.html +++ b/dist/demos/loan_calculator.html @@ -93,7 +93,7 @@ } }; - $.prompt(jqistates); + $.prompt.open(jqistates); } </script> </head> diff --git a/dist/demos/purecss.html b/dist/demos/purecss.html index e07c4bd..0e86eee 100644 --- a/dist/demos/purecss.html +++ b/dist/demos/purecss.html @@ -88,7 +88,7 @@ } } - $.prompt(temp,{ + $.prompt.open(temp,{ close: function(e,v,m,f){ if(v !== undefined){ var str = "You can now process with this given information:<br />"; diff --git a/dist/demos/select_filter.html b/dist/demos/select_filter.html index a5e3f96..2f45497 100644 --- a/dist/demos/select_filter.html +++ b/dist/demos/select_filter.html @@ -51,7 +51,7 @@ e.preventDefault(); - var $imp = $.prompt(html, { + var imp = $.prompt.open(html, { title: 'Search Options', buttons: { Ok:true,Cancel:false }, submit: function(e,v,m,f){ @@ -61,20 +61,20 @@ } }); - var $so = $('#searchoptions', $imp); + var $so = $('#searchoptions', imp.getPrompt()); $so.delegate('input', 'click', function(){ $('tr.checked', $so).removeClass('checked'); $(this).parent().parent().addClass('checked'); }); - $('#searchquery', $imp).focus().keydown(function(event){ + $('#searchquery', imp.getPrompt()).focus().keydown(function(event){ var $i = $(this), query = $i.val(), re = new RegExp(query,"i"), html = "", counter = 0, - $sos = $('#optionsearchstatus',$imp); + $sos = $('#optionsearchstatus',imp.getPrompt()); if(event.which == 13 || event.keyCode == 13 && $.trim(query) !== ''){ @@ -98,7 +98,7 @@ $so.find('tbody').html(html); $so.find('tr:eq(0)').addClass('checked').find('input').attr('checked',true); - $('#searchoptionswrapper',$imp).slideDown('slow', function(){ + $('#searchoptionswrapper',imp.getPrompt()).slideDown('slow', function(){ $sos.hide(); }); diff --git a/dist/demos/survey.html b/dist/demos/survey.html index d4f8c3f..f249661 100644 --- a/dist/demos/survey.html +++ b/dist/demos/survey.html @@ -78,7 +78,7 @@ } } - $.prompt(temp,{ + $.prompt.open(temp,{ close: function(e,v,m,f){ if(v !== undefined){ var str = "You can now process with this given information:<br />"; diff --git a/dist/demos/twitter_bootstrap.html b/dist/demos/twitter_bootstrap.html index eca1583..d595445 100644 --- a/dist/demos/twitter_bootstrap.html +++ b/dist/demos/twitter_bootstrap.html @@ -88,7 +88,7 @@ } } - $.prompt(temp,{ + $.prompt.open(temp,{ close: function(e,v,m,f){ if(v !== undefined){ var str = "You can now process with this given information:<br />"; diff --git a/dist/demos/user_manager.html b/dist/demos/user_manager.html index d23a660..0201617 100644 --- a/dist/demos/user_manager.html +++ b/dist/demos/user_manager.html @@ -37,7 +37,7 @@ '<div class="field"><label for="editfname">First Name</label><input type="text" id="editfname" name="editfname" value="'+ fname +'" /></div>'+ '<div class="field"><label for="editlname">Last Name</label><input type="text" id="editlname" name="editlname" value="'+ lname +'" /></div>'; - $.prompt(txt,{ + $.prompt.open(txt,{ buttons:{Change:true, Cancel:false}, submit: function(e,v,m,f){ //this is simple pre submit validation, the submit function diff --git a/dist/index.html b/dist/index.html index 6d70c1a..389ee22 100644 --- a/dist/index.html +++ b/dist/index.html @@ -128,7 +128,7 @@ <!-- ---------------------------------------------- --> <div id="Options" class="section"> <h2>Options</h2> - <pre><code>$.prompt( msg , options )</code></pre> + <pre><code>$.prompt.open( msg , options )</code></pre> <h3>msg</h3> <p>The message can either be an html string, or an object of "states". Each state has the following properties:</p> <dl> @@ -247,24 +247,27 @@ <div id="Methods" class="section"> <h2>Methods</h2> <dl class="methoddl"> - <dt>jQuery.prompt.setDefaults(options)</dt> + <dt>Impromptu.setDefaults(options)</dt> <dd> Sets the defaults for prompts.<br /> -<pre><code>jQuery.prompt.setDefaults({ +<pre><code>Impromptu.setDefaults({ prefix: 'myPrompt', show: 'slideDown' });</pre></code> </dd> - <dt>jQuery.prompt.setStateDefaults(options)</dt> + <dt>Impromptu.setStateDefaults(options)</dt> <dd> Sets the defaults for states.<br /> -<pre><code>jQuery.prompt.setStateDefaults({ +<pre><code>Impromptu.setStateDefaults({ buttons: { Ok:true, Cancel:false }, focus: 1 });</pre></code> </dd> + <dt>jQuery.prompt.open(states, options)</dt> + <dd>Returns an instance of Impromptu. Opens the prompt.</dd> + <dt>jQuery.prompt.getCurrentState()</dt> <dd>Returns a jquery object of the current visible state.</dd> @@ -311,7 +314,7 @@ myPrompt.on('impromptu:loaded', function(e){});</pre> <dt>impromptu:submit</dt> <dd>Same as submit option. Scope is the state element. These events are per state, so you must attach it directly to a state. <pre>var myPrompt = jQuery.prompt(/*...*/); -$.prompt.getStateContent('state2') +$.prompt.getState('state2') .on('impromptu:submit', function(e,v,m,f){});</pre></dd> <dt>impromptu:close</dt> @@ -335,7 +338,7 @@ $.prompt.getStateContent('state2') <div class="example-container"> <p>A prompt in the simplest of fashion.</p> -<pre class="code">$.prompt("Hello World!");</pre> +<pre class="code">$.prompt.open("Hello World!");</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -343,7 +346,7 @@ $.prompt.getStateContent('state2') <div class="example-container"> <p>Lets add some buttons and a title.</p> -<pre class="code">$.prompt("Proceeding may be good for your site..", { +<pre class="code">$.prompt.open("Proceeding may be good for your site..", { title: "Are you Ready?", buttons: { "Yes, I'm Ready": true, "No, Lets Wait": false } });</pre> @@ -354,7 +357,7 @@ $.prompt.getStateContent('state2') <div class="example-container"> <p>Use the submit function to get the answer.</p> -<pre class="code">$.prompt("Open your javascript console to see the answer.", { +<pre class="code">$.prompt.open("Open your javascript console to see the answer.", { title: "Are you Ready?", buttons: { "Yes, I'm Ready": true, "No, Lets Wait": false }, submit: function(e,v,m,f){ @@ -401,7 +404,7 @@ $.prompt.getStateContent('state2') } }; -$.prompt(statesdemo);</pre> +$.prompt.open(statesdemo);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -445,7 +448,7 @@ $.prompt(statesdemo);</pre> } }; -$.prompt(statesdemo);</pre> +$.prompt.open(statesdemo);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -514,7 +517,7 @@ tourStates = [ submit: tourSubmitFunc } ]; -$.prompt(tourStates);</pre> +$.prompt.open(tourStates);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -572,7 +575,7 @@ $.prompt(tourStates);</pre> }, }; -$.prompt(statesdemo);</pre> +$.prompt.open(statesdemo);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> diff --git a/dist/jquery-impromptu.js b/dist/jquery-impromptu.js index ac03bcd..6e23935 100644 --- a/dist/jquery-impromptu.js +++ b/dist/jquery-impromptu.js @@ -8,214 +8,32 @@ factory(root.jQuery);
}
}(this, function($) {
- "use strict";
+ 'use strict';
+
+ // ########################################################################
+ // Base object
+ // ########################################################################
/**
- * Class constructor
+ * Imp - Impromptu object - passing no params will not open, only return the instance
* @param message String/Object - String of html or Object of states
* @param options Object - Options to set the prompt
- * @return jQuery - container with overlay and prompt
+ * @return Imp - the instance of this Impromptu object
*/
- var Imp = function(message, options) {
- // only for backwards compat, to be removed in future version
- if(options !== undefined && options.classes !== undefined && typeof options.classes === 'string'){
- options = { box: options.classes };
- }
-
- Imp.options = $.extend({},Imp.defaults,options);
- Imp.currentPrefix = Imp.options.prefix;
-
- // Be sure any previous timeouts are destroyed
- if(Imp.timeout){
- clearTimeout(Imp.timeout);
- }
- Imp.timeout = false;
-
- var opts = Imp.options,
- $body = $(document.body),
- $window = $(window);
-
- //build the box and fade
- var msgbox = '<div class="'+ Imp.options.prefix +'box '+ opts.classes.box +'">';
- if(opts.useiframe && ($('object, applet').length > 0)) {
- msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></iframe>';
- } else {
- msgbox +='<div class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></div>';
- }
- msgbox += '<div class="'+ opts.prefix +' '+ opts.classes.prompt +'">'+
- '<form action="javascript:false;" onsubmit="return false;" class="'+ opts.prefix +'form '+ opts.classes.form +'">'+
- '<div class="'+ opts.prefix +'close '+ opts.classes.close +'">'+ opts.closeText +'</div>'+
- '<div class="'+ opts.prefix +'states"></div>'+
- '</form>'+
- '</div>'+
- '</div>';
-
- Imp.jqib = $(msgbox).appendTo($body);
- Imp.jqi = Imp.jqib.children('.'+ opts.prefix);//.data('jqi',opts);
- Imp.jqif = Imp.jqib.children('.'+ opts.prefix +'fade');
-
- //if a string was passed, convert to a single state
- if(message.constructor === String){
- message = {
- state0: {
- title: opts.title,
- html: message,
- buttons: opts.buttons,
- position: opts.position,
- focus: opts.focus,
- defaultButton: opts.defaultButton,
- submit: opts.submit
- }
- };
+ var Imp = function(message, options){
+ if(message){
+ this.open(message, options);
}
-
- //build the states
- Imp.options.states = {};
- var k,v;
- for(k in message){
- v = $.extend({},Imp.defaults.state,{name:k},message[k]);
- Imp.addState(v.name, v);
-
- if(Imp.currentStateName === ''){
- Imp.currentStateName = v.name;
- }
- }
-
- //Events
- Imp.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
- var $t = $(this),
- $state = $t.parents('.'+ opts.prefix +'state'),
- stateobj = Imp.options.states[$state.data('jqi-name')],
- msg = $state.children('.'+ opts.prefix +'message'),
- clicked = stateobj.buttons[$t.text()] || stateobj.buttons[$t.html()],
- forminputs = {};
-
- // if for some reason we couldn't get the value
- if(clicked === undefined){
- for(var i in stateobj.buttons){
- if(stateobj.buttons[i].title === $t.text() || stateobj.buttons[i].title === $t.html()){
- clicked = stateobj.buttons[i].value;
- }
- }
- }
-
- //collect all form element values from all states.
- $.each(Imp.jqi.children('form').serializeArray(),function(i,obj){
- if (forminputs[obj.name] === undefined) {
- forminputs[obj.name] = obj.value;
- } else if (typeof forminputs[obj.name] === Array || typeof forminputs[obj.name] === 'object') {
- forminputs[obj.name].push(obj.value);
- } else {
- forminputs[obj.name] = [forminputs[obj.name],obj.value];
- }
- });
-
- // trigger an event
- var promptsubmite = new $.Event('impromptu:submit');
- promptsubmite.stateName = stateobj.name;
- promptsubmite.state = $state;
- $state.trigger(promptsubmite, [clicked, msg, forminputs]);
-
- if(!promptsubmite.isDefaultPrevented()){
- Imp.close(true, clicked,msg,forminputs);
- }
- });
-
- // if the fade is clicked blink the prompt
- var fadeClicked = function(){
- if(opts.persistent){
- var offset = (opts.top.toString().indexOf('%') >= 0? ($window.height()*(parseInt(opts.top,10)/100)) : parseInt(opts.top,10)),
- top = parseInt(Imp.jqi.css('top').replace('px',''),10) - offset;
-
- //$window.scrollTop(top);
- $('html,body').animate({ scrollTop: top }, 'fast', function(){
- var i = 0;
- Imp.jqib.addClass(opts.prefix +'warning');
- var intervalid = setInterval(function(){
- Imp.jqib.toggleClass(opts.prefix +'warning');
- if(i++ > 1){
- clearInterval(intervalid);
- Imp.jqib.removeClass(opts.prefix +'warning');
- }
- }, 100);
- });
- }
- else {
- Imp.close(true);
- }
- };
-
- // listen for esc or tab keys
- var keyDownEventHandler = function(e){
- var key = (window.event) ? event.keyCode : e.keyCode;
-
- //escape key closes
- if(key === 27) {
- fadeClicked();
- }
-
- //enter key pressed trigger the default button if its not on it, ignore if it is a textarea
- if(key === 13){
- var $defBtn = Imp.getCurrentState().find('.'+ opts.prefix +'defaultbutton');
- var $tgt = $(e.target);
-
- if($tgt.is('textarea,.'+opts.prefix+'button') === false && $defBtn.length > 0){
- e.preventDefault();
- $defBtn.click();
- }
- }
-
- //constrain tabs, tabs should iterate through the state and not leave
- if (key === 9){
- var $inputels = $('input,select,textarea,button',Imp.getCurrentState());
- var fwd = !e.shiftKey && e.target === $inputels[$inputels.length-1];
- var back = e.shiftKey && e.target === $inputels[0];
- if (fwd || back) {
- setTimeout(function(){
- if (!$inputels){
- return;
- }
- var el = $inputels[back===true ? $inputels.length-1 : 0];
-
- if (el){
- el.focus();
- }
- },10);
- return false;
- }
- }
- };
-
- Imp.position();
- Imp.style();
-
- Imp.jqif.click(fadeClicked);
- $window.resize({animate:false}, Imp.position);
- Imp.jqi.find('.'+ opts.prefix +'close').click(Imp.close);
- Imp.jqib.on("keydown",keyDownEventHandler)
- .on('impromptu:loaded', opts.loaded)
- .on('impromptu:close', opts.close)
- .on('impromptu:statechanging', opts.statechanging)
- .on('impromptu:statechanged', opts.statechanged);
-
- // Show it
- Imp.jqif[opts.show](opts.overlayspeed);
- Imp.jqi[opts.show](opts.promptspeed, function(){
-
- var $firstState = Imp.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
- Imp.goToState($firstState.data('jqi-name'));
-
- Imp.jqib.trigger('impromptu:loaded');
- });
-
- // Timeout
- if(opts.timeout > 0){
- Imp.timeout = setTimeout(function(){ Imp.close(true); },opts.timeout);
- }
-
- return Imp.jqib;
+ return this;
};
+ // ########################################################################
+ // static properties and methods
+ // ########################################################################
+
+ /**
+ * defaults - the default options
+ */
Imp.defaults = {
prefix:'jqi',
classes: {
@@ -282,18 +100,6 @@ };
/**
- * currentPrefix String - At any time this show be the prefix
- * of the current prompt ex: "jqi"
- */
- Imp.currentPrefix = Imp.defaults.prefix;
-
- /**
- * currentStateName String - At any time this is the current state
- * of the current prompt ex: "state0"
- */
- Imp.currentStateName = "";
-
- /**
* setDefaults - Sets the default options
* @param o Object - Options to set as defaults
* @return void
@@ -311,406 +117,628 @@ Imp.defaults.state = $.extend({}, Imp.defaults.state, o);
};
- /**
- * position - Repositions the prompt (Used internally)
- * @return void
- */
- Imp.position = function(e){
- var restoreFx = $.fx.off,
- $state = Imp.getCurrentState(),
- stateObj = Imp.options.states[$state.data('jqi-name')],
- pos = stateObj? stateObj.position : undefined,
- $window = $(window),
- bodyHeight = document.body.scrollHeight, //$(document.body).outerHeight(true),
- windowHeight = $(window).height(),
- documentHeight = $(document).height(),
- height = bodyHeight > windowHeight ? bodyHeight : windowHeight,
- top = parseInt($window.scrollTop(),10) + (Imp.options.top.toString().indexOf('%') >= 0?
- (windowHeight*(parseInt(Imp.options.top,10)/100)) : parseInt(Imp.options.top,10));
-
- // when resizing the window turn off animation
- if(e !== undefined && e.data.animate === false){
- $.fx.off = true;
- }
+ // ########################################################################
+ // extend our object instance properties and methods
+ // ########################################################################
+ Imp.prototype = {
+
+ /**
+ * open - Opens the prompt
+ * @param message String/Object - String of html or Object of states
+ * @param options Object - Options to set the prompt
+ * @return Imp - the instance of this Impromptu object
+ */
+ open: function(message, options) {
+ var t = this;
+
+ t.options = $.extend({},Imp.defaults,options);
+
+ // Be sure any previous timeouts are destroyed
+ if(t.timeout){
+ clearTimeout(t.timeout);
+ }
+ t.timeout = false;
+
+ var opts = t.options,
+ $body = $(document.body),
+ $window = $(window);
+
+ //build the box and fade
+ var msgbox = '<div class="'+ opts.prefix +'box '+ opts.classes.box +'">';
+ if(opts.useiframe && ($('object, applet').length > 0)) {
+ msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></iframe>';
+ } else {
+ msgbox +='<div class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></div>';
+ }
+ msgbox += '<div class="'+ opts.prefix +' '+ opts.classes.prompt +'">'+
+ '<form action="javascript:false;" onsubmit="return false;" class="'+ opts.prefix +'form '+ opts.classes.form +'">'+
+ '<div class="'+ opts.prefix +'close '+ opts.classes.close +'">'+ opts.closeText +'</div>'+
+ '<div class="'+ opts.prefix +'states"></div>'+
+ '</form>'+
+ '</div>'+
+ '</div>';
+
+ t.jqib = $(msgbox).appendTo($body);
+ t.jqi = t.jqib.children('.'+ opts.prefix);//.data('jqi',opts);
+ t.jqif = t.jqib.children('.'+ opts.prefix +'fade');
+
+ //if a string was passed, convert to a single state
+ if(message.constructor === String){
+ message = {
+ state0: {
+ title: opts.title,
+ html: message,
+ buttons: opts.buttons,
+ position: opts.position,
+ focus: opts.focus,
+ defaultButton: opts.defaultButton,
+ submit: opts.submit
+ }
+ };
+ }
- Imp.jqib.css({
- position: "absolute",
- height: height,
- width: "100%",
- top: 0,
- left: 0,
- right: 0,
- bottom: 0
- });
- Imp.jqif.css({
- position: "fixed",
- height: height,
- width: "100%",
- top: 0,
- left: 0,
- right: 0,
- bottom: 0
- });
-
- // tour positioning
- if(pos && pos.container){
- var offset = $(pos.container).offset();
-
- if($.isPlainObject(offset) && offset.top !== undefined){
- Imp.jqi.css({
- position: "absolute"
- });
- Imp.jqi.animate({
- top: offset.top + pos.y,
- left: offset.left + pos.x,
- marginLeft: 0,
- width: (pos.width !== undefined)? pos.width : null
- });
- top = (offset.top + pos.y) - (Imp.options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt(Imp.options.top,10)/100)) : parseInt(Imp.options.top,10));
- $('html,body').animate({ scrollTop: top }, 'slow', 'swing', function(){});
+ //build the states
+ t.options.states = {};
+ var k,v;
+ for(k in message){
+ v = $.extend({},Imp.defaults.state,{name:k},message[k]);
+ t.addState(v.name, v);
+
+ if(t.currentStateName === ''){
+ t.currentStateName = v.name;
+ }
}
- }
- // custom state width animation
- else if(pos && pos.width){
- Imp.jqi.css({
- position: "absolute",
- left: '50%'
- });
- Imp.jqi.animate({
- top: pos.y || top,
- left: pos.x || '50%',
- marginLeft: ((pos.width/2)*-1),
- width: pos.width
+
+ //Events
+ t.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
+ var $t = $(this),
+ $state = $t.parents('.'+ opts.prefix +'state'),
+ stateobj = t.options.states[$state.data('jqi-name')],
+ msg = $state.children('.'+ opts.prefix +'message'),
+ clicked = stateobj.buttons[$t.text()] || stateobj.buttons[$t.html()],
+ forminputs = {};
+
+ // if for some reason we couldn't get the value
+ if(clicked === undefined){
+ for(var i in stateobj.buttons){
+ if(stateobj.buttons[i].title === $t.text() || stateobj.buttons[i].title === $t.html()){
+ clicked = stateobj.buttons[i].value;
+ }
+ }
+ }
+
+ //collect all form element values from all states.
+ $.each(t.jqi.children('form').serializeArray(),function(i,obj){
+ if (forminputs[obj.name] === undefined) {
+ forminputs[obj.name] = obj.value;
+ } else if (typeof forminputs[obj.name] === Array || typeof forminputs[obj.name] === 'object') {
+ forminputs[obj.name].push(obj.value);
+ } else {
+ forminputs[obj.name] = [forminputs[obj.name],obj.value];
+ }
});
- }
- // standard prompt positioning
- else{
- Imp.jqi.css({
- position: "absolute",
- top: top,
- left: '50%',//$window.width()/2,
- marginLeft: ((Imp.jqi.outerWidth(false)/2)*-1)
- });
- }
- // restore fx settings
- if(e !== undefined && e.data.animate === false){
- $.fx.off = restoreFx;
- }
- };
+ // trigger an event
+ var promptsubmite = new $.Event('impromptu:submit');
+ promptsubmite.stateName = stateobj.name;
+ promptsubmite.state = $state;
+ $state.trigger(promptsubmite, [clicked, msg, forminputs]);
- /**
- * style - Restyles the prompt (Used internally)
- * @return void
- */
- Imp.style = function(){
- Imp.jqif.css({
- zIndex: Imp.options.zIndex,
- display: "none",
- opacity: Imp.options.opacity
- });
- Imp.jqi.css({
- zIndex: Imp.options.zIndex+1,
- display: "none"
- });
- Imp.jqib.css({
- zIndex: Imp.options.zIndex
- });
- };
+ if(!promptsubmite.isDefaultPrevented()){
+ t.close(true, clicked,msg,forminputs);
+ }
+ });
- /**
- * get - Get the prompt
- * @return jQuery - the prompt
- */
- Imp.get = function(state) {
- return $('.'+ Imp.currentPrefix);
- };
+ // if the fade is clicked blink the prompt
+ var fadeClicked = function(){
+ if(opts.persistent){
+ var offset = (opts.top.toString().indexOf('%') >= 0? ($window.height()*(parseInt(opts.top,10)/100)) : parseInt(opts.top,10)),
+ top = parseInt(t.jqi.css('top').replace('px',''),10) - offset;
+
+ //$window.scrollTop(top);
+ $('html,body').animate({ scrollTop: top }, 'fast', function(){
+ var i = 0;
+ t.jqib.addClass(opts.prefix +'warning');
+ var intervalid = setInterval(function(){
+ t.jqib.toggleClass(opts.prefix +'warning');
+ if(i++ > 1){
+ clearInterval(intervalid);
+ t.jqib.removeClass(opts.prefix +'warning');
+ }
+ }, 100);
+ });
+ }
+ else {
+ t.close(true);
+ }
+ };
- /**
- * addState - Injects a state into the prompt
- * @param statename String - Name of the state
- * @param stateobj Object - options for the state
- * @param afterState String - selector of the state to insert after
- * @return jQuery - the newly created state
- */
- Imp.addState = function(statename, stateobj, afterState) {
- var state = "",
- $state = null,
- arrow = "",
- title = "",
- opts = Imp.options,
- $jqistates = $('.'+ Imp.currentPrefix +'states'),
- buttons = [],
- showHtml,defbtn,k,v,l,i=0;
-
- stateobj = $.extend({},Imp.defaults.state, {name:statename}, stateobj);
-
- if(stateobj.position.arrow !== null){
- arrow = '<div class="'+ opts.prefix + 'arrow '+ opts.prefix + 'arrow'+ stateobj.position.arrow +'"></div>';
- }
- if(stateobj.title && stateobj.title !== ''){
- title = '<div class="lead '+ opts.prefix + 'title '+ opts.classes.title +'">'+ stateobj.title +'</div>';
- }
+ // listen for esc or tab keys
+ var keyDownEventHandler = function(e){
+ var key = (window.event) ? event.keyCode : e.keyCode;
- showHtml = stateobj.html;
- if (typeof stateobj.html === 'function') {
- showHtml = 'Error: html function must return text';
- }
+ //escape key closes
+ if(key === 27) {
+ fadeClicked();
+ }
- state += '<div id="'+ opts.prefix +'state_'+ statename +'" class="'+ opts.prefix + 'state" data-jqi-name="'+ statename +'" style="display:none;">'+
- arrow + title +
- '<div class="'+ opts.prefix +'message '+ opts.classes.message +'">' + showHtml +'</div>'+
- '<div class="'+ opts.prefix +'buttons '+ opts.classes.buttons +'"'+ ($.isEmptyObject(stateobj.buttons)? 'style="display:none;"':'') +'>';
+ //enter key pressed trigger the default button if its not on it, ignore if it is a textarea
+ if(key === 13){
+ var $defBtn = t.getCurrentState().find('.'+ opts.prefix +'defaultbutton');
+ var $tgt = $(e.target);
- // state buttons may be in object or array, lets convert objects to arrays
- if($.isArray(stateobj.buttons)){
- buttons = stateobj.buttons;
- }
- else if($.isPlainObject(stateobj.buttons)){
- for(k in stateobj.buttons){
- if(stateobj.buttons.hasOwnProperty(k)){
- buttons.push({ title: k, value: stateobj.buttons[k] });
+ if($tgt.is('textarea,.'+opts.prefix+'button') === false && $defBtn.length > 0){
+ e.preventDefault();
+ $defBtn.click();
+ }
+ }
+
+ //constrain tabs, tabs should iterate through the state and not leave
+ if (key === 9){
+ var $inputels = $('input,select,textarea,button',t.getCurrentState());
+ var fwd = !e.shiftKey && e.target === $inputels[$inputels.length-1];
+ var back = e.shiftKey && e.target === $inputels[0];
+ if (fwd || back) {
+ setTimeout(function(){
+ if (!$inputels){
+ return;
+ }
+ var el = $inputels[back===true ? $inputels.length-1 : 0];
+
+ if (el){
+ el.focus();
+ }
+ },10);
+ return false;
+ }
}
+ };
+
+ t.position();
+ t.style();
+
+ t.jqif.click(fadeClicked);
+ $window.resize({animate:false}, function(){ t.position(); });
+ t.jqi.find('.'+ opts.prefix +'close').click(t.close);
+ t.jqib.on("keydown",keyDownEventHandler)
+ .on('impromptu:loaded', opts.loaded)
+ .on('impromptu:close', opts.close)
+ .on('impromptu:statechanging', opts.statechanging)
+ .on('impromptu:statechanged', opts.statechanged);
+
+ // Show it
+ t.jqif[opts.show](opts.overlayspeed);
+ t.jqi[opts.show](opts.promptspeed, function(){
+
+ var $firstState = t.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
+ t.goToState($firstState.data('jqi-name'));
+
+ t.jqib.trigger('impromptu:loaded');
+ });
+
+ // Timeout
+ if(opts.timeout > 0){
+ t.timeout = setTimeout(function(){ t.close(true); },opts.timeout);
}
- }
- // iterate over each button and create them
- for(i=0, l=buttons.length; i<l; i++){
- v = buttons[i],
- defbtn = stateobj.focus === i || (isNaN(stateobj.focus) && stateobj.defaultButton === i) ? (Imp.currentPrefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
+ return t;
+ },
- state += '<button class="'+ opts.classes.button +' '+ Imp.currentPrefix + 'button '+ defbtn;
+ /**
+ * close - Closes the prompt
+ * @param callback Function - called when the transition is complete
+ * @param clicked String - value of the button clicked (only used internally)
+ * @param msg jQuery - The state message body (only used internally)
+ * @param forvals Object - key/value pairs of all form field names and values (only used internally)
+ * @return Imp - the instance of this Impromptu object
+ */
+ close: function(callCallback, clicked, msg, formvals){
+ var t = this;
+
+ if(t.timeout){
+ clearTimeout(t.timeout);
+ t.timeout = false;
+ }
- if(typeof v.classes !== "undefined"){
- state += ' '+ ($.isArray(v.classes)? v.classes.join(' ') : v.classes) + ' ';
+ if(t.jqib){
+ t.jqib.fadeOut('fast',function(){
+
+ t.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
+
+ t.jqib.remove();
+
+ $(window).off('resize', function(){ t.position(); });
+
+ if(typeof callCallback === 'function'){
+ callCallback();
+ }
+ });
}
+ t.currentStateName = "";
- state += '" name="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" id="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
- }
-
- state += '</div></div>';
+ return t;
+ },
- $state = $(state);
+ /**
+ * addState - Injects a state into the prompt
+ * @param statename String - Name of the state
+ * @param stateobj Object - options for the state
+ * @param afterState String - selector of the state to insert after
+ * @return jQuery - the newly created state
+ */
+ addState: function(statename, stateobj, afterState) {
+ var t = this,
+ state = '',
+ $state = null,
+ arrow = '',
+ title = '',
+ opts = t.options,
+ $jqistates = $('.'+ opts.prefix +'states'),
+ buttons = [],
+ showHtml,defbtn,k,v,l,i=0;
+
+ stateobj = $.extend({},Imp.defaults.state, {name:statename}, stateobj);
+
+ if(stateobj.position.arrow !== null){
+ arrow = '<div class="'+ opts.prefix + 'arrow '+ opts.prefix + 'arrow'+ stateobj.position.arrow +'"></div>';
+ }
+ if(stateobj.title && stateobj.title !== ''){
+ title = '<div class="lead '+ opts.prefix + 'title '+ opts.classes.title +'">'+ stateobj.title +'</div>';
+ }
- $state.on('impromptu:submit', stateobj.submit);
+ showHtml = stateobj.html;
+ if (typeof stateobj.html === 'function') {
+ showHtml = 'Error: html function must return text';
+ }
- if(afterState !== undefined){
- $jqistates.find('#'+ Imp.currentPrefix +'state_'+ afterState).after($state);
- }
- else{
- $jqistates.append($state);
- }
+ state += '<div id="'+ opts.prefix +'state_'+ statename +'" class="'+ opts.prefix + 'state" data-jqi-name="'+ statename +'" style="display:none;">'+
+ arrow + title +
+ '<div class="'+ opts.prefix +'message '+ opts.classes.message +'">' + showHtml +'</div>'+
+ '<div class="'+ opts.prefix +'buttons '+ opts.classes.buttons +'"'+ ($.isEmptyObject(stateobj.buttons)? 'style="display:none;"':'') +'>';
- Imp.options.states[statename] = stateobj;
+ // state buttons may be in object or array, lets convert objects to arrays
+ if($.isArray(stateobj.buttons)){
+ buttons = stateobj.buttons;
+ }
+ else if($.isPlainObject(stateobj.buttons)){
+ for(k in stateobj.buttons){
+ if(stateobj.buttons.hasOwnProperty(k)){
+ buttons.push({ title: k, value: stateobj.buttons[k] });
+ }
+ }
+ }
- return $state;
- };
+ // iterate over each button and create them
+ for(i=0, l=buttons.length; i<l; i++){
+ v = buttons[i],
+ defbtn = stateobj.focus === i || (isNaN(stateobj.focus) && stateobj.defaultButton === i) ? (opts.prefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
- /**
- * removeState - Removes a state from the prompt
- * @param state String - Name of the state
- * @param newState String - Name of the state to transition to
- * @return Boolean - returns true on success, false on failure
- */
- Imp.removeState = function(state, newState) {
- var $state = Imp.getState(state),
- rm = function(){ $state.remove(); };
+ state += '<button class="'+ opts.classes.button +' '+ opts.prefix + 'button '+ defbtn;
- if($state.length === 0){
- return false;
- }
+ if(typeof v.classes !== "undefined"){
+ state += ' '+ ($.isArray(v.classes)? v.classes.join(' ') : v.classes) + ' ';
+ }
- // transition away from it before deleting
- if($state.css('display') !== 'none'){
- if(newState !== undefined && Imp.getState(newState).length > 0){
- Imp.goToState(newState, false, rm);
+ state += '" name="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" id="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
}
- else if($state.next().length > 0){
- Imp.nextState(rm);
- }
- else if($state.prev().length > 0){
- Imp.prevState(rm);
+
+ state += '</div></div>';
+
+ $state = $(state);
+
+ $state.on('impromptu:submit', stateobj.submit);
+
+ if(afterState !== undefined){
+ $jqistates.find('[data-jqi-name="'+afterState+'"]').after($state);
}
else{
- Imp.close();
+ $jqistates.append($state);
}
- }
- else{
- $state.slideUp('slow', rm);
- }
- return true;
- };
+ t.options.states[statename] = stateobj;
- /**
- * getState - Get the state by its name
- * @param state String - Name of the state
- * @return jQuery - the state
- */
- Imp.getState = function(state) {
- return $('#'+ Imp.currentPrefix +'state_'+ state);
- };
- Imp.getStateContent = function(state) {
- return Imp.getState(state);
- };
+ return $state;
+ },
- /**
- * getCurrentState - Get the current visible state
- * @return jQuery - the current visible state
- */
- Imp.getCurrentState = function() {
- return Imp.getState(Imp.getCurrentStateName());
- };
+ /**
+ * removeState - Removes a state from the prompt
+ * @param state String - Name of the state
+ * @param newState String - Name of the state to transition to
+ * @return Boolean - returns true on success, false on failure
+ */
+ removeState: function(state, newState) {
+ var t = this,
+ $state = t.getState(state),
+ rm = function(){ $state.remove(); };
+
+ if($state.length === 0){
+ return false;
+ }
- /**
- * getCurrentStateName - Get the name of the current visible state
- * @return String - the current visible state's name
- */
- Imp.getCurrentStateName = function() {
- return Imp.currentStateName;
- };
+ // transition away from it before deleting
+ if($state.css('display') !== 'none'){
+ if(newState !== undefined && t.getState(newState).length > 0){
+ t.goToState(newState, false, rm);
+ }
+ else if($state.next().length > 0){
+ t.nextState(rm);
+ }
+ else if($state.prev().length > 0){
+ t.prevState(rm);
+ }
+ else{
+ t.close();
+ }
+ }
+ else{
+ $state.slideUp('slow', rm);
+ }
- /**
- * goToState - Goto the specified state
- * @param state String - name of the state to transition to
- * @param subState Boolean - true to be a sub state within the currently open state
- * @param callback Function - called when the transition is complete
- * @return jQuery - the newly active state
- */
- Imp.goToState = function(state, subState, callback) {
- var $jqi = Imp.get(),
- jqiopts = Imp.options,
- $state = Imp.getState(state),
- stateobj = jqiopts.states[$state.data('jqi-name')],
- promptstatechanginge = new $.Event('impromptu:statechanging'),
- opts = Imp.options;
+ return true;
+ },
- if(stateobj !== undefined){
+ /**
+ * get - Get the prompt
+ * @return jQuery - the prompt
+ */
+ getPrompt: function() {
+ return this.jqi;
+ },
+ /**
+ * getState - Get the state by its name
+ * @param statename String - Name of the state
+ * @return jQuery - the state
+ */
+ getState: function(statename) {
+ return this.jqi.find('[data-jqi-name="'+ statename +'"]');
+ },
- if (typeof stateobj.html === 'function') {
- var contentLaterFunc = stateobj.html;
- $state.find('.' + opts.prefix +'message ').html(contentLaterFunc());
- }
+ /**
+ * getCurrentState - Get the current visible state
+ * @return jQuery - the current visible state
+ */
+ getCurrentState: function() {
+ return this.getState(this.getCurrentStateName());
+ },
+
+ /**
+ * getCurrentStateName - Get the name of the current visible state/substate
+ * @return String - the current visible state's name
+ */
+ getCurrentStateName: function() {
+ return this.currentStateName;
+ },
- // subState can be ommitted
- if(typeof subState === 'function'){
- callback = subState;
- subState = false;
+ /**
+ * position - Repositions the prompt (Used internally)
+ * @return void
+ */
+ position: function(e){
+ var t = this,
+ restoreFx = $.fx.off,
+ $state = t.getCurrentState(),
+ stateObj = t.options.states[$state.data('jqi-name')],
+ pos = stateObj? stateObj.position : undefined,
+ $window = $(window),
+ bodyHeight = document.body.scrollHeight, //$(document.body).outerHeight(true),
+ windowHeight = $(window).height(),
+ documentHeight = $(document).height(),
+ height = bodyHeight > windowHeight ? bodyHeight : windowHeight,
+ top = parseInt($window.scrollTop(),10) + (t.options.top.toString().indexOf('%') >= 0?
+ (windowHeight*(parseInt(t.options.top,10)/100)) : parseInt(t.options.top,10));
+
+ // when resizing the window turn off animation
+ if(e !== undefined && e.data.animate === false){
+ $.fx.off = true;
}
- Imp.jqib.trigger(promptstatechanginge, [Imp.getCurrentStateName(), state]);
+ t.jqib.css({
+ position: "absolute",
+ height: height,
+ width: "100%",
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0
+ });
+ t.jqif.css({
+ position: "fixed",
+ height: height,
+ width: "100%",
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0
+ });
- if(!promptstatechanginge.isDefaultPrevented() && $state.length > 0){
- Imp.jqi.find('.'+ Imp.currentPrefix +'parentstate').removeClass(Imp.currentPrefix +'parentstate');
+ // tour positioning
+ if(pos && pos.container){
+ var offset = $(pos.container).offset();
+
+ if($.isPlainObject(offset) && offset.top !== undefined){
+ t.jqi.css({
+ position: "absolute"
+ });
+ t.jqi.animate({
+ top: offset.top + pos.y,
+ left: offset.left + pos.x,
+ marginLeft: 0,
+ width: (pos.width !== undefined)? pos.width : null
+ });
+ top = (offset.top + pos.y) - (t.options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt(t.options.top,10)/100)) : parseInt(t.options.top,10));
+ $('html,body').animate({ scrollTop: top }, 'slow', 'swing', function(){});
+ }
+ }
+ // custom state width animation
+ else if(pos && pos.width){
+ t.jqi.css({
+ position: "absolute",
+ left: '50%'
+ });
+ t.jqi.animate({
+ top: pos.y || top,
+ left: pos.x || '50%',
+ marginLeft: ((pos.width/2)*-1),
+ width: pos.width
+ });
+ }
+ // standard prompt positioning
+ else{
+ t.jqi.css({
+ position: "absolute",
+ top: top,
+ left: '50%',//$window.width()/2,
+ marginLeft: ((t.jqi.outerWidth(false)/2)*-1)
+ });
+ }
- if(subState){ // hide any open substates
- // get rid of any substates
- Imp.jqi.find('.'+ Imp.currentPrefix +'substate').not($state)
- .slideUp(jqiopts.promptspeed)
- .removeClass('.'+ Imp.currentPrefix +'substate')
- .find('.'+ Imp.currentPrefix +'arrow').hide();
+ // restore fx settings
+ if(e !== undefined && e.data.animate === false){
+ $.fx.off = restoreFx;
+ }
+ },
- // add parent state class so it can be visible, but blocked
- Imp.jqi.find('.'+ Imp.currentPrefix +'state:visible').addClass(Imp.currentPrefix +'parentstate');
+ /**
+ * style - Restyles the prompt (Used internally)
+ * @return void
+ */
+ style: function(){
+ var t = this;
+
+ t.jqif.css({
+ zIndex: t.options.zIndex,
+ display: "none",
+ opacity: t.options.opacity
+ });
+ t.jqi.css({
+ zIndex: t.options.zIndex+1,
+ display: "none"
+ });
+ t.jqib.css({
+ zIndex: t.options.zIndex
+ });
+ },
- // add substate class so we know it will be smaller
- $state.addClass(Imp.currentPrefix +'substate');
+ /**
+ * goToState - Goto the specified state
+ * @param state String - name of the state to transition to
+ * @param subState Boolean - true to be a sub state within the currently open state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ goToState: function(state, subState, callback) {
+ var t = this,
+ $jqi = t.jqi,
+ jqiopts = t.options,
+ $state = t.getState(state),
+ stateobj = jqiopts.states[$state.data('jqi-name')],
+ promptstatechanginge = new $.Event('impromptu:statechanging'),
+ opts = t.options;
+
+ if(stateobj !== undefined){
+
+
+ if (typeof stateobj.html === 'function') {
+ var contentLaterFunc = stateobj.html;
+ $state.find('.' + opts.prefix +'message ').html(contentLaterFunc());
}
- else{ // hide any open states
- Imp.jqi.find('.'+ Imp.currentPrefix +'state').not($state)
- .slideUp(jqiopts.promptspeed)
- .find('.'+ Imp.currentPrefix +'arrow').hide();
+
+ // subState can be ommitted
+ if(typeof subState === 'function'){
+ callback = subState;
+ subState = false;
}
- Imp.currentStateName = stateobj.name;
- $state.slideDown(jqiopts.promptspeed,function(){
- var $t = $(this);
+ t.jqib.trigger(promptstatechanginge, [t.getCurrentStateName(), state]);
- // if focus is a selector, find it, else its button index
- if(typeof(stateobj.focus) === 'string'){
- $t.find(stateobj.focus).eq(0).focus();
- }
- else{
- $t.find('.'+ Imp.currentPrefix +'defaultbutton').focus();
- }
+ if(!promptstatechanginge.isDefaultPrevented() && $state.length > 0){
+ t.jqi.find('.'+ opts.prefix +'parentstate').removeClass(opts.prefix +'parentstate');
- $t.find('.'+ Imp.currentPrefix +'arrow').show(jqiopts.promptspeed);
+ if(subState){ // hide any open substates
+ // get rid of any substates
+ t.jqi.find('.'+ opts.prefix +'substate').not($state)
+ .slideUp(jqiopts.promptspeed)
+ .removeClass('.'+ opts.prefix +'substate')
+ .find('.'+ opts.prefix +'arrow').hide();
- if (typeof callback === 'function'){
- Imp.jqib.on('impromptu:statechanged', callback);
+ // add parent state class so it can be visible, but blocked
+ t.jqi.find('.'+ opts.prefix +'state:visible').addClass(opts.prefix +'parentstate');
+
+ // add substate class so we know it will be smaller
+ $state.addClass(opts.prefix +'substate');
}
- Imp.jqib.trigger('impromptu:statechanged', [state]);
- if (typeof callback === 'function'){
- Imp.jqib.off('impromptu:statechanged', callback);
+ else{ // hide any open states
+ t.jqi.find('.'+ opts.prefix +'state').not($state)
+ .slideUp(jqiopts.promptspeed)
+ .find('.'+ opts.prefix +'arrow').hide();
}
- });
- if(!subState){
- Imp.position();
- }
- } // end isDefaultPrevented()
- }// end stateobj !== undefined
+ t.currentStateName = stateobj.name;
- return $state;
- };
+ $state.slideDown(jqiopts.promptspeed,function(){
+ var $t = $(this);
- /**
- * nextState - Transition to the next state
- * @param callback Function - called when the transition is complete
- * @return jQuery - the newly active state
- */
- Imp.nextState = function(callback) {
- var $next = $('#'+ Imp.currentPrefix +'state_'+ Imp.getCurrentStateName()).next();
- if($next.length > 0){
- Imp.goToState( $next.attr('id').replace(Imp.currentPrefix +'state_',''), callback );
- }
- return $next;
- };
+ // if focus is a selector, find it, else its button index
+ if(typeof(stateobj.focus) === 'string'){
+ $t.find(stateobj.focus).eq(0).focus();
+ }
+ else{
+ $t.find('.'+ opts.prefix +'defaultbutton').focus();
+ }
- /**
- * prevState - Transition to the previous state
- * @param callback Function - called when the transition is complete
- * @return jQuery - the newly active state
- */
- Imp.prevState = function(callback) {
- var $prev = $('#'+ Imp.currentPrefix +'state_'+ Imp.getCurrentStateName()).prev();
- if($prev.length > 0){
- Imp.goToState( $prev.attr('id').replace(Imp.currentPrefix +'state_',''), callback );
- }
- return $prev;
- };
+ $t.find('.'+ opts.prefix +'arrow').show(jqiopts.promptspeed);
- /**
- * close - Closes the prompt
- * @param callback Function - called when the transition is complete
- * @param clicked String - value of the button clicked (only used internally)
- * @param msg jQuery - The state message body (only used internally)
- * @param forvals Object - key/value pairs of all form field names and values (only used internally)
- * @return jQuery - the newly active state
- */
- Imp.close = function(callCallback, clicked, msg, formvals){
- if(Imp.timeout){
- clearTimeout(Imp.timeout);
- Imp.timeout = false;
- }
+ if (typeof callback === 'function'){
+ t.jqib.on('impromptu:statechanged', callback);
+ }
+ t.jqib.trigger('impromptu:statechanged', [state]);
+ if (typeof callback === 'function'){
+ t.jqib.off('impromptu:statechanged', callback);
+ }
+ });
+ if(!subState){
+ t.position();
+ }
+ } // end isDefaultPrevented()
+ }// end stateobj !== undefined
- if(Imp.jqib){
- Imp.jqib.fadeOut('fast',function(){
-
- Imp.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
-
- Imp.jqib.remove();
-
- $(window).off('resize',Imp.position);
-
- if(typeof callCallback === 'function'){
- callCallback();
- }
- });
+ return $state;
+ },
+
+ /**
+ * nextState - Transition to the next state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ nextState: function(callback) {
+ var t = this,
+ $next = t.getCurrentState().next();
+ if($next.length > 0){
+ t.goToState( $next.data('jqi-name'), callback );
+ }
+ return $next;
+ },
+
+ /**
+ * prevState - Transition to the previous state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ prevState: function(callback) {
+ var t = this,
+ $prev = t.getCurrentState().prev();
+ if($prev.length > 0){
+ t.goToState( $prev.data('jqi-name'), callback );
+ }
+ return $prev;
}
- Imp.currentStateName = "";
+
};
+ // ########################################################################
+ // jQuery Plugin and public access
+ // ########################################################################
+
/**
* Enable using $('.selector').prompt({});
* This will grab the html within the prompt as the prompt message
@@ -726,7 +754,16 @@ new Imp($(this).clone(options.withDataAndEvents).html(),options);
};
- $.prompt = Imp;
+ /**
+ * Export it as Impromptu and $.prompt
+ * Can be used from here forth as new Impromptu(states, opts)
+ */
window.Impromptu = Imp;
+ /**
+ * Set $.prompt as a single instance
+ * Can be used from here forth as $.prompt.open(state, opts)
+ */
+ $.prompt = new Imp();
+
}));
diff --git a/dist/jquery-impromptu.min.js b/dist/jquery-impromptu.min.js index 7fdedbb..93d78ed 100644 --- a/dist/jquery-impromptu.min.js +++ b/dist/jquery-impromptu.min.js @@ -1,4 +1,4 @@ /*! jQuery-Impromptu - v5.3.1 - 2014-11-16 * http://trentrichardson.com/Impromptu * Copyright (c) 2014 Trent Richardson; Licensed MIT */ -(function(t,e){"function"==typeof define&&define.amd?define(["jquery"],e):e(t.jQuery)})(this,function(t){"use strict";var e=function(i,n){void 0!==n&&void 0!==n.classes&&"string"==typeof n.classes&&(n={box:n.classes}),e.options=t.extend({},e.defaults,n),e.currentPrefix=e.options.prefix,e.timeout&&clearTimeout(e.timeout),e.timeout=!1;var o=e.options,s=t(document.body),a=t(window),r='<div class="'+e.options.prefix+"box "+o.classes.box+'">';r+=o.useiframe&&t("object, applet").length>0?'<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+o.prefix+"fade "+o.classes.fade+'"></iframe>':'<div class="'+o.prefix+"fade "+o.classes.fade+'"></div>',r+='<div class="'+o.prefix+" "+o.classes.prompt+'">'+'<form action="javascript:false;" onsubmit="return false;" class="'+o.prefix+"form "+o.classes.form+'">'+'<div class="'+o.prefix+"close "+o.classes.close+'">'+o.closeText+"</div>"+'<div class="'+o.prefix+'states"></div>'+"</form>"+"</div>"+"</div>",e.jqib=t(r).appendTo(s),e.jqi=e.jqib.children("."+o.prefix),e.jqif=e.jqib.children("."+o.prefix+"fade"),i.constructor===String&&(i={state0:{title:o.title,html:i,buttons:o.buttons,position:o.position,focus:o.focus,defaultButton:o.defaultButton,submit:o.submit}}),e.options.states={};var u,f;for(u in i)f=t.extend({},e.defaults.state,{name:u},i[u]),e.addState(f.name,f),""===e.currentStateName&&(e.currentStateName=f.name);e.jqi.on("click","."+o.prefix+"buttons button",function(){var i=t(this),n=i.parents("."+o.prefix+"state"),s=e.options.states[n.data("jqi-name")],a=n.children("."+o.prefix+"message"),r=s.buttons[i.text()]||s.buttons[i.html()],u={};if(void 0===r)for(var f in s.buttons)(s.buttons[f].title===i.text()||s.buttons[f].title===i.html())&&(r=s.buttons[f].value);t.each(e.jqi.children("form").serializeArray(),function(t,e){void 0===u[e.name]?u[e.name]=e.value:typeof u[e.name]===Array||"object"==typeof u[e.name]?u[e.name].push(e.value):u[e.name]=[u[e.name],e.value]});var l=new t.Event("impromptu:submit");l.stateName=s.name,l.state=n,n.trigger(l,[r,a,u]),l.isDefaultPrevented()||e.close(!0,r,a,u)});var l=function(){if(o.persistent){var i=(""+o.top).indexOf("%")>=0?a.height()*(parseInt(o.top,10)/100):parseInt(o.top,10),n=parseInt(e.jqi.css("top").replace("px",""),10)-i;t("html,body").animate({scrollTop:n},"fast",function(){var t=0;e.jqib.addClass(o.prefix+"warning");var i=setInterval(function(){e.jqib.toggleClass(o.prefix+"warning"),t++>1&&(clearInterval(i),e.jqib.removeClass(o.prefix+"warning"))},100)})}else e.close(!0)},p=function(i){var n=window.event?event.keyCode:i.keyCode;if(27===n&&l(),13===n){var s=e.getCurrentState().find("."+o.prefix+"defaultbutton"),a=t(i.target);a.is("textarea,."+o.prefix+"button")===!1&&s.length>0&&(i.preventDefault(),s.click())}if(9===n){var r=t("input,select,textarea,button",e.getCurrentState()),u=!i.shiftKey&&i.target===r[r.length-1],f=i.shiftKey&&i.target===r[0];if(u||f)return setTimeout(function(){if(r){var t=r[f===!0?r.length-1:0];t&&t.focus()}},10),!1}};return e.position(),e.style(),e.jqif.click(l),a.resize({animate:!1},e.position),e.jqi.find("."+o.prefix+"close").click(e.close),e.jqib.on("keydown",p).on("impromptu:loaded",o.loaded).on("impromptu:close",o.close).on("impromptu:statechanging",o.statechanging).on("impromptu:statechanged",o.statechanged),e.jqif[o.show](o.overlayspeed),e.jqi[o.show](o.promptspeed,function(){var t=e.jqi.find("."+o.prefix+"states ."+o.prefix+"state").eq(0);e.goToState(t.data("jqi-name")),e.jqib.trigger("impromptu:loaded")}),o.timeout>0&&(e.timeout=setTimeout(function(){e.close(!0)},o.timeout)),e.jqib};e.defaults={prefix:"jqi",classes:{box:"",fade:"",prompt:"",form:"",close:"",title:"",message:"",buttons:"",button:"",defaultButton:""},title:"",closeText:"×",buttons:{Ok:!0},loaded:function(){},submit:function(){},close:function(){},statechanging:function(){},statechanged:function(){},opacity:.6,zIndex:999,overlayspeed:"slow",promptspeed:"fast",show:"fadeIn",focus:0,defaultButton:0,useiframe:!1,top:"15%",position:{container:null,x:null,y:null,arrow:null,width:null},persistent:!0,timeout:0,states:{},state:{name:null,title:"",html:"",buttons:{Ok:!0},focus:0,defaultButton:0,position:{container:null,x:null,y:null,arrow:null,width:null},submit:function(){return!0}}},e.currentPrefix=e.defaults.prefix,e.currentStateName="",e.setDefaults=function(i){e.defaults=t.extend({},e.defaults,i)},e.setStateDefaults=function(i){e.defaults.state=t.extend({},e.defaults.state,i)},e.position=function(i){var n=t.fx.off,o=e.getCurrentState(),s=e.options.states[o.data("jqi-name")],a=s?s.position:void 0,r=t(window),u=document.body.scrollHeight,f=t(window).height(),l=(t(document).height(),u>f?u:f),p=parseInt(r.scrollTop(),10)+((""+e.options.top).indexOf("%")>=0?f*(parseInt(e.options.top,10)/100):parseInt(e.options.top,10));if(void 0!==i&&i.data.animate===!1&&(t.fx.off=!0),e.jqib.css({position:"absolute",height:l,width:"100%",top:0,left:0,right:0,bottom:0}),e.jqif.css({position:"fixed",height:l,width:"100%",top:0,left:0,right:0,bottom:0}),a&&a.container){var c=t(a.container).offset();t.isPlainObject(c)&&void 0!==c.top&&(e.jqi.css({position:"absolute"}),e.jqi.animate({top:c.top+a.y,left:c.left+a.x,marginLeft:0,width:void 0!==a.width?a.width:null}),p=c.top+a.y-((""+e.options.top).indexOf("%")>=0?f*(parseInt(e.options.top,10)/100):parseInt(e.options.top,10)),t("html,body").animate({scrollTop:p},"slow","swing",function(){}))}else a&&a.width?(e.jqi.css({position:"absolute",left:"50%"}),e.jqi.animate({top:a.y||p,left:a.x||"50%",marginLeft:-1*(a.width/2),width:a.width})):e.jqi.css({position:"absolute",top:p,left:"50%",marginLeft:-1*(e.jqi.outerWidth(!1)/2)});void 0!==i&&i.data.animate===!1&&(t.fx.off=n)},e.style=function(){e.jqif.css({zIndex:e.options.zIndex,display:"none",opacity:e.options.opacity}),e.jqi.css({zIndex:e.options.zIndex+1,display:"none"}),e.jqib.css({zIndex:e.options.zIndex})},e.get=function(){return t("."+e.currentPrefix)},e.addState=function(i,n,o){var s,a,r,u,f,l="",p=null,c="",d="",m=e.options,x=t("."+e.currentPrefix+"states"),v=[],g=0;if(n=t.extend({},e.defaults.state,{name:i},n),null!==n.position.arrow&&(c='<div class="'+m.prefix+"arrow "+m.prefix+"arrow"+n.position.arrow+'"></div>'),n.title&&""!==n.title&&(d='<div class="lead '+m.prefix+"title "+m.classes.title+'">'+n.title+"</div>"),s=n.html,"function"==typeof n.html&&(s="Error: html function must return text"),l+='<div id="'+m.prefix+"state_"+i+'" class="'+m.prefix+'state" data-jqi-name="'+i+'" style="display:none;">'+c+d+'<div class="'+m.prefix+"message "+m.classes.message+'">'+s+"</div>"+'<div class="'+m.prefix+"buttons "+m.classes.buttons+'"'+(t.isEmptyObject(n.buttons)?'style="display:none;"':"")+">",t.isArray(n.buttons))v=n.buttons;else if(t.isPlainObject(n.buttons))for(r in n.buttons)n.buttons.hasOwnProperty(r)&&v.push({title:r,value:n.buttons[r]});for(g=0,f=v.length;f>g;g++)u=v[g],a=n.focus===g||isNaN(n.focus)&&n.defaultButton===g?e.currentPrefix+"defaultbutton "+m.classes.defaultButton:"",l+='<button class="'+m.classes.button+" "+e.currentPrefix+"button "+a,u.classes!==void 0&&(l+=" "+(t.isArray(u.classes)?u.classes.join(" "):u.classes)+" "),l+='" name="'+m.prefix+"_"+i+"_button"+u.title.replace(/[^a-z0-9]+/gi,"")+'" id="'+m.prefix+"_"+i+"_button"+u.title.replace(/[^a-z0-9]+/gi,"")+'" value="'+u.value+'">'+u.title+"</button>";return l+="</div></div>",p=t(l),p.on("impromptu:submit",n.submit),void 0!==o?x.find("#"+e.currentPrefix+"state_"+o).after(p):x.append(p),e.options.states[i]=n,p},e.removeState=function(t,i){var n=e.getState(t),o=function(){n.remove()};return 0===n.length?!1:("none"!==n.css("display")?void 0!==i&&e.getState(i).length>0?e.goToState(i,!1,o):n.next().length>0?e.nextState(o):n.prev().length>0?e.prevState(o):e.close():n.slideUp("slow",o),!0)},e.getState=function(i){return t("#"+e.currentPrefix+"state_"+i)},e.getStateContent=function(t){return e.getState(t)},e.getCurrentState=function(){return e.getState(e.getCurrentStateName())},e.getCurrentStateName=function(){return e.currentStateName},e.goToState=function(i,n,o){var s=(e.get(),e.options),a=e.getState(i),r=s.states[a.data("jqi-name")],u=new t.Event("impromptu:statechanging"),f=e.options;if(void 0!==r){if("function"==typeof r.html){var l=r.html;a.find("."+f.prefix+"message ").html(l())}"function"==typeof n&&(o=n,n=!1),e.jqib.trigger(u,[e.getCurrentStateName(),i]),!u.isDefaultPrevented()&&a.length>0&&(e.jqi.find("."+e.currentPrefix+"parentstate").removeClass(e.currentPrefix+"parentstate"),n?(e.jqi.find("."+e.currentPrefix+"substate").not(a).slideUp(s.promptspeed).removeClass("."+e.currentPrefix+"substate").find("."+e.currentPrefix+"arrow").hide(),e.jqi.find("."+e.currentPrefix+"state:visible").addClass(e.currentPrefix+"parentstate"),a.addClass(e.currentPrefix+"substate")):e.jqi.find("."+e.currentPrefix+"state").not(a).slideUp(s.promptspeed).find("."+e.currentPrefix+"arrow").hide(),e.currentStateName=r.name,a.slideDown(s.promptspeed,function(){var n=t(this);"string"==typeof r.focus?n.find(r.focus).eq(0).focus():n.find("."+e.currentPrefix+"defaultbutton").focus(),n.find("."+e.currentPrefix+"arrow").show(s.promptspeed),"function"==typeof o&&e.jqib.on("impromptu:statechanged",o),e.jqib.trigger("impromptu:statechanged",[i]),"function"==typeof o&&e.jqib.off("impromptu:statechanged",o)}),n||e.position())}return a},e.nextState=function(i){var n=t("#"+e.currentPrefix+"state_"+e.getCurrentStateName()).next();return n.length>0&&e.goToState(n.attr("id").replace(e.currentPrefix+"state_",""),i),n},e.prevState=function(i){var n=t("#"+e.currentPrefix+"state_"+e.getCurrentStateName()).prev();return n.length>0&&e.goToState(n.attr("id").replace(e.currentPrefix+"state_",""),i),n},e.close=function(i,n,o,s){e.timeout&&(clearTimeout(e.timeout),e.timeout=!1),e.jqib&&e.jqib.fadeOut("fast",function(){e.jqib.trigger("impromptu:close",[n,o,s]),e.jqib.remove(),t(window).off("resize",e.position),"function"==typeof i&&i()}),e.currentStateName=""},t.fn.prompt=function(i){void 0===i&&(i={}),void 0===i.withDataAndEvents&&(i.withDataAndEvents=!1),new e(t(this).clone(i.withDataAndEvents).html(),i)},t.prompt=e,window.Impromptu=e});
\ No newline at end of file +(function(t,e){"function"==typeof define&&define.amd?define(["jquery"],e):e(t.jQuery)})(this,function(t){"use strict";var e=function(t,e){return t&&this.open(t,e),this};e.defaults={prefix:"jqi",classes:{box:"",fade:"",prompt:"",form:"",close:"",title:"",message:"",buttons:"",button:"",defaultButton:""},title:"",closeText:"×",buttons:{Ok:!0},loaded:function(){},submit:function(){},close:function(){},statechanging:function(){},statechanged:function(){},opacity:.6,zIndex:999,overlayspeed:"slow",promptspeed:"fast",show:"fadeIn",focus:0,defaultButton:0,useiframe:!1,top:"15%",position:{container:null,x:null,y:null,arrow:null,width:null},persistent:!0,timeout:0,states:{},state:{name:null,title:"",html:"",buttons:{Ok:!0},focus:0,defaultButton:0,position:{container:null,x:null,y:null,arrow:null,width:null},submit:function(){return!0}}},e.setDefaults=function(i){e.defaults=t.extend({},e.defaults,i)},e.setStateDefaults=function(i){e.defaults.state=t.extend({},e.defaults.state,i)},e.prototype={open:function(i,n){var o=this;o.options=t.extend({},e.defaults,n),o.timeout&&clearTimeout(o.timeout),o.timeout=!1;var s=o.options,a=t(document.body),r=t(window),f='<div class="'+s.prefix+"box "+s.classes.box+'">';f+=s.useiframe&&t("object, applet").length>0?'<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+s.prefix+"fade "+s.classes.fade+'"></iframe>':'<div class="'+s.prefix+"fade "+s.classes.fade+'"></div>',f+='<div class="'+s.prefix+" "+s.classes.prompt+'">'+'<form action="javascript:false;" onsubmit="return false;" class="'+s.prefix+"form "+s.classes.form+'">'+'<div class="'+s.prefix+"close "+s.classes.close+'">'+s.closeText+"</div>"+'<div class="'+s.prefix+'states"></div>'+"</form>"+"</div>"+"</div>",o.jqib=t(f).appendTo(a),o.jqi=o.jqib.children("."+s.prefix),o.jqif=o.jqib.children("."+s.prefix+"fade"),i.constructor===String&&(i={state0:{title:s.title,html:i,buttons:s.buttons,position:s.position,focus:s.focus,defaultButton:s.defaultButton,submit:s.submit}}),o.options.states={};var u,l;for(u in i)l=t.extend({},e.defaults.state,{name:u},i[u]),o.addState(l.name,l),""===o.currentStateName&&(o.currentStateName=l.name);o.jqi.on("click","."+s.prefix+"buttons button",function(){var e=t(this),i=e.parents("."+s.prefix+"state"),n=o.options.states[i.data("jqi-name")],a=i.children("."+s.prefix+"message"),r=n.buttons[e.text()]||n.buttons[e.html()],f={};if(void 0===r)for(var u in n.buttons)(n.buttons[u].title===e.text()||n.buttons[u].title===e.html())&&(r=n.buttons[u].value);t.each(o.jqi.children("form").serializeArray(),function(t,e){void 0===f[e.name]?f[e.name]=e.value:typeof f[e.name]===Array||"object"==typeof f[e.name]?f[e.name].push(e.value):f[e.name]=[f[e.name],e.value]});var l=new t.Event("impromptu:submit");l.stateName=n.name,l.state=i,i.trigger(l,[r,a,f]),l.isDefaultPrevented()||o.close(!0,r,a,f)});var p=function(){if(s.persistent){var e=(""+s.top).indexOf("%")>=0?r.height()*(parseInt(s.top,10)/100):parseInt(s.top,10),i=parseInt(o.jqi.css("top").replace("px",""),10)-e;t("html,body").animate({scrollTop:i},"fast",function(){var t=0;o.jqib.addClass(s.prefix+"warning");var e=setInterval(function(){o.jqib.toggleClass(s.prefix+"warning"),t++>1&&(clearInterval(e),o.jqib.removeClass(s.prefix+"warning"))},100)})}else o.close(!0)},d=function(e){var i=window.event?event.keyCode:e.keyCode;if(27===i&&p(),13===i){var n=o.getCurrentState().find("."+s.prefix+"defaultbutton"),a=t(e.target);a.is("textarea,."+s.prefix+"button")===!1&&n.length>0&&(e.preventDefault(),n.click())}if(9===i){var r=t("input,select,textarea,button",o.getCurrentState()),f=!e.shiftKey&&e.target===r[r.length-1],u=e.shiftKey&&e.target===r[0];if(f||u)return setTimeout(function(){if(r){var t=r[u===!0?r.length-1:0];t&&t.focus()}},10),!1}};return o.position(),o.style(),o.jqif.click(p),r.resize({animate:!1},function(){o.position()}),o.jqi.find("."+s.prefix+"close").click(o.close),o.jqib.on("keydown",d).on("impromptu:loaded",s.loaded).on("impromptu:close",s.close).on("impromptu:statechanging",s.statechanging).on("impromptu:statechanged",s.statechanged),o.jqif[s.show](s.overlayspeed),o.jqi[s.show](s.promptspeed,function(){var t=o.jqi.find("."+s.prefix+"states ."+s.prefix+"state").eq(0);o.goToState(t.data("jqi-name")),o.jqib.trigger("impromptu:loaded")}),s.timeout>0&&(o.timeout=setTimeout(function(){o.close(!0)},s.timeout)),o},close:function(e,i,n,o){var s=this;return s.timeout&&(clearTimeout(s.timeout),s.timeout=!1),s.jqib&&s.jqib.fadeOut("fast",function(){s.jqib.trigger("impromptu:close",[i,n,o]),s.jqib.remove(),t(window).off("resize",function(){s.position()}),"function"==typeof e&&e()}),s.currentStateName="",s},addState:function(i,n,o){var s,a,r,f,u,l=this,p="",d=null,c="",m="",h=l.options,v=t("."+h.prefix+"states"),g=[],x=0;if(n=t.extend({},e.defaults.state,{name:i},n),null!==n.position.arrow&&(c='<div class="'+h.prefix+"arrow "+h.prefix+"arrow"+n.position.arrow+'"></div>'),n.title&&""!==n.title&&(m='<div class="lead '+h.prefix+"title "+h.classes.title+'">'+n.title+"</div>"),s=n.html,"function"==typeof n.html&&(s="Error: html function must return text"),p+='<div id="'+h.prefix+"state_"+i+'" class="'+h.prefix+'state" data-jqi-name="'+i+'" style="display:none;">'+c+m+'<div class="'+h.prefix+"message "+h.classes.message+'">'+s+"</div>"+'<div class="'+h.prefix+"buttons "+h.classes.buttons+'"'+(t.isEmptyObject(n.buttons)?'style="display:none;"':"")+">",t.isArray(n.buttons))g=n.buttons;else if(t.isPlainObject(n.buttons))for(r in n.buttons)n.buttons.hasOwnProperty(r)&&g.push({title:r,value:n.buttons[r]});for(x=0,u=g.length;u>x;x++)f=g[x],a=n.focus===x||isNaN(n.focus)&&n.defaultButton===x?h.prefix+"defaultbutton "+h.classes.defaultButton:"",p+='<button class="'+h.classes.button+" "+h.prefix+"button "+a,f.classes!==void 0&&(p+=" "+(t.isArray(f.classes)?f.classes.join(" "):f.classes)+" "),p+='" name="'+h.prefix+"_"+i+"_button"+f.title.replace(/[^a-z0-9]+/gi,"")+'" id="'+h.prefix+"_"+i+"_button"+f.title.replace(/[^a-z0-9]+/gi,"")+'" value="'+f.value+'">'+f.title+"</button>";return p+="</div></div>",d=t(p),d.on("impromptu:submit",n.submit),void 0!==o?v.find('[data-jqi-name="'+o+'"]').after(d):v.append(d),l.options.states[i]=n,d},removeState:function(t,e){var i=this,n=i.getState(t),o=function(){n.remove()};return 0===n.length?!1:("none"!==n.css("display")?void 0!==e&&i.getState(e).length>0?i.goToState(e,!1,o):n.next().length>0?i.nextState(o):n.prev().length>0?i.prevState(o):i.close():n.slideUp("slow",o),!0)},getPrompt:function(){return this.jqi},getState:function(t){return this.jqi.find('[data-jqi-name="'+t+'"]')},getCurrentState:function(){return this.getState(this.getCurrentStateName())},getCurrentStateName:function(){return this.currentStateName},position:function(e){var i=this,n=t.fx.off,o=i.getCurrentState(),s=i.options.states[o.data("jqi-name")],a=s?s.position:void 0,r=t(window),f=document.body.scrollHeight,u=t(window).height(),l=(t(document).height(),f>u?f:u),p=parseInt(r.scrollTop(),10)+((""+i.options.top).indexOf("%")>=0?u*(parseInt(i.options.top,10)/100):parseInt(i.options.top,10));if(void 0!==e&&e.data.animate===!1&&(t.fx.off=!0),i.jqib.css({position:"absolute",height:l,width:"100%",top:0,left:0,right:0,bottom:0}),i.jqif.css({position:"fixed",height:l,width:"100%",top:0,left:0,right:0,bottom:0}),a&&a.container){var d=t(a.container).offset();t.isPlainObject(d)&&void 0!==d.top&&(i.jqi.css({position:"absolute"}),i.jqi.animate({top:d.top+a.y,left:d.left+a.x,marginLeft:0,width:void 0!==a.width?a.width:null}),p=d.top+a.y-((""+i.options.top).indexOf("%")>=0?u*(parseInt(i.options.top,10)/100):parseInt(i.options.top,10)),t("html,body").animate({scrollTop:p},"slow","swing",function(){}))}else a&&a.width?(i.jqi.css({position:"absolute",left:"50%"}),i.jqi.animate({top:a.y||p,left:a.x||"50%",marginLeft:-1*(a.width/2),width:a.width})):i.jqi.css({position:"absolute",top:p,left:"50%",marginLeft:-1*(i.jqi.outerWidth(!1)/2)});void 0!==e&&e.data.animate===!1&&(t.fx.off=n)},style:function(){var t=this;t.jqif.css({zIndex:t.options.zIndex,display:"none",opacity:t.options.opacity}),t.jqi.css({zIndex:t.options.zIndex+1,display:"none"}),t.jqib.css({zIndex:t.options.zIndex})},goToState:function(e,i,n){var o=this,s=(o.jqi,o.options),a=o.getState(e),r=s.states[a.data("jqi-name")],f=new t.Event("impromptu:statechanging"),u=o.options;if(void 0!==r){if("function"==typeof r.html){var l=r.html;a.find("."+u.prefix+"message ").html(l())}"function"==typeof i&&(n=i,i=!1),o.jqib.trigger(f,[o.getCurrentStateName(),e]),!f.isDefaultPrevented()&&a.length>0&&(o.jqi.find("."+u.prefix+"parentstate").removeClass(u.prefix+"parentstate"),i?(o.jqi.find("."+u.prefix+"substate").not(a).slideUp(s.promptspeed).removeClass("."+u.prefix+"substate").find("."+u.prefix+"arrow").hide(),o.jqi.find("."+u.prefix+"state:visible").addClass(u.prefix+"parentstate"),a.addClass(u.prefix+"substate")):o.jqi.find("."+u.prefix+"state").not(a).slideUp(s.promptspeed).find("."+u.prefix+"arrow").hide(),o.currentStateName=r.name,a.slideDown(s.promptspeed,function(){var i=t(this);"string"==typeof r.focus?i.find(r.focus).eq(0).focus():i.find("."+u.prefix+"defaultbutton").focus(),i.find("."+u.prefix+"arrow").show(s.promptspeed),"function"==typeof n&&o.jqib.on("impromptu:statechanged",n),o.jqib.trigger("impromptu:statechanged",[e]),"function"==typeof n&&o.jqib.off("impromptu:statechanged",n)}),i||o.position())}return a},nextState:function(t){var e=this,i=e.getCurrentState().next();return i.length>0&&e.goToState(i.data("jqi-name"),t),i},prevState:function(t){var e=this,i=e.getCurrentState().prev();return i.length>0&&e.goToState(i.data("jqi-name"),t),i}},t.fn.prompt=function(i){void 0===i&&(i={}),void 0===i.withDataAndEvents&&(i.withDataAndEvents=!1),new e(t(this).clone(i.withDataAndEvents).html(),i)},window.Impromptu=e,t.prompt=new e});
\ No newline at end of file diff --git a/src/demos/loan_calculator.html b/src/demos/loan_calculator.html index 3fbbfbb..c4909b2 100644 --- a/src/demos/loan_calculator.html +++ b/src/demos/loan_calculator.html @@ -93,7 +93,7 @@ } }; - $.prompt(jqistates); + $.prompt.open(jqistates); } </script> </head> diff --git a/src/demos/purecss.html b/src/demos/purecss.html index e07c4bd..0e86eee 100644 --- a/src/demos/purecss.html +++ b/src/demos/purecss.html @@ -88,7 +88,7 @@ } } - $.prompt(temp,{ + $.prompt.open(temp,{ close: function(e,v,m,f){ if(v !== undefined){ var str = "You can now process with this given information:<br />"; diff --git a/src/demos/select_filter.html b/src/demos/select_filter.html index a5e3f96..2f45497 100644 --- a/src/demos/select_filter.html +++ b/src/demos/select_filter.html @@ -51,7 +51,7 @@ e.preventDefault(); - var $imp = $.prompt(html, { + var imp = $.prompt.open(html, { title: 'Search Options', buttons: { Ok:true,Cancel:false }, submit: function(e,v,m,f){ @@ -61,20 +61,20 @@ } }); - var $so = $('#searchoptions', $imp); + var $so = $('#searchoptions', imp.getPrompt()); $so.delegate('input', 'click', function(){ $('tr.checked', $so).removeClass('checked'); $(this).parent().parent().addClass('checked'); }); - $('#searchquery', $imp).focus().keydown(function(event){ + $('#searchquery', imp.getPrompt()).focus().keydown(function(event){ var $i = $(this), query = $i.val(), re = new RegExp(query,"i"), html = "", counter = 0, - $sos = $('#optionsearchstatus',$imp); + $sos = $('#optionsearchstatus',imp.getPrompt()); if(event.which == 13 || event.keyCode == 13 && $.trim(query) !== ''){ @@ -98,7 +98,7 @@ $so.find('tbody').html(html); $so.find('tr:eq(0)').addClass('checked').find('input').attr('checked',true); - $('#searchoptionswrapper',$imp).slideDown('slow', function(){ + $('#searchoptionswrapper',imp.getPrompt()).slideDown('slow', function(){ $sos.hide(); }); diff --git a/src/demos/survey.html b/src/demos/survey.html index d4f8c3f..f249661 100644 --- a/src/demos/survey.html +++ b/src/demos/survey.html @@ -78,7 +78,7 @@ } } - $.prompt(temp,{ + $.prompt.open(temp,{ close: function(e,v,m,f){ if(v !== undefined){ var str = "You can now process with this given information:<br />"; diff --git a/src/demos/twitter_bootstrap.html b/src/demos/twitter_bootstrap.html index eca1583..d595445 100644 --- a/src/demos/twitter_bootstrap.html +++ b/src/demos/twitter_bootstrap.html @@ -88,7 +88,7 @@ } } - $.prompt(temp,{ + $.prompt.open(temp,{ close: function(e,v,m,f){ if(v !== undefined){ var str = "You can now process with this given information:<br />"; diff --git a/src/demos/user_manager.html b/src/demos/user_manager.html index d23a660..0201617 100644 --- a/src/demos/user_manager.html +++ b/src/demos/user_manager.html @@ -37,7 +37,7 @@ '<div class="field"><label for="editfname">First Name</label><input type="text" id="editfname" name="editfname" value="'+ fname +'" /></div>'+ '<div class="field"><label for="editlname">Last Name</label><input type="text" id="editlname" name="editlname" value="'+ lname +'" /></div>'; - $.prompt(txt,{ + $.prompt.open(txt,{ buttons:{Change:true, Cancel:false}, submit: function(e,v,m,f){ //this is simple pre submit validation, the submit function diff --git a/src/index.html b/src/index.html index c05b582..6444650 100644 --- a/src/index.html +++ b/src/index.html @@ -128,7 +128,7 @@ <!-- ---------------------------------------------- --> <div id="Options" class="section"> <h2>Options</h2> - <pre><code>$.prompt( msg , options )</code></pre> + <pre><code>$.prompt.open( msg , options )</code></pre> <h3>msg</h3> <p>The message can either be an html string, or an object of "states". Each state has the following properties:</p> <dl> @@ -247,24 +247,27 @@ <div id="Methods" class="section"> <h2>Methods</h2> <dl class="methoddl"> - <dt>jQuery.prompt.setDefaults(options)</dt> + <dt>Impromptu.setDefaults(options)</dt> <dd> Sets the defaults for prompts.<br /> -<pre><code>jQuery.prompt.setDefaults({ +<pre><code>Impromptu.setDefaults({ prefix: 'myPrompt', show: 'slideDown' });</pre></code> </dd> - <dt>jQuery.prompt.setStateDefaults(options)</dt> + <dt>Impromptu.setStateDefaults(options)</dt> <dd> Sets the defaults for states.<br /> -<pre><code>jQuery.prompt.setStateDefaults({ +<pre><code>Impromptu.setStateDefaults({ buttons: { Ok:true, Cancel:false }, focus: 1 });</pre></code> </dd> + <dt>jQuery.prompt.open(states, options)</dt> + <dd>Returns an instance of Impromptu. Opens the prompt.</dd> + <dt>jQuery.prompt.getCurrentState()</dt> <dd>Returns a jquery object of the current visible state.</dd> @@ -311,7 +314,7 @@ myPrompt.on('impromptu:loaded', function(e){});</pre> <dt>impromptu:submit</dt> <dd>Same as submit option. Scope is the state element. These events are per state, so you must attach it directly to a state. <pre>var myPrompt = jQuery.prompt(/*...*/); -$.prompt.getStateContent('state2') +$.prompt.getState('state2') .on('impromptu:submit', function(e,v,m,f){});</pre></dd> <dt>impromptu:close</dt> @@ -335,7 +338,7 @@ $.prompt.getStateContent('state2') <div class="example-container"> <p>A prompt in the simplest of fashion.</p> -<pre class="code">$.prompt("Hello World!");</pre> +<pre class="code">$.prompt.open("Hello World!");</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -343,7 +346,7 @@ $.prompt.getStateContent('state2') <div class="example-container"> <p>Lets add some buttons and a title.</p> -<pre class="code">$.prompt("Proceeding may be good for your site..", { +<pre class="code">$.prompt.open("Proceeding may be good for your site..", { title: "Are you Ready?", buttons: { "Yes, I'm Ready": true, "No, Lets Wait": false } });</pre> @@ -354,7 +357,7 @@ $.prompt.getStateContent('state2') <div class="example-container"> <p>Use the submit function to get the answer.</p> -<pre class="code">$.prompt("Open your javascript console to see the answer.", { +<pre class="code">$.prompt.open("Open your javascript console to see the answer.", { title: "Are you Ready?", buttons: { "Yes, I'm Ready": true, "No, Lets Wait": false }, submit: function(e,v,m,f){ @@ -401,7 +404,7 @@ $.prompt.getStateContent('state2') } }; -$.prompt(statesdemo);</pre> +$.prompt.open(statesdemo);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -445,7 +448,7 @@ $.prompt(statesdemo);</pre> } }; -$.prompt(statesdemo);</pre> +$.prompt.open(statesdemo);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -514,7 +517,7 @@ tourStates = [ submit: tourSubmitFunc } ]; -$.prompt(tourStates);</pre> +$.prompt.open(tourStates);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> @@ -572,7 +575,7 @@ $.prompt(tourStates);</pre> }, }; -$.prompt(statesdemo);</pre> +$.prompt.open(statesdemo);</pre> <div class="buttons"> <button class="run">Run It!</button> </div> diff --git a/src/jquery-impromptu.js b/src/jquery-impromptu.js index b41c33c..2afaaa9 100644 --- a/src/jquery-impromptu.js +++ b/src/jquery-impromptu.js @@ -7,212 +7,30 @@ }(this, function($) {
'use strict';
+ // ########################################################################
+ // Base object
+ // ########################################################################
+
/**
- * Class constructor
+ * Imp - Impromptu object - passing no params will not open, only return the instance
* @param message String/Object - String of html or Object of states
* @param options Object - Options to set the prompt
- * @return jQuery - container with overlay and prompt
+ * @return Imp - the instance of this Impromptu object
*/
- var Imp = function(message, options) {
- // only for backwards compat, to be removed in future version
- if(options !== undefined && options.classes !== undefined && typeof options.classes === 'string'){
- options = { box: options.classes };
- }
-
- Imp.options = $.extend({},Imp.defaults,options);
- Imp.currentPrefix = Imp.options.prefix;
-
- // Be sure any previous timeouts are destroyed
- if(Imp.timeout){
- clearTimeout(Imp.timeout);
- }
- Imp.timeout = false;
-
- var opts = Imp.options,
- $body = $(document.body),
- $window = $(window);
-
- //build the box and fade
- var msgbox = '<div class="'+ Imp.options.prefix +'box '+ opts.classes.box +'">';
- if(opts.useiframe && ($('object, applet').length > 0)) {
- msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></iframe>';
- } else {
- msgbox +='<div class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></div>';
- }
- msgbox += '<div class="'+ opts.prefix +' '+ opts.classes.prompt +'">'+
- '<form action="javascript:false;" onsubmit="return false;" class="'+ opts.prefix +'form '+ opts.classes.form +'">'+
- '<div class="'+ opts.prefix +'close '+ opts.classes.close +'">'+ opts.closeText +'</div>'+
- '<div class="'+ opts.prefix +'states"></div>'+
- '</form>'+
- '</div>'+
- '</div>';
-
- Imp.jqib = $(msgbox).appendTo($body);
- Imp.jqi = Imp.jqib.children('.'+ opts.prefix);//.data('jqi',opts);
- Imp.jqif = Imp.jqib.children('.'+ opts.prefix +'fade');
-
- //if a string was passed, convert to a single state
- if(message.constructor === String){
- message = {
- state0: {
- title: opts.title,
- html: message,
- buttons: opts.buttons,
- position: opts.position,
- focus: opts.focus,
- defaultButton: opts.defaultButton,
- submit: opts.submit
- }
- };
+ var Imp = function(message, options){
+ if(message){
+ this.open(message, options);
}
-
- //build the states
- Imp.options.states = {};
- var k,v;
- for(k in message){
- v = $.extend({},Imp.defaults.state,{name:k},message[k]);
- Imp.addState(v.name, v);
-
- if(Imp.currentStateName === ''){
- Imp.currentStateName = v.name;
- }
- }
-
- //Events
- Imp.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
- var $t = $(this),
- $state = $t.parents('.'+ opts.prefix +'state'),
- stateobj = Imp.options.states[$state.data('jqi-name')],
- msg = $state.children('.'+ opts.prefix +'message'),
- clicked = stateobj.buttons[$t.text()] || stateobj.buttons[$t.html()],
- forminputs = {};
-
- // if for some reason we couldn't get the value
- if(clicked === undefined){
- for(var i in stateobj.buttons){
- if(stateobj.buttons[i].title === $t.text() || stateobj.buttons[i].title === $t.html()){
- clicked = stateobj.buttons[i].value;
- }
- }
- }
-
- //collect all form element values from all states.
- $.each(Imp.jqi.children('form').serializeArray(),function(i,obj){
- if (forminputs[obj.name] === undefined) {
- forminputs[obj.name] = obj.value;
- } else if (typeof forminputs[obj.name] === Array || typeof forminputs[obj.name] === 'object') {
- forminputs[obj.name].push(obj.value);
- } else {
- forminputs[obj.name] = [forminputs[obj.name],obj.value];
- }
- });
-
- // trigger an event
- var promptsubmite = new $.Event('impromptu:submit');
- promptsubmite.stateName = stateobj.name;
- promptsubmite.state = $state;
- $state.trigger(promptsubmite, [clicked, msg, forminputs]);
-
- if(!promptsubmite.isDefaultPrevented()){
- Imp.close(true, clicked,msg,forminputs);
- }
- });
-
- // if the fade is clicked blink the prompt
- var fadeClicked = function(){
- if(opts.persistent){
- var offset = (opts.top.toString().indexOf('%') >= 0? ($window.height()*(parseInt(opts.top,10)/100)) : parseInt(opts.top,10)),
- top = parseInt(Imp.jqi.css('top').replace('px',''),10) - offset;
-
- //$window.scrollTop(top);
- $('html,body').animate({ scrollTop: top }, 'fast', function(){
- var i = 0;
- Imp.jqib.addClass(opts.prefix +'warning');
- var intervalid = setInterval(function(){
- Imp.jqib.toggleClass(opts.prefix +'warning');
- if(i++ > 1){
- clearInterval(intervalid);
- Imp.jqib.removeClass(opts.prefix +'warning');
- }
- }, 100);
- });
- }
- else {
- Imp.close(true);
- }
- };
-
- // listen for esc or tab keys
- var keyDownEventHandler = function(e){
- var key = (window.event) ? event.keyCode : e.keyCode;
-
- //escape key closes
- if(key === 27) {
- fadeClicked();
- }
-
- //enter key pressed trigger the default button if its not on it, ignore if it is a textarea
- if(key === 13){
- var $defBtn = Imp.getCurrentState().find('.'+ opts.prefix +'defaultbutton');
- var $tgt = $(e.target);
-
- if($tgt.is('textarea,.'+opts.prefix+'button') === false && $defBtn.length > 0){
- e.preventDefault();
- $defBtn.click();
- }
- }
-
- //constrain tabs, tabs should iterate through the state and not leave
- if (key === 9){
- var $inputels = $('input,select,textarea,button',Imp.getCurrentState());
- var fwd = !e.shiftKey && e.target === $inputels[$inputels.length-1];
- var back = e.shiftKey && e.target === $inputels[0];
- if (fwd || back) {
- setTimeout(function(){
- if (!$inputels){
- return;
- }
- var el = $inputels[back===true ? $inputels.length-1 : 0];
-
- if (el){
- el.focus();
- }
- },10);
- return false;
- }
- }
- };
-
- Imp.position();
- Imp.style();
-
- Imp.jqif.click(fadeClicked);
- $window.resize({animate:false}, Imp.position);
- Imp.jqi.find('.'+ opts.prefix +'close').click(Imp.close);
- Imp.jqib.on("keydown",keyDownEventHandler)
- .on('impromptu:loaded', opts.loaded)
- .on('impromptu:close', opts.close)
- .on('impromptu:statechanging', opts.statechanging)
- .on('impromptu:statechanged', opts.statechanged);
-
- // Show it
- Imp.jqif[opts.show](opts.overlayspeed);
- Imp.jqi[opts.show](opts.promptspeed, function(){
-
- var $firstState = Imp.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
- Imp.goToState($firstState.data('jqi-name'));
-
- Imp.jqib.trigger('impromptu:loaded');
- });
-
- // Timeout
- if(opts.timeout > 0){
- Imp.timeout = setTimeout(function(){ Imp.close(true); },opts.timeout);
- }
-
- return Imp.jqib;
+ return this;
};
+ // ########################################################################
+ // static properties and methods
+ // ########################################################################
+
+ /**
+ * defaults - the default options
+ */
Imp.defaults = {
prefix:'jqi',
classes: {
@@ -279,18 +97,6 @@ };
/**
- * currentPrefix String - At any time this show be the prefix
- * of the current prompt ex: "jqi"
- */
- Imp.currentPrefix = Imp.defaults.prefix;
-
- /**
- * currentStateName String - At any time this is the current state
- * of the current prompt ex: "state0"
- */
- Imp.currentStateName = "";
-
- /**
* setDefaults - Sets the default options
* @param o Object - Options to set as defaults
* @return void
@@ -308,406 +114,628 @@ Imp.defaults.state = $.extend({}, Imp.defaults.state, o);
};
- /**
- * position - Repositions the prompt (Used internally)
- * @return void
- */
- Imp.position = function(e){
- var restoreFx = $.fx.off,
- $state = Imp.getCurrentState(),
- stateObj = Imp.options.states[$state.data('jqi-name')],
- pos = stateObj? stateObj.position : undefined,
- $window = $(window),
- bodyHeight = document.body.scrollHeight, //$(document.body).outerHeight(true),
- windowHeight = $(window).height(),
- documentHeight = $(document).height(),
- height = bodyHeight > windowHeight ? bodyHeight : windowHeight,
- top = parseInt($window.scrollTop(),10) + (Imp.options.top.toString().indexOf('%') >= 0?
- (windowHeight*(parseInt(Imp.options.top,10)/100)) : parseInt(Imp.options.top,10));
-
- // when resizing the window turn off animation
- if(e !== undefined && e.data.animate === false){
- $.fx.off = true;
- }
+ // ########################################################################
+ // extend our object instance properties and methods
+ // ########################################################################
+ Imp.prototype = {
+
+ /**
+ * open - Opens the prompt
+ * @param message String/Object - String of html or Object of states
+ * @param options Object - Options to set the prompt
+ * @return Imp - the instance of this Impromptu object
+ */
+ open: function(message, options) {
+ var t = this;
+
+ t.options = $.extend({},Imp.defaults,options);
+
+ // Be sure any previous timeouts are destroyed
+ if(t.timeout){
+ clearTimeout(t.timeout);
+ }
+ t.timeout = false;
+
+ var opts = t.options,
+ $body = $(document.body),
+ $window = $(window);
+
+ //build the box and fade
+ var msgbox = '<div class="'+ opts.prefix +'box '+ opts.classes.box +'">';
+ if(opts.useiframe && ($('object, applet').length > 0)) {
+ msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></iframe>';
+ } else {
+ msgbox +='<div class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></div>';
+ }
+ msgbox += '<div class="'+ opts.prefix +' '+ opts.classes.prompt +'">'+
+ '<form action="javascript:false;" onsubmit="return false;" class="'+ opts.prefix +'form '+ opts.classes.form +'">'+
+ '<div class="'+ opts.prefix +'close '+ opts.classes.close +'">'+ opts.closeText +'</div>'+
+ '<div class="'+ opts.prefix +'states"></div>'+
+ '</form>'+
+ '</div>'+
+ '</div>';
+
+ t.jqib = $(msgbox).appendTo($body);
+ t.jqi = t.jqib.children('.'+ opts.prefix);//.data('jqi',opts);
+ t.jqif = t.jqib.children('.'+ opts.prefix +'fade');
+
+ //if a string was passed, convert to a single state
+ if(message.constructor === String){
+ message = {
+ state0: {
+ title: opts.title,
+ html: message,
+ buttons: opts.buttons,
+ position: opts.position,
+ focus: opts.focus,
+ defaultButton: opts.defaultButton,
+ submit: opts.submit
+ }
+ };
+ }
- Imp.jqib.css({
- position: "absolute",
- height: height,
- width: "100%",
- top: 0,
- left: 0,
- right: 0,
- bottom: 0
- });
- Imp.jqif.css({
- position: "fixed",
- height: height,
- width: "100%",
- top: 0,
- left: 0,
- right: 0,
- bottom: 0
- });
-
- // tour positioning
- if(pos && pos.container){
- var offset = $(pos.container).offset();
-
- if($.isPlainObject(offset) && offset.top !== undefined){
- Imp.jqi.css({
- position: "absolute"
- });
- Imp.jqi.animate({
- top: offset.top + pos.y,
- left: offset.left + pos.x,
- marginLeft: 0,
- width: (pos.width !== undefined)? pos.width : null
- });
- top = (offset.top + pos.y) - (Imp.options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt(Imp.options.top,10)/100)) : parseInt(Imp.options.top,10));
- $('html,body').animate({ scrollTop: top }, 'slow', 'swing', function(){});
+ //build the states
+ t.options.states = {};
+ var k,v;
+ for(k in message){
+ v = $.extend({},Imp.defaults.state,{name:k},message[k]);
+ t.addState(v.name, v);
+
+ if(t.currentStateName === ''){
+ t.currentStateName = v.name;
+ }
}
- }
- // custom state width animation
- else if(pos && pos.width){
- Imp.jqi.css({
- position: "absolute",
- left: '50%'
- });
- Imp.jqi.animate({
- top: pos.y || top,
- left: pos.x || '50%',
- marginLeft: ((pos.width/2)*-1),
- width: pos.width
+
+ //Events
+ t.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
+ var $t = $(this),
+ $state = $t.parents('.'+ opts.prefix +'state'),
+ stateobj = t.options.states[$state.data('jqi-name')],
+ msg = $state.children('.'+ opts.prefix +'message'),
+ clicked = stateobj.buttons[$t.text()] || stateobj.buttons[$t.html()],
+ forminputs = {};
+
+ // if for some reason we couldn't get the value
+ if(clicked === undefined){
+ for(var i in stateobj.buttons){
+ if(stateobj.buttons[i].title === $t.text() || stateobj.buttons[i].title === $t.html()){
+ clicked = stateobj.buttons[i].value;
+ }
+ }
+ }
+
+ //collect all form element values from all states.
+ $.each(t.jqi.children('form').serializeArray(),function(i,obj){
+ if (forminputs[obj.name] === undefined) {
+ forminputs[obj.name] = obj.value;
+ } else if (typeof forminputs[obj.name] === Array || typeof forminputs[obj.name] === 'object') {
+ forminputs[obj.name].push(obj.value);
+ } else {
+ forminputs[obj.name] = [forminputs[obj.name],obj.value];
+ }
});
- }
- // standard prompt positioning
- else{
- Imp.jqi.css({
- position: "absolute",
- top: top,
- left: '50%',//$window.width()/2,
- marginLeft: ((Imp.jqi.outerWidth(false)/2)*-1)
- });
- }
- // restore fx settings
- if(e !== undefined && e.data.animate === false){
- $.fx.off = restoreFx;
- }
- };
+ // trigger an event
+ var promptsubmite = new $.Event('impromptu:submit');
+ promptsubmite.stateName = stateobj.name;
+ promptsubmite.state = $state;
+ $state.trigger(promptsubmite, [clicked, msg, forminputs]);
- /**
- * style - Restyles the prompt (Used internally)
- * @return void
- */
- Imp.style = function(){
- Imp.jqif.css({
- zIndex: Imp.options.zIndex,
- display: "none",
- opacity: Imp.options.opacity
- });
- Imp.jqi.css({
- zIndex: Imp.options.zIndex+1,
- display: "none"
- });
- Imp.jqib.css({
- zIndex: Imp.options.zIndex
- });
- };
+ if(!promptsubmite.isDefaultPrevented()){
+ t.close(true, clicked,msg,forminputs);
+ }
+ });
- /**
- * get - Get the prompt
- * @return jQuery - the prompt
- */
- Imp.get = function(state) {
- return $('.'+ Imp.currentPrefix);
- };
+ // if the fade is clicked blink the prompt
+ var fadeClicked = function(){
+ if(opts.persistent){
+ var offset = (opts.top.toString().indexOf('%') >= 0? ($window.height()*(parseInt(opts.top,10)/100)) : parseInt(opts.top,10)),
+ top = parseInt(t.jqi.css('top').replace('px',''),10) - offset;
+
+ //$window.scrollTop(top);
+ $('html,body').animate({ scrollTop: top }, 'fast', function(){
+ var i = 0;
+ t.jqib.addClass(opts.prefix +'warning');
+ var intervalid = setInterval(function(){
+ t.jqib.toggleClass(opts.prefix +'warning');
+ if(i++ > 1){
+ clearInterval(intervalid);
+ t.jqib.removeClass(opts.prefix +'warning');
+ }
+ }, 100);
+ });
+ }
+ else {
+ t.close(true);
+ }
+ };
- /**
- * addState - Injects a state into the prompt
- * @param statename String - Name of the state
- * @param stateobj Object - options for the state
- * @param afterState String - selector of the state to insert after
- * @return jQuery - the newly created state
- */
- Imp.addState = function(statename, stateobj, afterState) {
- var state = "",
- $state = null,
- arrow = "",
- title = "",
- opts = Imp.options,
- $jqistates = $('.'+ Imp.currentPrefix +'states'),
- buttons = [],
- showHtml,defbtn,k,v,l,i=0;
-
- stateobj = $.extend({},Imp.defaults.state, {name:statename}, stateobj);
-
- if(stateobj.position.arrow !== null){
- arrow = '<div class="'+ opts.prefix + 'arrow '+ opts.prefix + 'arrow'+ stateobj.position.arrow +'"></div>';
- }
- if(stateobj.title && stateobj.title !== ''){
- title = '<div class="lead '+ opts.prefix + 'title '+ opts.classes.title +'">'+ stateobj.title +'</div>';
- }
+ // listen for esc or tab keys
+ var keyDownEventHandler = function(e){
+ var key = (window.event) ? event.keyCode : e.keyCode;
- showHtml = stateobj.html;
- if (typeof stateobj.html === 'function') {
- showHtml = 'Error: html function must return text';
- }
+ //escape key closes
+ if(key === 27) {
+ fadeClicked();
+ }
- state += '<div id="'+ opts.prefix +'state_'+ statename +'" class="'+ opts.prefix + 'state" data-jqi-name="'+ statename +'" style="display:none;">'+
- arrow + title +
- '<div class="'+ opts.prefix +'message '+ opts.classes.message +'">' + showHtml +'</div>'+
- '<div class="'+ opts.prefix +'buttons '+ opts.classes.buttons +'"'+ ($.isEmptyObject(stateobj.buttons)? 'style="display:none;"':'') +'>';
+ //enter key pressed trigger the default button if its not on it, ignore if it is a textarea
+ if(key === 13){
+ var $defBtn = t.getCurrentState().find('.'+ opts.prefix +'defaultbutton');
+ var $tgt = $(e.target);
- // state buttons may be in object or array, lets convert objects to arrays
- if($.isArray(stateobj.buttons)){
- buttons = stateobj.buttons;
- }
- else if($.isPlainObject(stateobj.buttons)){
- for(k in stateobj.buttons){
- if(stateobj.buttons.hasOwnProperty(k)){
- buttons.push({ title: k, value: stateobj.buttons[k] });
+ if($tgt.is('textarea,.'+opts.prefix+'button') === false && $defBtn.length > 0){
+ e.preventDefault();
+ $defBtn.click();
+ }
+ }
+
+ //constrain tabs, tabs should iterate through the state and not leave
+ if (key === 9){
+ var $inputels = $('input,select,textarea,button',t.getCurrentState());
+ var fwd = !e.shiftKey && e.target === $inputels[$inputels.length-1];
+ var back = e.shiftKey && e.target === $inputels[0];
+ if (fwd || back) {
+ setTimeout(function(){
+ if (!$inputels){
+ return;
+ }
+ var el = $inputels[back===true ? $inputels.length-1 : 0];
+
+ if (el){
+ el.focus();
+ }
+ },10);
+ return false;
+ }
}
+ };
+
+ t.position();
+ t.style();
+
+ t.jqif.click(fadeClicked);
+ $window.resize({animate:false}, function(){ t.position(); });
+ t.jqi.find('.'+ opts.prefix +'close').click(t.close);
+ t.jqib.on("keydown",keyDownEventHandler)
+ .on('impromptu:loaded', opts.loaded)
+ .on('impromptu:close', opts.close)
+ .on('impromptu:statechanging', opts.statechanging)
+ .on('impromptu:statechanged', opts.statechanged);
+
+ // Show it
+ t.jqif[opts.show](opts.overlayspeed);
+ t.jqi[opts.show](opts.promptspeed, function(){
+
+ var $firstState = t.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
+ t.goToState($firstState.data('jqi-name'));
+
+ t.jqib.trigger('impromptu:loaded');
+ });
+
+ // Timeout
+ if(opts.timeout > 0){
+ t.timeout = setTimeout(function(){ t.close(true); },opts.timeout);
}
- }
- // iterate over each button and create them
- for(i=0, l=buttons.length; i<l; i++){
- v = buttons[i],
- defbtn = stateobj.focus === i || (isNaN(stateobj.focus) && stateobj.defaultButton === i) ? (Imp.currentPrefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
+ return t;
+ },
- state += '<button class="'+ opts.classes.button +' '+ Imp.currentPrefix + 'button '+ defbtn;
+ /**
+ * close - Closes the prompt
+ * @param callback Function - called when the transition is complete
+ * @param clicked String - value of the button clicked (only used internally)
+ * @param msg jQuery - The state message body (only used internally)
+ * @param forvals Object - key/value pairs of all form field names and values (only used internally)
+ * @return Imp - the instance of this Impromptu object
+ */
+ close: function(callCallback, clicked, msg, formvals){
+ var t = this;
+
+ if(t.timeout){
+ clearTimeout(t.timeout);
+ t.timeout = false;
+ }
- if(typeof v.classes !== "undefined"){
- state += ' '+ ($.isArray(v.classes)? v.classes.join(' ') : v.classes) + ' ';
+ if(t.jqib){
+ t.jqib.fadeOut('fast',function(){
+
+ t.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
+
+ t.jqib.remove();
+
+ $(window).off('resize', function(){ t.position(); });
+
+ if(typeof callCallback === 'function'){
+ callCallback();
+ }
+ });
}
+ t.currentStateName = "";
- state += '" name="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" id="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
- }
-
- state += '</div></div>';
+ return t;
+ },
- $state = $(state);
+ /**
+ * addState - Injects a state into the prompt
+ * @param statename String - Name of the state
+ * @param stateobj Object - options for the state
+ * @param afterState String - selector of the state to insert after
+ * @return jQuery - the newly created state
+ */
+ addState: function(statename, stateobj, afterState) {
+ var t = this,
+ state = '',
+ $state = null,
+ arrow = '',
+ title = '',
+ opts = t.options,
+ $jqistates = $('.'+ opts.prefix +'states'),
+ buttons = [],
+ showHtml,defbtn,k,v,l,i=0;
+
+ stateobj = $.extend({},Imp.defaults.state, {name:statename}, stateobj);
+
+ if(stateobj.position.arrow !== null){
+ arrow = '<div class="'+ opts.prefix + 'arrow '+ opts.prefix + 'arrow'+ stateobj.position.arrow +'"></div>';
+ }
+ if(stateobj.title && stateobj.title !== ''){
+ title = '<div class="lead '+ opts.prefix + 'title '+ opts.classes.title +'">'+ stateobj.title +'</div>';
+ }
- $state.on('impromptu:submit', stateobj.submit);
+ showHtml = stateobj.html;
+ if (typeof stateobj.html === 'function') {
+ showHtml = 'Error: html function must return text';
+ }
- if(afterState !== undefined){
- $jqistates.find('#'+ Imp.currentPrefix +'state_'+ afterState).after($state);
- }
- else{
- $jqistates.append($state);
- }
+ state += '<div id="'+ opts.prefix +'state_'+ statename +'" class="'+ opts.prefix + 'state" data-jqi-name="'+ statename +'" style="display:none;">'+
+ arrow + title +
+ '<div class="'+ opts.prefix +'message '+ opts.classes.message +'">' + showHtml +'</div>'+
+ '<div class="'+ opts.prefix +'buttons '+ opts.classes.buttons +'"'+ ($.isEmptyObject(stateobj.buttons)? 'style="display:none;"':'') +'>';
- Imp.options.states[statename] = stateobj;
+ // state buttons may be in object or array, lets convert objects to arrays
+ if($.isArray(stateobj.buttons)){
+ buttons = stateobj.buttons;
+ }
+ else if($.isPlainObject(stateobj.buttons)){
+ for(k in stateobj.buttons){
+ if(stateobj.buttons.hasOwnProperty(k)){
+ buttons.push({ title: k, value: stateobj.buttons[k] });
+ }
+ }
+ }
- return $state;
- };
+ // iterate over each button and create them
+ for(i=0, l=buttons.length; i<l; i++){
+ v = buttons[i],
+ defbtn = stateobj.focus === i || (isNaN(stateobj.focus) && stateobj.defaultButton === i) ? (opts.prefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
- /**
- * removeState - Removes a state from the prompt
- * @param state String - Name of the state
- * @param newState String - Name of the state to transition to
- * @return Boolean - returns true on success, false on failure
- */
- Imp.removeState = function(state, newState) {
- var $state = Imp.getState(state),
- rm = function(){ $state.remove(); };
+ state += '<button class="'+ opts.classes.button +' '+ opts.prefix + 'button '+ defbtn;
- if($state.length === 0){
- return false;
- }
+ if(typeof v.classes !== "undefined"){
+ state += ' '+ ($.isArray(v.classes)? v.classes.join(' ') : v.classes) + ' ';
+ }
- // transition away from it before deleting
- if($state.css('display') !== 'none'){
- if(newState !== undefined && Imp.getState(newState).length > 0){
- Imp.goToState(newState, false, rm);
+ state += '" name="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" id="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
}
- else if($state.next().length > 0){
- Imp.nextState(rm);
- }
- else if($state.prev().length > 0){
- Imp.prevState(rm);
+
+ state += '</div></div>';
+
+ $state = $(state);
+
+ $state.on('impromptu:submit', stateobj.submit);
+
+ if(afterState !== undefined){
+ $jqistates.find('[data-jqi-name="'+afterState+'"]').after($state);
}
else{
- Imp.close();
+ $jqistates.append($state);
}
- }
- else{
- $state.slideUp('slow', rm);
- }
- return true;
- };
+ t.options.states[statename] = stateobj;
- /**
- * getState - Get the state by its name
- * @param state String - Name of the state
- * @return jQuery - the state
- */
- Imp.getState = function(state) {
- return $('#'+ Imp.currentPrefix +'state_'+ state);
- };
- Imp.getStateContent = function(state) {
- return Imp.getState(state);
- };
+ return $state;
+ },
- /**
- * getCurrentState - Get the current visible state
- * @return jQuery - the current visible state
- */
- Imp.getCurrentState = function() {
- return Imp.getState(Imp.getCurrentStateName());
- };
+ /**
+ * removeState - Removes a state from the prompt
+ * @param state String - Name of the state
+ * @param newState String - Name of the state to transition to
+ * @return Boolean - returns true on success, false on failure
+ */
+ removeState: function(state, newState) {
+ var t = this,
+ $state = t.getState(state),
+ rm = function(){ $state.remove(); };
+
+ if($state.length === 0){
+ return false;
+ }
- /**
- * getCurrentStateName - Get the name of the current visible state
- * @return String - the current visible state's name
- */
- Imp.getCurrentStateName = function() {
- return Imp.currentStateName;
- };
+ // transition away from it before deleting
+ if($state.css('display') !== 'none'){
+ if(newState !== undefined && t.getState(newState).length > 0){
+ t.goToState(newState, false, rm);
+ }
+ else if($state.next().length > 0){
+ t.nextState(rm);
+ }
+ else if($state.prev().length > 0){
+ t.prevState(rm);
+ }
+ else{
+ t.close();
+ }
+ }
+ else{
+ $state.slideUp('slow', rm);
+ }
- /**
- * goToState - Goto the specified state
- * @param state String - name of the state to transition to
- * @param subState Boolean - true to be a sub state within the currently open state
- * @param callback Function - called when the transition is complete
- * @return jQuery - the newly active state
- */
- Imp.goToState = function(state, subState, callback) {
- var $jqi = Imp.get(),
- jqiopts = Imp.options,
- $state = Imp.getState(state),
- stateobj = jqiopts.states[$state.data('jqi-name')],
- promptstatechanginge = new $.Event('impromptu:statechanging'),
- opts = Imp.options;
+ return true;
+ },
- if(stateobj !== undefined){
+ /**
+ * get - Get the prompt
+ * @return jQuery - the prompt
+ */
+ getPrompt: function() {
+ return this.jqi;
+ },
+ /**
+ * getState - Get the state by its name
+ * @param statename String - Name of the state
+ * @return jQuery - the state
+ */
+ getState: function(statename) {
+ return this.jqi.find('[data-jqi-name="'+ statename +'"]');
+ },
- if (typeof stateobj.html === 'function') {
- var contentLaterFunc = stateobj.html;
- $state.find('.' + opts.prefix +'message ').html(contentLaterFunc());
- }
+ /**
+ * getCurrentState - Get the current visible state
+ * @return jQuery - the current visible state
+ */
+ getCurrentState: function() {
+ return this.getState(this.getCurrentStateName());
+ },
+
+ /**
+ * getCurrentStateName - Get the name of the current visible state/substate
+ * @return String - the current visible state's name
+ */
+ getCurrentStateName: function() {
+ return this.currentStateName;
+ },
- // subState can be ommitted
- if(typeof subState === 'function'){
- callback = subState;
- subState = false;
+ /**
+ * position - Repositions the prompt (Used internally)
+ * @return void
+ */
+ position: function(e){
+ var t = this,
+ restoreFx = $.fx.off,
+ $state = t.getCurrentState(),
+ stateObj = t.options.states[$state.data('jqi-name')],
+ pos = stateObj? stateObj.position : undefined,
+ $window = $(window),
+ bodyHeight = document.body.scrollHeight, //$(document.body).outerHeight(true),
+ windowHeight = $(window).height(),
+ documentHeight = $(document).height(),
+ height = bodyHeight > windowHeight ? bodyHeight : windowHeight,
+ top = parseInt($window.scrollTop(),10) + (t.options.top.toString().indexOf('%') >= 0?
+ (windowHeight*(parseInt(t.options.top,10)/100)) : parseInt(t.options.top,10));
+
+ // when resizing the window turn off animation
+ if(e !== undefined && e.data.animate === false){
+ $.fx.off = true;
}
- Imp.jqib.trigger(promptstatechanginge, [Imp.getCurrentStateName(), state]);
+ t.jqib.css({
+ position: "absolute",
+ height: height,
+ width: "100%",
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0
+ });
+ t.jqif.css({
+ position: "fixed",
+ height: height,
+ width: "100%",
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0
+ });
- if(!promptstatechanginge.isDefaultPrevented() && $state.length > 0){
- Imp.jqi.find('.'+ Imp.currentPrefix +'parentstate').removeClass(Imp.currentPrefix +'parentstate');
+ // tour positioning
+ if(pos && pos.container){
+ var offset = $(pos.container).offset();
+
+ if($.isPlainObject(offset) && offset.top !== undefined){
+ t.jqi.css({
+ position: "absolute"
+ });
+ t.jqi.animate({
+ top: offset.top + pos.y,
+ left: offset.left + pos.x,
+ marginLeft: 0,
+ width: (pos.width !== undefined)? pos.width : null
+ });
+ top = (offset.top + pos.y) - (t.options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt(t.options.top,10)/100)) : parseInt(t.options.top,10));
+ $('html,body').animate({ scrollTop: top }, 'slow', 'swing', function(){});
+ }
+ }
+ // custom state width animation
+ else if(pos && pos.width){
+ t.jqi.css({
+ position: "absolute",
+ left: '50%'
+ });
+ t.jqi.animate({
+ top: pos.y || top,
+ left: pos.x || '50%',
+ marginLeft: ((pos.width/2)*-1),
+ width: pos.width
+ });
+ }
+ // standard prompt positioning
+ else{
+ t.jqi.css({
+ position: "absolute",
+ top: top,
+ left: '50%',//$window.width()/2,
+ marginLeft: ((t.jqi.outerWidth(false)/2)*-1)
+ });
+ }
- if(subState){ // hide any open substates
- // get rid of any substates
- Imp.jqi.find('.'+ Imp.currentPrefix +'substate').not($state)
- .slideUp(jqiopts.promptspeed)
- .removeClass('.'+ Imp.currentPrefix +'substate')
- .find('.'+ Imp.currentPrefix +'arrow').hide();
+ // restore fx settings
+ if(e !== undefined && e.data.animate === false){
+ $.fx.off = restoreFx;
+ }
+ },
- // add parent state class so it can be visible, but blocked
- Imp.jqi.find('.'+ Imp.currentPrefix +'state:visible').addClass(Imp.currentPrefix +'parentstate');
+ /**
+ * style - Restyles the prompt (Used internally)
+ * @return void
+ */
+ style: function(){
+ var t = this;
+
+ t.jqif.css({
+ zIndex: t.options.zIndex,
+ display: "none",
+ opacity: t.options.opacity
+ });
+ t.jqi.css({
+ zIndex: t.options.zIndex+1,
+ display: "none"
+ });
+ t.jqib.css({
+ zIndex: t.options.zIndex
+ });
+ },
- // add substate class so we know it will be smaller
- $state.addClass(Imp.currentPrefix +'substate');
+ /**
+ * goToState - Goto the specified state
+ * @param state String - name of the state to transition to
+ * @param subState Boolean - true to be a sub state within the currently open state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ goToState: function(state, subState, callback) {
+ var t = this,
+ $jqi = t.jqi,
+ jqiopts = t.options,
+ $state = t.getState(state),
+ stateobj = jqiopts.states[$state.data('jqi-name')],
+ promptstatechanginge = new $.Event('impromptu:statechanging'),
+ opts = t.options;
+
+ if(stateobj !== undefined){
+
+
+ if (typeof stateobj.html === 'function') {
+ var contentLaterFunc = stateobj.html;
+ $state.find('.' + opts.prefix +'message ').html(contentLaterFunc());
}
- else{ // hide any open states
- Imp.jqi.find('.'+ Imp.currentPrefix +'state').not($state)
- .slideUp(jqiopts.promptspeed)
- .find('.'+ Imp.currentPrefix +'arrow').hide();
+
+ // subState can be ommitted
+ if(typeof subState === 'function'){
+ callback = subState;
+ subState = false;
}
- Imp.currentStateName = stateobj.name;
- $state.slideDown(jqiopts.promptspeed,function(){
- var $t = $(this);
+ t.jqib.trigger(promptstatechanginge, [t.getCurrentStateName(), state]);
- // if focus is a selector, find it, else its button index
- if(typeof(stateobj.focus) === 'string'){
- $t.find(stateobj.focus).eq(0).focus();
- }
- else{
- $t.find('.'+ Imp.currentPrefix +'defaultbutton').focus();
- }
+ if(!promptstatechanginge.isDefaultPrevented() && $state.length > 0){
+ t.jqi.find('.'+ opts.prefix +'parentstate').removeClass(opts.prefix +'parentstate');
- $t.find('.'+ Imp.currentPrefix +'arrow').show(jqiopts.promptspeed);
+ if(subState){ // hide any open substates
+ // get rid of any substates
+ t.jqi.find('.'+ opts.prefix +'substate').not($state)
+ .slideUp(jqiopts.promptspeed)
+ .removeClass('.'+ opts.prefix +'substate')
+ .find('.'+ opts.prefix +'arrow').hide();
- if (typeof callback === 'function'){
- Imp.jqib.on('impromptu:statechanged', callback);
+ // add parent state class so it can be visible, but blocked
+ t.jqi.find('.'+ opts.prefix +'state:visible').addClass(opts.prefix +'parentstate');
+
+ // add substate class so we know it will be smaller
+ $state.addClass(opts.prefix +'substate');
}
- Imp.jqib.trigger('impromptu:statechanged', [state]);
- if (typeof callback === 'function'){
- Imp.jqib.off('impromptu:statechanged', callback);
+ else{ // hide any open states
+ t.jqi.find('.'+ opts.prefix +'state').not($state)
+ .slideUp(jqiopts.promptspeed)
+ .find('.'+ opts.prefix +'arrow').hide();
}
- });
- if(!subState){
- Imp.position();
- }
- } // end isDefaultPrevented()
- }// end stateobj !== undefined
+ t.currentStateName = stateobj.name;
- return $state;
- };
+ $state.slideDown(jqiopts.promptspeed,function(){
+ var $t = $(this);
- /**
- * nextState - Transition to the next state
- * @param callback Function - called when the transition is complete
- * @return jQuery - the newly active state
- */
- Imp.nextState = function(callback) {
- var $next = $('#'+ Imp.currentPrefix +'state_'+ Imp.getCurrentStateName()).next();
- if($next.length > 0){
- Imp.goToState( $next.attr('id').replace(Imp.currentPrefix +'state_',''), callback );
- }
- return $next;
- };
+ // if focus is a selector, find it, else its button index
+ if(typeof(stateobj.focus) === 'string'){
+ $t.find(stateobj.focus).eq(0).focus();
+ }
+ else{
+ $t.find('.'+ opts.prefix +'defaultbutton').focus();
+ }
- /**
- * prevState - Transition to the previous state
- * @param callback Function - called when the transition is complete
- * @return jQuery - the newly active state
- */
- Imp.prevState = function(callback) {
- var $prev = $('#'+ Imp.currentPrefix +'state_'+ Imp.getCurrentStateName()).prev();
- if($prev.length > 0){
- Imp.goToState( $prev.attr('id').replace(Imp.currentPrefix +'state_',''), callback );
- }
- return $prev;
- };
+ $t.find('.'+ opts.prefix +'arrow').show(jqiopts.promptspeed);
- /**
- * close - Closes the prompt
- * @param callback Function - called when the transition is complete
- * @param clicked String - value of the button clicked (only used internally)
- * @param msg jQuery - The state message body (only used internally)
- * @param forvals Object - key/value pairs of all form field names and values (only used internally)
- * @return jQuery - the newly active state
- */
- Imp.close = function(callCallback, clicked, msg, formvals){
- if(Imp.timeout){
- clearTimeout(Imp.timeout);
- Imp.timeout = false;
- }
+ if (typeof callback === 'function'){
+ t.jqib.on('impromptu:statechanged', callback);
+ }
+ t.jqib.trigger('impromptu:statechanged', [state]);
+ if (typeof callback === 'function'){
+ t.jqib.off('impromptu:statechanged', callback);
+ }
+ });
+ if(!subState){
+ t.position();
+ }
+ } // end isDefaultPrevented()
+ }// end stateobj !== undefined
- if(Imp.jqib){
- Imp.jqib.fadeOut('fast',function(){
-
- Imp.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
-
- Imp.jqib.remove();
-
- $(window).off('resize',Imp.position);
-
- if(typeof callCallback === 'function'){
- callCallback();
- }
- });
+ return $state;
+ },
+
+ /**
+ * nextState - Transition to the next state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ nextState: function(callback) {
+ var t = this,
+ $next = t.getCurrentState().next();
+ if($next.length > 0){
+ t.goToState( $next.data('jqi-name'), callback );
+ }
+ return $next;
+ },
+
+ /**
+ * prevState - Transition to the previous state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ prevState: function(callback) {
+ var t = this,
+ $prev = t.getCurrentState().prev();
+ if($prev.length > 0){
+ t.goToState( $prev.data('jqi-name'), callback );
+ }
+ return $prev;
}
- Imp.currentStateName = "";
+
};
+ // ########################################################################
+ // jQuery Plugin and public access
+ // ########################################################################
+
/**
* Enable using $('.selector').prompt({});
* This will grab the html within the prompt as the prompt message
@@ -723,7 +751,16 @@ new Imp($(this).clone(options.withDataAndEvents).html(),options);
};
- $.prompt = Imp;
+ /**
+ * Export it as Impromptu and $.prompt
+ * Can be used from here forth as new Impromptu(states, opts)
+ */
window.Impromptu = Imp;
+ /**
+ * Set $.prompt as a single instance
+ * Can be used from here forth as $.prompt.open(state, opts)
+ */
+ $.prompt = new Imp();
+
}));
diff --git a/test/jquery-impromptu_spec.js b/test/jquery-impromptu_spec.js index 0e3cf25..d615424 100644 --- a/test/jquery-impromptu_spec.js +++ b/test/jquery-impromptu_spec.js @@ -24,7 +24,39 @@ describe('jquery-impromptu', function() { var expectedTitle = 'This is a title', expectedText = 'This is a test'; - $.prompt(expectedText, { title: expectedTitle }); + $.prompt.open(expectedText, { title: expectedTitle }); + + expect($('.jqibox')).toExist(); + expect($('.jqifade')).toExist(); + expect($('.jqi')).toExist(); + expect($('.jqi .jqititle')).toHaveText(expectedTitle); + expect($('.jqi .jqimessage')).toHaveText(expectedText); + }); + + }); + + // ==================================================================================== + describe('instance initialization', function() { + var imp = new Impromptu(); + + beforeEach(function() { + $.fx.off = true; // for our testing lets turn off fx + }); + + afterEach(function() { + imp.close(); + }); + + it('should be defined', function() { + + expect(imp).not.toBeUndefined(); + }); + + it('should generate markup', function() { + var expectedTitle = 'This is a title', + expectedText = 'This is a test'; + + imp.open(expectedText, { title: expectedTitle }); expect($('.jqibox')).toExist(); expect($('.jqifade')).toExist(); @@ -50,7 +82,7 @@ describe('jquery-impromptu', function() { it('should generate buttons from hash', function() { - $.prompt('This is a test', { + $.prompt.open('This is a test', { buttons: { Ok:true, Cancel:false } }); var okBtn = $('#jqi_state0_buttonOk'), @@ -70,7 +102,7 @@ describe('jquery-impromptu', function() { it('should generate buttons from array', function() { - $.prompt('This is a test', { + $.prompt.open('This is a test', { buttons: [ { title: 'Ok', value: true }, { title: 'Cancel', value: false } @@ -93,7 +125,7 @@ describe('jquery-impromptu', function() { it('should add classes to buttons', function() { - $.prompt('This is a test', { + $.prompt.open('This is a test', { buttons: [ { title: 'Ok', value: true, classes: ['ok1','ok2'] }, { title: 'Cancel', value: false, classes: 'cancel1 cancel2' } @@ -111,7 +143,7 @@ describe('jquery-impromptu', function() { it('should add classes to buttons from classes obj', function() { - $.prompt('This is a test', { + $.prompt.open('This is a test', { buttons: [ { title: 'Ok', value: true, classes: ['ok1','ok2'] }, { title: 'Cancel', value: false, classes: 'cancel1 cancel2' } @@ -127,7 +159,7 @@ describe('jquery-impromptu', function() { it('should default correct button', function() { - $.prompt('This is a test', { + $.prompt.open('This is a test', { buttons: [ { title: 'Ok', value: 1 }, { title: 'Cancel', value: 2 }, @@ -146,7 +178,7 @@ describe('jquery-impromptu', function() { it('should default correct button when focus on an input', function() { - $.prompt('This is a test <input type="text" id="testInput" />', { + $.prompt.open('This is a test <input type="text" id="testInput" />', { buttons: [ { title: 'Ok', value: 1 }, { title: 'Cancel', value: 2 }, @@ -179,7 +211,7 @@ describe('jquery-impromptu', function() { it('should create a single state from string', function() { - $.prompt('This is a test'); + $.prompt.open('This is a test'); expect($('.jqistate')).toExist(); }); @@ -191,7 +223,7 @@ describe('jquery-impromptu', function() { s3: { html: 'state 3' } }; - $.prompt(states); + $.prompt.open(states); expect($('.jqistate')).toHaveLength(3); @@ -207,7 +239,7 @@ describe('jquery-impromptu', function() { { html: 'state 3' } ]; - $.prompt(states); + $.prompt.open(states); expect($('.jqistate')).toHaveLength(3); @@ -225,7 +257,7 @@ describe('jquery-impromptu', function() { { html: 'state 3' } ]; - $.prompt(states); + $.prompt.open(states); expect($('#jqistate_0')).toHaveCss({display:'block'}); expect($('#jqistate_1')).toHaveCss({display:'none'}); @@ -239,7 +271,7 @@ describe('jquery-impromptu', function() { { name: 's3', html: 'state 3' } ]; - $.prompt(states); + $.prompt.open(states); expect($('#jqistate_s1')).toExist(); expect($('#jqistate_s2')).toExist(); @@ -267,304 +299,306 @@ describe('jquery-impromptu', function() { $.prompt.close(); }); + describe('static methods', function() { + // ==================================================================================== + describe('Impromptu.setDefaults()', function() { + it('should change the default values', function() { + var origDefs = $.extend(true, {}, Impromptu.defaults), + overrides = { prefix: 'myjqi', classes: { box: 'boxclass' } }; + + Impromptu.setDefaults(overrides); - // ==================================================================================== - describe('$.prompt.setDefaults()', function() { - it('should change the default values', function() { - var origDefs = $.extend(true, {}, $.prompt.defaults), - overrides = { prefix: 'myjqi', classes: { box: 'boxclass' } }; - - $.prompt.setDefaults(overrides); - - expect($.prompt.defaults.prefix).toBe(overrides.prefix); - expect($.prompt.defaults.classes.box).toBe(overrides.classes.box); - expect($.prompt.defaults.speed).toBe(origDefs.speed); + expect(Impromptu.defaults.prefix).toBe(overrides.prefix); + expect(Impromptu.defaults.classes.box).toBe(overrides.classes.box); + expect(Impromptu.defaults.speed).toBe(origDefs.speed); - $.prompt.defaults = origDefs; + Impromptu.defaults = origDefs; + }); }); - }); - - // ==================================================================================== - describe('$.prompt.setStateDefaults()', function() { - it('should change the default state values', function() { - var origDefs = $.extend(true, {}, $.prompt.defaults), - overrides = { title: 'My Title', position: { width: 123 } }; - - $.prompt.setStateDefaults(overrides); - - expect($.prompt.defaults.state.title).toBe(overrides.title); - expect($.prompt.defaults.state.position.width).toBe(overrides.position.width); - expect($.prompt.defaults.state.focus).toBe(origDefs.state.focus); - - $.prompt.defaults = origDefs; + + // ==================================================================================== + describe('Impromptu.setStateDefaults()', function() { + it('should change the default state values', function() { + var origDefs = $.extend(true, {}, Impromptu.defaults), + overrides = { title: 'My Title', position: { width: 123 } }; + + Impromptu.setStateDefaults(overrides); + + expect(Impromptu.defaults.state.title).toBe(overrides.title); + expect(Impromptu.defaults.state.position.width).toBe(overrides.position.width); + expect(Impromptu.defaults.state.focus).toBe(origDefs.state.focus); + + Impromptu.defaults = origDefs; + }); }); }); - // ==================================================================================== - describe('$.prompt.get()', function() { - it('should return the prompt jquery object', function() { - - $.prompt('This is a test'); + describe('instance methods', function() { + // ==================================================================================== + describe('$.prompt.getPrompt()', function() { + it('should return the prompt jquery object', function() { + + $.prompt.open('This is a test'); - var actualResult = $.prompt.get(), - expectedResult = $('.jqi'); + var actualResult = $.prompt.getPrompt(), + expectedResult = $('.jqi'); - expect(actualResult[0]).toBe(expectedResult[0]); + expect(actualResult[0]).toBe(expectedResult[0]); + }); }); - }); - // ==================================================================================== - describe('$.prompt.getState()', function() { - it('should return the state jquery object', function() { + // ==================================================================================== + describe('$.prompt.getState()', function() { + it('should return the state jquery object', function() { - $.prompt(states); - - var actualResult = $.prompt.getState('s2'), - expectedResult = $('#jqistate_s2'); + $.prompt.open(states); + + var actualResult = $.prompt.getState('s2'), + expectedResult = $('#jqistate_s2'); - expect(actualResult[0]).toBe(expectedResult[0]); + expect(actualResult[0]).toBe(expectedResult[0]); + }); }); - }); - // ==================================================================================== - describe('$.prompt.getCurrentState()', function() { - it('should return the current state jquery object', function() { + // ==================================================================================== + describe('$.prompt.getCurrentState()', function() { + it('should return the current state jquery object', function() { - $.prompt(states); - - var actualResult = $.prompt.getCurrentState(), - expectedResult = $('#jqistate_s1'); + $.prompt.open(states); + + var actualResult = $.prompt.getCurrentState(), + expectedResult = $('#jqistate_s1'); - expect(actualResult[0]).toBe(expectedResult[0]); - }); + expect(actualResult[0]).toBe(expectedResult[0]); + }); - it('should return the current state jquery object after a state change', function() { + it('should return the current state jquery object after a state change', function() { - $.prompt(states); - $.prompt.goToState('s2'); - var actualResult = $.prompt.getCurrentState(), - expectedResult = $('#jqistate_s2'); + $.prompt.open(states); + $.prompt.goToState('s2'); + var actualResult = $.prompt.getCurrentState(), + expectedResult = $('#jqistate_s2'); - expect(actualResult[0]).toBe(expectedResult[0]); + expect(actualResult[0]).toBe(expectedResult[0]); + }); }); - }); - // ==================================================================================== - describe('$.prompt.getCurrentStateName()', function() { - it('should return the current state name', function() { + // ==================================================================================== + describe('$.prompt.getCurrentStateName()', function() { + it('should return the current state name', function() { - $.prompt(states); - - var actualResult = $.prompt.getCurrentStateName(), - expectedResult = 's1'; + $.prompt.open(states); + + var actualResult = $.prompt.getCurrentStateName(), + expectedResult = 's1'; - expect(actualResult).toBe(expectedResult); - }); + expect(actualResult).toBe(expectedResult); + }); - it('should return the current state name after a state change', function() { + it('should return the current state name after a state change', function() { - $.prompt(states); - $.prompt.goToState('s2'); - var actualResult = $.prompt.getCurrentStateName(), - expectedResult = 's2'; + $.prompt.open(states); + $.prompt.goToState('s2'); + var actualResult = $.prompt.getCurrentStateName(), + expectedResult = 's2'; - expect(actualResult).toBe(expectedResult); + expect(actualResult).toBe(expectedResult); + }); }); - }); - // ==================================================================================== - describe('$.prompt.goToState()', function() { - it('should make the requested state visible', function() { + // ==================================================================================== + describe('$.prompt.goToState()', function() { + it('should make the requested state visible', function() { - $.prompt(states); - - $.prompt.goToState('s3'); + $.prompt.open(states); + + $.prompt.goToState('s3'); - expect($('#jqistate_s1')).toHaveCss({display:'none'}); - expect($('#jqistate_s2')).toHaveCss({display:'none'}); - expect($('#jqistate_s3')).toHaveCss({display:'block'}); - }); + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'block'}); + }); - it('should do nothing if the state is not available', function() { + it('should do nothing if the state is not available', function() { - $.prompt(states); - - $.prompt.goToState('s4'); + $.prompt.open(states); + + $.prompt.goToState('s4'); - expect($('#jqistate_s1')).toHaveCss({display:'block'}); - expect($('#jqistate_s2')).toHaveCss({display:'none'}); - expect($('#jqistate_s3')).toHaveCss({display:'none'}); - }); + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); + }); - it('should handle substate option', function() { + it('should handle substate option', function() { - $.prompt(states); - - $.prompt.goToState('s2',true); + $.prompt.open(states); + + $.prompt.goToState('s2',true); - expect($('#jqistate_s1')).toHaveCss({display:'block'}); - expect($('#jqistate_s2')).toHaveCss({display:'block'}); - expect($('#jqistate_s3')).toHaveCss({display:'none'}); + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); - expect($('#jqistate_s2')).toHaveClass('jqisubstate'); + expect($('#jqistate_s2')).toHaveClass('jqisubstate'); + }); }); - }); - // ==================================================================================== - describe('$.prompt.nextState()', function() { - it('should make the next state visible', function() { - - $.prompt(states); - - $.prompt.nextState(); - - expect($('#jqistate_s1')).toHaveCss({display:'none'}); - expect($('#jqistate_s2')).toHaveCss({display:'block'}); - expect($('#jqistate_s3')).toHaveCss({display:'none'}); - }); + // ==================================================================================== + describe('$.prompt.nextState()', function() { + it('should make the next state visible', function() { + + $.prompt.open(states); + + $.prompt.nextState(); + + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); + }); - it('should do nothing if the state is not available', function() { + it('should do nothing if the state is not available', function() { - $.prompt(states); - - $.prompt.goToState('s3'); - $.prompt.nextState(); + $.prompt.open(states); + + $.prompt.goToState('s3'); + $.prompt.nextState(); - expect($('#jqistate_s1')).toHaveCss({display:'none'}); - expect($('#jqistate_s2')).toHaveCss({display:'none'}); - expect($('#jqistate_s3')).toHaveCss({display:'block'}); + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'block'}); + }); }); - }); - // ==================================================================================== - describe('$.prompt.prevState()', function() { - it('should make the previous state visible', function() { - - $.prompt(states); - - $.prompt.goToState('s3'); - $.prompt.prevState(); + // ==================================================================================== + describe('$.prompt.prevState()', function() { + it('should make the previous state visible', function() { + + $.prompt.open(states); + + $.prompt.goToState('s3'); + $.prompt.prevState(); + + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); + }); + + it('should do nothing if the state is not available', function() { + + $.prompt.open(states); + + $.prompt.prevState(); - expect($('#jqistate_s1')).toHaveCss({display:'none'}); - expect($('#jqistate_s2')).toHaveCss({display:'block'}); - expect($('#jqistate_s3')).toHaveCss({display:'none'}); + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); + }); }); - it('should do nothing if the state is not available', function() { + // ==================================================================================== + describe('$.prompt.addState()', function() { + it('should add a new state as the last state', function() { + var newState = { + name: 's4', + title: 's4', + html: 'testing s4', + buttons: { Ok:true,Cancel:false} + }; - $.prompt(states); - - $.prompt.prevState(); + $.prompt.open(states); + + var $stateobj = $.prompt.addState(newState.name, newState); - expect($('#jqistate_s1')).toHaveCss({display:'block'}); - expect($('#jqistate_s2')).toHaveCss({display:'none'}); - expect($('#jqistate_s3')).toHaveCss({display:'none'}); - }); - }); + // element created? + expect($stateobj).toExist(); - // ==================================================================================== - describe('$.prompt.addState()', function() { - it('should add a new state as the last state', function() { - var newState = { - name: 's4', - title: 's4', - html: 'testing s4', - buttons: { Ok:true,Cancel:false} - }; + // element in the right place? + expect($stateobj.prev()).toHaveId('jqistate_s3'); - $.prompt(states); - - var $stateobj = $.prompt.addState(newState.name, newState); + // element visibility correct? + expect($('#jqistate_s1')).toHaveCss({display:'block'}); + expect($stateobj).toHaveCss({display:'none'}); - // element created? - expect($stateobj).toExist(); + // content generated ok? + expect($stateobj.find('.jqimessage')).toHaveText(newState.html); + expect($stateobj.find('.jqititle')).toHaveText(newState.title); + expect($stateobj.find('.jqibutton')).toHaveLength(2); + }); - // element in the right place? - expect($stateobj.prev()).toHaveId('jqistate_s3'); + it('should add a new state after specified state', function() { + var newState = { + name: 's4', + title: 's4', + html: 'testing s4', + buttons: { Ok:true,Cancel:false} + }, + afterState = 's2'; - // element visibility correct? - expect($('#jqistate_s1')).toHaveCss({display:'block'}); - expect($stateobj).toHaveCss({display:'none'}); + $.prompt.open(states); + + var $stateobj = $.prompt.addState(newState.name, newState, afterState); - // content generated ok? - expect($stateobj.find('.jqimessage')).toHaveText(newState.html); - expect($stateobj.find('.jqititle')).toHaveText(newState.title); - expect($stateobj.find('.jqibutton')).toHaveLength(2); + expect($stateobj.prev()).toHaveId('jqistate_'+afterState); + }); }); - it('should add a new state after specified state', function() { - var newState = { - name: 's4', - title: 's4', - html: 'testing s4', - buttons: { Ok:true,Cancel:false} - }, - afterState = 's2'; + // ==================================================================================== + describe('$.prompt.removeState()', function() { + it('should remove the specified state', function() { + + $.prompt.open(states); + + $.prompt.removeState('s2'); - $.prompt(states); + expect($('#jqistate_s2')).not.toExist(); + }); - var $stateobj = $.prompt.addState(newState.name, newState, afterState); + it('should display requested state', function() { - expect($stateobj.prev()).toHaveId('jqistate_'+afterState); - }); - }); + $.prompt.open(states); + + $.prompt.removeState('s1','s3'); - // ==================================================================================== - describe('$.prompt.removeState()', function() { - it('should remove the specified state', function() { - - $.prompt(states); + expect($('#jqistate_s2')).toHaveCss({display:'none'}); + expect($('#jqistate_s3')).toHaveCss({display:'block'}); + }); - $.prompt.removeState('s2'); + it('should display next state', function() { - expect($('#jqistate_s2')).not.toExist(); - }); - - it('should display requested state', function() { + $.prompt.open(states); + + $.prompt.removeState('s1'); - $.prompt(states); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + expect($('#jqistate_s3')).toHaveCss({display:'none'}); + }); - $.prompt.removeState('s1','s3'); + it('should display previous state', function() { - expect($('#jqistate_s2')).toHaveCss({display:'none'}); - expect($('#jqistate_s3')).toHaveCss({display:'block'}); - }); - - it('should display next state', function() { + $.prompt.open(states); + $.prompt.goToState('s3'); + $.prompt.removeState('s3'); - $.prompt(states); + expect($('#jqistate_s1')).toHaveCss({display:'none'}); + expect($('#jqistate_s2')).toHaveCss({display:'block'}); + }); - $.prompt.removeState('s1'); - - expect($('#jqistate_s2')).toHaveCss({display:'block'}); - expect($('#jqistate_s3')).toHaveCss({display:'none'}); }); - - it('should display previous state', function() { - - $.prompt(states); - $.prompt.goToState('s3'); - $.prompt.removeState('s3'); - expect($('#jqistate_s1')).toHaveCss({display:'none'}); - expect($('#jqistate_s2')).toHaveCss({display:'block'}); - }); - - }); + // ==================================================================================== + describe('$.prompt.close()', function() { + it('should close the prompt', function() { + + $.prompt.open(states); + + $.prompt.close(); - // ==================================================================================== - describe('$.prompt.close()', function() { - it('should close the prompt', function() { - - $.prompt(states); - - $.prompt.close(); + expect($('.jqibox')).not.toExist(); + }); - expect($('.jqibox')).not.toExist(); }); - - }); - + }); // end instance methods }); // end api methods // ==================================================================================== @@ -593,7 +627,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ spyEventCalled = false; $('body').on('impromptu:loaded', '.jqibox', function(){ spyEventCalled=true; done(); }); - $.prompt(states); + $.prompt.open(states); }); it('should fire event', function(){ @@ -606,7 +640,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ spyEventCalled = false; - $.prompt(states, { loaded: function(){ spyEventCalled = true; done(); } }); + $.prompt.open(states, { loaded: function(){ spyEventCalled = true; done(); } }); }); it('should allow event function as option parameter', function(){ @@ -624,7 +658,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ spyEventCalled = false; $('body').on('impromptu:close', '.jqibox', function(){ spyEventCalled=true; done(); }); - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.close(); } @@ -641,7 +675,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ spyEventCalled = false; - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.close(); }, close: function(){ spyEventCalled = true; done(); } }); @@ -664,7 +698,7 @@ describe('jquery-impromptu', function() { spyEventCalled = false; $('body').on('impromptu:statechanging', '.jqibox', function(){ spyEventCalled = true; done(); }); - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.goToState('s2'); } @@ -682,7 +716,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ spyEventCalled = false; - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.goToState('s2'); }, @@ -699,7 +733,7 @@ describe('jquery-impromptu', function() { it('should allow preventDefault', function(){ var spyEvent = spyOnEvent('body', 'impromptu:statechanging'); - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.goToState('s2'); }, @@ -724,7 +758,7 @@ describe('jquery-impromptu', function() { spyEventCalled = false; $('body').on('impromptu:statechanged', '.jqibox', function(){ spyEventCalled = true; done(); }); - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.goToState('s2'); } @@ -742,7 +776,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ spyEventCalled = false; - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.goToState('s2'); }, @@ -767,7 +801,7 @@ describe('jquery-impromptu', function() { spyEventCalled = false; $('body').on('impromptu:submit', '.jqibox', function(){ spyEventCalled = true; done(); }); - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.getState('s1').find('.jqibutton:first').click(); } @@ -787,7 +821,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ spyEventCalled = false; - $.prompt('Test message', { + $.prompt.open('Test message', { loaded: function(){ $('.jqibutton:first').click(); }, @@ -815,7 +849,7 @@ describe('jquery-impromptu', function() { done(); }); - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ $.prompt.getState('s1').find('#jqi_s1_buttoncancel').click(); } @@ -867,7 +901,7 @@ describe('jquery-impromptu', function() { done(); }); - $.prompt(tmpStates, { + $.prompt.open(tmpStates, { loaded: function(){ $.prompt.getState('s1').find('#jqi_s1_buttonnext').click(); } @@ -909,7 +943,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ var e = $.Event('keydown'); e.keyCode = 27; @@ -930,7 +964,7 @@ describe('jquery-impromptu', function() { beforeEach(function(done){ - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ var e = $.Event('keydown'); e.keyCode = 27; @@ -957,7 +991,7 @@ describe('jquery-impromptu', function() { done(); }); - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ var e = $.Event('keydown'); e.keyCode = 13; @@ -979,7 +1013,7 @@ describe('jquery-impromptu', function() { describe('fade click', function(){ beforeEach(function(done){ - $.prompt(states, { + $.prompt.open(states, { loaded: function(){ var e = $.Event('click'); $.prompt.jqib.trigger(e); |