|
@@ -0,0 +1,863 @@
|
|
1
|
+/*! jQuery-Impromptu - v6.0.0 - 2014-12-27
|
|
2
|
+* http://trentrichardson.com/Impromptu
|
|
3
|
+* Copyright (c) 2014 Trent Richardson; Licensed MIT */
|
|
4
|
+(function(root, factory) {
|
|
5
|
+ if (typeof define === 'function' && define.amd) {
|
|
6
|
+ define(['jquery'], factory);
|
|
7
|
+ } else {
|
|
8
|
+ factory(root.jQuery);
|
|
9
|
+ }
|
|
10
|
+}(this, function($) {
|
|
11
|
+ 'use strict';
|
|
12
|
+
|
|
13
|
+ // ########################################################################
|
|
14
|
+ // Base object
|
|
15
|
+ // ########################################################################
|
|
16
|
+
|
|
17
|
+ /**
|
|
18
|
+ * Imp - Impromptu object - passing no params will not open, only return the instance
|
|
19
|
+ * @param message String/Object - String of html or Object of states
|
|
20
|
+ * @param options Object - Options to set the prompt
|
|
21
|
+ * @return Imp - the instance of this Impromptu object
|
|
22
|
+ */
|
|
23
|
+ var Imp = function(message, options){
|
|
24
|
+ var t = this;
|
|
25
|
+ t.id = Imp.count++;
|
|
26
|
+
|
|
27
|
+ Imp.lifo.push(t);
|
|
28
|
+
|
|
29
|
+ if(message){
|
|
30
|
+ t.open(message, options);
|
|
31
|
+ }
|
|
32
|
+ return t;
|
|
33
|
+ };
|
|
34
|
+
|
|
35
|
+ // ########################################################################
|
|
36
|
+ // static properties and methods
|
|
37
|
+ // ########################################################################
|
|
38
|
+
|
|
39
|
+ /**
|
|
40
|
+ * defaults - the default options
|
|
41
|
+ */
|
|
42
|
+ Imp.defaults = {
|
|
43
|
+ prefix:'jqi',
|
|
44
|
+ classes: {
|
|
45
|
+ box: '',
|
|
46
|
+ fade: '',
|
|
47
|
+ prompt: '',
|
|
48
|
+ form: '',
|
|
49
|
+ close: '',
|
|
50
|
+ title: '',
|
|
51
|
+ message: '',
|
|
52
|
+ buttons: '',
|
|
53
|
+ button: '',
|
|
54
|
+ defaultButton: ''
|
|
55
|
+ },
|
|
56
|
+ title: '',
|
|
57
|
+ closeText: '×',
|
|
58
|
+ buttons: {
|
|
59
|
+ Ok: true
|
|
60
|
+ },
|
|
61
|
+ loaded: function(e){},
|
|
62
|
+ submit: function(e,v,m,f){},
|
|
63
|
+ close: function(e,v,m,f){},
|
|
64
|
+ statechanging: function(e, from, to){},
|
|
65
|
+ statechanged: function(e, to){},
|
|
66
|
+ opacity: 0.6,
|
|
67
|
+ zIndex: 999,
|
|
68
|
+ overlayspeed: 'slow',
|
|
69
|
+ promptspeed: 'fast',
|
|
70
|
+ show: 'fadeIn',
|
|
71
|
+ hide: 'fadeOut',
|
|
72
|
+ focus: 0,
|
|
73
|
+ defaultButton: 0,
|
|
74
|
+ useiframe: false,
|
|
75
|
+ top: '15%',
|
|
76
|
+ position: {
|
|
77
|
+ container: null,
|
|
78
|
+ x: null,
|
|
79
|
+ y: null,
|
|
80
|
+ arrow: null,
|
|
81
|
+ width: null
|
|
82
|
+ },
|
|
83
|
+ persistent: true,
|
|
84
|
+ timeout: 0,
|
|
85
|
+ states: {},
|
|
86
|
+ state: {
|
|
87
|
+ name: null,
|
|
88
|
+ title: '',
|
|
89
|
+ html: '',
|
|
90
|
+ buttons: {
|
|
91
|
+ Ok: true
|
|
92
|
+ },
|
|
93
|
+ focus: 0,
|
|
94
|
+ defaultButton: 0,
|
|
95
|
+ position: {
|
|
96
|
+ container: null,
|
|
97
|
+ x: null,
|
|
98
|
+ y: null,
|
|
99
|
+ arrow: null,
|
|
100
|
+ width: null
|
|
101
|
+ },
|
|
102
|
+ submit: function(e,v,m,f){
|
|
103
|
+ return true;
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ };
|
|
107
|
+
|
|
108
|
+ /**
|
|
109
|
+ * setDefaults - Sets the default options
|
|
110
|
+ * @param o Object - Options to set as defaults
|
|
111
|
+ * @return void
|
|
112
|
+ */
|
|
113
|
+ Imp.setDefaults = function(o) {
|
|
114
|
+ Imp.defaults = $.extend({}, Imp.defaults, o);
|
|
115
|
+ };
|
|
116
|
+
|
|
117
|
+ /**
|
|
118
|
+ * setStateDefaults - Sets the default options for a state
|
|
119
|
+ * @param o Object - Options to set as defaults
|
|
120
|
+ * @return void
|
|
121
|
+ */
|
|
122
|
+ Imp.setStateDefaults = function(o) {
|
|
123
|
+ Imp.defaults.state = $.extend({}, Imp.defaults.state, o);
|
|
124
|
+ };
|
|
125
|
+
|
|
126
|
+ /**
|
|
127
|
+ * @var Int - A counter used to provide a unique ID for new prompts
|
|
128
|
+ */
|
|
129
|
+ Imp.count = 0;
|
|
130
|
+
|
|
131
|
+ /**
|
|
132
|
+ * @var Array - An array of Impromptu intances in a LIFO queue (last in first out)
|
|
133
|
+ */
|
|
134
|
+ Imp.lifo = [];
|
|
135
|
+
|
|
136
|
+ /**
|
|
137
|
+ * getLast - get the last element from the queue (doesn't pop, just returns)
|
|
138
|
+ * @return Imp - the instance of this Impromptu object or false if queue is empty
|
|
139
|
+ */
|
|
140
|
+ Imp.getLast = function(){
|
|
141
|
+ var l = Imp.lifo.length;
|
|
142
|
+ return (l > 0)? Imp.lifo[l-1] : false;
|
|
143
|
+ };
|
|
144
|
+
|
|
145
|
+ /**
|
|
146
|
+ * removeFromStack - remove an element from the lifo stack by its id
|
|
147
|
+ * @param id int - id of the instance to remove
|
|
148
|
+ * @return api - The api of the element removed from the stack or void
|
|
149
|
+ */
|
|
150
|
+ Imp.removeFromStack = function(id){
|
|
151
|
+ for(var i=Imp.lifo.length-1; i>=0; i--){
|
|
152
|
+ if(Imp.lifo[i].id === id){
|
|
153
|
+ return Imp.lifo.splice(i,1)[0];
|
|
154
|
+ }
|
|
155
|
+ }
|
|
156
|
+ };
|
|
157
|
+
|
|
158
|
+ // ########################################################################
|
|
159
|
+ // extend our object instance properties and methods
|
|
160
|
+ // ########################################################################
|
|
161
|
+ Imp.prototype = {
|
|
162
|
+
|
|
163
|
+ /**
|
|
164
|
+ * @var Int - A unique id, simply an autoincremented number
|
|
165
|
+ */
|
|
166
|
+ id: null,
|
|
167
|
+
|
|
168
|
+ /**
|
|
169
|
+ * open - Opens the prompt
|
|
170
|
+ * @param message String/Object - String of html or Object of states
|
|
171
|
+ * @param options Object - Options to set the prompt
|
|
172
|
+ * @return Imp - the instance of this Impromptu object
|
|
173
|
+ */
|
|
174
|
+ open: function(message, options) {
|
|
175
|
+ var t = this;
|
|
176
|
+
|
|
177
|
+ t.options = $.extend({},Imp.defaults,options);
|
|
178
|
+
|
|
179
|
+ // Be sure any previous timeouts are destroyed
|
|
180
|
+ if(t.timeout){
|
|
181
|
+ clearTimeout(t.timeout);
|
|
182
|
+ }
|
|
183
|
+ t.timeout = false;
|
|
184
|
+
|
|
185
|
+ var opts = t.options,
|
|
186
|
+ $body = $(document.body),
|
|
187
|
+ $window = $(window);
|
|
188
|
+
|
|
189
|
+ //build the box and fade
|
|
190
|
+ var msgbox = '<div class="'+ opts.prefix +'box '+ opts.classes.box +'">';
|
|
191
|
+ if(opts.useiframe && ($('object, applet').length > 0)) {
|
|
192
|
+ msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></iframe>';
|
|
193
|
+ } else {
|
|
194
|
+ msgbox += '<div class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></div>';
|
|
195
|
+ }
|
|
196
|
+ msgbox += '<div class="'+ opts.prefix +' '+ opts.classes.prompt +'">'+
|
|
197
|
+ '<form action="javascript:false;" onsubmit="return false;" class="'+ opts.prefix +'form '+ opts.classes.form +'">'+
|
|
198
|
+ '<div class="'+ opts.prefix +'close '+ opts.classes.close +'">'+ opts.closeText +'</div>'+
|
|
199
|
+ '<div class="'+ opts.prefix +'states"></div>'+
|
|
200
|
+ '</form>'+
|
|
201
|
+ '</div>'+
|
|
202
|
+ '</div>';
|
|
203
|
+
|
|
204
|
+ t.jqib = $(msgbox).appendTo($body);
|
|
205
|
+ t.jqi = t.jqib.children('.'+ opts.prefix);
|
|
206
|
+ t.jqif = t.jqib.children('.'+ opts.prefix +'fade');
|
|
207
|
+
|
|
208
|
+ //if a string was passed, convert to a single state
|
|
209
|
+ if(message.constructor === String){
|
|
210
|
+ message = {
|
|
211
|
+ state0: {
|
|
212
|
+ title: opts.title,
|
|
213
|
+ html: message,
|
|
214
|
+ buttons: opts.buttons,
|
|
215
|
+ position: opts.position,
|
|
216
|
+ focus: opts.focus,
|
|
217
|
+ defaultButton: opts.defaultButton,
|
|
218
|
+ submit: opts.submit
|
|
219
|
+ }
|
|
220
|
+ };
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ //build the states
|
|
224
|
+ t.options.states = {};
|
|
225
|
+ var k,v;
|
|
226
|
+ for(k in message){
|
|
227
|
+ v = $.extend({},Imp.defaults.state,{name:k},message[k]);
|
|
228
|
+ t.addState(v.name, v);
|
|
229
|
+
|
|
230
|
+ if(t.currentStateName === ''){
|
|
231
|
+ t.currentStateName = v.name;
|
|
232
|
+ }
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+ //Events
|
|
236
|
+ t.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
|
|
237
|
+ var $t = $(this),
|
|
238
|
+ $state = $t.parents('.'+ opts.prefix +'state'),
|
|
239
|
+ stateobj = t.options.states[$state.data('jqi-name')],
|
|
240
|
+ msg = $state.children('.'+ opts.prefix +'message'),
|
|
241
|
+ clicked = stateobj.buttons[$t.text()] || stateobj.buttons[$t.html()],
|
|
242
|
+ forminputs = {};
|
|
243
|
+
|
|
244
|
+ // if for some reason we couldn't get the value
|
|
245
|
+ if(clicked === undefined){
|
|
246
|
+ for(var i in stateobj.buttons){
|
|
247
|
+ if(stateobj.buttons[i].title === $t.text() || stateobj.buttons[i].title === $t.html()){
|
|
248
|
+ clicked = stateobj.buttons[i].value;
|
|
249
|
+ }
|
|
250
|
+ }
|
|
251
|
+ }
|
|
252
|
+
|
|
253
|
+ //collect all form element values from all states.
|
|
254
|
+ $.each(t.jqi.children('form').serializeArray(),function(i,obj){
|
|
255
|
+ if (forminputs[obj.name] === undefined) {
|
|
256
|
+ forminputs[obj.name] = obj.value;
|
|
257
|
+ } else if (typeof forminputs[obj.name] === Array || typeof forminputs[obj.name] === 'object') {
|
|
258
|
+ forminputs[obj.name].push(obj.value);
|
|
259
|
+ } else {
|
|
260
|
+ forminputs[obj.name] = [forminputs[obj.name],obj.value];
|
|
261
|
+ }
|
|
262
|
+ });
|
|
263
|
+
|
|
264
|
+ // trigger an event
|
|
265
|
+ var promptsubmite = new $.Event('impromptu:submit');
|
|
266
|
+ promptsubmite.stateName = stateobj.name;
|
|
267
|
+ promptsubmite.state = $state;
|
|
268
|
+ $state.trigger(promptsubmite, [clicked, msg, forminputs]);
|
|
269
|
+
|
|
270
|
+ if(!promptsubmite.isDefaultPrevented()){
|
|
271
|
+ t.close(true, clicked,msg,forminputs);
|
|
272
|
+ }
|
|
273
|
+ });
|
|
274
|
+
|
|
275
|
+ // if the fade is clicked blink the prompt
|
|
276
|
+ var fadeClicked = function(){
|
|
277
|
+ if(opts.persistent){
|
|
278
|
+ var offset = (opts.top.toString().indexOf('%') >= 0? ($window.height()*(parseInt(opts.top,10)/100)) : parseInt(opts.top,10)),
|
|
279
|
+ top = parseInt(t.jqi.css('top').replace('px',''),10) - offset;
|
|
280
|
+
|
|
281
|
+ //$window.scrollTop(top);
|
|
282
|
+ $('html,body').animate({ scrollTop: top }, 'fast', function(){
|
|
283
|
+ var i = 0;
|
|
284
|
+ t.jqib.addClass(opts.prefix +'warning');
|
|
285
|
+ var intervalid = setInterval(function(){
|
|
286
|
+ t.jqib.toggleClass(opts.prefix +'warning');
|
|
287
|
+ if(i++ > 1){
|
|
288
|
+ clearInterval(intervalid);
|
|
289
|
+ t.jqib.removeClass(opts.prefix +'warning');
|
|
290
|
+ }
|
|
291
|
+ }, 100);
|
|
292
|
+ });
|
|
293
|
+ }
|
|
294
|
+ else {
|
|
295
|
+ t.close(true);
|
|
296
|
+ }
|
|
297
|
+ };
|
|
298
|
+
|
|
299
|
+ // listen for esc or tab keys
|
|
300
|
+ var keyDownEventHandler = function(e){
|
|
301
|
+ var key = (window.event) ? event.keyCode : e.keyCode;
|
|
302
|
+
|
|
303
|
+ //escape key closes
|
|
304
|
+ if(key === 27) {
|
|
305
|
+ fadeClicked();
|
|
306
|
+ }
|
|
307
|
+
|
|
308
|
+ //enter key pressed trigger the default button if its not on it, ignore if it is a textarea
|
|
309
|
+ if(key === 13){
|
|
310
|
+ var $defBtn = t.getCurrentState().find('.'+ opts.prefix +'defaultbutton');
|
|
311
|
+ var $tgt = $(e.target);
|
|
312
|
+
|
|
313
|
+ if($tgt.is('textarea,.'+opts.prefix+'button') === false && $defBtn.length > 0){
|
|
314
|
+ e.preventDefault();
|
|
315
|
+ $defBtn.click();
|
|
316
|
+ }
|
|
317
|
+ }
|
|
318
|
+
|
|
319
|
+ //constrain tabs, tabs should iterate through the state and not leave
|
|
320
|
+ if (key === 9){
|
|
321
|
+ var $inputels = $('input,select,textarea,button',t.getCurrentState());
|
|
322
|
+ var fwd = !e.shiftKey && e.target === $inputels[$inputels.length-1];
|
|
323
|
+ var back = e.shiftKey && e.target === $inputels[0];
|
|
324
|
+ if (fwd || back) {
|
|
325
|
+ setTimeout(function(){
|
|
326
|
+ if (!$inputels){
|
|
327
|
+ return;
|
|
328
|
+ }
|
|
329
|
+ var el = $inputels[back===true ? $inputels.length-1 : 0];
|
|
330
|
+
|
|
331
|
+ if (el){
|
|
332
|
+ el.focus();
|
|
333
|
+ }
|
|
334
|
+ },10);
|
|
335
|
+ return false;
|
|
336
|
+ }
|
|
337
|
+ }
|
|
338
|
+ };
|
|
339
|
+
|
|
340
|
+ t.position();
|
|
341
|
+ t.style();
|
|
342
|
+
|
|
343
|
+ // store copy of the window resize function for interal use only
|
|
344
|
+ t._windowResize = function(e){
|
|
345
|
+ t.position(e);
|
|
346
|
+ };
|
|
347
|
+ $window.resize({ animate: false }, t._windowResize);
|
|
348
|
+
|
|
349
|
+ t.jqif.click(fadeClicked);
|
|
350
|
+ t.jqi.find('.'+ opts.prefix +'close').click(function(){ t.close(); });
|
|
351
|
+ t.jqib.on("keydown",keyDownEventHandler)
|
|
352
|
+ .on('impromptu:loaded', opts.loaded)
|
|
353
|
+ .on('impromptu:close', opts.close)
|
|
354
|
+ .on('impromptu:statechanging', opts.statechanging)
|
|
355
|
+ .on('impromptu:statechanged', opts.statechanged);
|
|
356
|
+
|
|
357
|
+ // Show it
|
|
358
|
+ t.jqif[opts.show](opts.overlayspeed);
|
|
359
|
+ t.jqi[opts.show](opts.promptspeed, function(){
|
|
360
|
+
|
|
361
|
+ var $firstState = t.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
|
|
362
|
+ t.goToState($firstState.data('jqi-name'));
|
|
363
|
+
|
|
364
|
+ t.jqib.trigger('impromptu:loaded');
|
|
365
|
+ });
|
|
366
|
+
|
|
367
|
+ // Timeout
|
|
368
|
+ if(opts.timeout > 0){
|
|
369
|
+ t.timeout = setTimeout(function(){ t.close(true); },opts.timeout);
|
|
370
|
+ }
|
|
371
|
+
|
|
372
|
+ return t;
|
|
373
|
+ },
|
|
374
|
+
|
|
375
|
+ /**
|
|
376
|
+ * close - Closes the prompt
|
|
377
|
+ * @param callback Function - called when the transition is complete
|
|
378
|
+ * @param clicked String - value of the button clicked (only used internally)
|
|
379
|
+ * @param msg jQuery - The state message body (only used internally)
|
|
380
|
+ * @param forvals Object - key/value pairs of all form field names and values (only used internally)
|
|
381
|
+ * @return Imp - the instance of this Impromptu object
|
|
382
|
+ */
|
|
383
|
+ close: function(callCallback, clicked, msg, formvals){
|
|
384
|
+ var t = this;
|
|
385
|
+ Imp.removeFromStack(t.id);
|
|
386
|
+
|
|
387
|
+ if(t.timeout){
|
|
388
|
+ clearTimeout(t.timeout);
|
|
389
|
+ t.timeout = false;
|
|
390
|
+ }
|
|
391
|
+
|
|
392
|
+ if(t.jqib){
|
|
393
|
+ t.jqib[t.options.hide]('fast',function(){
|
|
394
|
+
|
|
395
|
+ t.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
|
|
396
|
+
|
|
397
|
+ t.jqib.remove();
|
|
398
|
+
|
|
399
|
+ $(window).off('resize', t._windowResize);
|
|
400
|
+
|
|
401
|
+ if(typeof callCallback === 'function'){
|
|
402
|
+ callCallback();
|
|
403
|
+ }
|
|
404
|
+ });
|
|
405
|
+ }
|
|
406
|
+ t.currentStateName = "";
|
|
407
|
+
|
|
408
|
+ return t;
|
|
409
|
+ },
|
|
410
|
+
|
|
411
|
+ /**
|
|
412
|
+ * addState - Injects a state into the prompt
|
|
413
|
+ * @param statename String - Name of the state
|
|
414
|
+ * @param stateobj Object - options for the state
|
|
415
|
+ * @param afterState String - selector of the state to insert after
|
|
416
|
+ * @return jQuery - the newly created state
|
|
417
|
+ */
|
|
418
|
+ addState: function(statename, stateobj, afterState) {
|
|
419
|
+ var t = this,
|
|
420
|
+ state = '',
|
|
421
|
+ $state = null,
|
|
422
|
+ arrow = '',
|
|
423
|
+ title = '',
|
|
424
|
+ opts = t.options,
|
|
425
|
+ $jqistates = $('.'+ opts.prefix +'states'),
|
|
426
|
+ buttons = [],
|
|
427
|
+ showHtml,defbtn,k,v,l,i=0;
|
|
428
|
+
|
|
429
|
+ stateobj = $.extend({},Imp.defaults.state, {name:statename}, stateobj);
|
|
430
|
+
|
|
431
|
+ if(stateobj.position.arrow !== null){
|
|
432
|
+ arrow = '<div class="'+ opts.prefix + 'arrow '+ opts.prefix + 'arrow'+ stateobj.position.arrow +'"></div>';
|
|
433
|
+ }
|
|
434
|
+ if(stateobj.title && stateobj.title !== ''){
|
|
435
|
+ title = '<div class="lead '+ opts.prefix + 'title '+ opts.classes.title +'">'+ stateobj.title +'</div>';
|
|
436
|
+ }
|
|
437
|
+
|
|
438
|
+ showHtml = stateobj.html;
|
|
439
|
+ if (typeof stateobj.html === 'function') {
|
|
440
|
+ showHtml = 'Error: html function must return text';
|
|
441
|
+ }
|
|
442
|
+
|
|
443
|
+ state += '<div class="'+ opts.prefix + 'state" data-jqi-name="'+ statename +'" style="display:none;">'+
|
|
444
|
+ arrow + title +
|
|
445
|
+ '<div class="'+ opts.prefix +'message '+ opts.classes.message +'">' + showHtml +'</div>'+
|
|
446
|
+ '<div class="'+ opts.prefix +'buttons '+ opts.classes.buttons +'"'+ ($.isEmptyObject(stateobj.buttons)? 'style="display:none;"':'') +'>';
|
|
447
|
+
|
|
448
|
+ // state buttons may be in object or array, lets convert objects to arrays
|
|
449
|
+ if($.isArray(stateobj.buttons)){
|
|
450
|
+ buttons = stateobj.buttons;
|
|
451
|
+ }
|
|
452
|
+ else if($.isPlainObject(stateobj.buttons)){
|
|
453
|
+ for(k in stateobj.buttons){
|
|
454
|
+ if(stateobj.buttons.hasOwnProperty(k)){
|
|
455
|
+ buttons.push({ title: k, value: stateobj.buttons[k] });
|
|
456
|
+ }
|
|
457
|
+ }
|
|
458
|
+ }
|
|
459
|
+
|
|
460
|
+ // iterate over each button and create them
|
|
461
|
+ for(i=0, l=buttons.length; i<l; i++){
|
|
462
|
+ v = buttons[i],
|
|
463
|
+ defbtn = stateobj.focus === i || (isNaN(stateobj.focus) && stateobj.defaultButton === i) ? (opts.prefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
|
|
464
|
+
|
|
465
|
+ state += '<button class="'+ opts.classes.button +' '+ opts.prefix + 'button '+ defbtn;
|
|
466
|
+
|
|
467
|
+ if(typeof v.classes !== "undefined"){
|
|
468
|
+ state += ' '+ ($.isArray(v.classes)? v.classes.join(' ') : v.classes) + ' ';
|
|
469
|
+ }
|
|
470
|
+
|
|
471
|
+ state += '" name="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
|
|
472
|
+ }
|
|
473
|
+
|
|
474
|
+ state += '</div></div>';
|
|
475
|
+
|
|
476
|
+ $state = $(state);
|
|
477
|
+
|
|
478
|
+ $state.on('impromptu:submit', stateobj.submit);
|
|
479
|
+
|
|
480
|
+ if(afterState !== undefined){
|
|
481
|
+ $jqistates.find('[data-jqi-name="'+afterState+'"]').after($state);
|
|
482
|
+ }
|
|
483
|
+ else{
|
|
484
|
+ $jqistates.append($state);
|
|
485
|
+ }
|
|
486
|
+
|
|
487
|
+ t.options.states[statename] = stateobj;
|
|
488
|
+
|
|
489
|
+ return $state;
|
|
490
|
+ },
|
|
491
|
+
|
|
492
|
+ /**
|
|
493
|
+ * removeState - Removes a state from the prompt
|
|
494
|
+ * @param state String - Name of the state
|
|
495
|
+ * @param newState String - Name of the state to transition to
|
|
496
|
+ * @return Boolean - returns true on success, false on failure
|
|
497
|
+ */
|
|
498
|
+ removeState: function(state, newState) {
|
|
499
|
+ var t = this,
|
|
500
|
+ $state = t.getState(state),
|
|
501
|
+ rm = function(){ $state.remove(); };
|
|
502
|
+
|
|
503
|
+ if($state.length === 0){
|
|
504
|
+ return false;
|
|
505
|
+ }
|
|
506
|
+
|
|
507
|
+ // transition away from it before deleting
|
|
508
|
+ if($state.css('display') !== 'none'){
|
|
509
|
+ if(newState !== undefined && t.getState(newState).length > 0){
|
|
510
|
+ t.goToState(newState, false, rm);
|
|
511
|
+ }
|
|
512
|
+ else if($state.next().length > 0){
|
|
513
|
+ t.nextState(rm);
|
|
514
|
+ }
|
|
515
|
+ else if($state.prev().length > 0){
|
|
516
|
+ t.prevState(rm);
|
|
517
|
+ }
|
|
518
|
+ else{
|
|
519
|
+ t.close();
|
|
520
|
+ }
|
|
521
|
+ }
|
|
522
|
+ else{
|
|
523
|
+ $state.slideUp('slow', rm);
|
|
524
|
+ }
|
|
525
|
+
|
|
526
|
+ return true;
|
|
527
|
+ },
|
|
528
|
+
|
|
529
|
+ /**
|
|
530
|
+ * getApi - Get the api, so you can extract it from $.prompt stack
|
|
531
|
+ * @return jQuery - the prompt
|
|
532
|
+ */
|
|
533
|
+ getApi: function() {
|
|
534
|
+ return this;
|
|
535
|
+ },
|
|
536
|
+
|
|
537
|
+ /**
|
|
538
|
+ * getBox - Get the box containing fade and prompt
|
|
539
|
+ * @return jQuery - the prompt
|
|
540
|
+ */
|
|
541
|
+ getBox: function() {
|
|
542
|
+ return this.jqib;
|
|
543
|
+ },
|
|
544
|
+
|
|
545
|
+ /**
|
|
546
|
+ * getPrompt - Get the prompt
|
|
547
|
+ * @return jQuery - the prompt
|
|
548
|
+ */
|
|
549
|
+ getPrompt: function() {
|
|
550
|
+ return this.jqi;
|
|
551
|
+ },
|
|
552
|
+
|
|
553
|
+ /**
|
|
554
|
+ * getState - Get the state by its name
|
|
555
|
+ * @param statename String - Name of the state
|
|
556
|
+ * @return jQuery - the state
|
|
557
|
+ */
|
|
558
|
+ getState: function(statename) {
|
|
559
|
+ return this.jqi.find('[data-jqi-name="'+ statename +'"]');
|
|
560
|
+ },
|
|
561
|
+
|
|
562
|
+ /**
|
|
563
|
+ * getCurrentState - Get the current visible state
|
|
564
|
+ * @return jQuery - the current visible state
|
|
565
|
+ */
|
|
566
|
+ getCurrentState: function() {
|
|
567
|
+ return this.getState(this.getCurrentStateName());
|
|
568
|
+ },
|
|
569
|
+
|
|
570
|
+ /**
|
|
571
|
+ * getCurrentStateName - Get the name of the current visible state/substate
|
|
572
|
+ * @return String - the current visible state's name
|
|
573
|
+ */
|
|
574
|
+ getCurrentStateName: function() {
|
|
575
|
+ return this.currentStateName;
|
|
576
|
+ },
|
|
577
|
+
|
|
578
|
+ /**
|
|
579
|
+ * position - Repositions the prompt (Used internally)
|
|
580
|
+ * @return void
|
|
581
|
+ */
|
|
582
|
+ position: function(e){
|
|
583
|
+ var t = this,
|
|
584
|
+ restoreFx = $.fx.off,
|
|
585
|
+ $state = t.getCurrentState(),
|
|
586
|
+ stateObj = t.options.states[$state.data('jqi-name')],
|
|
587
|
+ pos = stateObj? stateObj.position : undefined,
|
|
588
|
+ $window = $(window),
|
|
589
|
+ bodyHeight = document.body.scrollHeight, //$(document.body).outerHeight(true),
|
|
590
|
+ windowHeight = $(window).height(),
|
|
591
|
+ documentHeight = $(document).height(),
|
|
592
|
+ height = bodyHeight > windowHeight ? bodyHeight : windowHeight,
|
|
593
|
+ top = parseInt($window.scrollTop(),10) + (t.options.top.toString().indexOf('%') >= 0?
|
|
594
|
+ (windowHeight*(parseInt(t.options.top,10)/100)) : parseInt(t.options.top,10));
|
|
595
|
+
|
|
596
|
+ // when resizing the window turn off animation
|
|
597
|
+ if(e !== undefined && e.data.animate === false){
|
|
598
|
+ $.fx.off = true;
|
|
599
|
+ }
|
|
600
|
+
|
|
601
|
+ t.jqib.css({
|
|
602
|
+ position: "absolute",
|
|
603
|
+ height: height,
|
|
604
|
+ width: "100%",
|
|
605
|
+ top: 0,
|
|
606
|
+ left: 0,
|
|
607
|
+ right: 0,
|
|
608
|
+ bottom: 0
|
|
609
|
+ });
|
|
610
|
+ t.jqif.css({
|
|
611
|
+ position: "fixed",
|
|
612
|
+ height: height,
|
|
613
|
+ width: "100%",
|
|
614
|
+ top: 0,
|
|
615
|
+ left: 0,
|
|
616
|
+ right: 0,
|
|
617
|
+ bottom: 0
|
|
618
|
+ });
|
|
619
|
+
|
|
620
|
+ // tour positioning
|
|
621
|
+ if(pos && pos.container){
|
|
622
|
+ var offset = $(pos.container).offset();
|
|
623
|
+
|
|
624
|
+ if($.isPlainObject(offset) && offset.top !== undefined){
|
|
625
|
+ t.jqi.css({
|
|
626
|
+ position: "absolute"
|
|
627
|
+ });
|
|
628
|
+ t.jqi.animate({
|
|
629
|
+ top: offset.top + pos.y,
|
|
630
|
+ left: offset.left + pos.x,
|
|
631
|
+ marginLeft: 0,
|
|
632
|
+ width: (pos.width !== undefined)? pos.width : null
|
|
633
|
+ });
|
|
634
|
+ top = (offset.top + pos.y) - (t.options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt(t.options.top,10)/100)) : parseInt(t.options.top,10));
|
|
635
|
+ $('html,body').animate({ scrollTop: top }, 'slow', 'swing', function(){});
|
|
636
|
+ }
|
|
637
|
+ }
|
|
638
|
+ // custom state width animation
|
|
639
|
+ else if(pos && pos.width){
|
|
640
|
+ t.jqi.css({
|
|
641
|
+ position: "absolute",
|
|
642
|
+ left: '50%'
|
|
643
|
+ });
|
|
644
|
+ t.jqi.animate({
|
|
645
|
+ top: pos.y || top,
|
|
646
|
+ left: pos.x || '50%',
|
|
647
|
+ marginLeft: ((pos.width/2)*-1),
|
|
648
|
+ width: pos.width
|
|
649
|
+ });
|
|
650
|
+ }
|
|
651
|
+ // standard prompt positioning
|
|
652
|
+ else{
|
|
653
|
+ t.jqi.css({
|
|
654
|
+ position: "absolute",
|
|
655
|
+ top: top,
|
|
656
|
+ left: '50%',//$window.width()/2,
|
|
657
|
+ marginLeft: ((t.jqi.outerWidth(false)/2)*-1)
|
|
658
|
+ });
|
|
659
|
+ }
|
|
660
|
+
|
|
661
|
+ // restore fx settings
|
|
662
|
+ if(e !== undefined && e.data.animate === false){
|
|
663
|
+ $.fx.off = restoreFx;
|
|
664
|
+ }
|
|
665
|
+ },
|
|
666
|
+
|
|
667
|
+ /**
|
|
668
|
+ * style - Restyles the prompt (Used internally)
|
|
669
|
+ * @return void
|
|
670
|
+ */
|
|
671
|
+ style: function(){
|
|
672
|
+ var t = this;
|
|
673
|
+
|
|
674
|
+ t.jqif.css({
|
|
675
|
+ zIndex: t.options.zIndex,
|
|
676
|
+ display: "none",
|
|
677
|
+ opacity: t.options.opacity
|
|
678
|
+ });
|
|
679
|
+ t.jqi.css({
|
|
680
|
+ zIndex: t.options.zIndex+1,
|
|
681
|
+ display: "none"
|
|
682
|
+ });
|
|
683
|
+ t.jqib.css({
|
|
684
|
+ zIndex: t.options.zIndex
|
|
685
|
+ });
|
|
686
|
+ },
|
|
687
|
+
|
|
688
|
+ /**
|
|
689
|
+ * goToState - Goto the specified state
|
|
690
|
+ * @param state String - name of the state to transition to
|
|
691
|
+ * @param subState Boolean - true to be a sub state within the currently open state
|
|
692
|
+ * @param callback Function - called when the transition is complete
|
|
693
|
+ * @return jQuery - the newly active state
|
|
694
|
+ */
|
|
695
|
+ goToState: function(state, subState, callback) {
|
|
696
|
+ var t = this,
|
|
697
|
+ $jqi = t.jqi,
|
|
698
|
+ jqiopts = t.options,
|
|
699
|
+ $state = t.getState(state),
|
|
700
|
+ stateobj = jqiopts.states[$state.data('jqi-name')],
|
|
701
|
+ promptstatechanginge = new $.Event('impromptu:statechanging'),
|
|
702
|
+ opts = t.options;
|
|
703
|
+
|
|
704
|
+ if(stateobj !== undefined){
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+ if (typeof stateobj.html === 'function') {
|
|
708
|
+ var contentLaterFunc = stateobj.html;
|
|
709
|
+ $state.find('.' + opts.prefix +'message ').html(contentLaterFunc());
|
|
710
|
+ }
|
|
711
|
+
|
|
712
|
+ // subState can be ommitted
|
|
713
|
+ if(typeof subState === 'function'){
|
|
714
|
+ callback = subState;
|
|
715
|
+ subState = false;
|
|
716
|
+ }
|
|
717
|
+
|
|
718
|
+ t.jqib.trigger(promptstatechanginge, [t.getCurrentStateName(), state]);
|
|
719
|
+
|
|
720
|
+ if(!promptstatechanginge.isDefaultPrevented() && $state.length > 0){
|
|
721
|
+ t.jqi.find('.'+ opts.prefix +'parentstate').removeClass(opts.prefix +'parentstate');
|
|
722
|
+
|
|
723
|
+ if(subState){ // hide any open substates
|
|
724
|
+ // get rid of any substates
|
|
725
|
+ t.jqi.find('.'+ opts.prefix +'substate').not($state)
|
|
726
|
+ .slideUp(jqiopts.promptspeed)
|
|
727
|
+ .removeClass('.'+ opts.prefix +'substate')
|
|
728
|
+ .find('.'+ opts.prefix +'arrow').hide();
|
|
729
|
+
|
|
730
|
+ // add parent state class so it can be visible, but blocked
|
|
731
|
+ t.jqi.find('.'+ opts.prefix +'state:visible').addClass(opts.prefix +'parentstate');
|
|
732
|
+
|
|
733
|
+ // add substate class so we know it will be smaller
|
|
734
|
+ $state.addClass(opts.prefix +'substate');
|
|
735
|
+ }
|
|
736
|
+ else{ // hide any open states
|
|
737
|
+ t.jqi.find('.'+ opts.prefix +'state').not($state)
|
|
738
|
+ .slideUp(jqiopts.promptspeed)
|
|
739
|
+ .find('.'+ opts.prefix +'arrow').hide();
|
|
740
|
+ }
|
|
741
|
+ t.currentStateName = stateobj.name;
|
|
742
|
+
|
|
743
|
+ $state.slideDown(jqiopts.promptspeed,function(){
|
|
744
|
+ var $t = $(this);
|
|
745
|
+
|
|
746
|
+ // if focus is a selector, find it, else its button index
|
|
747
|
+ if(typeof(stateobj.focus) === 'string'){
|
|
748
|
+ $t.find(stateobj.focus).eq(0).focus();
|
|
749
|
+ }
|
|
750
|
+ else{
|
|
751
|
+ $t.find('.'+ opts.prefix +'defaultbutton').focus();
|
|
752
|
+ }
|
|
753
|
+
|
|
754
|
+ $t.find('.'+ opts.prefix +'arrow').show(jqiopts.promptspeed);
|
|
755
|
+
|
|
756
|
+ if (typeof callback === 'function'){
|
|
757
|
+ t.jqib.on('impromptu:statechanged', callback);
|
|
758
|
+ }
|
|
759
|
+ t.jqib.trigger('impromptu:statechanged', [state]);
|
|
760
|
+ if (typeof callback === 'function'){
|
|
761
|
+ t.jqib.off('impromptu:statechanged', callback);
|
|
762
|
+ }
|
|
763
|
+ });
|
|
764
|
+ if(!subState){
|
|
765
|
+ t.position();
|
|
766
|
+ }
|
|
767
|
+ } // end isDefaultPrevented()
|
|
768
|
+ }// end stateobj !== undefined
|
|
769
|
+
|
|
770
|
+ return $state;
|
|
771
|
+ },
|
|
772
|
+
|
|
773
|
+ /**
|
|
774
|
+ * nextState - Transition to the next state
|
|
775
|
+ * @param callback Function - called when the transition is complete
|
|
776
|
+ * @return jQuery - the newly active state
|
|
777
|
+ */
|
|
778
|
+ nextState: function(callback) {
|
|
779
|
+ var t = this,
|
|
780
|
+ $next = t.getCurrentState().next();
|
|
781
|
+ if($next.length > 0){
|
|
782
|
+ t.goToState( $next.data('jqi-name'), callback );
|
|
783
|
+ }
|
|
784
|
+ return $next;
|
|
785
|
+ },
|
|
786
|
+
|
|
787
|
+ /**
|
|
788
|
+ * prevState - Transition to the previous state
|
|
789
|
+ * @param callback Function - called when the transition is complete
|
|
790
|
+ * @return jQuery - the newly active state
|
|
791
|
+ */
|
|
792
|
+ prevState: function(callback) {
|
|
793
|
+ var t = this,
|
|
794
|
+ $prev = t.getCurrentState().prev();
|
|
795
|
+ if($prev.length > 0){
|
|
796
|
+ t.goToState( $prev.data('jqi-name'), callback );
|
|
797
|
+ }
|
|
798
|
+ return $prev;
|
|
799
|
+ }
|
|
800
|
+
|
|
801
|
+ };
|
|
802
|
+
|
|
803
|
+ // ########################################################################
|
|
804
|
+ // $.prompt will manage a queue of Impromptu instances
|
|
805
|
+ // ########################################################################
|
|
806
|
+
|
|
807
|
+ /**
|
|
808
|
+ * $.prompt create a new Impromptu instance and push it on the stack of instances
|
|
809
|
+ * @param message String/Object - String of html or Object of states
|
|
810
|
+ * @param options Object - Options to set the prompt
|
|
811
|
+ * @return jQuery - the jQuery object of the prompt within the modal
|
|
812
|
+ */
|
|
813
|
+ $.prompt = function(message, options){
|
|
814
|
+ var api = new Imp(message, options);
|
|
815
|
+ return api.jqi;
|
|
816
|
+ };
|
|
817
|
+
|
|
818
|
+ /**
|
|
819
|
+ * Copy over static methods
|
|
820
|
+ */
|
|
821
|
+ $.each(Imp, function(k,v){
|
|
822
|
+ $.prompt[k] = v;
|
|
823
|
+ });
|
|
824
|
+
|
|
825
|
+ /**
|
|
826
|
+ * Create a proxy for accessing all instance methods. The close method pops from queue.
|
|
827
|
+ */
|
|
828
|
+ $.each(Imp.prototype, function(k,v){
|
|
829
|
+ $.prompt[k] = function(){
|
|
830
|
+ var api = Imp.getLast(); // always use the last instance on the stack
|
|
831
|
+
|
|
832
|
+ if(api && typeof api[k] === "function"){
|
|
833
|
+ return api[k].apply(api, arguments);
|
|
834
|
+ }
|
|
835
|
+ };
|
|
836
|
+ });
|
|
837
|
+
|
|
838
|
+ // ########################################################################
|
|
839
|
+ // jQuery Plugin and public access
|
|
840
|
+ // ########################################################################
|
|
841
|
+
|
|
842
|
+ /**
|
|
843
|
+ * Enable using $('.selector').prompt({});
|
|
844
|
+ * This will grab the html within the prompt as the prompt message
|
|
845
|
+ */
|
|
846
|
+ $.fn.prompt = function(options){
|
|
847
|
+ if(options === undefined){
|
|
848
|
+ options = {};
|
|
849
|
+ }
|
|
850
|
+ if(options.withDataAndEvents === undefined){
|
|
851
|
+ options.withDataAndEvents = false;
|
|
852
|
+ }
|
|
853
|
+
|
|
854
|
+ $.prompt($(this).clone(options.withDataAndEvents).html(),options);
|
|
855
|
+ };
|
|
856
|
+
|
|
857
|
+ /**
|
|
858
|
+ * Export it as Impromptu and $.prompt
|
|
859
|
+ * Can be used from here forth as new Impromptu(states, opts)
|
|
860
|
+ */
|
|
861
|
+ window.Impromptu = Imp;
|
|
862
|
+
|
|
863
|
+}));
|