forked from trentrichardson/jQuery-Impromptu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-impromptu.js
372 lines (315 loc) · 11 KB
/
jquery-impromptu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* jQuery Impromptu
* By: Trent Richardson [http://trentrichardson.com]
* Version 3.2
* Last Modified: 10/31/2011
*
* Copyright 2011 Trent Richardson
* Dual licensed under the MIT and GPL licenses.
* http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
* http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
*
*/
(function($) {
$.prompt = function(message, options) {
options = $.extend({},$.prompt.defaults,options);
$.prompt.currentPrefix = options.prefix;
var ie6 = ($.browser.msie && $.browser.version < 7);
var $body = $(document.body);
var $window = $(window);
options.classes = $.trim(options.classes);
if(options.classes != '')
options.classes = ' '+ options.classes;
//build the box and fade
var msgbox = '<div class="'+ options.prefix +'box'+ options.classes +'" id="'+ options.prefix +'box">';
if(options.useiframe && (($('object, applet').length > 0) || ie6)) {
msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ options.prefix +'fade" id="'+ options.prefix +'fade"></iframe>';
} else {
if(ie6) {
$('select').css('visibility','hidden');
}
msgbox +='<div class="'+ options.prefix +'fade" id="'+ options.prefix +'fade"></div>';
}
msgbox += '<div class="'+ options.prefix +'" id="'+ options.prefix +'"><div class="'+ options.prefix +'container"><div class="';
msgbox += options.prefix +'close">X</div><div id="'+ options.prefix +'states"></div>';
msgbox += '</div></div></div>';
var $jqib = $(msgbox).appendTo($body);
var $jqi = $jqib.children('#'+ options.prefix);
var $jqif = $jqib.children('#'+ options.prefix +'fade');
//if a string was passed, convert to a single state
if(message.constructor == String){
message = {
state0: {
html: message,
buttons: options.buttons,
focus: options.focus,
submit: options.submit
}
};
}
//build the states
var states = "";
$.each(message,function(statename,stateobj){
stateobj = $.extend({},$.prompt.defaults.state,stateobj);
message[statename] = stateobj;
states += '<div id="'+ options.prefix +'_state_'+ statename +'" class="'+ options.prefix + '_state" style="display:none;"><div class="'+ options.prefix +'message">' + stateobj.html +'</div><div class="'+ options.prefix +'buttons">';
$.each(stateobj.buttons, function(k, v){
if(typeof v == 'object')
states += '<button name="' + options.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" id="' + options.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
else states += '<button name="' + options.prefix + '_' + statename + '_button' + k + '" id="' + options.prefix + '_' + statename + '_button' + k + '" value="' + v + '">' + k + '</button>';
});
states += '</div></div>';
});
//insert the states...
$jqi.find('#'+ options.prefix +'states').html(states).children('.'+ options.prefix +'_state:first').css('display','block');
$jqi.find('.'+ options.prefix +'buttons:empty').css('display','none');
//Events
$.each(message,function(statename,stateobj){
var $state = $jqi.find('#'+ options.prefix +'_state_'+ statename);
$state.children('.'+ options.prefix +'buttons').children('button').click(function(){
var msg = $state.children('.'+ options.prefix +'message');
var clicked = stateobj.buttons[$(this).text()];
if(clicked == undefined){
for(var i in stateobj.buttons)
if(stateobj.buttons[i].title == $(this).text())
clicked = stateobj.buttons[i].value;
}
if(typeof clicked == 'object')
clicked = clicked.value;
var forminputs = {};
//collect all form element values from all states
$.each($jqi.find('#'+ options.prefix +'states :input').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];
}
});
var close = stateobj.submit(clicked,msg,forminputs);
if(close === undefined || close) {
removePrompt(true,clicked,msg,forminputs);
}
});
$state.find('.'+ options.prefix +'buttons button:eq('+ stateobj.focus +')').addClass(options.prefix +'defaultbutton');
});
var fadeClicked = function(){
if(options.persistent){
var offset = (options.top.toString().indexOf('%') >= 0? ($window.height()*(parseInt(options.top,10)/100)) : parseInt(options.top,10)),
top = parseInt($jqi.css('top').replace('px',''),10) - offset;
//$window.scrollTop(top);
$('html,body').animate({ scrollTop: top }, 'fast', function(){
var i = 0;
$jqib.addClass(options.prefix +'warning');
var intervalid = setInterval(function(){
$jqib.toggleClass(options.prefix +'warning');
if(i++ > 1){
clearInterval(intervalid);
$jqib.removeClass(options.prefix +'warning');
}
}, 100);
});
}
else {
removePrompt();
}
};
var keyPressEventHandler = function(e){
var key = (window.event) ? event.keyCode : e.keyCode; // MSIE or Firefox?
//escape key closes
if(key==27) {
fadeClicked();
}
//constrain tabs
if (key == 9){
var $inputels = $(':input:enabled:visible',$jqib);
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;
}
}
};
var positionPrompt = function(){
var bodyHeight = $body.outerHeight(true),
windowHeight = $window.height(),
documentHeight = $(document).height(),
height = bodyHeight > windowHeight ? bodyHeight : windowHeight,
top = parseInt($window.scrollTop(),10) + (options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt(options.top,10)/100)) : parseInt(options.top,10));
height = height > documentHeight? height : documentHeight;
$jqib.css({
position: "absolute",
height: height,
width: "100%",
top: 0,
left: 0,
right: 0,
bottom: 0
});
$jqif.css({
position: "absolute",
height: height,
width: "100%",
top: 0,
left: 0,
right: 0,
bottom: 0
});
$jqi.css({
position: "absolute",
top: top,
left: "50%",
marginLeft: (($jqi.outerWidth()/2)*-1)
});
};
var stylePrompt = function(){
$jqif.css({
zIndex: options.zIndex,
display: "none",
opacity: options.opacity
});
$jqi.css({
zIndex: options.zIndex+1,
display: "none"
});
$jqib.css({
zIndex: options.zIndex
});
};
var removePrompt = function(callCallback, clicked, msg, formvals){
$jqi.remove();
$window.unbind('resize',positionPrompt);
$jqif.fadeOut(options.overlayspeed,function(){
$jqif.unbind('click',fadeClicked);
$jqif.remove();
if(callCallback) {
options.callback(clicked,msg,formvals);
}
$jqib.unbind('keypress',keyPressEventHandler);
$jqib.remove();
if(ie6 && !options.useiframe) {
$('select').css('visibility','visible');
}
});
};
positionPrompt();
stylePrompt();
$jqif.click(fadeClicked);
$window.resize(positionPrompt);
$jqib.bind("keydown keypress",keyPressEventHandler);
$jqi.find('.'+ options.prefix +'close').click(removePrompt);
//Show it
$jqif.fadeIn(options.overlayspeed);
$jqi[options.show](options.promptspeed,options.loaded);
$jqi.find('#'+ options.prefix +'states .'+ options.prefix +'_state:first .'+ options.prefix +'defaultbutton').focus();
if(options.timeout > 0)
setTimeout($.prompt.close,options.timeout);
return $jqib;
};
$.prompt.defaults = {
prefix:'jqi',
classes: '',
buttons: {
Ok: true
},
loaded: function(){
},
submit: function(){
return true;
},
callback: function(){
},
opacity: 0.6,
zIndex: 999,
overlayspeed: 'slow',
promptspeed: 'fast',
show: 'promptDropIn',
focus: 0,
useiframe: false,
top: '15%',
persistent: true,
timeout: 0,
state: {
html: '',
buttons: {
Ok: true
},
focus: 0,
submit: function(){
return true;
}
}
};
$.prompt.currentPrefix = $.prompt.defaults.prefix;
$.prompt.setDefaults = function(o) {
$.prompt.defaults = $.extend({}, $.prompt.defaults, o);
};
$.prompt.setStateDefaults = function(o) {
$.prompt.defaults.state = $.extend({}, $.prompt.defaults.state, o);
};
$.prompt.getStateContent = function(state) {
return $('#'+ $.prompt.currentPrefix +'_state_'+ state);
};
$.prompt.getCurrentState = function() {
return $('.'+ $.prompt.currentPrefix +'_state:visible');
};
$.prompt.getCurrentStateName = function() {
var stateid = $.prompt.getCurrentState().attr('id');
return stateid.replace($.prompt.currentPrefix +'_state_','');
};
$.prompt.goToState = function(state, callback) {
$('.'+ $.prompt.currentPrefix +'_state').slideUp('slow');
$('#'+ $.prompt.currentPrefix +'_state_'+ state).slideDown('slow',function(){
$(this).find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
if (typeof callback == 'function')
callback();
});
};
$.prompt.nextState = function(callback) {
var $next = $('.'+ $.prompt.currentPrefix +'_state:visible').next();
$('.'+ $.prompt.currentPrefix +'_state').slideUp('slow');
$next.slideDown('slow',function(){
$next.find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
if (typeof callback == 'function')
callback();
});
};
$.prompt.prevState = function(callback) {
var $next = $('.'+ $.prompt.currentPrefix +'_state:visible').prev();
$('.'+ $.prompt.currentPrefix +'_state').slideUp('slow');
$next.slideDown('slow',function(){
$next.find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
if (typeof callback == 'function')
callback();
});
};
$.prompt.close = function() {
$('#'+ $.prompt.currentPrefix +'box').fadeOut('fast',function(){
$(this).remove();
});
};
$.fn.extend({
prompt: function(options){
if(options == undefined)
options = {};
if(options.withDataAndEvents == undefined)
options.withDataAndEvents = false;
$.prompt($(this).clone(options.withDataAndEvents).html(),options);
},
promptDropIn: function(speed, callback){
var $t = $(this);
if($t.css("display") == "none"){
var eltop = $t.css('top');
$t.css({ top: $(window).scrollTop(), display: 'block' }).animate({ top: eltop },speed,'swing',callback);
}
}
});
})(jQuery);