-
Notifications
You must be signed in to change notification settings - Fork 0
/
mModal.js
166 lines (140 loc) · 7.33 KB
/
mModal.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
(function ($) {
$.fn.mModal = function (options, waitForEvent) {
var settings = $.extend({
modal: null, // the modal itself
contentUrl: '', // load content from the given URL
callbacks: [], // button callbacks
appearAnimation: 'fade', // 'fade' or 'slide'
disappearAnimation: 'fade', // 'fade' or 'slide'
appearSlideDirection: 'down', // 'up', 'right', 'down' or 'left'
disappearSlideDirection: 'up', // 'up', 'right', 'down' or 'left'
animateResize: true, // animate to the new centre position instead of instant
closeButton: true, // show a close button at the upper right corner
closeOnCoverClick: true // close the modal if the cover is clicked
}, options);
if (settings.modal == null || typeof settings.modal == 'undefined') {
console.error('The given modal was not set or found!');
return false;
}
if (typeof waitForEvent == 'undefined') {
waitForEvent = true;
}
var modalLink = $(this);
var modalContent = settings.modal.find('.mModal-content');
var modalButtons = settings.modal.find('.mModal-buttons').children();
var modalCloseButton = (settings.closeButton) ? $('<div class="mModal-close"></div>').appendTo(settings.modal) : $();
var modalCover = null;
settings.modal.off('click').click(function (e) {
e.stopImmediatePropagation();
}).hide();
modalButtons.outerWidth(settings.modal.width() / modalButtons.length);
settings.modal.openModal = function () {
if (settings.contentUrl.length > 0) {
var matches = settings.contentUrl.match(/(http(s)?:\/\/)?(www\.)?youtu(\.)?be(.+)?v=(.+)/i);
if (matches != null) {
var youtubeIframe = $('<iframe width="100%" height="100%" src="https://www.youtube.com/embed/' + matches[6] + '?rel=0&showinfo=0&autoplay=1" frameborder="0"></iframe>').appendTo(modalContent).hide();
youtubeIframe.load(function () {
$(this).delay(100).fadeIn('slow');
});
} else {
modalContent.load(settings.contentUrl);
}
modalContent.outerHeight(settings.modal.outerHeight());
settings.modal.centerModal();
}
modalCover = settings.modal.wrap($('<div class="mModal-cover"></div>')).parent();
modalCover.off('click').click(function (e) {
if (settings.closeOnCoverClick) {
settings.modal.closeModal();
}
}).hide();
if (settings.appearAnimation == 'slide') {
modalCover.fadeIn('fast', function () {
settings.modal.show();
var modalPositionLeft = ($(window).outerWidth() / 2) - (settings.modal.outerWidth() / 2);
var modalPositionTop = ($(window).outerHeight() / 2) - (settings.modal.outerHeight() / 2);
var startPositionLeft = (settings.appearSlideDirection == 'left' || settings.appearSlideDirection == 'right') ?
(settings.appearSlideDirection == 'left' ? ($(window).outerWidth()) : (-modalPositionLeft)) : modalPositionLeft;
var startPositionTop = (settings.appearSlideDirection == 'up' || settings.appearSlideDirection == 'down') ?
(settings.appearSlideDirection == 'up' ? ($(window).outerHeight()) : (-modalPositionTop)) : modalPositionTop;
settings.modal.css({left: startPositionLeft, top: startPositionTop});
settings.modal.stop(true, false).animate({left: modalPositionLeft, top: modalPositionTop});
});
} else {
modalCover.fadeIn('fast');
settings.modal.show();
}
modalButtons.off('click').click(function (e) {
e.preventDefault();
var modalButtonCallback = $(this).attr('data-mModalCallback');
if (typeof modalButtonCallback == 'undefined') {
return false;
}
if (typeof settings.callbacks[modalButtonCallback] != 'undefined') {
settings.callbacks[modalButtonCallback](settings.modal);
}
});
};
settings.modal.centerModal = function () {
var modalPositionLeft = ($(window).outerWidth() / 2) - (settings.modal.outerWidth() / 2);
var modalPositionTop = ($(window).outerHeight() / 2) - (settings.modal.outerHeight() / 2);
if (settings.animateResize == true) {
settings.modal.stop(true, false).animate({
left: modalPositionLeft,
top: modalPositionTop
}, 'fast', 'swing');
} else {
settings.modal.css({left: modalPositionLeft, top: modalPositionTop});
}
};
settings.modal.closeModal = function () {
if (settings.disappearAnimation == 'slide') {
var modalPositionLeft = ($(window).outerWidth() / 2) - (settings.modal.outerWidth() / 2);
var modalPositionTop = ($(window).outerHeight() / 2) - (settings.modal.outerHeight() / 2);
var endPositionLeft = (settings.disappearSlideDirection == 'left' || settings.disappearSlideDirection == 'right') ?
(settings.disappearSlideDirection == 'left' ? (-modalPositionLeft) : ($(window).outerWidth())) : modalPositionLeft;
var endPositionTop = (settings.disappearSlideDirection == 'up' || settings.disappearSlideDirection == 'down') ?
(settings.disappearSlideDirection == 'up' ? (-modalPositionTop) : ($(window).outerHeight())) : modalPositionTop;
settings.modal.stop(true, false).animate({
left: endPositionLeft,
top: endPositionTop
}, 'fast', 'swing', function () {
modalCover.fadeOut('fast', function () {
settings.modal.hide();
settings.modal.unwrap();
if (settings.contentUrl.length > 0) {
modalContent.empty();
}
});
});
}
else {
modalCover.fadeOut('fast', function () {
settings.modal.hide();
settings.modal.unwrap();
if (settings.contentUrl.length > 0) {
modalContent.empty();
}
});
}
};
modalCloseButton.off('click').click(function (e) {
e.preventDefault();
settings.modal.closeModal();
});
$(window).resize(function (e) {
settings.modal.centerModal();
}).trigger('resize');
if (waitForEvent == true) {
modalLink.click(function (e) {
e.preventDefault();
settings.modal.openModal();
});
} else {
settings.modal.openModal();
}
};
$.mModal = function (options) {
$.fn.mModal(options, false);
};
}(jQuery));