views:

90

answers:

1

I found tvanfosson retheme plugin here. I simplified the plugin to suit my need. It works fine. I have a dropdown select list with numerical option values to choose stylesheets. But then I need to set a cookie for each style. The cookie is created, but had trouble to retain the cookie. Here is the modified plugin:

    ;(function($) {

    $.fn.extend({
        retheme: function(options,callback) {
            options = $.extend( {}, $.Retheme.defaults, options );

            this.each(function() {
                new $.Retheme(this,options,callback);
            });
            return this;
        }
    });

    $.Retheme = function( ctl, options, callback ) {
        if (options.styleSheet.match(/^\w/)) {
            options.styleSheet = '#' + options.styleSheet;
        } else {
            //add checking based on url
            options.styleSheet = $('link[href*=' + options.baseUrl + ']');
        }

        $(ctl).filter('select').change( function() {
            loadTheme( themeSelector(this), options );
            if (callback) callback.apply(this);
            return false;
        });
    };

    function themeSelector(ctl) {
        var $this = $(ctl);  
        var theme = $this.attr('href');
        if (!theme) {
            theme = $this.val();
        }

        return theme;
    }

    function loadTheme( theme, options ) {
        //var cookedHref = options.baseUrl + theme;
        //$.cookie('cthemeHref', cookedHref, { expires: 1 });
        //var themeHref = $.cookie('cthemeHref');

        var themeHref = options.baseUrl + theme;

        if (!themeHref.match(/\.css$/)) {
            themeHref += '.css';
        }

        var styleSheet = $(options.styleSheet);

        // set a cookie
        $.cookie('links', styleSheet, {expires: 1, path: '/'});

        var speed = options.speed;
        var delay = options.delay;

        var overlay = buildOverlay();
            overlay.fadeIn( speed, function() {
                styleSheet.attr( 'href', themeHref );
                // set a cookie
                $.cookie('css', themeHref , {expires: 1, path: '/'});
                alert($.cookie('css'));
                //$.get( themeHref, function() { // don't want to keep loading
                   setTimeout( function() {
                     overlay.fadeOut( speed, function() {
                       dieOverlay();
                     });
                   }, delay );
                 //});
               });
    };

    $.Retheme.defaults = {
        baseUrl: '/styles/',
        styleSheet: '',
        speed: 'slow',
        delay: 500
    };

})(jQuery);

Then I place the following to grab the cookies. The problem is all the link in href attributes are replaced by a single selected cookie while I want to grab or activate the cookie one at a time for each selected link.

if($.cookie('css')) {
    //$('link').attr('href', $.cookie('css')); //kill all other hrefs
    $.cookie('links').attr('href', $.cookie('css')); // $.cookie('links') is not a function
} else {
$(function() {
    $('#edit-apple').retheme({baseUrl: '/apple/style-'});
    $('#edit-orange').retheme({baseUrl: '/orange/style-'});
    $('#edit-mango').retheme({baseUrl: '/mango/style-'});
}

Notes: in apple folder I have stylesheets style-1.css, style-2.css, etc. With else the retheme fails. Without else, it works but no cookie grabbed.

As always any help would be very much appreciated. Thanks

+1  A: 

What is exactly the values stored inside $.cookie('css') and the $.cookie('links')?

Why not just store a css filename in a cookies, then when page load, check this cookie for a value. If the cookie exist, and the value is valid (contains a limited set of permitted value), then use that value and load the stylesheet file. If the cookie is not valid or not have any value yet, then load a default stylesheet page.

You can also take a look on how jquery ui themeroller implement theme switcher here.

Donny Kurnia
@Donny Kurnia: I made too many cookies I guess. Actually what I need is probably the $.cookie('css') part. Still working on it and have trouble to manage several different target links and several related select boxes like fruit and vegetables stuff with their child styles. That is why I need theme switcher with callback function. And thanks for the link, it will be much easier if only I had a single target link to work with :)
swan