-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.wsmodal.js
190 lines (170 loc) · 5.31 KB
/
jquery.wsmodal.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
/*!
* jquery.wsmodal.js v0.1.1
* http://wingsline.com
*
* Copyright 2013 Arpad Olasz
* All rights reserved.
*
*/
(function($) {
/**
* Plugin namespace
*
* @type {String}
*/
var __NS__ = 'wsmodal',
/**
* Global variables, applies to all the modals
*
* @type {Object}
*/
global = {
bg: 'wsmodal__bg',
close : 'wsmodal__close',
opacity: 0.7,
speed: 200,
/**
* Global open function. This function is applied
* when the modal is opened or when tabbing
*
*/
open: function () {
$('.text-input,.btn', this).first().focus();
}
},
opened = [], // array of opened modals
methods = {
init: function(options) {
return this.each(function() {
var $this = $(this),
data = $this.data(__NS__);
// If the plugin hasn't been initialized yet
if (!data) {
// default options
data = $.extend({
global: {},
open: global.open,
close: function () {}
}, options);
$(this).data(__NS__, data);
// extend the global
global = $.extend(data.global, global);
// create the modal bg
var bg = $('.' + global.bg);
if(bg.length === 0) {
bg = $('<div class="' + global.bg + '"/>')
.css('opacity', global.opacity)
.appendTo('body')
.on('click.' + __NS__, function () {
methods.close.apply(bg, [1]);
});
}
// add the click event
$(this).on('click.' + __NS__, function (e) {
e.preventDefault();
methods.open.apply(this);
});
// move the container to the end of the body
data.container = $('#' + $this.data('wsmodalId'));
data.container.data(__NS__ + 'Open', data.open);
data.container.data(__NS__ + 'Close', data.close);
$('body').append( data.container.detach());
// add the close events to the close classes
$('body').off('.' + __NS__, '.' + global.close).on('click.' + __NS__, '.' + global.close, function (e) {
e.preventDefault();
methods.close.apply(bg);
});
$('body').off('keyup.' + __NS__).on('keyup.' + __NS__, function (e) {
if(e.which === 27) {
e.preventDefault();
methods.close.apply(bg);
}
});
}
});
}, // end init()
destroy: function() {
return this.each(function() {
var $this = $(this),
data = $this.data(__NS__);
$(window).unbind('.' + __NS__);
data[__NS__].remove();
$this.removeData(__NS__);
});
}, // end destroy()
/**
* Will open a modal window
*
*/
open: function() {
return $(this).each(function() {
var $this = $(this),
data = $this.data(__NS__),
pos = parseInt(data.container.css('top'), 10);
if (undefined === data.container.data(__NS__)) {
data.container.data(__NS__, pos);
}
$('.' + global.bg).fadeIn(global.speed/2);
data.container.css({
'top': -data.container.outerHeight(),
'opacity' : 0,
'visibility' : 'visible'
}).animate({
'top': pos,
'opacity' : 1
}, global.speed, function () {
// callback on open
data.container.data(__NS__ + 'Open').apply(data.container);
});
opened.push(data.container);
// block everything behind the bg
var bg = $('.' + global.bg);
bg.prevAll().on('keyup.' + __NS__, function (e) {
e.preventDefault();
global.open.apply(data.container);
});
});
}, // end open()
/**
* Will close a modal window
*
* @param {boolean} all When set to true all modals will be closed
*
*/
close: function (all) {
if (all) {
jQuery.each(opened, resetModal);
opened = [];
} else if (!all && opened.length) {
var container = opened.pop();
container.animate({
'top': -container.outerHeight(),
'opacity':0
}, global.speed/2, resetModal);
}
if (!opened.length) {
$(this).fadeOut(global.speed/2);
}
} // end close();
}; // end methods
/**
* Will reset the modal window to it's original position after the animation
*
*/
var resetModal = function () {
var $this = $(this);
$this.css({'top':$this.data(__NS__), 'opacity' : 1, 'visibility' : 'hidden'});
// callbacks on close
$this.data(__NS__ + 'Close').apply($this);
};
$.fn.wsmodal = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.wsmodal');
}
}; // end plugin
})(jQuery);
$('a[data-wsmodal-id]').wsmodal();