<commit_before>'use strict';

// start the angular app after the app's json schema file has been loaded
angular.element(document).ready(function() {
    $.ajax({
        url: '/simulation-schema?' + SIREPO_APP_VERSION,
        data: {
            simulationType: SIREPO_APP_NAME,
        },
        success: function(result) {
            APP_SCHEMA = result;
            angular.bootstrap(document, ['SirepoApp']);
        },
        error: function(xhr, status, err) {
            if (! APP_SCHEMA)
                console.log("schema load failed: ", err);
        },
        method: 'POST',
        dataType: 'json',
    });
});

var app_local_routes = {
    simulations: '/simulations',
    source: '/source/:simulationId',
    notFound: '/not-found',
    notFoundCopy: '/copy-session/:simulationIds',
};

var appDefaultSimulationValues = {};

var app = angular.module('SirepoApp', ['ngDraggable', 'ngRoute', 'd3', 'shagstrom.angular-split-pane']);

app.value('localRoutes', app_local_routes);

app.config(function($routeProvider, localRoutesProvider) {
    var localRoutes = localRoutesProvider.$get();
    $routeProvider
        .when(localRoutes.simulations, {
            controller: 'SimulationsController as simulations',
            templateUrl: '/static/html/simulations.html?' + SIREPO_APP_VERSION,
        })
        .when(localRoutes.notFound, {
            templateUrl: '/static/html/not-found.html?' + SIREPO_APP_VERSION,
        })
        .when(localRoutes.notFoundCopy, {
            controller: 'NotFoundCopyController as notFoundCopy',
            templateUrl: '/static/html/copy-session.html?' + SIREPO_APP_VERSION,
        })
        .otherwise({
            redirectTo: localRoutes.simulations,
        });
});

app.factory('activeSection', function($route, $rootScope, $location, appState) {
    var self = this;

    self.getActiveSection = function() {
        var match = ($location.path() || '').match(/^\/([^\/]+)/);
        return match
            ? match[1]
            : null;
    };

    $rootScope.$on('$routeChangeSuccess', function() {
        if ($route.current.params.simulationId)
            appState.loadModels($route.current.params.simulationId);
    });

    return self;
});

app.factory('appState', function(requestSender, $rootScope, $timeout) {
    var self = {};
    self.models = {};
    var autoSaveTimer = null;
    var savedModelValues = {};

    function broadcastClear() {
        $rootScope.$broadcast('clearCache');
    }

    function broadcastChanged(name) {
        $rootScope.$broadcast(name + '.changed');
        $rootScope.$broadcast('modelChanged', name);
    }

    function broadcastLoaded() {
        $rootScope.$broadcast('modelsLoaded');
    }

    function updateReports() {
        broadcastClear();
        for (var key in self.models) {
            if (self.isReportModelName(key))
                broadcastChanged(key);
        }
    }

    self.applicationState = function() {
        return savedModelValues;
    };

    self.autoSave = function(callback) {
        if (! self.isLoaded)
            return;
        self.resetAutoSaveTimer();
        requestSender.sendRequest(
            'saveSimulationData',
            callback,
            {
                models: savedModelValues,
                simulationType: APP_SCHEMA.simulationType,
            });
    };

    self.cancelChanges = function(name) {
        // cancel changes on a model by name, or by an array of names
        if (typeof(name) == 'string')
            name = [name];

        for (var i = 0; i < name.length; i++) {
            if (savedModelValues[name[i]])
                self.models[name[i]] = self.clone(savedModelValues[name[i]]);
            $rootScope.$broadcast('cancelChanges', name[i]);
        }
    };

    self.clearModels = function(emptyValues) {
        broadcastClear();
        self.models = emptyValues || {};<commit_msg>auto-save no longer calls server if data has not changed since last check<commit_after>'use strict';

// start the angular app after the app's json schema file has been loaded
angular.element(document).ready(function() {
    $.ajax({
        url: '/simulation-schema?' + SIREPO_APP_VERSION,
        data: {
            simulationType: SIREPO_APP_NAME,
        },
        success: function(result) {
            APP_SCHEMA = result;
            angular.bootstrap(document, ['SirepoApp']);
        },
        error: function(xhr, status, err) {
            if (! APP_SCHEMA)
                console.log("schema load failed: ", err);
        },
        method: 'POST',
        dataType: 'json',
    });
});

var app_local_routes = {
    simulations: '/simulations',
    source: '/source/:simulationId',
    notFound: '/not-found',
    notFoundCopy: '/copy-session/:simulationIds',
};

var appDefaultSimulationValues = {};

var app = angular.module('SirepoApp', ['ngDraggable', 'ngRoute', 'd3', 'shagstrom.angular-split-pane']);

app.value('localRoutes', app_local_routes);

app.config(function($routeProvider, localRoutesProvider) {
    var localRoutes = localRoutesProvider.$get();
    $routeProvider
        .when(localRoutes.simulations, {
            controller: 'SimulationsController as simulations',
            templateUrl: '/static/html/simulations.html?' + SIREPO_APP_VERSION,
        })
        .when(localRoutes.notFound, {
            templateUrl: '/static/html/not-found.html?' + SIREPO_APP_VERSION,
        })
        .when(localRoutes.notFoundCopy, {
            controller: 'NotFoundCopyController as notFoundCopy',
            templateUrl: '/static/html/copy-session.html?' + SIREPO_APP_VERSION,
        })
        .otherwise({
            redirectTo: localRoutes.simulations,
        });
});

app.factory('activeSection', function($route, $rootScope, $location, appState) {
    var self = this;

    self.getActiveSection = function() {
        var match = ($location.path() || '').match(/^\/([^\/]+)/);
        return match
            ? match[1]
            : null;
    };

    $rootScope.$on('$routeChangeSuccess', function() {
        if ($route.current.params.simulationId)
            appState.loadModels($route.current.params.simulationId);
    });

    return self;
});

app.factory('appState', function(requestSender, $rootScope, $timeout) {
    var self = {};
    self.models = {};
    var lastAutoSaveData = null;
    var autoSaveTimer = null;
    var savedModelValues = {};

    function broadcastClear() {
        $rootScope.$broadcast('clearCache');
    }

    function broadcastChanged(name) {
        $rootScope.$broadcast(name + '.changed');
        $rootScope.$broadcast('modelChanged', name);
    }

    function broadcastLoaded() {
        $rootScope.$broadcast('modelsLoaded');
    }

    function updateReports() {
        broadcastClear();
        for (var key in self.models) {
            if (self.isReportModelName(key))
                broadcastChanged(key);
        }
    }

    self.applicationState = function() {
        return savedModelValues;
    };

    self.autoSave = function(callback) {
        if (! self.isLoaded)
            return;
        self.resetAutoSaveTimer();
        if (lastAutoSaveData && self.deepEquals(lastAutoSaveData, savedModelValues)) {
            // no changes
            if (callback)
                callback();
            return;
        }
        lastAutoSaveData = self.clone(savedModelValues);
        requestSender.sendRequest(
            'saveSimulationData',
            callback,
            {
                models: savedModelValues,
                simulationType: APP_SCHEMA.simulationType,
            });
    };

    self.cancelChanges = function(name) {
        // cancel changes on a model by name, or by an array of names
        if (typeof(name) == 'string')
            name = [name];

        for (var i = 0; i < name.length; i++) {
            if (savedModelValues[name[i]])
                self.models[name[i]] = self.clone(savedModelValues[name[i]]);
            $rootScope.$broadcast('cancelChanges', name[i]);
        }
    };

    self.clearModels = function(emptyValues) {
        broadcastClear();
        self.models = emptyValues || {};
{'fraction_non_alphanumeric': 0.1349527665317139, 'fraction_numerical': 0.000736105999263894, 'mean_word_length': 2.299069202751922, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5203049', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2297, 'n_words': 443}