<commit_before>'use strict';

/* Task page */

angular.module('cmsocial')
  .factory('taskbarManager', function() {
    var activeTab = 0;
    return {
      isActiveTab: function(tab) {
        return tab == activeTab;
      },
      setActiveTab: function(tab) {
        activeTab = tab;
      }
    };
  })
  .controller('TaskbarCtrl', function($scope, $stateParams, $http, $state,
    $rootScope, $timeout, userManager, notificationHub, taskbarManager,
    l10n, contestManager, API_PREFIX) {

    delete $rootScope.task;

    $timeout(function() {
      $(".my-popover").popover(); // enable popovers
    });

    $scope.tag = {};
    $scope.isActiveTab = taskbarManager.isActiveTab;
    $scope.isLogged = contestManager.hasParticipation;
    $scope.taskName = $stateParams.taskName;

    $scope.tagClicked = function(tag) {
      $("#tags_detail").modal('hide');
      $('#tags_detail').on('hidden.bs.modal', function(e) {
        $state.go('tasklist.page', {
          'pageNum': 1,
          'tag': tag
        });
      });
    };

    $scope.tagAdd = function() {
      $http.post(API_PREFIX + 'tag', {
          'action': 'add',
          'tag': $scope.tag.newtag,
          'task': $rootScope.task.name,
          'username': userManager.getUser().username,
          'token': userManager.getUser().token
        })
        .success(function(data, status, headers, config) {
          if (data.success === 0) {
            notificationHub.createAlert('danger', l10n.get(data['error']), 3);
          } else {
            $scope.loadTaskPromise = $scope.loadTask();
            notificationHub.createAlert('success', l10n.get('Task correctly tagged'), 2);
          }
        })
        .error(function(data, status, headers, config) {
          notificationHub.serverError(status);
        });
    };

    $scope.tagDelete = function(tag) {
      if (confirm("Are you sure?")) {
        $http.post(API_PREFIX + 'tag', {
            'action': 'remove',
            'tag': tag,
            'task': $rootScope.task.name,
            'username': userManager.getUser().username,
            'token': userManager.getUser().token
          })
          .success(function(data, status, headers, config) {
            if (data.success === 0) {
              notificationHub.createAlert('danger', l10n.get(data['error']), 3);
            } else {
              $scope.loadTaskPromise = $scope.loadTask();
              notificationHub.createAlert('success', l10n.get('Task correctly untagged'), 2);
            }
          })
          .error(function(data, status, headers, config) {
            notificationHub.serverError(status);
          });
      }
    };

    $scope.newTag = function() {
      $(".newtagstuff").show();
      $http.post(API_PREFIX + 'tag', {
          'action': 'list'
        })
        .success(function(data, status, headers, config) {
          $scope.tags = data['tags'];
          $("#tagloader").hide();
          $("#tagseparator").show();
          $("#tagchooser").removeAttr("disabled");
        })
        .error(function(data, status, headers, config) {
          notificationHub.serverError(status);
        });
    };

    $scope.loadTask = function() {
      return $http.post(API_PREFIX + 'task', {
          'name': $stateParams.taskName,
          'username': userManager.getUser().username,
          'token': userManager.getUser().token,
          'action': 'get'
        })
        .then(
          function(result) {
            $rootScope.task = result.data;
          },
          function(result) {
            notificationHub.serverError(result.status);
          }
        );
    };

    $scope.loadTaskPromise = $scope.loadTask();
  })
  .controller('StatementCtrl', function($scope, $window, taskbarManager) {
    taskbarManager.setActiveTab(1);
  })
  .controller('AttachmentsCtrl', function(taskbarManager) {
    taskbarManager.setActiveTab(2);
  })
  .controller('StatsCtrl', function($scope, $stateParams, $http,
    notificationHub, userManager, taskbarManager, l10n, API_PREFIX) {
    taskbarManager.setActiveTab(3);
    $scope.getStats = function() {
      $http.post(API_PREFIX + 'task', {
          'name': $stateParams.taskName,
          'username': userManager.getUser().username,
          'token': userManager.getUser().token,
          'action': 'stats'
        }).success(function(data, status, headers, config) {
          $scope.nsubs = data.nsubs;
          $scope.nusers = data.nusers;
          $scope.nsubscorrect = data.nsubscorrect;
          $scope.nuserscorrect = data.nuserscorrect;
          $scope.best = data.best;
        })
        .error(function(data, status, headers, config) {
          notificationHub.serverError(status);
        });
    };
    $scope.getStats();
  })
  .controller('SubmissionsCtrl', function($scope, $stateParams, $location,
    $http, $timeout, $rootScope, userManager, notificationHub,
    subsDatabase, taskbarManager, l10n, contestManager, API_PREFIX) {
    taskbarManager.setActiveTab(4);
    subsDatabase.load($stateParams.taskName);
    $scope.areThereSubs = function(name) {
      return $rootScope.submissions[name] !== undefined &&
        $rootScope.submissions[name].length > 0;
    };
    var aceModeMap = {
      "C": "c_cpp",
      "C++": "c_cpp",
      "Pascal": "pascal"
    };
    var langExtMap = {
      "C": ".c",
      "C++": ".cpp",
      "Pascal": ".pas"
    };
    var cmsLanguageMap = {
      "c": "C",
      "cpp": "C++",
      "pas": "Pascal"
    };
    $scope.languages = [];
    for (var lang in contestManager.getContest().languages) {
      $scope.languages.push(cmsLanguageMap[contestManager.getContest().languages[lang]]);
    }

    var preferred_language_key = "preferred_language_" + contestManager.getContest().name;

    if (!localStorage.getItem(preferred_language_key) ||
      $scope.languages.indexOf(localStorage.getItem(preferred_language_key)) == -1) {
      localStorage.setItem(preferred_language_key, $scope.languages[0]);
    }

    $scope.language = localStorage.getItem(preferred_language_key);
    $scope.aceOption = {
      mode: aceModeMap[$scope.language],
      showPrintMargin: false,
      onLoad: function(_ace) {
        $scope.aceSession = _ace.getSession();<commit_msg>Add support for Python in the editor.<commit_after>'use strict';

/* Task page */

angular.module('cmsocial')
  .factory('taskbarManager', function() {
    var activeTab = 0;
    return {
      isActiveTab: function(tab) {
        return tab == activeTab;
      },
      setActiveTab: function(tab) {
        activeTab = tab;
      }
    };
  })
  .controller('TaskbarCtrl', function($scope, $stateParams, $http, $state,
    $rootScope, $timeout, userManager, notificationHub, taskbarManager,
    l10n, contestManager, API_PREFIX) {

    delete $rootScope.task;

    $timeout(function() {
      $(".my-popover").popover(); // enable popovers
    });

    $scope.tag = {};
    $scope.isActiveTab = taskbarManager.isActiveTab;
    $scope.isLogged = contestManager.hasParticipation;
    $scope.taskName = $stateParams.taskName;

    $scope.tagClicked = function(tag) {
      $("#tags_detail").modal('hide');
      $('#tags_detail').on('hidden.bs.modal', function(e) {
        $state.go('tasklist.page', {
          'pageNum': 1,
          'tag': tag
        });
      });
    };

    $scope.tagAdd = function() {
      $http.post(API_PREFIX + 'tag', {
          'action': 'add',
          'tag': $scope.tag.newtag,
          'task': $rootScope.task.name,
          'username': userManager.getUser().username,
          'token': userManager.getUser().token
        })
        .success(function(data, status, headers, config) {
          if (data.success === 0) {
            notificationHub.createAlert('danger', l10n.get(data['error']), 3);
          } else {
            $scope.loadTaskPromise = $scope.loadTask();
            notificationHub.createAlert('success', l10n.get('Task correctly tagged'), 2);
          }
        })
        .error(function(data, status, headers, config) {
          notificationHub.serverError(status);
        });
    };

    $scope.tagDelete = function(tag) {
      if (confirm("Are you sure?")) {
        $http.post(API_PREFIX + 'tag', {
            'action': 'remove',
            'tag': tag,
            'task': $rootScope.task.name,
            'username': userManager.getUser().username,
            'token': userManager.getUser().token
          })
          .success(function(data, status, headers, config) {
            if (data.success === 0) {
              notificationHub.createAlert('danger', l10n.get(data['error']), 3);
            } else {
              $scope.loadTaskPromise = $scope.loadTask();
              notificationHub.createAlert('success', l10n.get('Task correctly untagged'), 2);
            }
          })
          .error(function(data, status, headers, config) {
            notificationHub.serverError(status);
          });
      }
    };

    $scope.newTag = function() {
      $(".newtagstuff").show();
      $http.post(API_PREFIX + 'tag', {
          'action': 'list'
        })
        .success(function(data, status, headers, config) {
          $scope.tags = data['tags'];
          $("#tagloader").hide();
          $("#tagseparator").show();
          $("#tagchooser").removeAttr("disabled");
        })
        .error(function(data, status, headers, config) {
          notificationHub.serverError(status);
        });
    };

    $scope.loadTask = function() {
      return $http.post(API_PREFIX + 'task', {
          'name': $stateParams.taskName,
          'username': userManager.getUser().username,
          'token': userManager.getUser().token,
          'action': 'get'
        })
        .then(
          function(result) {
            $rootScope.task = result.data;
          },
          function(result) {
            notificationHub.serverError(result.status);
          }
        );
    };

    $scope.loadTaskPromise = $scope.loadTask();
  })
  .controller('StatementCtrl', function($scope, $window, taskbarManager) {
    taskbarManager.setActiveTab(1);
  })
  .controller('AttachmentsCtrl', function(taskbarManager) {
    taskbarManager.setActiveTab(2);
  })
  .controller('StatsCtrl', function($scope, $stateParams, $http,
    notificationHub, userManager, taskbarManager, l10n, API_PREFIX) {
    taskbarManager.setActiveTab(3);
    $scope.getStats = function() {
      $http.post(API_PREFIX + 'task', {
          'name': $stateParams.taskName,
          'username': userManager.getUser().username,
          'token': userManager.getUser().token,
          'action': 'stats'
        }).success(function(data, status, headers, config) {
          $scope.nsubs = data.nsubs;
          $scope.nusers = data.nusers;
          $scope.nsubscorrect = data.nsubscorrect;
          $scope.nuserscorrect = data.nuserscorrect;
          $scope.best = data.best;
        })
        .error(function(data, status, headers, config) {
          notificationHub.serverError(status);
        });
    };
    $scope.getStats();
  })
  .controller('SubmissionsCtrl', function($scope, $stateParams, $location,
    $http, $timeout, $rootScope, userManager, notificationHub,
    subsDatabase, taskbarManager, l10n, contestManager, API_PREFIX) {
    taskbarManager.setActiveTab(4);
    subsDatabase.load($stateParams.taskName);
    $scope.areThereSubs = function(name) {
      return $rootScope.submissions[name] !== undefined &&
        $rootScope.submissions[name].length > 0;
    };
    var aceModeMap = {
      "C": "c_cpp",
      "C++": "c_cpp",
      "Pascal": "pascal",
      "Python": "python"
    };
    var langExtMap = {
      "C": ".c",
      "C++": ".cpp",
      "Pascal": ".pas",
      "Python": ".py"
    };
    var cmsLanguageMap = {
      "c": "C",
      "cpp": "C++",
      "pas": "Pascal",
      "py": "Python"
    };
    $scope.languages = [];
    for (var lang in contestManager.getContest().languages) {
      $scope.languages.push(cmsLanguageMap[contestManager.getContest().languages[lang]]);
    }

    var preferred_language_key = "preferred_language_" + contestManager.getContest().name;

    if (!localStorage.getItem(preferred_language_key) ||
      $scope.languages.indexOf(localStorage.getItem(preferred_language_key)) == -1) {
      localStorage.setItem(preferred_language_key, $scope.languages[0]);
    }

    $scope.language = localStorage.getItem(preferred_language_key);
    $scope.aceOption = {
      mode: aceModeMap[$scope.language],
      showPrintMargin: false,
      onLoad: function(_ace) {
        $scope.aceSession = _ace.getSession();
{'fraction_non_alphanumeric': 0.15870998490745888, 'fraction_numerical': 0.004607196759075383, 'mean_word_length': 2.444596443228454, 'pattern_counts': {'":': 21, '<': 3, '<?xml version=': 0, '>': 5, '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': '5296661', 'n_tokens_mistral': 3846, 'n_tokens_neox': 3772, 'n_words': 613}