<commit_before>﻿// Conventions
//	1) All Views must have their HTML element tags
//      include a CSS class named .view.
// 	2) Before registering routes with router,
//      the viewmodels should exist.
// ----------------------------------------------
define(['jquery', 'underscore','sammy', 'presenter','config', 'routeMediator'],
    function ($, _, Sammy, presenter, config, routeMediator) {
        var
            window = config.window,
            logger = config.logger,
            isRedirecting = false,
            startupUrl = '',

            sammy = new Sammy.Application(function () {
                if (Sammy.Title) {
                    this.use(Sammy.Title);
                    this.setTitle(config.title);
                }else {
                    logger.warning('Sammy.Title is not loaded.');
                }

                registerBeforeLeaving();

                this.get('', function () {
                    this.app.runRoute('get', startupUrl);
                });
            }),

            register = function (options) {
                if (options.routes) {
                    _.each(options.routes, function (route) {
                        registerRoute(route.route, route.title, route.callback, options.view, route.group);
                    });
                    return;
                }

                registerRoute(options.route, options.title, options.callback, options.view, options.group);
            },

            registerRoute = function (route, title, callback, view, group) {
                if (!callback) {
                    throw Error('callback must be specified.');
                }
                //var hash = new RegExp('#\\/' + route + '.*')
                //var hash = new RegExp('\\^' + route + '$.*')

                sammy.get(route, function (context) {
                    //context is 'this'
                    callback(context.params);
                    $('.view').hide();
                    presenter.transitionTo($(view), context.path, group);
                    //context.$element().append('<h1>hello</h1>') //PAPA: for testing
                    this.title(title);
                });
            },

            navigateTo = function (url) {
                //window.location.href = url;
                sammy.setLocation(url); //TODO: test this
            },

            registerBeforeLeaving = function () {
                sammy.before(/.*/, function () {
                    // Can cancel the route if this returns false
                    var
                        context = this,
                        response = routeMediator.canLeave();

                    if (!isRedirecting && !response.val) {
                        isRedirecting = true;
                        logger.warning(response.message);
                        // Keep hash url the same in address bar
                        window.history.back();
                        //this.redirect('#/Sessions'); 
                    }
                    else {
                        isRedirecting = false;
                    }
                    return response.val;
                });
            },

            run = function (url) {
                startupUrl = url;
                sammy.run();
                navigateTo(url);
            };

        return {
            navigateTo: navigateTo,
            register: register,
            run: run
        };
    });<commit_msg> I think John broke it in 193c41when he moved registerBeforeLeaving inside the function that configures the new Sammy.Application (your last checkin). I couldn't figure out what should really happen so I just commented it out ... and now it "works".<commit_after>﻿// Conventions
//	1) All Views must have their HTML element tags
//      include a CSS class named .view.
// 	2) Before registering routes with router,
//      the viewmodels should exist.
// ----------------------------------------------
define(['jquery', 'underscore','sammy', 'presenter','config', 'routeMediator'],
    function ($, _, Sammy, presenter, config, routeMediator) {
        var
            window = config.window,
            logger = config.logger,
            isRedirecting = false,
            startupUrl = '',

            sammy = new Sammy.Application(function () {
                if (Sammy.Title) {
                    this.use(Sammy.Title);
                    this.setTitle(config.title);
                }else {
                    logger.warning('Sammy.Title is not loaded.');
                }

                //registerBeforeLeaving();

                this.get('', function () {
                    this.app.runRoute('get', startupUrl);
                });
            }),

            register = function (options) {
                if (options.routes) {
                    _.each(options.routes, function (route) {
                        registerRoute(route.route, route.title, route.callback, options.view, route.group);
                    });
                    return;
                }

                registerRoute(options.route, options.title, options.callback, options.view, options.group);
            },

            registerRoute = function (route, title, callback, view, group) {
                if (!callback) {
                    throw Error('callback must be specified.');
                }
                //var hash = new RegExp('#\\/' + route + '.*')
                //var hash = new RegExp('\\^' + route + '$.*')

                sammy.get(route, function (context) {
                    //context is 'this'
                    callback(context.params);
                    $('.view').hide();
                    presenter.transitionTo($(view), context.path, group);
                    //context.$element().append('<h1>hello</h1>') //PAPA: for testing
                    this.title(title);
                });
            },

            navigateTo = function (url) {
                //window.location.href = url;
                sammy.setLocation(url); //TODO: test this
            },

            registerBeforeLeaving = function () {
                sammy.before(/.*/, function () {
                    // Can cancel the route if this returns false
                    var
                        context = this,
                        response = routeMediator.canLeave();

                    if (!isRedirecting && !response.val) {
                        isRedirecting = true;
                        logger.warning(response.message);
                        // Keep hash url the same in address bar
                        window.history.back();
                        //this.redirect('#/Sessions'); 
                    }
                    else {
                        isRedirecting = false;
                    }
                    return response.val;
                });
            },

            run = function (url) {
                startupUrl = url;
                sammy.run();
                navigateTo(url);
            };

        return {
            navigateTo: navigateTo,
            register: register,
            run: run
        };
    });
{'fraction_non_alphanumeric': 0.1244413407821229, 'fraction_numerical': 0.0018156424581005587, 'mean_word_length': 1.1130126881085867, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7041128', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1578, 'n_words': 401}