<commit_before>/* global io, angular */

var fapp = angular.module("fapp",
  ['angularSpinner',
    'ngReallyClickModule',
    'numberPadding',
    'yaru22.md'
  ]);

fapp.controller("referenceCtrl", ['$scope', '$filter', function ($scope, $filter) {
  $scope.newStuff = {
    newComment: ""
  };
  $scope.modSaveError = "";
  $scope.ok = {
    approveAll: {}
  };
  $scope.spin = {
    approveAll: {}
  };
  $scope.saving = {};
  $scope.refUser = {
    name: window.location.pathname.substring(3)
  };
  $scope.selectedRef = {};
  $scope.referenceToRevert = {};
  $scope.indexOk = {};
  $scope.indexSpin = {};


  $scope.editReference = function (ref) {
    $scope.selectedRef = ref;
    $scope.referenceToRevert = $.extend(true, {}, ref);
  };

  $scope.revertRef = function () {
    var index = $scope.user.references.indexOf($scope.selectedRef);
    $scope.user.references[index] = $.extend(true, {}, $scope.referenceToRevert);
  };

  $scope.editRef = function () {
    $scope.editRefError = "";
    $scope.indexOk.editRef = false;
    var ref = $scope.selectedRef,
        url = "/reference/edit",
        regexp = /(http(s?):\/\/)?(www|[a-z]*\.)?reddit\.com\/r\/((pokemontrades)|(SVExchange)|(poketradereferences))\/comments\/([a-z\d]*)\/([^\/]+)\/([a-z\d]+)(\?[a-z\d]+)?/,
        regexpGive = /(http(s?):\/\/)?(www|[a-z]*\.)?reddit\.com\/r\/((SVExchange)|(poketradereferences)|(Pokemongiveaway)|(SVgiveaway))\/comments\/([a-z\d]*)\/([^\/]+)\/?/,
        regexpMisc = /(http(s?):\/\/)?(www|[a-z]*\.)?reddit\.com.*/;

    if (!ref.type) {
      $scope.editRefError = "Please choose a type.";
      return;
    }
    if (ref.type === "egg" || ref.type === "giveaway" || ref.type === "misc") {
      if (!ref.description) {
        $scope.editRefError = "Make sure you enter all the information";
        return;
      }
    } else {
      if (!ref.got || !ref.gave) {
        $scope.editRefError = "Make sure you enter all the information";
        return;
      }
    }
    if (!ref.url ||
        ((ref.type !== "giveaway" && ref.type !== "misc") && !ref.user2)) {
      $scope.editRefError = "Make sure you enter all the information";
      return;
    }
    if ((ref.type === "giveaway" && !regexpGive.test(ref.url)) ||
        (ref.type !== "giveaway" && ref.type !== "misc" &&
        !regexp.test(ref.url)) ||
        (ref.type === "misc" && !regexpMisc.test(ref.url))) {
      $scope.editRefError = "Looks like you didn't input a proper permalink";
      return;
    }

    if (ref.user2.indexOf("/u/") === -1) {
      ref.user2 = "/u/" + ref.user2;
    }

    $scope.indexSpin.editRef = true;
    io.socket.post(url, ref, function (data, res) {
      $scope.indexSpin.editRef = false;
      if (res.statusCode !== 200) {
        $scope.editRefError = "There was an issue.";
        console.log(res);
      } else {
        $scope.indexOk.editRef = true;
      }
      $scope.$apply();
    });

  };

  $scope.isEvent = function (el) {
    return el.type === "event" || el.type === "redemption";
  };

  $scope.isShiny = function (el) {
    return el.type === "shiny";
  };

  $scope.isCasual = function (el) {
    return el.type === "casual";
  };

  $scope.numberOfTrades = function () {
    if (!$scope.refUser.references) {
      return 0;
    }
    var refs = $scope.refUser.references;
    var event = $filter("filter")(refs, $scope.isEvent).length;
    var shiny = $filter("filter")(refs, $scope.isShiny).length;
    var casual = $filter("filter")(refs, $scope.isCasual).length;
    return event + shiny + casual;
  };

  io.socket.get("/user/get/" + $scope.refUser.name, function (data, res) {
    if (res.statusCode === 200) {
      $scope.refUser = data;
      if(!$scope.refUser.friendCodes || $scope.refUser.friendCodes.length === 0) {
        $scope.refUser.friendCodes = [""];
      }
      if($scope.refUser.games.length === 0) {
        $scope.refUser.games = [{tsv: "", ign: ""}];
      }
      $scope.$apply();
    }<commit_msg>Add user's name into the title. Fixes #133<commit_after>/* global io, angular */

var fapp = angular.module("fapp",
  ['angularSpinner',
    'ngReallyClickModule',
    'numberPadding',
    'yaru22.md'
  ]);

fapp.controller("referenceCtrl", ['$scope', '$filter', function ($scope, $filter) {
  $scope.newStuff = {
    newComment: ""
  };
  $scope.modSaveError = "";
  $scope.ok = {
    approveAll: {}
  };
  $scope.spin = {
    approveAll: {}
  };
  $scope.saving = {};
  $scope.refUser = {
    name: window.location.pathname.substring(3)
  };
  $scope.selectedRef = {};
  $scope.referenceToRevert = {};
  $scope.indexOk = {};
  $scope.indexSpin = {};


  $scope.editReference = function (ref) {
    $scope.selectedRef = ref;
    $scope.referenceToRevert = $.extend(true, {}, ref);
  };

  $scope.revertRef = function () {
    var index = $scope.user.references.indexOf($scope.selectedRef);
    $scope.user.references[index] = $.extend(true, {}, $scope.referenceToRevert);
  };

  $scope.editRef = function () {
    $scope.editRefError = "";
    $scope.indexOk.editRef = false;
    var ref = $scope.selectedRef,
        url = "/reference/edit",
        regexp = /(http(s?):\/\/)?(www|[a-z]*\.)?reddit\.com\/r\/((pokemontrades)|(SVExchange)|(poketradereferences))\/comments\/([a-z\d]*)\/([^\/]+)\/([a-z\d]+)(\?[a-z\d]+)?/,
        regexpGive = /(http(s?):\/\/)?(www|[a-z]*\.)?reddit\.com\/r\/((SVExchange)|(poketradereferences)|(Pokemongiveaway)|(SVgiveaway))\/comments\/([a-z\d]*)\/([^\/]+)\/?/,
        regexpMisc = /(http(s?):\/\/)?(www|[a-z]*\.)?reddit\.com.*/;

    if (!ref.type) {
      $scope.editRefError = "Please choose a type.";
      return;
    }
    if (ref.type === "egg" || ref.type === "giveaway" || ref.type === "misc") {
      if (!ref.description) {
        $scope.editRefError = "Make sure you enter all the information";
        return;
      }
    } else {
      if (!ref.got || !ref.gave) {
        $scope.editRefError = "Make sure you enter all the information";
        return;
      }
    }
    if (!ref.url ||
        ((ref.type !== "giveaway" && ref.type !== "misc") && !ref.user2)) {
      $scope.editRefError = "Make sure you enter all the information";
      return;
    }
    if ((ref.type === "giveaway" && !regexpGive.test(ref.url)) ||
        (ref.type !== "giveaway" && ref.type !== "misc" &&
        !regexp.test(ref.url)) ||
        (ref.type === "misc" && !regexpMisc.test(ref.url))) {
      $scope.editRefError = "Looks like you didn't input a proper permalink";
      return;
    }

    if (ref.user2.indexOf("/u/") === -1) {
      ref.user2 = "/u/" + ref.user2;
    }

    $scope.indexSpin.editRef = true;
    io.socket.post(url, ref, function (data, res) {
      $scope.indexSpin.editRef = false;
      if (res.statusCode !== 200) {
        $scope.editRefError = "There was an issue.";
        console.log(res);
      } else {
        $scope.indexOk.editRef = true;
      }
      $scope.$apply();
    });

  };

  $scope.isEvent = function (el) {
    return el.type === "event" || el.type === "redemption";
  };

  $scope.isShiny = function (el) {
    return el.type === "shiny";
  };

  $scope.isCasual = function (el) {
    return el.type === "casual";
  };

  $scope.numberOfTrades = function () {
    if (!$scope.refUser.references) {
      return 0;
    }
    var refs = $scope.refUser.references;
    var event = $filter("filter")(refs, $scope.isEvent).length;
    var shiny = $filter("filter")(refs, $scope.isShiny).length;
    var casual = $filter("filter")(refs, $scope.isCasual).length;
    return event + shiny + casual;
  };

  io.socket.get("/user/get/" + $scope.refUser.name, function (data, res) {
    if (res.statusCode === 200) {
      $scope.refUser = data;
      if(!$scope.refUser.friendCodes || $scope.refUser.friendCodes.length === 0) {
        $scope.refUser.friendCodes = [""];
      }
      if($scope.refUser.games.length === 0) {
        $scope.refUser.games = [{tsv: "", ign: ""}];
      }
      window.document.title = data.name + "'s reference";
      $scope.$apply();
    }
{'fraction_non_alphanumeric': 0.2145622580242288, 'fraction_numerical': 0.004620956662919945, 'mean_word_length': 2.6072072072072072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '4373593', 'n_tokens_mistral': 2969, 'n_tokens_neox': 2870, 'n_words': 479}