This is a plugin for inserting content, which is for fallback for CSS generated content for IE
Please review and suggest optimization tips and or improvement of code. See the TODO in code and offer advice.
//jQuery plugin for generating content
(function( $ ){
$.fn.genContent = function($$options) {
//;;;_debug(this);
var $settings = $.extend({}, $.fn.genContent.defaults, $$options);
return this.each(function() {
$this = $(this);
//If metadata plugin present, use it http://plugins.jquery.com/project/metadata
var o = $.meta ? $.extend({}, $settings, $this.data()) : $settings;
//update element styles
$this.css({
margin : o.elStyles.margin,
padding : o.elStyles.padding
});
//TODO: Build in support for custom conditions
if ( this.previousSibling != null ) {
switch(o.placement){
case 'prepend':
var $markup = $.fn.genContent.format(o);
$($markup)
.prependTo($this);
break;
case 'append':
var $markup = $.fn.genContent.format(o);
$($markup)
.appendTo($this);
break;
case 'before':
var $markup = $.fn.genContent.format(o);
$($markup)
.insertBefore($this);
break;
case 'after':
var $markup = $.fn.genContent.format(o);
$($markup)
.insertAfter($this);
break;
default:
var $markup = $.fn.genContent.format(o);
$($markup)
.prependTo($this);
}
}
});
};
//
// private function for debugging
//
function _debug($obj) {
alert( 'elements affected count: ' + $obj.size() );
};
//
//Expose the format function
//We expose this function to customization for other uses.
//May re-factor for use with jQuery Template Plugin.
//NOTE: We could use a css class to style this element
// however I like the flexibility of controlling style
// with the plugin options and this exposed function.
//
$.fn.genContent.format = function(o) {
return '<' + o.wrap +
' style="' + o.wrapStyles.margin +
'' + o.wrapStyles.padding +
'">' + o.content + '</' + o.wrap + '>';
};
//
//Plugin Defaults
//
$.fn.genContent.defaults = {
'placement' : 'prepend', //{before,after,prepend,append}
'elStyles' : {
'margin' : '0',
'padding' : '0'
},
'wrap' : 'span',
'wrapStyles' : {
'margin' : ' margin: .8em;',
'padding' : ' padding: 0;'
},
'content' : '•'
};
})( jQuery );