<commit_before>/* global angular: true */

angular.module('webPGQ')
    .controller('MainController', [
        '$scope', '$cookies', '$timeout', '$location', '$window', '$', 'graph', 'keybinding', 'logger', 'queueDigest', 'sql',
        function($scope, $cookies, $timeout, $location, $window, $, graph, keybinding, logger, queueDigest, sql)
        {
            function applyIfNecessary()
            {
                if($scope.$root.$$phase === null)
                {
                    $scope.$apply();
                } // end if
            } // end applyIfNecessary


            $scope.tabSize = 4;
            $scope.softTabs = true;

            var allEditors = [];

            $scope.$watch('tabSize', function(value)
            {
                allEditors.forEach(function(editor)
                {
                    editor.getSession().setTabSize(value);
                });
            });
            $scope.$watch('softTabs', function(value)
            {
                allEditors.forEach(function(editor)
                {
                    editor.getSession().setUseSoftTabs(value);
                });
            });


            $scope.commonEditorConfig = {
                theme: 'idle_fingers',
                useWrapMode: true,
                onLoad: function(editor)
                {
                    // Options
                    var session = editor.getSession();
                    session.setTabSize($scope.tabSize);
                    session.setUseSoftTabs($scope.softTabs);

                    var editorElem = $(editor.renderer.getContainerElement());
                    var scrollbars = $('.ace_scrollbar', editorElem)
                        .css('overflow', 'hidden');

                    scrollbars.filter('.ace_scrollbar-v')
                        .perfectScrollbar({ includePadding: true, minScrollbarLength: 12, suppressScrollX: true });
                    scrollbars.filter('.ace_scrollbar-h')
                        .perfectScrollbar({ includePadding: true, minScrollbarLength: 12, suppressScrollY: true });

                    session.on('change', function()
                    {
                        scrollbars.perfectScrollbar('update');
                    });

                    editorElem.hover(
                        function() { scrollbars.addClass('hover'); },
                        function() { scrollbars.removeClass('hover'); }
                    );

                    allEditors.push(editor);
                    editorElem.on('$destroy', function()
                    {
                        var editorIdx = allEditors.indexOf(editor);
                        allEditors.splice(editorIdx, 1);
                    }); // end '$destroy' handler

                    return { editor: editorElem, scrollbars: scrollbars };
                }
            }; // end $scope.commonEditorConfig

            var mainEditor, mainEditorInfo, mainEditorScrollbars;
            $scope.mainEditorConfig = angular.extend({}, $scope.commonEditorConfig, {
                mode: 'pgsql',
                onLoad: function(editor)
                {
                    mainEditor = editor;
                    var elements = $scope.commonEditorConfig.onLoad(editor);

                    // Options
                    var session = editor.getSession();

                    mainEditorScrollbars = elements.scrollbars;

                    mainEditorInfo = {
                        rows: session.getDocument().getLength(),
                        pos: editor.getCursorPosition(),
                        overwrite: false
                    };

                    session.on('change', function()
                    {
                        mainEditorInfo.rows = session.getDocument().getLength();
                        queueUpdateEditorInfo();
                    });

                    session.on('changeOverwrite', function()
                    {
                        mainEditorInfo.overwrite = session.getOverwrite();
                        queueUpdateEditorInfo();
                    });
                    session.selection.on('changeCursor', function()
                    {
                        mainEditorInfo.pos = editor.getCursorPosition();
                        queueUpdateEditorInfo();
                    });
                },
                require: ['ace/ext/language_tools'],
                advanced: {
                    enableSnippets: true,
                    enableBasicAutocompletion: true,
                    enableLiveAutocompletion: true
                }
            }); // end $scope.mainEditorConfig

            function queueUpdateEditorInfo()
            {
                queueDigest(updateEditorInfo, 0);
            } // end queueUpdateEditorInfo

            function updateEditorInfo()
            {
                $scope.editor = mainEditorInfo;
            } // end updateEditorInfo

            // Connections //
            $scope.connections = JSON.parse($cookies.connections || '{}');
            $scope.$watch('connections', function(value)
            {
                $cookies.connections = JSON.stringify(value);
            });

            $scope.currentConnection = null;

            $scope.editingConnectionIsNew = false;
            $scope.editingConnectionIsValid = true;
            $scope.editingConnectionName = '';
            $scope.editingConnectionNewName = '';
            $scope.editingConnection = {};

            var editConnectionDimmer;
            function showEditConnection()
            {
                if(editConnectionDimmer)
                {
                    editConnectionDimmer.dimmer('show');
                } // end if
            } // end showEditConnection

            $scope.addConnection = function()
            {
                $scope.editingConnectionIsNew = true;
                showEditConnection();
            }; // end $scope.addConnection

            $scope.editConnection = function(connectionName)
            {
                var connInfo = $scope.connections[connectionName];
                if(!connInfo)
                {
                    logger.error("No connection named:", connectionName);
                    return;
                } // end if

                $scope.editingConnectionIsNew = false;
                $scope.editingConnectionIsValid = true;
                $scope.editingConnectionName = connectionName;
                $scope.editingConnectionNewName = connectionName;
                $scope.editingConnection = angular.copy(connInfo);
                showEditConnection();
            }; // end $scope.editConnection

            $scope.hideEditConnection = function()
            {
                if(editConnectionDimmer)
                {
                    editConnectionDimmer.dimmer('hide');
                } // end if

                $scope.editConnectionError = undefined;
            }; // end $scope.hideEditConnection

            $scope.saveConnection = function()
            {
                if(!$scope.editingConnectionIsValid)
                {
                    // Don't do anything.
                }
                else
                {
                    if(editConnectionDimmer)
                    {
                        editConnectionDimmer.dimmer('hide');
                    } // end if

                    $scope.editConnectionError = undefined;

                    if($scope.editingConnectionName)
                    {<commit_msg>Added extra checking for editingConnectionNewName back in, since editingConnectionIsValid is broken.<commit_after>/* global angular: true */

angular.module('webPGQ')
    .controller('MainController', [
        '$scope', '$cookies', '$timeout', '$location', '$window', '$', 'graph', 'keybinding', 'logger', 'queueDigest', 'sql',
        function($scope, $cookies, $timeout, $location, $window, $, graph, keybinding, logger, queueDigest, sql)
        {
            function applyIfNecessary()
            {
                if($scope.$root.$$phase === null)
                {
                    $scope.$apply();
                } // end if
            } // end applyIfNecessary


            $scope.tabSize = 4;
            $scope.softTabs = true;

            var allEditors = [];

            $scope.$watch('tabSize', function(value)
            {
                allEditors.forEach(function(editor)
                {
                    editor.getSession().setTabSize(value);
                });
            });
            $scope.$watch('softTabs', function(value)
            {
                allEditors.forEach(function(editor)
                {
                    editor.getSession().setUseSoftTabs(value);
                });
            });


            $scope.commonEditorConfig = {
                theme: 'idle_fingers',
                useWrapMode: true,
                onLoad: function(editor)
                {
                    // Options
                    var session = editor.getSession();
                    session.setTabSize($scope.tabSize);
                    session.setUseSoftTabs($scope.softTabs);

                    var editorElem = $(editor.renderer.getContainerElement());
                    var scrollbars = $('.ace_scrollbar', editorElem)
                        .css('overflow', 'hidden');

                    scrollbars.filter('.ace_scrollbar-v')
                        .perfectScrollbar({ includePadding: true, minScrollbarLength: 12, suppressScrollX: true });
                    scrollbars.filter('.ace_scrollbar-h')
                        .perfectScrollbar({ includePadding: true, minScrollbarLength: 12, suppressScrollY: true });

                    session.on('change', function()
                    {
                        scrollbars.perfectScrollbar('update');
                    });

                    editorElem.hover(
                        function() { scrollbars.addClass('hover'); },
                        function() { scrollbars.removeClass('hover'); }
                    );

                    allEditors.push(editor);
                    editorElem.on('$destroy', function()
                    {
                        var editorIdx = allEditors.indexOf(editor);
                        allEditors.splice(editorIdx, 1);
                    }); // end '$destroy' handler

                    return { editor: editorElem, scrollbars: scrollbars };
                }
            }; // end $scope.commonEditorConfig

            var mainEditor, mainEditorInfo, mainEditorScrollbars;
            $scope.mainEditorConfig = angular.extend({}, $scope.commonEditorConfig, {
                mode: 'pgsql',
                onLoad: function(editor)
                {
                    mainEditor = editor;
                    var elements = $scope.commonEditorConfig.onLoad(editor);

                    // Options
                    var session = editor.getSession();

                    mainEditorScrollbars = elements.scrollbars;

                    mainEditorInfo = {
                        rows: session.getDocument().getLength(),
                        pos: editor.getCursorPosition(),
                        overwrite: false
                    };

                    session.on('change', function()
                    {
                        mainEditorInfo.rows = session.getDocument().getLength();
                        queueUpdateEditorInfo();
                    });

                    session.on('changeOverwrite', function()
                    {
                        mainEditorInfo.overwrite = session.getOverwrite();
                        queueUpdateEditorInfo();
                    });
                    session.selection.on('changeCursor', function()
                    {
                        mainEditorInfo.pos = editor.getCursorPosition();
                        queueUpdateEditorInfo();
                    });
                },
                require: ['ace/ext/language_tools'],
                advanced: {
                    enableSnippets: true,
                    enableBasicAutocompletion: true,
                    enableLiveAutocompletion: true
                }
            }); // end $scope.mainEditorConfig

            function queueUpdateEditorInfo()
            {
                queueDigest(updateEditorInfo, 0);
            } // end queueUpdateEditorInfo

            function updateEditorInfo()
            {
                $scope.editor = mainEditorInfo;
            } // end updateEditorInfo

            // Connections //
            $scope.connections = JSON.parse($cookies.connections || '{}');
            $scope.$watch('connections', function(value)
            {
                $cookies.connections = JSON.stringify(value);
            });

            $scope.currentConnection = null;

            $scope.editingConnectionIsNew = false;
            $scope.editingConnectionIsValid = true;
            $scope.editingConnectionName = '';
            $scope.editingConnectionNewName = '';
            $scope.editingConnection = {};

            var editConnectionDimmer;
            function showEditConnection()
            {
                if(editConnectionDimmer)
                {
                    editConnectionDimmer.dimmer('show');
                } // end if
            } // end showEditConnection

            $scope.addConnection = function()
            {
                $scope.editingConnectionIsNew = true;
                showEditConnection();
            }; // end $scope.addConnection

            $scope.editConnection = function(connectionName)
            {
                var connInfo = $scope.connections[connectionName];
                if(!connInfo)
                {
                    logger.error("No connection named:", connectionName);
                    return;
                } // end if

                $scope.editingConnectionIsNew = false;
                $scope.editingConnectionIsValid = true;
                $scope.editingConnectionName = connectionName;
                $scope.editingConnectionNewName = connectionName;
                $scope.editingConnection = angular.copy(connInfo);
                showEditConnection();
            }; // end $scope.editConnection

            $scope.hideEditConnection = function()
            {
                if(editConnectionDimmer)
                {
                    editConnectionDimmer.dimmer('hide');
                } // end if

                $scope.editConnectionError = undefined;
            }; // end $scope.hideEditConnection

            $scope.saveConnection = function()
            {
                if(!$scope.editingConnectionIsValid)
                {
                    // Don't do anything.
                }
                else if($scope.editingConnectionNewName.length === 0)
                {
                    $scope.editConnectionError = "You must specify a name for the new connection!";
                }
                else
                {
                    if(editConnectionDimmer)
                    {
                        editConnectionDimmer.dimmer('hide');
                    } // end if

                    $scope.editConnectionError = undefined;

                    if($scope.editingConnectionName)
                    {
{'fraction_non_alphanumeric': 0.09988908462190906, 'fraction_numerical': 0.0009786651008025053, 'mean_word_length': 1.209919261822376, 'pattern_counts': {'":': 0, '<': 3, '<?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': '64999', 'n_tokens_mistral': 3557, 'n_tokens_neox': 3215, 'n_words': 549}