Skip to content

Commit

Permalink
jquery reiant details polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqname committed Jan 13, 2012
1 parent 8023a83 commit fb5356b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions details.polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
jQuery.fn.extend({
addRule : function (rule) {
rule = '\n' + rule;
return this.each(function(){
if (this.nodeName.toLowerCase() === 'style') {
if (this.styleSheet && this.styleSheet.cssText !== undefined) { //for ie
this.styleSheet.cssText = rule;
} else { this.appendChild(document.createTextNode(rule)); }
}
});
}
});
$(document).ready(function(){
'use strict';
var $deets = $('details'),
$deetStyles = $('<style />').attr('type', 'text/css'),
rules = 'details { display: block; overflow:hidden; } \n' +
'details[open] { height: auto; } \n' +
'summary { display: block; }';
$deetStyles.addRule(rules);
$deets.each(function(){
var detailsID = 'd' + Math.floor(Math.random() * 10000),
$deet = $(this).attr('data-detailsID', detailsID),
$summary = $deet.find('summary'),
height;

//Can't assume there is a summary element
if (! $summary.length) {
$summary = $('<summary>Details</summary>');
$deet.prepend($summary);
}
height = $summary.height();
$deetStyles.addRule('details[data-detailsid="' + detailsID + '"] { height: ' + height + 'px; }\n' +
'details[data-detailsid="' + detailsID + '"][open] { height: auto; }');
});
$('head').append($deetStyles);
$('summary').on('click', function(e){
var $detail = $(e.target).parents('details').first();
if ($detail.attr('open')) { $detail.removeAttr('open');
} else { $detail.attr('open', 'open'); }
});
});

0 comments on commit fb5356b

Please sign in to comment.