-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.simplemodal.js
162 lines (132 loc) · 4.07 KB
/
jquery.simplemodal.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
(function($, undefined) {
'use strict';
var Plugin, defaults, namespace;
// Default options and settings
namespace ='simplemodal';
defaults = {
top: null,
overlay: 0.5,
closeButton: null,
className: null,
duration: 200,
autoOpen: false,
onOpen: function() {},
onClose: function() {}
};
// PLUGIN --------------------------------------------------------------------
//
// Declare the plugin fuction, isolated in a sub-module pattern to prevent
// leaks and preserve vars scope.
//
Plugin = (function() {
// The Plugin object
function Plugin(element, options) {
// Store elements to access it later easily
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = namespace;
// Fire the init method
this.init();
}
Plugin.prototype.init = function() {
this.$overlay = setOverlay.call(this);
this.$el = setModal.call(this);
if (this.options.autoOpen) this.open();
};
var setOverlay = function () {
var $el = $('<div/>', {'class': 'sm-overlay'})
.css({
position: 'fixed',
zIndex: 9999,
top: 0,
left: 0,
height: '100%',
width: '100%',
background: '#000'
})
.hide()
.on('click', $.proxy(this.close, this))
.appendTo('body');
$('<div/>', {'class': 'sm-loader'})
.hide()
.appendTo($el);
return $el;
};
var setModal = function (el) {
var $el,
className = 'sm-modal' + (this.options.className? ' ' + this.options.className : '');
$el = $('<div/>', {'class': className})
.css({
position : 'fixed',
zIndex: 9999,
top: this.options.top? this.options.top : '50%',
left : '50%'
})
.hide()
.appendTo('body');
$('<div/>', {'class': 'sm-content'})
.append( $(this.element) )
.appendTo($el);
if (this.options.closeButton) {
$('<button/>', {'class': 'sm-close'})
.append( $('<span/>', {'text': 'close'}) )
.on('click', $.proxy(this.close, this))
.appendTo($el);
}
return $el;
};
var toggleLoader = function () {
this.$overlay.find('.sm-loader').toggle();
};
Plugin.prototype.open = function() {
var close = $.proxy(this.close, this);
this.$overlay
.fadeTo(this.options.duration, this.options.overlay, $.proxy(toggleLoader, this));
this.$el
.css({
marginTop: this.options.top? 0 : this.$el.outerHeight() / -2 + 'px',
marginLeft: this.$el.outerWidth() / -2 + 'px'
})
.fadeIn(this.options.duration, $.proxy(this.options.onOpen, this.element));
$(document).one('keyup', function (e) {
if (e.which == 27) close();
});
};
Plugin.prototype.close = function() {
this.$overlay
.fadeOut(this.options.duration, $.proxy(toggleLoader, this));
this.$el
.fadeOut(this.options.duration, $.proxy(this.options.onClose, this.element));
};
Plugin.prototype.destroy = function() {
this.$overlay.remove();
this.$el.remove();
};
// Expose the Plugin externaly
return Plugin;
})();
// JQUERY --------------------------------------------------------------------
//
// Expose the plugin in the jQury prototype
//
$.fn[namespace] = function(options) {
// Explode arguments tio use it when callbacking
var args, _;
_ = arguments[0];
args = [].slice.call(arguments, 1);
// Iterate over jQuery elements
return this.each(function() {
var plugin;
// Get the plugin attached to the element
plugin = $.data(this, "plugin_" + namespace);
if(!plugin) {
// Instanciate a new plugin and store it in the jQuery object
$.data(this, "plugin_" + namespace, new Plugin(this, options));
} else if(plugin[_] != null && $.type(plugin[_]) == 'function') {
// If the plugin contains the called method, apply it
return plugin[_].apply(plugin, args);
}
});
};
})(jQuery);