<commit_before>﻿angular.module('dbPlannerApp').controller('DatabasesController',
                ['$scope', '$mdDialog', '$mdToast', '$mdSidenav', '$mdComponentRegistry', 'CurrentDatabaseService', 'FileGeneratorService',
        function ($scope, $mdDialog, $mdToast, $mdSidenav, $mdComponentRegistry, CurrentDatabaseService, FileGeneratorService) {
            $scope.setFocus = function (elementId) {
                document.getElementById(elementId).focus();
            };

            function showToast(text) {
                $mdToast.show(
                    $mdToast.simple()
                    .textContent(text)
                    .position('bottom left')
                    .hideDelay(3000)
                );
            }

            /* Databases */
            $scope.databases = [];

            $scope.toggleDatabases = function () {
                $mdSidenav('databases').toggle()
                    .then(function () {
                        $scope.setFocus('databaseNameEditor');
                    });
            };
            $scope.closeDatabases = function () {
                $mdComponentRegistry.when('databases').then(function () {
                    var sidenav = $mdSidenav('databases');
                    if (sidenav.isOpen()) {
                        sidenav.close();
                        $scope.setFocus('tableNameEditor');
                    }
                });
            };

            $scope.selectDatabase = function (database) {
                CurrentDatabaseService.set(database);
                $scope.tableNameEditor = '';
                $scope.closeDatabases();
            };
            $scope.getCurrentDatabase = function () {
                return CurrentDatabaseService.get();
            };
            $scope.addDatabase = function (name) {
                if (name.trim().length === 0) {
                    return;
                }

                var database = {
                    name: name,
                    tables: []
                };

                $scope.databases.push(database);
                $scope.databaseNameEditor = '';
                $scope.selectDatabase(database);
            };
            $scope.deleteDatabase = function (ev, index) {
                function deleteWithoutConfirm(index) {
                    var isSelected = database === CurrentDatabaseService.get();
                    $scope.databases.splice(index, 1);

                    if (isSelected && $scope.databases.length > 0) {
                        $scope.selectDatabase($scope.databases[0]);
                    }
                }

                var database = $scope.databases[index];

                var confirm = $mdDialog.confirm()
                  .clickOutsideToClose(true)
                  .title('Delete database: ' + database.name + '?')
                  .ariaLabel('Confirm delete database')
                  .targetEvent(ev)
                  .ok('Delete')
                  .cancel('Cancel');

                $mdDialog.show(confirm).then(function () {
                    deleteWithoutConfirm(index);
                });
            };

            /* Tables */
            $scope.selectTable = function (table) {
                $scope.selectedTable = table;
            };
            $scope.addTable = function (name) {
                if (name.trim().length === 0) {
                    return;
                }

                var table = {
                    name: name,
                    columns: []
                };

                CurrentDatabaseService.get().tables.push(table);
                $scope.tableNameEditor = '';
                $scope.selectTable(table);
            };<commit_msg>Confirm database delete if more than one table<commit_after>﻿angular.module('dbPlannerApp').controller('DatabasesController',
                ['$scope', '$mdDialog', '$mdToast', '$mdSidenav', '$mdComponentRegistry', 'CurrentDatabaseService', 'FileGeneratorService',
        function ($scope, $mdDialog, $mdToast, $mdSidenav, $mdComponentRegistry, CurrentDatabaseService, FileGeneratorService) {
            $scope.setFocus = function (elementId) {
                document.getElementById(elementId).focus();
            };

            function showToast(text) {
                $mdToast.show(
                    $mdToast.simple()
                    .textContent(text)
                    .position('bottom left')
                    .hideDelay(3000)
                );
            }

            /* Databases */
            $scope.databases = [];

            $scope.toggleDatabases = function () {
                $mdSidenav('databases').toggle()
                    .then(function () {
                        $scope.setFocus('databaseNameEditor');
                    });
            };
            $scope.closeDatabases = function () {
                $mdComponentRegistry.when('databases').then(function () {
                    var sidenav = $mdSidenav('databases');
                    if (sidenav.isOpen()) {
                        sidenav.close();
                        $scope.setFocus('tableNameEditor');
                    }
                });
            };

            $scope.selectDatabase = function (database) {
                CurrentDatabaseService.set(database);
                $scope.tableNameEditor = '';
                $scope.closeDatabases();
            };
            $scope.getCurrentDatabase = function () {
                return CurrentDatabaseService.get();
            };
            $scope.addDatabase = function (name) {
                if (name.trim().length === 0) {
                    return;
                }

                var database = {
                    name: name,
                    tables: []
                };

                $scope.databases.push(database);
                $scope.databaseNameEditor = '';
                $scope.selectDatabase(database);
            };
            $scope.deleteDatabase = function (ev, index) {
                var database = $scope.databases[index];

                function deleteWithoutConfirm(index) {
                    var isSelected = database === CurrentDatabaseService.get();
                    $scope.databases.splice(index, 1);

                    if (isSelected && $scope.databases.length > 0) {
                        $scope.selectDatabase($scope.databases[0]);
                    }
                }

                var confirm = $mdDialog.confirm()
                  .clickOutsideToClose(true)
                  .title('Delete database: ' + database.name + '?')
                  .ariaLabel('Confirm delete database')
                  .targetEvent(ev)
                  .ok('Delete')
                  .cancel('Cancel');

                if (database.tables.length > 0) {
                    $mdDialog.show(confirm).then(function () {
                        deleteWithoutConfirm(index);
                    });
                } else {
                    deleteWithoutConfirm(index);
                }
            };

            /* Tables */
            $scope.selectTable = function (table) {
                $scope.selectedTable = table;
            };
            $scope.addTable = function (name) {
                if (name.trim().length === 0) {
                    return;
                }

                var table = {
                    name: name,
                    columns: []
                };

                CurrentDatabaseService.get().tables.push(table);
                $scope.tableNameEditor = '';
                $scope.selectTable(table);
            };
{'fraction_non_alphanumeric': 0.11540983606557377, 'fraction_numerical': 0.0024918032786885244, 'mean_word_length': 1.1888633754305395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '394405', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1668, 'n_words': 264}