-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery-expose.js
113 lines (88 loc) · 3.91 KB
/
jquery-expose.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
/*
Expose DOM elements with ease
Author: Davide Callegari - http://www.brokenseal.it/
Home page: http://github.com/brokenseal/jquery-expose/
License: MIT
*/
;(function($){
var
defaultOptions= {
dark: true // light or dark exposer
,mainElement: 'body' // main element where to append the exposer
}
,$window= $(window)
// cloning is faster than creating
,exposer= $('<div class="ui-expose-exposer" style="display:none;"></div>')
// private methods
,expose= function(options, exposed, exposer){
var
placeholder= exposed.clone()
,unExposeHandler= function(){
// un-expose the element
unExpose(options, exposed, exposer, placeholder);
// unbind the esc key press from the page for this handler
$window.unbind('keypress, keydown, keyup', escKeypressHandler);
}
,escKeypressHandler= function(e){
if(e.keyCode == 27) {
unExposeHandler();
}
}
;
// save the original style for later use
exposed.each(function(){
this.exposeStyle= $(this).attr('style');
});
// force the layout of the exposed element to be what we expect it to be
exposed.css({
left: exposed.position().left
,top: exposed.position().top
,width: exposed.width()
});
// place the placeholder to let the original layout of the page intact
exposed.before(placeholder);
// highlitght the exposed element
exposed.addClass('ui-expose-exposed');
// append the exposer to the main selected element
exposer.prependTo(options.mainElement);
// fade the exposer in and bind the unexpose function to it
exposer.fadeIn();
exposer.click(function(){
unExposeHandler();
});
// un-expose the element on 'esc' key press
$window.bind('keypress, keydown, keyup', escKeypressHandler);
}
,unExpose= function(options, exposed, exposer, placeholder){
// fade out the exposer
exposer.fadeOut(function(){
// remove the placeholder from the DOM
placeholder.remove();
// un-highlight the exposed element
exposed.removeClass('ui-expose-exposed');
// reset the layout of the exposed element to the original values
exposed.each(function(){
$(this).attr('style', this.exposeStyle || '');
delete this.exposeStyle;
});
// remove the saved layout values from the DOM object
delete exposed.expose;
});
}
;
// the main plugin function
$.fn.expose= function(options){
var
newExposer= exposer.clone()
;
// merge provided options with default options
options= $.fn.extend(defaultOptions, options);
options.dark ? newExposer.addClass('ui-expose-dark') : newExposer.addClass('ui-expose-light');
return this.each(function(){
var
element= $(this)
;
expose(options, element, newExposer);
});
};
})(jQuery);