details.js
4.26 KB
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
webshims.register('details', function($, webshims, window, doc, undefined, options){
var isInterActiveSummary = function(summary){
var details = $(summary).parent('details');
if(details[0] && details.children(':first').get(0) === summary){
return details;
}
};
var bindDetailsSummary = function(summary, details){
summary = $(summary);
details = $(details);
var oldSummary = $.data(details[0], 'summaryElement');
$.data(summary[0], 'detailsElement', details);
if(!oldSummary || summary[0] !== oldSummary[0]){
if(oldSummary){
if(oldSummary.hasClass('fallback-summary')){
oldSummary.remove();
} else {
oldSummary
.off('.summaryPolyfill')
.removeData('detailsElement')
.removeAttr('role')
.removeAttr('tabindex')
.removeAttr('aria-expanded')
.removeClass('summary-button')
.find('span.details-open-indicator')
.remove()
;
}
}
$.data(details[0], 'summaryElement', summary);
details.prop('open', details.prop('open'));
}
};
var getSummary = function(details){
var summary = $.data(details, 'summaryElement');
if(!summary){
summary = $(details).children('summary:first-child');
if(!summary[0]){
$(details).prependPolyfill('<summary class="fallback-summary">'+ options.text +'</summary>');
summary = $.data(details, 'summaryElement');
} else {
bindDetailsSummary(summary, details);
}
}
return summary;
};
// var isOriginalPrevented = function(e){
// var src = e.originalEvent;
// if(!src){return e.isDefaultPrevented();}
//
// return src.defaultPrevented || src.returnValue === false ||
// src.getPreventDefault && src.getPreventDefault();
// };
webshims.createElement('summary', function(){
var details = isInterActiveSummary(this);
if(!details || $.data(this, 'detailsElement')){return;}
var timer;
var stopNativeClickTest;
var tabindex = $.attr(this, 'tabIndex') || '0';
bindDetailsSummary(this, details);
$(this)
.on({
'focus.summaryPolyfill': function(){
$(this).addClass('summary-has-focus');
},
'blur.summaryPolyfill': function(){
$(this).removeClass('summary-has-focus');
},
'mouseenter.summaryPolyfill': function(){
$(this).addClass('summary-has-hover');
},
'mouseleave.summaryPolyfill': function(){
$(this).removeClass('summary-has-hover');
},
'click.summaryPolyfill': function(e){
var details = isInterActiveSummary(this);
if(details){
if(!stopNativeClickTest && e.originalEvent){
stopNativeClickTest = true;
e.stopImmediatePropagation();
e.preventDefault();
$(this).trigger('click');
stopNativeClickTest = false;
return false;
} else {
clearTimeout(timer);
timer = setTimeout(function(){
if(!e.isDefaultPrevented()){
details.prop('open', !details.prop('open'));
}
}, 0);
}
}
},
'keydown.summaryPolyfill': function(e){
if( (e.keyCode == 13 || e.keyCode == 32) && !e.isDefaultPrevented()){
stopNativeClickTest = true;
e.preventDefault();
$(this).trigger('click');
stopNativeClickTest = false;
}
}
})
.attr({tabindex: tabindex, role: 'button'})
.prepend('<span class="details-open-indicator" />')
;
webshims.moveToFirstEvent(this, 'click');
});
var initDetails;
webshims.defineNodeNamesBooleanProperty('details', 'open', function(val){
var summary = $($.data(this, 'summaryElement'));
if(!summary){return;}
var action = (val) ? 'removeClass' : 'addClass';
var details = $(this);
if (!initDetails && options.animate){
details.stop().css({width: '', height: ''});
var start = {
width: details.width(),
height: details.height()
};
}
summary.attr('aria-expanded', ''+val);
details[action]('closed-details-summary').children().not(summary[0])[action]('closed-details-child');
if(!initDetails && options.animate){
var end = {
width: details.width(),
height: details.height()
};
details.css(start).animate(end, {
complete: function(){
$(this).css({width: '', height: ''});
}
});
}
});
webshims.createElement('details', function(){
initDetails = true;
var summary = getSummary(this);
$.prop(this, 'open', $.prop(this, 'open'));
initDetails = false;
});
});