-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.modal.js
141 lines (104 loc) · 3.87 KB
/
jquery.modal.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
/*
* Part of Nitriques Solutions inc. (http://www.nitriques.com) jQuery plug-in bundle
* Liscence under the The Code Project Open License (CPOL)
* http://www.codeproject.com/info/cpol10.aspx
* Name: jquery.modal.js
* Date: 2010-10-30
* Version: 1.1
* Version 1.1 : Added multiple instance support
Added public methods (show, hide, toggle)
Added ntr$modal dummy namespace
Added private block declaration
Added a cancel method
* Version 1.0 : First release
* Pre-requisites:
* jquery.center.js version 1.0
* jquery.scroll.js version 1.0
* Copyrights (c) Solutions Nitriques inc.
*/
(function ($) {
// dummy namespace function
$.ntr$modal = function (b) { alert('x ' + b) };
// actual plugin
$.fn.ntr$modal = function (b) {
var vp = 'isVisible',
th = this,
bg;
function setVisible(t, v) { if (!t) t = $(this); else t = $(t); t.data(vp, v); }
function getVisible(t) { if (!t) t = $(this); else t = $(t); return t.data(vp); }
function show(t) {
var o = { opacity: 0.6 };
var d = { duration: 800 };
if (!t) t = $(this); else t = $(t);
// add background to DOM
bg.appendTo('body');
//$(bg).click(function () { toggle(t) }); // debug purpose
// stop all animations
cancel(t);
// scroll background
bg.ntr$scroll();
// show modal
t.show();
// center it: needs jquery.center.js
t.ntr$center();
// animate background
bg.animate(o, d.duration, 'swing', function () { }); // Don't know why, but the other signature creates too much recursion
// full opacity
o.opacity = 1;
// animate modal
t.animate(o, d);
// Set flag
setVisible(t, 1);
return this;
}
$.ntr$modal.show = show;
function hide(t) {
return hide(t, 500);
}
function hide(t, dur) {
var o = { opacity: 0 };
if (!t) t = $(this); else t = $(t);
// stop all animations
cancel(t);
// animate background, then remove it from DOM
bg.animate(o, dur, 'swing', function () { bg.remove() });
// animate modal, then hide it
t.animate(o, dur, 'swing', function () { t.hide(); });
// Set flag
setVisible(t, 0);
return this;
}
$.ntr$modal.hide = hide;
function toggle(t) {
if (getVisible(t) == 1) { hide(t); } else { show(t); }
return false;
}
$.ntr$modal.toggle = toggle;
function cancel(t) {
if (!t) t = $(this); else t = $(t);
t.stop(true, true); // clear queue, jump to end
bg.stop(true, true);
}
$.ntr$modal.cancel = cancel;
function createBackground() {
return $('<div id="ntr$modal$bg" style="width:100%;height:100%;background-color:#000000;position:absolute;top:0;left:0;z-index:999998;"></div>');
}
// Dynamic background
bg = createBackground();
hide(th, 0); // hide with no delay to set css values
// register toggle on bouton(s)
// use anonymus function in order to pass the th param by value not as a ref
// so changing the th value after wards wont affect the on click event
$(b).click(function () { toggle(th) });
//register for scroll
$(window).scroll(function () {
bg.ntr$scroll();
$(th).ntr$center();
});
//register for resize
$(window).resize(function () {
$(th).ntr$center();
});
return this; // return jQuery object
} // ntr$modal
})(jQuery); // private block