summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrent Richardson <trentdrichardson@gmail.com>2014-10-07 16:59:31 -0400
committerTrent Richardson <trentdrichardson@gmail.com>2014-10-07 16:59:31 -0400
commit9dc41c883b5be3a3e73f3d17826735d989954de0 (patch)
tree27268b9423bb1a1707ad9e234f829793efa0bdb2
parent6a357e5f152167e24d9aecb0ebd0e0ab11edc4b9 (diff)
downloadjQuery-Impromptu-9dc41c883b5be3a3e73f3d17826735d989954de0.zip
jQuery-Impromptu-9dc41c883b5be3a3e73f3d17826735d989954de0.tar.gz
jQuery-Impromptu-9dc41c883b5be3a3e73f3d17826735d989954de0.tar.bz2
Add a few utility functions, evaluating this setup with utility functions
-rw-r--r--src/jquery-impromptu.js361
-rw-r--r--test/jquery-impromptu_spec.js113
2 files changed, 332 insertions, 142 deletions
diff --git a/src/jquery-impromptu.js b/src/jquery-impromptu.js
index e339045..9e142d8 100644
--- a/src/jquery-impromptu.js
+++ b/src/jquery-impromptu.js
@@ -8,32 +8,32 @@
"use strict";
/**
- * setDefaults - Sets the default options
+ * Class constructor
* @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
*/
- $.prompt = function(message, options) {
+ 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 };
}
- $.prompt.options = $.extend({},$.prompt.defaults,options);
- $.prompt.currentPrefix = $.prompt.options.prefix;
+ imp.options = $.extend({},imp.defaults,options);
+ imp.currentPrefix = imp.options.prefix;
// Be sure any previous timeouts are destroyed
- if($.prompt.timeout){
- clearTimeout($.prompt.timeout);
+ if(imp.timeout){
+ clearTimeout(imp.timeout);
}
- $.prompt.timeout = false;
+ imp.timeout = false;
- var opts = $.prompt.options,
+ var opts = imp.options,
$body = $(document.body),
$window = $(window);
//build the box and fade
- var msgbox = '<div class="'+ $.prompt.options.prefix +'box '+ opts.classes.box +'">';
+ 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 {
@@ -47,9 +47,9 @@
'</div>'+
'</div>';
- $.prompt.jqib = $(msgbox).appendTo($body);
- $.prompt.jqi = $.prompt.jqib.children('.'+ opts.prefix);//.data('jqi',opts);
- $.prompt.jqif = $.prompt.jqib.children('.'+ opts.prefix +'fade');
+ 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){
@@ -67,22 +67,22 @@
}
//build the states
- $.prompt.options.states = {};
+ imp.options.states = {};
var k,v;
for(k in message){
- v = $.extend({},$.prompt.defaults.state,{name:k},message[k]);
- $.prompt.addState(v.name, v);
+ v = $.extend({},imp.defaults.state,{name:k},message[k]);
+ imp.addState(v.name, v);
- if($.prompt.currentStateName === ''){
- $.prompt.currentStateName = v.name;
+ if(imp.currentStateName === ''){
+ imp.currentStateName = v.name;
}
}
//Events
- $.prompt.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
+ imp.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
var $t = $(this),
$state = $t.parents('.'+ opts.prefix +'state'),
- stateobj = $.prompt.options.states[$state.data('jqi-name')],
+ stateobj = imp.options.states[$state.data('jqi-name')],
msg = $state.children('.'+ opts.prefix +'message'),
clicked = stateobj.buttons[$t.text()] || stateobj.buttons[$t.html()],
forminputs = {};
@@ -97,7 +97,7 @@
}
//collect all form element values from all states.
- $.each($.prompt.jqi.children('form').serializeArray(),function(i,obj){
+ $.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') {
@@ -114,7 +114,7 @@
$state.trigger(promptsubmite, [clicked, msg, forminputs]);
if(!promptsubmite.isDefaultPrevented()){
- $.prompt.close(true, clicked,msg,forminputs);
+ imp.close(true, clicked,msg,forminputs);
}
});
@@ -122,23 +122,23 @@
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($.prompt.jqi.css('top').replace('px',''),10) - offset;
+ top = parseInt(imp.jqi.css('top').replace('px',''),10) - offset;
//$window.scrollTop(top);
$('html,body').animate({ scrollTop: top }, 'fast', function(){
var i = 0;
- $.prompt.jqib.addClass(opts.prefix +'warning');
+ imp.jqib.addClass(opts.prefix +'warning');
var intervalid = setInterval(function(){
- $.prompt.jqib.toggleClass(opts.prefix +'warning');
+ imp.jqib.toggleClass(opts.prefix +'warning');
if(i++ > 1){
clearInterval(intervalid);
- $.prompt.jqib.removeClass(opts.prefix +'warning');
+ imp.jqib.removeClass(opts.prefix +'warning');
}
}, 100);
});
}
else {
- $.prompt.close(true);
+ imp.close(true);
}
};
@@ -153,7 +153,7 @@
//enter key pressed trigger the default button if its not on it, ignore if it is a textarea
if(key === 13){
- var $defBtn = $.prompt.getCurrentState().find('.'+ opts.prefix +'defaultbutton');
+ var $defBtn = imp.getCurrentState().find('.'+ opts.prefix +'defaultbutton');
var $tgt = $(e.target);
if($tgt.is('textarea,.'+opts.prefix+'button') === false && $defBtn.length > 0){
@@ -164,7 +164,7 @@
//constrain tabs, tabs should iterate through the state and not leave
if (key === 9){
- var $inputels = $('input,select,textarea,button',$.prompt.getCurrentState());
+ 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) {
@@ -183,37 +183,37 @@
}
};
- $.prompt.position();
- $.prompt.style();
+ imp.position();
+ imp.style();
- $.prompt.jqif.click(fadeClicked);
- $window.resize({animate:false}, $.prompt.position);
- $.prompt.jqi.find('.'+ opts.prefix +'close').click($.prompt.close);
- $.prompt.jqib.on("keydown",keyDownEventHandler)
+ 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
- $.prompt.jqif[opts.show](opts.overlayspeed);
- $.prompt.jqi[opts.show](opts.promptspeed, function(){
+ imp.jqif[opts.show](opts.overlayspeed);
+ imp.jqi[opts.show](opts.promptspeed, function(){
- var $firstState = $.prompt.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
- $.prompt.goToState($firstState.data('jqi-name'));
+ var $firstState = imp.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
+ imp.goToState($firstState.data('jqi-name'));
- $.prompt.jqib.trigger('impromptu:loaded');
+ imp.jqib.trigger('impromptu:loaded');
});
// Timeout
if(opts.timeout > 0){
- $.prompt.timeout = setTimeout(function(){ $.prompt.close(true); },opts.timeout);
+ imp.timeout = setTimeout(function(){ imp.close(true); },opts.timeout);
}
- return $.prompt.jqib;
+ return imp.jqib;
};
- $.prompt.defaults = {
+ imp.defaults = {
prefix:'jqi',
classes: {
box: '',
@@ -282,21 +282,21 @@
* currentPrefix String - At any time this show be the prefix
* of the current prompt ex: "jqi"
*/
- $.prompt.currentPrefix = $.prompt.defaults.prefix;
+ imp.currentPrefix = imp.defaults.prefix;
/**
* currentStateName String - At any time this is the current state
* of the current prompt ex: "state0"
*/
- $.prompt.currentStateName = "";
+ imp.currentStateName = "";
/**
* setDefaults - Sets the default options
* @param o Object - Options to set as defaults
* @return void
*/
- $.prompt.setDefaults = function(o) {
- $.prompt.defaults = $.extend({}, $.prompt.defaults, o);
+ imp.setDefaults = function(o) {
+ imp.defaults = $.extend({}, imp.defaults, o);
};
/**
@@ -304,33 +304,33 @@
* @param o Object - Options to set as defaults
* @return void
*/
- $.prompt.setStateDefaults = function(o) {
- $.prompt.defaults.state = $.extend({}, $.prompt.defaults.state, o);
+ imp.setStateDefaults = function(o) {
+ imp.defaults.state = $.extend({}, imp.defaults.state, o);
};
/**
* position - Repositions the prompt (Used internally)
* @return void
*/
- $.prompt.position = function(e){
+ imp.position = function(e){
var restoreFx = $.fx.off,
- $state = $.prompt.getCurrentState(),
- stateObj = $.prompt.options.states[$state.data('jqi-name')],
+ $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) + ($.prompt.options.top.toString().indexOf('%') >= 0?
- (windowHeight*(parseInt($.prompt.options.top,10)/100)) : parseInt($.prompt.options.top,10));
+ 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;
}
- $.prompt.jqib.css({
+ imp.jqib.css({
position: "absolute",
height: height,
width: "100%",
@@ -339,7 +339,7 @@
right: 0,
bottom: 0
});
- $.prompt.jqif.css({
+ imp.jqif.css({
position: "fixed",
height: height,
width: "100%",
@@ -354,26 +354,26 @@
var offset = $(pos.container).offset();
if($.isPlainObject(offset) && offset.top !== undefined){
- $.prompt.jqi.css({
+ imp.jqi.css({
position: "absolute"
});
- $.prompt.jqi.animate({
+ 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) - ($.prompt.options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt($.prompt.options.top,10)/100)) : parseInt($.prompt.options.top,10));
+ 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(){});
}
}
// custom state width animation
else if(pos && pos.width){
- $.prompt.jqi.css({
+ imp.jqi.css({
position: "absolute",
left: '50%'
});
- $.prompt.jqi.animate({
+ imp.jqi.animate({
top: pos.y || top,
left: pos.x || '50%',
marginLeft: ((pos.width/2)*-1),
@@ -382,11 +382,11 @@
}
// standard prompt positioning
else{
- $.prompt.jqi.css({
+ imp.jqi.css({
position: "absolute",
top: top,
left: '50%',//$window.width()/2,
- marginLeft: (($.prompt.jqi.outerWidth(false)/2)*-1)
+ marginLeft: ((imp.jqi.outerWidth(false)/2)*-1)
});
}
@@ -400,18 +400,18 @@
* style - Restyles the prompt (Used internally)
* @return void
*/
- $.prompt.style = function(){
- $.prompt.jqif.css({
- zIndex: $.prompt.options.zIndex,
+ imp.style = function(){
+ imp.jqif.css({
+ zIndex: imp.options.zIndex,
display: "none",
- opacity: $.prompt.options.opacity
+ opacity: imp.options.opacity
});
- $.prompt.jqi.css({
- zIndex: $.prompt.options.zIndex+1,
+ imp.jqi.css({
+ zIndex: imp.options.zIndex+1,
display: "none"
});
- $.prompt.jqib.css({
- zIndex: $.prompt.options.zIndex
+ imp.jqib.css({
+ zIndex: imp.options.zIndex
});
};
@@ -419,8 +419,8 @@
* get - Get the prompt
* @return jQuery - the prompt
*/
- $.prompt.get = function(state) {
- return $('.'+ $.prompt.currentPrefix);
+ imp.get = function(state) {
+ return $('.'+ imp.currentPrefix);
};
/**
@@ -430,17 +430,17 @@
* @param afterState String - selector of the state to insert after
* @return jQuery - the newly created state
*/
- $.prompt.addState = function(statename, stateobj, afterState) {
+ imp.addState = function(statename, stateobj, afterState) {
var state = "",
$state = null,
arrow = "",
title = "",
- opts = $.prompt.options,
- $jqistates = $('.'+ $.prompt.currentPrefix +'states'),
+ opts = imp.options,
+ $jqistates = $('.'+ imp.currentPrefix +'states'),
buttons = [],
showHtml,defbtn,k,v,l,i=0;
- stateobj = $.extend({},$.prompt.defaults.state, {name:statename}, stateobj);
+ 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>';
@@ -474,9 +474,9 @@
// 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) ? ($.prompt.currentPrefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
+ defbtn = stateobj.focus === i || (isNaN(stateobj.focus) && stateobj.defaultButton === i) ? (imp.currentPrefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
- state += '<button class="'+ opts.classes.button +' '+ $.prompt.currentPrefix + 'button '+ defbtn;
+ state += '<button class="'+ opts.classes.button +' '+ imp.currentPrefix + 'button '+ defbtn;
if(typeof v.classes !== "undefined"){
state += ' '+ ($.isArray(v.classes)? v.classes.join(' ') : v.classes) + ' ';
@@ -492,13 +492,13 @@
$state.on('impromptu:submit', stateobj.submit);
if(afterState !== undefined){
- $jqistates.find('#'+ $.prompt.currentPrefix +'state_'+ afterState).after($state);
+ $jqistates.find('#'+ imp.currentPrefix +'state_'+ afterState).after($state);
}
else{
$jqistates.append($state);
}
- $.prompt.options.states[statename] = stateobj;
+ imp.options.states[statename] = stateobj;
return $state;
};
@@ -509,8 +509,8 @@
* @param newState String - Name of the state to transition to
* @return Boolean - returns true on success, false on failure
*/
- $.prompt.removeState = function(state, newState) {
- var $state = $.prompt.getState(state),
+ imp.removeState = function(state, newState) {
+ var $state = imp.getState(state),
rm = function(){ $state.remove(); };
if($state.length === 0){
@@ -519,17 +519,17 @@
// transition away from it before deleting
if($state.css('display') !== 'none'){
- if(newState !== undefined && $.prompt.getState(newState).length > 0){
- $.prompt.goToState(newState, false, rm);
+ if(newState !== undefined && imp.getState(newState).length > 0){
+ imp.goToState(newState, false, rm);
}
else if($state.next().length > 0){
- $.prompt.nextState(rm);
+ imp.nextState(rm);
}
else if($state.prev().length > 0){
- $.prompt.prevState(rm);
+ imp.prevState(rm);
}
else{
- $.prompt.close();
+ imp.close();
}
}
else{
@@ -544,27 +544,27 @@
* @param state String - Name of the state
* @return jQuery - the state
*/
- $.prompt.getState = function(state) {
- return $('#'+ $.prompt.currentPrefix +'state_'+ state);
+ imp.getState = function(state) {
+ return $('#'+ imp.currentPrefix +'state_'+ state);
};
- $.prompt.getStateContent = function(state) {
- return $.prompt.getState(state);
+ imp.getStateContent = function(state) {
+ return imp.getState(state);
};
/**
* getCurrentState - Get the current visible state
* @return jQuery - the current visible state
*/
- $.prompt.getCurrentState = function() {
- return $.prompt.getState($.prompt.getCurrentStateName());
+ imp.getCurrentState = function() {
+ return imp.getState(imp.getCurrentStateName());
};
/**
* getCurrentStateName - Get the name of the current visible state
* @return String - the current visible state's name
*/
- $.prompt.getCurrentStateName = function() {
- return $.prompt.currentStateName;
+ imp.getCurrentStateName = function() {
+ return imp.currentStateName;
};
/**
@@ -574,13 +574,13 @@
* @param callback Function - called when the transition is complete
* @return jQuery - the newly active state
*/
- $.prompt.goToState = function(state, subState, callback) {
- var $jqi = $.prompt.get(),
- jqiopts = $.prompt.options,
- $state = $.prompt.getState(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 = $.prompt.options;
+ opts = imp.options;
if(stateobj !== undefined){
@@ -596,30 +596,30 @@
subState = false;
}
- $.prompt.jqib.trigger(promptstatechanginge, [$.prompt.getCurrentStateName(), state]);
+ imp.jqib.trigger(promptstatechanginge, [imp.getCurrentStateName(), state]);
if(!promptstatechanginge.isDefaultPrevented() && $state.length > 0){
- $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'parentstate').removeClass($.prompt.currentPrefix +'parentstate');
+ imp.jqi.find('.'+ imp.currentPrefix +'parentstate').removeClass(imp.currentPrefix +'parentstate');
if(subState){ // hide any open substates
// get rid of any substates
- $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'substate').not($state)
+ imp.jqi.find('.'+ imp.currentPrefix +'substate').not($state)
.slideUp(jqiopts.promptspeed)
- .removeClass('.'+ $.prompt.currentPrefix +'substate')
- .find('.'+ $.prompt.currentPrefix +'arrow').hide();
+ .removeClass('.'+ imp.currentPrefix +'substate')
+ .find('.'+ imp.currentPrefix +'arrow').hide();
// add parent state class so it can be visible, but blocked
- $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'state:visible').addClass($.prompt.currentPrefix +'parentstate');
+ imp.jqi.find('.'+ imp.currentPrefix +'state:visible').addClass(imp.currentPrefix +'parentstate');
// add substate class so we know it will be smaller
- $state.addClass($.prompt.currentPrefix +'substate');
+ $state.addClass(imp.currentPrefix +'substate');
}
else{ // hide any open states
- $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'state').not($state)
+ imp.jqi.find('.'+ imp.currentPrefix +'state').not($state)
.slideUp(jqiopts.promptspeed)
- .find('.'+ $.prompt.currentPrefix +'arrow').hide();
+ .find('.'+ imp.currentPrefix +'arrow').hide();
}
- $.prompt.currentStateName = stateobj.name;
+ imp.currentStateName = stateobj.name;
$state.slideDown(jqiopts.promptspeed,function(){
var $t = $(this);
@@ -629,21 +629,21 @@
$t.find(stateobj.focus).eq(0).focus();
}
else{
- $t.find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
+ $t.find('.'+ imp.currentPrefix +'defaultbutton').focus();
}
- $t.find('.'+ $.prompt.currentPrefix +'arrow').show(jqiopts.promptspeed);
+ $t.find('.'+ imp.currentPrefix +'arrow').show(jqiopts.promptspeed);
if (typeof callback === 'function'){
- $.prompt.jqib.on('impromptu:statechanged', callback);
+ imp.jqib.on('impromptu:statechanged', callback);
}
- $.prompt.jqib.trigger('impromptu:statechanged', [state]);
+ imp.jqib.trigger('impromptu:statechanged', [state]);
if (typeof callback === 'function'){
- $.prompt.jqib.off('impromptu:statechanged', callback);
+ imp.jqib.off('impromptu:statechanged', callback);
}
});
if(!subState){
- $.prompt.position();
+ imp.position();
}
} // end isDefaultPrevented()
}// end stateobj !== undefined
@@ -656,10 +656,10 @@
* @param callback Function - called when the transition is complete
* @return jQuery - the newly active state
*/
- $.prompt.nextState = function(callback) {
- var $next = $('#'+ $.prompt.currentPrefix +'state_'+ $.prompt.getCurrentStateName()).next();
+ imp.nextState = function(callback) {
+ var $next = $('#'+ imp.currentPrefix +'state_'+ imp.getCurrentStateName()).next();
if($next.length > 0){
- $.prompt.goToState( $next.attr('id').replace($.prompt.currentPrefix +'state_',''), callback );
+ imp.goToState( $next.attr('id').replace(imp.currentPrefix +'state_',''), callback );
}
return $next;
};
@@ -669,10 +669,10 @@
* @param callback Function - called when the transition is complete
* @return jQuery - the newly active state
*/
- $.prompt.prevState = function(callback) {
- var $prev = $('#'+ $.prompt.currentPrefix +'state_'+ $.prompt.getCurrentStateName()).prev();
+ imp.prevState = function(callback) {
+ var $prev = $('#'+ imp.currentPrefix +'state_'+ imp.getCurrentStateName()).prev();
if($prev.length > 0){
- $.prompt.goToState( $prev.attr('id').replace($.prompt.currentPrefix +'state_',''), callback );
+ imp.goToState( $prev.attr('id').replace(imp.currentPrefix +'state_',''), callback );
}
return $prev;
};
@@ -685,38 +685,115 @@
* @param forvals Object - key/value pairs of all form field names and values (only used internally)
* @return jQuery - the newly active state
*/
- $.prompt.close = function(callCallback, clicked, msg, formvals){
- if($.prompt.timeout){
- clearTimeout($.prompt.timeout);
- $.prompt.timeout = false;
+ imp.close = function(callCallback, clicked, msg, formvals){
+ if(imp.timeout){
+ clearTimeout(imp.timeout);
+ imp.timeout = false;
}
- if($.prompt.jqib){
- $.prompt.jqib.fadeOut('fast',function(){
+ if(imp.jqib){
+ imp.jqib.fadeOut('fast',function(){
- $.prompt.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
+ imp.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
- $.prompt.jqib.remove();
+ imp.jqib.remove();
- $(window).off('resize',$.prompt.position);
+ $(window).off('resize',imp.position);
});
}
- $.prompt.currentStateName = "";
+ imp.currentStateName = "";
};
/**
- * Enable using $('.selector').prompt({});
- * This will grab the html within the prompt as the prompt message
+ * @var _ A utility object for internal use only.
*/
- $.fn.prompt = function(options){
- if(options === undefined){
- options = {};
- }
- if(options.withDataAndEvents === undefined){
- options.withDataAndEvents = false;
+ imp._ = {
+ /**
+ * query - query the document for elements
+ * @param selector String - css selector
+ * @param scope Element - Dom element to search from
+ * @return Class - selector class with properties to manipulate collection of elements
+ */
+ query: function(selector, scope){
+ // this returns a mini object with css, data, on event methods
+ var sel = function(selector, scope){
+ this.scope = scope || document;
+ this.els = this.scope.querySelectorAll(selector);
+ this.length = this.els.length;
+ return this;
+ };
+ sel.prototype.each = function(fn){
+ for(var i=0; i<this.length; i++){
+ fn.apply(this.els[i], [i, this.els[i]]);
+ }
+ return this;
+ };
+ sel.prototype.data = function(props){
+ if(typeof props === 'string' && this.els.length > 0){
+ return this.els[0].getAttribute('data-'+props);
+ }
+ return this.each(function(el, i){
+ for(var k in props){
+ if(props.hasOwnProperty(k)){
+ el.setAttribute('data-'+k, props[k]);
+ }
+ }
+ });
+ };
+ sel.prototype.css = function(props){
+ if(typeof props === 'string' && this.els.length > 0){
+ return this.els[0].style[props];
+ }
+ return this.each(function(el, i){
+ for(var k in props){
+ if(props.hasOwnProperty(k)){
+ el.style[k] = props[k];
+ }
+ }
+ });
+ }
+ return new sel(selector, scope);
+ },
+ extend: function(rec, o1){
+ var i,j,v,t,args = Array.prototype.slice.call(arguments);
+
+ for(i=2; i < args.length; i++){
+ for(j in args[i]){
+ if(args[i].hasOwnProperty(j)){
+ v = args[i][j];
+ t = Object.prototype.toString.call(v);
+ if(rec && t === '[object Object]' && t !== null){
+ o1[j] = imp._.extend(rec, o1[j], v);
+ }
+ else{
+ o1[j] = args[i][j];
+ }
+ }
+ }
+ }
+ return o1;
}
-
- $.prompt($(this).clone(options.withDataAndEvents).html(),options);
};
+ if($){
+ /**
+ * Enable using $('.selector').prompt({});
+ * This will grab the html within the prompt as the prompt message
+ */
+ $.fn.prompt = function(options){
+ if(options === undefined){
+ options = {};
+ }
+ if(options.withDataAndEvents === undefined){
+ options.withDataAndEvents = false;
+ }
+
+ imp($(this).clone(options.withDataAndEvents).html(),options);
+ };
+
+ $.prompt = imp;
+ }
+
+ window.Impromptu = imp;
+
}));
diff --git a/test/jquery-impromptu_spec.js b/test/jquery-impromptu_spec.js
index dd725bc..6127362 100644
--- a/test/jquery-impromptu_spec.js
+++ b/test/jquery-impromptu_spec.js
@@ -1,5 +1,118 @@
describe('jquery-impromptu', function() {
+ // ====================================================================================
+ // ====================================================================================
+ describe('utilities', function(){
+
+ // ====================================================================================
+ describe('extend', function(){
+
+ it('should extend an object non recursive', function(){
+ var o1 = { foo: 'foo', bar: 'bar' },
+ o2 = { foo: 'foo2' },
+ o3 = { foo: 'foo3', foobar: 'foobar' },
+ result = Impromptu._.extend(false, o1,o2,o3),
+ compare = { foo: 'foo3', bar: 'bar', foobar: 'foobar' };
+
+ expect(result).toEqual(compare);
+
+ o1 = { foo: { a:1, b:2 }, bar: [1,2,3,4,5] };
+ o2 = { foo: { a: -1, c: 3 } };
+ o3 = { bar: [9,8,7] };
+ result = Impromptu._.extend(false, o1,o2,o3);
+ compare = { foo: { a: -1, c: 3 }, bar: [9,8,7] };
+
+ expect(result).toEqual(compare);
+ });
+
+ it('should extend an object recursively', function(){
+ var o1 = { foo: 'foo', bar: 'bar' },
+ o2 = { foo: 'foo2' },
+ o3 = { foo: 'foo3', foobar: 'foobar' },
+ result = Impromptu._.extend(true, o1,o2,o3),
+ compare = { foo: 'foo3', bar: 'bar', foobar: 'foobar' };
+
+ expect(result).toEqual(compare);
+
+ o1 = { foo: { a:1, b:2 }, bar: [1,2,3,4,5] };
+ o2 = { foo: { a: -1, c: 3 } };
+ o3 = { bar: [9,8,7] };
+ result = Impromptu._.extend(true, o1,o2,o3);
+ compare = { foo: { a: -1, b: 2, c: 3 }, bar: [9,8,7] };
+
+ expect(result).toEqual(compare);
+ });
+ });
+
+ describe('query', function(){
+ beforeEach(function(){
+ $('body').append('<div id="dummyels" style="display:none">'+
+ '<form action="" method="post" class="dummyels-form">'+
+ '<label class="field"><input type="text" name="test1" value="test val 1"></label>'+
+ '<label class="field"><input type="password" name="test2" value="test val 2"></label>'+
+ '<label class="field"><input type="file" name="test3"></label>'+
+ '<label class="field"><input type="checkbox" name="test4" value="test val 4 - a"></label>'+
+ '<label class="field"><input type="checkbox" name="test4" value="test val 4 - b" checked></label>'+
+ '<label class="field"><input type="checkbox" name="test4" value="test val 4 - c"></label>'+
+ '<label class="field"><input type="checkbox" name="test4" value="test val 4 - d" checked></label>'+
+ '<label class="field"><input type="checkbox" name="test4" value="test val 4 - e"></label>'+
+ '<label class="field"><input type="radio" name="test5" value="test val 5 - a"></label>'+
+ '<label class="field"><input type="radio" name="test5" value="test val 5 - b" checked></label>'+
+ '<label class="field"><input type="radio" name="test5" value="test val 5 - c"></label>'+
+ '<label class="field"><textarea name="test6">test val 6</textarea></label>'+
+ '<label class="field">'+
+ '<select name="test7">'+
+ '<option value="test val 7 - a">'+
+ '<option value="test val 7 - b">'+
+ '<option value="test val 7 - c" selected>'+
+ '</select>'+
+ '</label>'+
+ '<label class="field">'+
+ '<select name="test8" multiple>'+
+ '<option value="test val 8 - a">'+
+ '<option value="test val 8 - b" selected>'+
+ '<option value="test val 8 - c" selected>'+
+ '</select>'+
+ '</label>'+
+ '</form>'+
+ '</div>');
+ });
+ afterEach(function(){
+ $('#dummyels').remove();
+ });
+
+ describe('find', function(){
+
+ it('should find elements', function(){
+ var result = Impromptu._.query('#dummyels'),
+ compare = 1;
+
+ expect(true).toBeDefined();
+ expect(result.length).toHaveLength(1);
+ });
+ });
+
+ describe('each', function(){
+
+ it('return a result set', function(){
+ var result = 0,
+ compare = 14,
+ els = Impromptu._.query('#dummyels form label').each(function(i,el){ result++; });
+
+ expect(els.length).toHaveLength(1);
+ });
+
+ it('should loop every over every found element', function(){
+ var result = 0,
+ compare = 14,
+ els = Impromptu._.query('#dummyels form label').each(function(i,el){ result++; });
+
+ expect(result).toBe(compare);
+ });
+ });
+ });
+
+ });
// ====================================================================================
// ====================================================================================