-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mloverlay.js
116 lines (100 loc) · 3.66 KB
/
jquery.mloverlay.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
(function ( $ ) {
var PLUGIN_IDENTIFIER = 'mlOverlay';
function hideOverlay(overlay) {
var settings = $(overlay).data(PLUGIN_IDENTIFIER);
if(settings._isVisible) {
settings.onHide(overlay);
settings._isVisible = false;
}
}
function showOverlay(overlay) {
var settings = $(overlay).data(PLUGIN_IDENTIFIER);
if(!settings._isVisible) {
settings.onShow(overlay);
settings._isVisible = true;
}
}
var methods = {
init : function(options) {
var settings = $.extend({
ignore: '',
target: '',
hideOnOutsideClick: true,
hideOnTargetClick: true,
hideOnEsc: true,
onShow: function(overlay) {
$(overlay).fadeIn();
},
onHide: function(overlay) {
$(overlay).fadeOut();
},
_isVisible : false
}, options);
if (settings.ignore.length > 0) {
settings.ignore += ",";
}
settings.ignore += settings.target;
this.each(function() {
var _this = this;
$(_this).data(PLUGIN_IDENTIFIER, settings);
$(settings.target).bind('click.'+PLUGIN_IDENTIFIER, function() {
var isOverlayVisible = settings._isVisible;
if(isOverlayVisible) {
if(settings.hideOnTargetClick) {
hideOverlay(_this);
}
} else {
showOverlay(_this);
}
});
if (settings.hideOnOutsideClick) {
$(document).bind('click.'+PLUGIN_IDENTIFIER, function(e) {
if ($(e.target).closest(settings.ignore).length == 0 && $(e.target).closest(_this).length == 0) {
hideOverlay(_this);
}
});
}
if(settings.hideOnEsc) {
$(document).bind('keyup.'+PLUGIN_IDENTIFIER, function(e) {
if (e.keyCode == 27) {
hideOverlay(_this);
}
});
}
});
},
destroy: function() {
this.each(function() {
var settings = $(this).data(PLUGIN_IDENTIFIER);
if(settings) {
$(settings.target).unbind('click.'+PLUGIN_IDENTIFIER);
$(document).unbind('click.'+PLUGIN_IDENTIFIER);
$(document).unbind('keyup.'+PLUGIN_IDENTIFIER);
$(this).removeData();
}
});
},
show : function() {
this.each(function() {
showOverlay(this);
});
},
hide : function() {
this.each(function() {
hideOverlay(this);
});
}
};
$.fn.mlOverlay = function(methodOrOptions) {
if ( methods[methodOrOptions] ) {
return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
// prevent re-initialization
if(!$(this).data(PLUGIN_IDENTIFIER)) {
return methods.init.apply( this, arguments );
}
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.'+PLUGIN_IDENTIFIER );
}
};
}( jQuery ));