<commit_before>var util = require('util');
var webutil = require('../util/web');
var Tab = require('../client/tab').Tab;
var Amount = ripple.Amount;
var Currency = ripple.Currency;

var TrustTab = function ()
{
  Tab.call(this);

};

util.inherits(TrustTab, Tab);

TrustTab.prototype.tabName = 'trust';
TrustTab.prototype.mainMenu = 'fund';

TrustTab.prototype.generateHtml = function ()
{
  return require('../../jade/tabs/trust.jade')();
};

TrustTab.prototype.angular = function (module)
{
  module.controller('TrustCtrl', ['$scope', 'rpBooks', '$timeout', '$routeParams', 'rpId',
                                  '$filter', 'rpNetwork', 'rpTracker', 'rpKeychain',
                                  function ($scope, books, $timeout, $routeParams, id,
                                            $filter, $network, $rpTracker, keychain)
  {
    if (!id.loginStatus) return id.goId();

    $scope.advanced_feature_switch = Options.advanced_feature_switch;
    $scope.trust = {};

    // Trust line sorting
    $scope.sorting = {
      predicate: 'balance',
      reverse: true,
      sort: function(line){
        return $scope.sorting.predicate === 'currency' ? line.currency : line.balance.to_number();
      }
    };

    $scope.validation_pattern = /^0*(([1-9][0-9]*.?[0-9]*)|(.0*[1-9][0-9]*))$/; //Don't allow zero for new trust lines.

    $scope.reset = function () {
      $scope.mode = 'main';
      var usdCurrency = Currency.from_human('USD');
      $scope.currency = usdCurrency.to_human({full_name:$scope.currencies_all_keyed[usdCurrency.get_iso()].name});
      $scope.addform_visible = false;
      $scope.edituser = '';
      $scope.amount = '';
      $scope.allowrippling = false;
      $scope.counterparty = '';
      $scope.counterparty_view = '';
      $scope.counterparty_address = '';
      $scope.saveAddressName = '';
      $scope.error_account_reserve = false;
    };

    $scope.toggle_form = function () {

      if($scope.addform_visible) {
        $scope.reset();
      } else {
        $scope.addform_visible = true;
      }
    };


    // User should not even be able to try granting a trust if the reserve is insufficient
    $scope.$watch('account', function() {
      $scope.can_add_trust = false;
      if ($scope.account.Balance && $scope.account.reserve_to_add_trust) {
        if (!$scope.account.reserve_to_add_trust.subtract($scope.account.Balance).is_positive()
          || $.isEmptyObject($scope.lines))
        {
          $scope.can_add_trust = true;
        }
      }
    }, true);

    $scope.$watch('counterparty', function() {
      $scope.error_account_reserve = false;
      $scope.contact = webutil.getContact($scope.userBlob.data.contacts,$scope.counterparty);
      if ($scope.contact) {
        $scope.counterparty_name = $scope.contact.name;
        $scope.counterparty_address = $scope.contact.address;
      } else {
        $scope.counterparty_name = '';
        $scope.counterparty_address = $scope.counterparty;
      }
    }, true);

    /**

     * N2. Confirmation page
     */
    $scope.grant = function ()
    {
      // set variable to show throbber
      $scope.verifying = true;
      $scope.error_account_reserve = false;
      // test if account is valid
      $network.remote.request_account_info($scope.counterparty_address)
        // if account is valid then just to confirm page
        .on('success', function (m){
          $scope.$apply(function(){
            // hide throbber
            $scope.verifying = false;

            $scope.lineCurrencyObj = Currency.from_human($scope.currency);
            var matchedCurrency = $scope.lineCurrencyObj.has_interest() ? $scope.lineCurrencyObj.to_hex() : $scope.lineCurrencyObj.get_iso();
            var match = /^([a-zA-Z0-9]{3}|[A-Fa-f0-9]{40})\b/.exec(matchedCurrency);

            if (!match) {
              // Currency code not recognized, should have been caught by
              // form validator.
              console.error('Currency code:', match, 'is not recognized');
              return;
            }

            if ($scope.advanced_feature_switch === false || $scope.amount === "") {
              // $scope.amount = Number(ripple.Amount.consts.max_value);
              $scope.amount = Options.gateway_max_limit;
            }

            var amount = ripple.Amount.from_human('' + $scope.amount + ' ' + $scope.lineCurrencyObj.to_hex(), {reference_date: new Date(+new Date() + 5*60000)});

            amount.set_issuer($scope.counterparty_address);
            if (!amount.is_valid()) {
              // Invalid amount. Indicates a bug in one of the validators.
              return;
            }

            $scope.amount_feedback = amount;

            $scope.confirm_wait = true;
            $timeout(function () {
              $scope.confirm_wait = false;
            }, 1000, true);

            $scope.mode = 'confirm';

            /**
             * Warning messages
             *
             * - firstIssuer
             * - sameIssuer
             * - multipleIssuers
             */
            currency = amount.currency().to_human({full_name:$scope.currencies_all_keyed[amount.currency().get_iso()].name});
            var balance = $scope.balances[currency];
            $scope.currencyWarning = false;

            // New trust on a currency or no rippling enabled
            // if (!balance || !$scope.allowrippling) {
            //   $scope.currencyWarning = 'firstIssuer';
            // }
            // else {
              // Trust limit change
              // for (var counterparty in balance.components) {
              //   if (counterparty === $scope.counterparty_address)
              //     $scope.currencyWarning = 'sameIssuer';
              // }

              // Multiple trusts on a same currency
              // if (!$scope.currencyWarning)
              //   $scope.currencyWarning = 'multipleIssuers';
            // }
          });
        })
        .on('error', function (m){
          setImmediate(function () {<commit_msg>[FIX] remove unused code<commit_after>var util = require('util');
var webutil = require('../util/web');
var Tab = require('../client/tab').Tab;
var Amount = ripple.Amount;
var Currency = ripple.Currency;

var TrustTab = function ()
{
  Tab.call(this);

};

util.inherits(TrustTab, Tab);

TrustTab.prototype.tabName = 'trust';
TrustTab.prototype.mainMenu = 'fund';

TrustTab.prototype.generateHtml = function ()
{
  return require('../../jade/tabs/trust.jade')();
};

TrustTab.prototype.angular = function (module)
{
  module.controller('TrustCtrl', ['$scope', 'rpBooks', '$timeout', '$routeParams', 'rpId',
                                  '$filter', 'rpNetwork', 'rpTracker', 'rpKeychain',
                                  function ($scope, books, $timeout, $routeParams, id,
                                            $filter, $network, $rpTracker, keychain)
  {
    if (!id.loginStatus) return id.goId();

    $scope.advanced_feature_switch = Options.advanced_feature_switch;
    $scope.trust = {};

    // Trust line sorting
    $scope.sorting = {
      predicate: 'balance',
      reverse: true,
      sort: function(line){
        return $scope.sorting.predicate === 'currency' ? line.currency : line.balance.to_number();
      }
    };

    $scope.validation_pattern = /^0*(([1-9][0-9]*.?[0-9]*)|(.0*[1-9][0-9]*))$/; //Don't allow zero for new trust lines.

    $scope.reset = function () {
      $scope.mode = 'main';
      var usdCurrency = Currency.from_human('USD');
      $scope.currency = usdCurrency.to_human({full_name:$scope.currencies_all_keyed[usdCurrency.get_iso()].name});
      $scope.addform_visible = false;
      $scope.edituser = '';
      $scope.amount = '';
      $scope.allowrippling = false;
      $scope.counterparty = '';
      $scope.counterparty_view = '';
      $scope.counterparty_address = '';
      $scope.saveAddressName = '';
      $scope.error_account_reserve = false;
    };

    $scope.toggle_form = function () {

      if($scope.addform_visible) {
        $scope.reset();
      } else {
        $scope.addform_visible = true;
      }
    };


    // User should not even be able to try granting a trust if the reserve is insufficient
    $scope.$watch('account', function() {
      $scope.can_add_trust = false;
      if ($scope.account.Balance && $scope.account.reserve_to_add_trust) {
        if (!$scope.account.reserve_to_add_trust.subtract($scope.account.Balance).is_positive()
          || $.isEmptyObject($scope.lines))
        {
          $scope.can_add_trust = true;
        }
      }
    }, true);

    $scope.$watch('counterparty', function() {
      $scope.error_account_reserve = false;
      $scope.contact = webutil.getContact($scope.userBlob.data.contacts,$scope.counterparty);
      if ($scope.contact) {
        $scope.counterparty_name = $scope.contact.name;
        $scope.counterparty_address = $scope.contact.address;
      } else {
        $scope.counterparty_name = '';
        $scope.counterparty_address = $scope.counterparty;
      }
    }, true);

    /**

     * N2. Confirmation page
     */
    $scope.grant = function ()
    {
      // set variable to show throbber
      $scope.verifying = true;
      $scope.error_account_reserve = false;
      // test if account is valid
      $network.remote.request_account_info($scope.counterparty_address)
        // if account is valid then just to confirm page
        .on('success', function (m){
          $scope.$apply(function(){
            // hide throbber
            $scope.verifying = false;

            $scope.lineCurrencyObj = Currency.from_human($scope.currency);
            var matchedCurrency = $scope.lineCurrencyObj.has_interest() ? $scope.lineCurrencyObj.to_hex() : $scope.lineCurrencyObj.get_iso();
            var match = /^([a-zA-Z0-9]{3}|[A-Fa-f0-9]{40})\b/.exec(matchedCurrency);

            if (!match) {
              // Currency code not recognized, should have been caught by
              // form validator.
              console.error('Currency code:', match, 'is not recognized');
              return;
            }

            if ($scope.advanced_feature_switch === false || $scope.amount === "") {
              // $scope.amount = Number(ripple.Amount.consts.max_value);
              $scope.amount = Options.gateway_max_limit;
            }

            var amount = ripple.Amount.from_human('' + $scope.amount + ' ' + $scope.lineCurrencyObj.to_hex(), {reference_date: new Date(+new Date() + 5*60000)});

            amount.set_issuer($scope.counterparty_address);
            if (!amount.is_valid()) {
              // Invalid amount. Indicates a bug in one of the validators.
              return;
            }

            $scope.amount_feedback = amount;

            $scope.confirm_wait = true;
            $timeout(function () {
              $scope.confirm_wait = false;
            }, 1000, true);

            $scope.mode = 'confirm';

            /**
             * Warning messages
             *
             * - firstIssuer
             * - sameIssuer
             * - multipleIssuers
             */
            // var currency = amount.currency().to_human({full_name:$scope.currencies_all_keyed[amount.currency().get_iso()].name});
            // var balance = $scope.balances[currency];
            // $scope.currencyWarning = false;

            // New trust on a currency or no rippling enabled
            // if (!balance || !$scope.allowrippling) {
            //   $scope.currencyWarning = 'firstIssuer';
            // }
            // else {
              // Trust limit change
              // for (var counterparty in balance.components) {
              //   if (counterparty === $scope.counterparty_address)
              //     $scope.currencyWarning = 'sameIssuer';
              // }

              // Multiple trusts on a same currency
              // if (!$scope.currencyWarning)
              //   $scope.currencyWarning = 'multipleIssuers';
            // }
          });
        })
        .on('error', function (m){
          setImmediate(function () {
{'fraction_non_alphanumeric': 0.14363817097415507, 'fraction_numerical': 0.004970178926441352, 'mean_word_length': 2.041057934508816, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7446631', 'n_tokens_mistral': 3568, 'n_tokens_neox': 3454, 'n_words': 646}