forked from denis-sokolov/details-tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.details.js
178 lines (151 loc) · 5.51 KB
/
jquery.details.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
167
168
169
170
171
172
173
174
175
176
177
178
/*!Denis Sokolov, http://akral.github.io/details-tag/, [email protected]. GPL, MIT */
(function($){
var support = 'open' in document.createElement('details'), init;
// API
$.fn.details = function(command) {
if (command === 'open')
{
if (support)
this.prop('open', true);
else
this.trigger('open');
}
if (command === 'close')
{
if (support)
this.prop('open', false);
else
this.trigger('close');
}
if (command === 'init')
init($(this));
if (!command) {
if (!support)
return this.hasClass('open');
var open = false;
this.each(function(){
if (this.open) {
open = true;
return false;
}
});
return open;
}
};
init = function(elements) {
elements = elements.not('.details-inited').addClass('details-inited');
/* Animated tags */
elements.filter('.animated').each(function(){
var me = $(this);
var summary = me.children('summary').remove();
var wrapper = $('<div>').addClass('details-wrapper hide').append(me.children());
me.append(wrapper).prepend(summary);
});
if (support)
return;
elements
// Add missing summary tags
.each(function(){
var me = $(this);
if (!me.children('summary').length)
me.prepend('<summary>Details</summary>');
})
.children('summary')
.filter(':not(tabindex)').attr('tabindex', 0).end()
.end()
// Wrap plain text nodes in spans for CSS visibility
.contents(':not(summary)').filter(function(){
return (this.nodeType === 3) && (/[^\t\n\r ]/.test(this.data));
})
.wrap('<span>')
.end().end()
// Initial positions
.filter(':not([open])')
.prop('open', false)
.end()
.filter('[open]')
.addClass('open')
.prop('open', true)
.end();
elements.filter(':not(.open)').children().not('summary').hide();
};
$(function(){
$('body')
.on('open.details', 'details.animated', function(){
var details = $(this);
var wrapper = details.children('.details-wrapper');
details.prop('open', true).addClass('open');
setTimeout(function(){ // Simple .height() redraw is not enough for Chrome
wrapper.slideDown(details.data('animation-speed'));
}, 0);
})
.on('close.details', 'details.animated', function(){
var details = $(this);
var wrapper = details.children('.details-wrapper');
setTimeout(function(){
wrapper.slideUp(details.data('animation-speed'), function(){
details.prop('open', false).removeClass('open');
});
}, 0);
});
if (support)
{
init($('details'));
// Add our triggers on native implementation
$('body').on('click', 'summary', function(e){
e.preventDefault();
var details = $(this).parent();
setTimeout(function(){
if (details.prop('open'))
details.trigger('close');
else
details.trigger('open');
}, 0);
});
return;
}
// Everything below is a polyfill
$('html').addClass('no-details');
/* Better to move this to a separate stylesheet so it can be minified */
$('head').prepend('<style>'+
// Style
'details{display:block}'+
'summary{cursor:pointer}'+
'details>summary::before{content:"?"}'+
'details[open]>summary::before,details.open>summary::before{content:"?"}'+
'.no-details details>*:not(summary){display:none}'+
'summary::-webkit-details-marker {display:none}'+
'.hide{display:none}'+
'</style>'
);
$('body')
.on('open.details', 'details', function(e){
$(this).addClass('open').trigger('change.details');
e.preventDefault();
e.stopPropagation();
})
.on('close.details', 'details', function(e){
$(this).removeClass('open').trigger('change.details');
e.preventDefault();
e.stopPropagation();
})
.on('toggle.details', 'details', function(e){
var me = $(this);
if (me.hasClass('open'))
me.trigger('close');
else
me.trigger('open');
e.stopPropagation();
})
.on('click', 'summary', function(){
$(this).parent().trigger('toggle');
})
.on('keyup', 'summary', function(e){
// 32 - space
// 13 - Enter. Opera triggers .click()
if (e.keyCode === 32 || e.keyCode === 13)
$(this).parent().trigger('toggle');
});
init($('details'));
});
})(jQuery);