2
\$\begingroup\$

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'   : '&#149'
    };
})( jQuery );
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
//jQuery plugin for generating content
(function( $ ){
var $_func = $.fn.genContent = function($$options) {
    //;;;_debug(this);
    var $settings = $.extend({}, $_func.defaults, $$options);
    return this.each(function() {
        ///If metadata plugin present, use it http://plugins.jquery.com/project/metadata/update element styles          
        var $this = $(this),
            o = $.meta ? $.extend({}, $settings, $this.data()) : $settings,
            o_elStyles,
            $markup;

        $this.css({
            margin  :   o_elStyles.margin,
            padding :   o_elStyles.padding  
            });
        //TODO: Build in support for custom conditions
        if ( this.previousSibling != null ) {
            $markup = $.fn.genContent.format(o);
            switch(o.placement){
            case 'append':
                    $($markup)
                    .appendTo($this);
                break;
            case 'before':
                    $($markup)
                    .insertBefore($this);
                break;
            case 'after':
                    $($markup)
                    .insertAfter($this);
                break;
            case 'prepend':
            default:
                    $($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.
//
$_func.format = function(o) {
    var o_wrap = o.wrap,
        o_wrapStyles = o.wrapStyles;
    return '<' + o_wrap + 
    ' style="' + o_wrapStyles.margin + 
    '' + o_wrapStyles.padding + 
    '">' + o.content + '</' + o_wrap + '>';
};  
//
//Plugin Defaults
//
$_func.defaults = {
        'placement' : 'prepend', //{before,after,prepend,append}
        'elStyles'  : {
            'margin'    :   '0',
            'padding'   :   '0'
        },
        'wrap'  : 'span',
        'wrapStyles' : {
            'margin'    : ' margin: .8em;',
            'padding'   : ' padding: 0;'
        },
        'content'   : '&#149'
    };
})( jQuery );

What I've done is, collected your variables to top of the scope, created new variables to reduce property lookup and reduced a few lines of code because of repetition. Also, I've found, running through the Google Closure compiler with --compilation_level SIMPLE_OPTIMIZATIONS doesn't hurt ;)

On your TODO, the custom conditions can be stored as closures, which can be contained in an object or array - array maybe better. Then loop through the array - yeap, array is better - calling the closures to see if conditions succeed, and then do what you want with it.

\$\endgroup\$
2
  • \$\begingroup\$ thank you for the review and comments. You used an underscore for the var $_func=... I understand this to be used on private functions, please share with me why an underscore was used in your optimization. Thank you again! \$\endgroup\$
    – Upworks
    Commented Sep 12, 2011 at 16:45
  • \$\begingroup\$ I use underscores to write variable names. However it only becomes considered private if it starts with _ ot __. \$\endgroup\$
    – NebulaFox
    Commented Sep 12, 2011 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.