summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/JQTouch/extensions/jqt.themeswitcher.js
blob: ef3a75d8643ef0169c2d7cbba2f5c03518dc64df (plain)
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
/*

            _/    _/_/    _/_/_/_/_/                              _/
               _/    _/      _/      _/_/    _/    _/    _/_/_/  _/_/_/
          _/  _/  _/_/      _/    _/    _/  _/    _/  _/        _/    _/
         _/  _/    _/      _/    _/    _/  _/    _/  _/        _/    _/
        _/    _/_/  _/    _/      _/_/      _/_/_/    _/_/_/  _/    _/
       _/
    _/

    Documentation and issue tracking on Google Code <http://code.google.com/p/jqtouch/>

    (c) 2011 by jQTouch project members.
    See LICENSE.txt for license.

*/

(function($) {
    if ($.jQTouch) {

        var scriptpath = $("script").last().attr("src").split('?')[0].split('/').slice(0, -1).join('/')+'/';

        $.jQTouch.addExtension(function ThemeSwitcher(jQT) {

            var current,
                link,
                titles = {},
                defaults = {
                    themeStyleSelector: 'link[rel="stylesheet"][title]',
                    themeIncluded: [
                        {title: 'jQTouch', href: scriptpath + '../themes/css/jqtouch.css'},
                        {title: 'Apple', href: scriptpath + '../themes/css/apple.css'},
                        {title: 'Vanilla', href: scriptpath + '../themes/css/vanilla.css'}

                    ]
                },
                options = $.extend({}, defaults, jQT.settings);

            function setStyleState(item, title) {
                var $item = $(item);

                if ($item.attr('title') === title) {
                    item.disabled = false; // workaround for Firefox on Zepto
                    $item.removeAttr('disabled');
                } else {
                  item.disabled = true; // workaround for Firefox on Zepto
                  $item.attr('disabled', true);
                }
            }

            function initializeStyleState(item, title) {
              // and, workaround for WebKit by initializing the 'disabled' attribute
              if (!current) {
                  current = title;
              }
              setStyleState(item, current);
            }

            // public
            function switchStyle(title) {
                current = title;
                $(options.themeStyleSelector).each(function(i, item) {
                    setStyleState(item, title);
                });
            }

            // collect title names, from <head>
            $(options.themeStyleSelector).each(function(i, item) {
                var $item = $(item);
                var title = $item.attr('title');

                titles[title] = true;

                initializeStyleState(item, title);
            });

            // add included theme
            for (var i=0; i < options.themeIncluded.length; i++) {
                var hash = options.themeIncluded[i];
                if (!(hash.title in titles)) {
                    link = $('<link title="' + hash.title + '" href="' + hash.href + '" rel="stylesheet">');
                    $('head').append($(link));

                    titles[hash.title] = true;

                    initializeStyleState(link, hash.title);
                }
            }

            if (options.themeSelectionSelector) {
                // create UI items
                for (var title in titles) {
                    var $item = $('<li><a href="#" data-title="' + title + '">' + title + '</a></li>');
                    $(options.themeSelectionSelector).append($item);
                }

                // bind to UI items
                $(options.themeSelectionSelector).delegate('* > a', 'tap', function(e) {
                    e.preventDefault();
                    e.stopPropagation();

                    var $a = $(this).closest('a');
                    $a.removeClass('active');
                    switchStyle($a.attr('data-title'));

                    // poor-man simulation of radio button behaviour
                    $(options.themeSelectionSelector).find('a').removeClass('selected');
                    $a.addClass('selected');
                });

                // poor-man simulation of radio button behaviour
                $(options.themeSelectionSelector).closest('#jqt > *').bind('pageAnimationEnd', function(e, data){
                    if (data.direction === 'in') {
                        $(options.themeSelectionSelector).find('a[data-title="' + current + '"]').addClass('selected');
                    }
                });
            }

            return {switchStyle: switchStyle};

        });
    }
})($);