<commit_before>// Copyright (C) 2012 <NAME>.

'use strict';

angular.module('jskom.controllers', ['jskom.httpkom', 'jskom.services', 'jskom.settings']).
  controller('ConnectionsCtrl', [
    '$scope', '$rootScope', '$log', '$location',
    'connectionFactory', 'connectionsService', 'httpkom', 'messagesService', 'sessionsService',
    'keybindingService',
    function($scope, $rootScope, $log, $location,
             connectionFactory, connectionsService, httpkom, messagesService, sessionsService,
             keybindingService) {
      
      // This is a work-around for Twitter Bootstrap dropdown plugin
      // incompatibility with AngularJS. The dropdown plugin stops
      // AngularJS from capturing link clicks, which causes page
      // reloads. Angular in its turn stops the clicks from closing
      // the dropdown when it updates the location. We want to do both
      // (but not for a.dropdown-toggle)!  TODO: Make a directive of
      // this. This is the wrong place for this code.
      jQuery('.dropdown').on('click', function(event) {
        var aElement = jQuery(event.target).closest('a');
        if (!aElement.hasClass('dropdown-toggle')) {
          var url = aElement.attr('href');
          if (url) {
            $scope.$apply(function(scope) {
              $location.url(url);
            });
          }
          jQuery(this).removeClass('open');
          event.preventDefault();
        }
      });
      
      
      $scope.newConnection = function() {
        connectionsService.newConnectionPromise().then(
          function(conn) {
            connectionsService.setCurrentConnection(conn);
          },
          function(response) {
            $log.log("ConnectionsCtrl - newConnection() - error");
            messagesService.showMessage('error', 'Failed to create new connection.',
                                        response.data);
          });
      };
      
      $scope.selectConnection = function(conn) {
        var curConn = connectionsService.getCurrentConnection();
        if (conn !== curConn) {
          messagesService.clearAll();
          $location.url('/');
        }
        connectionsService.setCurrentConnection(conn);
      };
      
      $scope.$watch(connectionsService.getConnections, function(newConnMap) {
        $scope.connections = newConnMap;
      });
      
      $scope.$watch(connectionsService.getCurrentConnection, function(newCurrentConn, oldConn) {
        if (newCurrentConn) {
          $log.log("New current connection (" + newCurrentConn.id + ") - session: " +
                   angular.toJson(newCurrentConn.session));
          $scope.connection = newCurrentConn;
        } else {
          //$log.log("New current connection: " + newCurrentConn);
          $scope.connection = null;
        }
      });
      
      $scope.logout = function() {
        // We don't actually logout, but rather delete the session
        // directly instead.
        sessionsService.deleteSession($scope.connection, 0).then(
          function() {
            $log.log("ConnectionsCtrl - logout() - success");
            connectionsService.removeConnection($scope.connection);
          },
          function(response) {
            $log.log("ConnectionsCtrl - logout() - error");
            messagesService.showMessage('error', 'Error when logging out.', response.data);
          });
      };
      
      $scope.servers = null;
      connectionsService.getServers().then(
        function(servers) {
          $log.log("ConnectionsCtrl - getServers() - success");
          $scope.servers = servers;
          if (!$scope.connection) {
            $scope.newConnection();
          }
        },
        function(response) {
          $log.log("ConnectionsCtrl - getServers() - error");
          messagesService.showMessage('error', 'Failed to get server list.', response.data);
        });
      
      
      $scope.selectNextConnection = function() {
        if (_.size($scope.connections) > 1) {
          var newConn;
          var connections = _.sortBy(_.values($scope.connections), function(conn) {
            return conn.id;
          });
          
          if ($scope.connection) {
            var connectionIds = _.map(connections, function(conn) { return conn.id; });
            var index = connectionIds.indexOf($scope.connection.id);
            if (index + 1 < connectionIds.length) {
              newConn = connections[index+1];
            } else {
              newConn = _.first(connections);
            }
          } else {
            newConn = _.first(connections);
          }
          
          $scope.selectConnection(newConn);
        }
      };
      
      keybindingService.bindGeneral('N', 'Next LysKOM session', function() {
        $log.log("n l");
        $scope.$apply(function() {
          $scope.selectNextConnection();
        });
        return false;
      });
    }
  ]).<commit_msg>Removed logging that is not needed.<commit_after>// Copyright (C) 2012 <NAME>.

'use strict';

angular.module('jskom.controllers', ['jskom.httpkom', 'jskom.services', 'jskom.settings']).
  controller('ConnectionsCtrl', [
    '$scope', '$rootScope', '$log', '$location',
    'connectionFactory', 'connectionsService', 'httpkom', 'messagesService', 'sessionsService',
    'keybindingService',
    function($scope, $rootScope, $log, $location,
             connectionFactory, connectionsService, httpkom, messagesService, sessionsService,
             keybindingService) {
      
      // This is a work-around for Twitter Bootstrap dropdown plugin
      // incompatibility with AngularJS. The dropdown plugin stops
      // AngularJS from capturing link clicks, which causes page
      // reloads. Angular in its turn stops the clicks from closing
      // the dropdown when it updates the location. We want to do both
      // (but not for a.dropdown-toggle)!  TODO: Make a directive of
      // this. This is the wrong place for this code.
      jQuery('.dropdown').on('click', function(event) {
        var aElement = jQuery(event.target).closest('a');
        if (!aElement.hasClass('dropdown-toggle')) {
          var url = aElement.attr('href');
          if (url) {
            $scope.$apply(function(scope) {
              $location.url(url);
            });
          }
          jQuery(this).removeClass('open');
          event.preventDefault();
        }
      });
      
      
      $scope.newConnection = function() {
        connectionsService.newConnectionPromise().then(
          function(conn) {
            connectionsService.setCurrentConnection(conn);
          },
          function(response) {
            $log.log("ConnectionsCtrl - newConnection() - error");
            messagesService.showMessage('error', 'Failed to create new connection.',
                                        response.data);
          });
      };
      
      $scope.selectConnection = function(conn) {
        var curConn = connectionsService.getCurrentConnection();
        if (conn !== curConn) {
          messagesService.clearAll();
          $location.url('/');
        }
        connectionsService.setCurrentConnection(conn);
      };
      
      $scope.$watch(connectionsService.getConnections, function(newConnMap) {
        $scope.connections = newConnMap;
      });
      
      $scope.$watch(connectionsService.getCurrentConnection, function(newCurrentConn, oldConn) {
        if (newCurrentConn) {
          $log.log("New current connection (" + newCurrentConn.id + ") - session: " +
                   angular.toJson(newCurrentConn.session));
          $scope.connection = newCurrentConn;
        } else {
          //$log.log("New current connection: " + newCurrentConn);
          $scope.connection = null;
        }
      });
      
      $scope.logout = function() {
        // We don't actually logout, but rather delete the session
        // directly instead.
        sessionsService.deleteSession($scope.connection, 0).then(
          function() {
            $log.log("ConnectionsCtrl - logout() - success");
            connectionsService.removeConnection($scope.connection);
          },
          function(response) {
            $log.log("ConnectionsCtrl - logout() - error");
            messagesService.showMessage('error', 'Error when logging out.', response.data);
          });
      };
      
      $scope.servers = null;
      connectionsService.getServers().then(
        function(servers) {
          $log.log("ConnectionsCtrl - getServers() - success");
          $scope.servers = servers;
          if (!$scope.connection) {
            $scope.newConnection();
          }
        },
        function(response) {
          $log.log("ConnectionsCtrl - getServers() - error");
          messagesService.showMessage('error', 'Failed to get server list.', response.data);
        });
      
      
      $scope.selectNextConnection = function() {
        if (_.size($scope.connections) > 1) {
          var newConn;
          var connections = _.sortBy(_.values($scope.connections), function(conn) {
            return conn.id;
          });
          
          if ($scope.connection) {
            var connectionIds = _.map(connections, function(conn) { return conn.id; });
            var index = connectionIds.indexOf($scope.connection.id);
            if (index + 1 < connectionIds.length) {
              newConn = connections[index+1];
            } else {
              newConn = _.first(connections);
            }
          } else {
            newConn = _.first(connections);
          }
          
          $scope.selectConnection(newConn);
        }
      };
      
      keybindingService.bindGeneral('N', 'Next LysKOM session', function() {
        $scope.$apply(function() {
          $scope.selectNextConnection();
        });
        return false;
      });
    }
  ]).
{'fraction_non_alphanumeric': 0.127499746218658, 'fraction_numerical': 0.0016242005887727134, 'mean_word_length': 2.0036585365853656, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '106367', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2482, 'n_words': 531}