0

Using jQuery mobile 1.3.2, I have a PhoneGap application in which I would like to update an initial login screen to reflect a data theme, based on the state of the application. login page html is:

login html:

<div id="login" data-role="page">

    <div data-role="header">
        <h1>Survey login</h1>
    </div>



    <div data-role="content">
        <!--div id="logincontent"></div-->

        <form id="form-login">
            <div data-role="fieldcontain" class="ui-hide-label">
                <label for="login-password">Password:</label>
                <input type="password" name="login-password" id="login-password" value="" placeholder="Password" />
            </div>
            <a href="#" id="login-button" data-role="button" onclick="checkLogin()">Login</a>
        </form>
    </div>



    <div data-role="footer" id="login-footer" data-theme="a">
        <h4 id="login-footer-header">UIC &amp; EVL</h4>
    </div>

</div>

JS function to change theme of login page:

    function displayAppStatus(type){
        if(type == 'suspend'){
            $("#login").page({theme:'g'});
            $("#login").trigger('create');
            $("#login-footer-header").text("Log in to break suspension");

        }
        else if(type == 'bedtime'){
            $("#login").page({theme:'f'});
            $("#login").trigger('create');
            $("#login-footer-header").text("Log in to break bedtime");
        }
        else if(type == 'delay'){
            $("#login").page({theme:'h'});
            $("#login").trigger('create');
            $("#login-footer-header").text("Log in to break delayed notification");
        }
        //Cancel appStatus display
        else if(type == 'cancel'){
            $("#login").page({theme:'a'});
            $("#login").trigger('create');
            $("#login-footer-header").text("UIC & EVL");
        }
    }

linked stylesheets & scripts (just in case):

    <link rel="stylesheet" href="css/main.css" />
    <link rel="stylesheet" href="css/themes/ecig/ecig_themes.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile.structure-1.3.2.css" />

    <script src="cordova.js"></script>

    <script src="js/jquery-1.10.2.js"></script>
    <script src="js/jquery.mobile-1.3.2.js"></script>

And I call displayAppStatus throughout my code, if a user delays a notification, or suspends any notifications, or puts the app to sleep.

What happens is that I will see the login page flash the color of the correct data-theme, but then the theme for the page will switch back to the default quickly.

I have been here: Changing JQuery Mobile data-theme dynamically and jQuery mobile dynamically added theme to page

but neither of these threads have solved my problem.

Community
  • 1
  • 1
Gthoma2
  • 687
  • 1
  • 9
  • 22

1 Answers1

0

That is very tricky specially because jquery mobile create a lot of CSS not visible in the code, to apply its styles.

I am pasting my bits of code that are working and allowing me to change the them of a page based on a product category. Here I am changing mainly the nav, header and footer styles, but just pass to the function the class you want to change.

Hope it helps!

Here is how I call my function "Element_theme_refresh", newTheme is basically the jquery mobile theme names.

function UpdateNavBarStyles ()
{
    var newTheme = AssignMeTheme ( sessionStorage.categoriaID );
    Element_theme_refresh ( '#pag_fichaProducto', newTheme );
    Element_theme_refresh ( '.ui-header', newTheme );
    Element_theme_refresh ( '.ui-footer', newTheme );
    Element_theme_refresh ( '.menuPanelButton', newTheme ); // Once the collapsible exists, update its theme
    Element_theme_refresh ( '.backButton', newTheme );          // Back button, apply new theme
    Element_theme_refresh ( '.quoteBtn', newTheme );            // Quote Cart widget button, update its theme
    Element_theme_refresh ( '.syncroBtn', newTheme );           // Data Syncronization button, update its theme
}

And here the actual function.

/* This function query the element for the current applied jquery mobile theme and change
it to the input data-theme or swatch */
function Element_theme_refresh( element, newTheme )
{
    var curTheme = $(element).attr('data-theme') || 'a';

    $(element).attr('data-theme', newTheme);

    if( $(element).attr('class') ) {
        // Theme classes end in "-[a-z]$", so match that
        var classPattern = new RegExp('-' + curTheme + '$');
        newTheme = '-' + newTheme;

        var classes =  $(element).attr('class').split(' ');

        for( var key in classes ) {
            if( classPattern.test( classes[key] ) ) {
                classes[key] = classes[key].replace( classPattern, newTheme );
            }
        }

        $(element).attr('class', classes.join(' '));
    }
}
VicM
  • 2,449
  • 1
  • 18
  • 20