<commit_before>/*!
 * wq.app - app.js
 * Utilizes store and pages to dynamically load and render
 * content from a wq.db-compatible REST service
 * (c) 2012 <NAME>
 * http://wq.io/license
 */

define(['./lib/jquery', './lib/jquery.mobile',
        './store', './pages', './template', './spinner',
        './lib/es5-shim'],
function($, jqm, ds, pages, tmpl, spin) {

var app = {};

app.init = function(config, templates, svc) {
    if (svc === undefined)
       svc = '';
    app.config = app.default_config = config;
    app.native = !!window.cordova;
    app.can_login = !!config.pages.login;
    ds.init(svc, {'format':'json'}, {'applyResult': _applyResult});
    pages.init();
    tmpl.init(templates, templates.partials, config.defaults);
    tmpl.setDefault('native', app.native);

    if (app.can_login) {
        var user = ds.get('user');
        if (user) {
            app.user = user;
            tmpl.setDefault('user', user);
            app.config = ds.get({'url': 'config'});
        }
        app.check_login();
        pages.register('logout\/?', app.logout);
    }

    if (config.transitions) {
        var def = "default";
        if (config.transitions[def])
            jqm.defaultPageTransition = config.transitions[def];
        if (config.transitions.dialog)
            jqm.defaultDialogTransition = config.transitions.dialog;
        if (config.transitions.save)
            _saveTransition = config.transitions.save;
        jqm.maxTransitionWidth = config.transitions.maxwidth || 800;
    }
    
    for (var page in app.config.pages) {
        var conf = _getConf(page);
        if (conf.list) {
            _registerList(page);
            _registerDetail(page);
            _registerEdit(page);
        } else if (conf) {
            _registerOther(page);
        }
    }

    $(document).on('submit', 'form', _handleForm);
}

app.logout = function() {
    if (!app.can_login)
        return;
    delete app.user;
    ds.set('user', null);
    tmpl.setDefault('user', null);
    app.config = app.default_config;
    ds.fetch({'url': 'logout'}, true, undefined, true);
};

app.save_login = function(user, config) {
    if (!app.can_login)
        return;
    app.config = config;
    ds.set({'url': 'config'}, config);
    app.user = user;
    tmpl.setDefault('user', user);
    ds.set('user', user);
};

app.check_login = function() {
    if (!app.can_login)
        return;
    ds.fetch({'url': 'login'}, false, function(result) {
        if (result && result.user && result.config) {
            app.save_login(result.user, result.config);
        } else if (result && app.user) {
            app.logout();
        }
    }, true);
};

// Internal variables and functions
var _saveTransition = "none";

// Wrappers for pages.register & pages.go to handle common use cases

// Determine appropriate context & template for pages.go
app.go = function(page, ui, params, itemid, edit, url) {
    if (ui && ui.options && ui.options.data) return; // Ignore form actions
    var conf = _getConf(page);
    if (!conf.list) {
        _renderOther(page, ui, params);
        return;
    }<commit_msg>custom events to track login status<commit_after>/*!
 * wq.app - app.js
 * Utilizes store and pages to dynamically load and render
 * content from a wq.db-compatible REST service
 * (c) 2012 S. <NAME>
 * http://wq.io/license
 */

define(['./lib/jquery', './lib/jquery.mobile',
        './store', './pages', './template', './spinner',
        './lib/es5-shim'],
function($, jqm, ds, pages, tmpl, spin) {

var app = {};

app.init = function(config, templates, svc) {
    if (svc === undefined)
       svc = '';
    app.config = app.default_config = config;
    app.native = !!window.cordova;
    app.can_login = !!config.pages.login;
    ds.init(svc, {'format':'json'}, {'applyResult': _applyResult});
    pages.init();
    tmpl.init(templates, templates.partials, config.defaults);
    tmpl.setDefault('native', app.native);

    if (app.can_login) {
        var user = ds.get('user');
        if (user) {
            app.user = user;
            tmpl.setDefault('user', user);
            tmpl.setDefault('is_authenticated', true);
            app.config = ds.get({'url': 'config'});
            $('body').trigger('login');
        }
        app.check_login();
        pages.register('logout\/?', app.logout);
    }

    if (config.transitions) {
        var def = "default";
        if (config.transitions[def])
            jqm.defaultPageTransition = config.transitions[def];
        if (config.transitions.dialog)
            jqm.defaultDialogTransition = config.transitions.dialog;
        if (config.transitions.save)
            _saveTransition = config.transitions.save;
        jqm.maxTransitionWidth = config.transitions.maxwidth || 800;
    }
    
    for (var page in app.config.pages) {
        var conf = _getConf(page);
        if (conf.list) {
            _registerList(page);
            _registerDetail(page);
            _registerEdit(page);
        } else if (conf) {
            _registerOther(page);
        }
    }

    $(document).on('submit', 'form', _handleForm);
}

app.logout = function() {
    if (!app.can_login)
        return;
    delete app.user;
    ds.set('user', null);
    tmpl.setDefault('user', null);
    tmpl.setDefault('is_authenticated', false);
    app.config = app.default_config;
    ds.fetch({'url': 'logout'}, true, undefined, true);
    $('body').trigger('logout');
};

app.save_login = function(user, config) {
    if (!app.can_login)
        return;
    app.config = config;
    ds.set({'url': 'config'}, config);
    app.user = user;
    tmpl.setDefault('user', user);
    tmpl.setDefault('is_authenticated', true);
    ds.set('user', user);
    $('body').trigger('login');
};

app.check_login = function() {
    if (!app.can_login)
        return;
    ds.fetch({'url': 'login'}, false, function(result) {
        if (result && result.user && result.config) {
            app.save_login(result.user, result.config);
        } else if (result && app.user) {
            app.logout();
        }
    }, true);
};

// Internal variables and functions
var _saveTransition = "none";

// Wrappers for pages.register & pages.go to handle common use cases

// Determine appropriate context & template for pages.go
app.go = function(page, ui, params, itemid, edit, url) {
    if (ui && ui.options && ui.options.data) return; // Ignore form actions
    var conf = _getConf(page);
    if (!conf.list) {
        _renderOther(page, ui, params);
        return;
    }
{'fraction_non_alphanumeric': 0.16166104811523138, 'fraction_numerical': 0.002451731535396874, 'mean_word_length': 2.4443271767810026, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1000779', 'n_tokens_mistral': 2081, 'n_tokens_neox': 2050, 'n_words': 457}