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

var TradeTab = function ()
{
  Tab.call(this);
};

util.inherits(TradeTab, Tab);

TradeTab.prototype.mainMenu = 'advanced';

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

TradeTab.prototype.angularDeps = Tab.prototype.angularDeps.concat(['books']);

TradeTab.prototype.extraRoutes = [ 
  { name: '/trade/:first/:second' }
];

TradeTab.prototype.angular = function(module)
{
  module.controller('TradeCtrl', ['rpBooks', '$scope', 'rpId', 'rpNetwork', '$routeParams', '$location',
                                  function (books, $scope, $id, $network, $routeParams, $location)
  {
    if (!$id.loginStatus) return $id.goId();

    $scope.mode = "confirm";
    $scope.asum = [];
    $scope.bsum = [];

    var pairs = $scope.pairs_all;
    $scope.pairs_query = webutil.queryFromOptions(pairs);

    $scope.$watch('userBlob.data.contacts', function (contacts) {
      $scope.issuer_query = webutil.queryFromOptions(contacts);
    }, true);

    $scope.reset = function (keepPair) {
      var pair = keepPair ? $scope.order.currency_pair :
            store.get('ripple_trade_currency_pair') || pairs[0].name;
      var fIssuer = keepPair ? $scope.order.first_issuer : $id.account;
      var sIssuer = keepPair ? $scope.order.second_issuer : $id.account;
      var type = keepPair ? $scope.order.type : 'buy';

      if ($scope.orderForm) $scope.orderForm.$setPristine();
      $scope.mode = "trade";
      $scope.order = {
        type: type,
        first: '',
        price: '',
        second: '',
        currency_pair: pair,
        first_currency: pair.slice(0, 3),
        second_currency: pair.slice(4, 7),
        first_issuer: fIssuer,
        second_issuer: sIssuer,
        listing: 'orderbook',

        // This variable is true if both the pair and the issuers are set to
        // valid values. It is used to enable or disable all the functionality
        // on the page.
        valid_settings: false
      };

      updateSettings();
    };

    // Show "My Orders" if account has been funded
    $scope.$watch('account.Balance', function(){
      if ($scope.account.Balance) {
        $scope.order.listing = 'my';
      }
    }, true);

    $scope.back = function () {
      $scope.mode = "trade";
    };

    $scope.place_order = function () {
      $scope.mode = "confirm";
      if ($scope.order.type === 'buy') {
        $scope.order.sell_amount = $scope.order.second_amount;
        $scope.order.buy_amount = $scope.order.first_amount;
      } else {
        $scope.order.sell_amount = $scope.order.first_amount;
        $scope.order.buy_amount = $scope.order.second_amount;
      }
    };

    $scope.cancel_order = function ()
    {
      var tx = $network.remote.transaction();
      tx.offer_cancel($id.account, this.entry.seq);
      tx.on('success', function () {
        $scope.$apply(function () {
        });
      });
      tx.on('error', function () {
        $scope.$apply(function () {
          $scope.mode = "error";
        });
      });
      tx.submit();

      this.cancelled = true;
    };

    $scope.order_confirmed = function ()
    {
      var tx = $network.remote.transaction();
      tx.offer_create($id.account, $scope.order.buy_amount, $scope.order.sell_amount);

      tx.on('success', function (res) {
        $scope.$apply(function () {
          setEngineStatus(res, false);
          $scope.done(tx.hash);

          // Remember pair and increase order
          var found;

          for (var i = 0; i < $scope.pairs_all.length; i++) {
            if ($scope.pairs_all[i].name == $scope.order.currency_pair) {
              $scope.pairs_all[i].order++;
              found = true;
              break;
            }
          }

          if (!found) {
            $scope.pairs_all.push({<commit_msg>Trade tab: Include tfSell flag for sell offers. #714<commit_after>var util = require('util');
var webutil = require('../util/web');
var Tab = require('../client/tab').Tab;
var Amount = ripple.Amount;

var TradeTab = function ()
{
  Tab.call(this);
};

util.inherits(TradeTab, Tab);

TradeTab.prototype.mainMenu = 'advanced';

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

TradeTab.prototype.angularDeps = Tab.prototype.angularDeps.concat(['books']);

TradeTab.prototype.extraRoutes = [ 
  { name: '/trade/:first/:second' }
];

TradeTab.prototype.angular = function(module)
{
  module.controller('TradeCtrl', ['rpBooks', '$scope', 'rpId', 'rpNetwork', '$routeParams', '$location',
                                  function (books, $scope, $id, $network, $routeParams, $location)
  {
    if (!$id.loginStatus) return $id.goId();

    $scope.mode = "confirm";
    $scope.asum = [];
    $scope.bsum = [];

    var pairs = $scope.pairs_all;
    $scope.pairs_query = webutil.queryFromOptions(pairs);

    $scope.$watch('userBlob.data.contacts', function (contacts) {
      $scope.issuer_query = webutil.queryFromOptions(contacts);
    }, true);

    $scope.reset = function (keepPair) {
      var pair = keepPair ? $scope.order.currency_pair :
            store.get('ripple_trade_currency_pair') || pairs[0].name;
      var fIssuer = keepPair ? $scope.order.first_issuer : $id.account;
      var sIssuer = keepPair ? $scope.order.second_issuer : $id.account;
      var type = keepPair ? $scope.order.type : 'buy';

      if ($scope.orderForm) $scope.orderForm.$setPristine();
      $scope.mode = "trade";
      $scope.order = {
        type: type,
        first: '',
        price: '',
        second: '',
        currency_pair: pair,
        first_currency: pair.slice(0, 3),
        second_currency: pair.slice(4, 7),
        first_issuer: fIssuer,
        second_issuer: sIssuer,
        listing: 'orderbook',

        // This variable is true if both the pair and the issuers are set to
        // valid values. It is used to enable or disable all the functionality
        // on the page.
        valid_settings: false
      };

      updateSettings();
    };

    // Show "My Orders" if account has been funded
    $scope.$watch('account.Balance', function(){
      if ($scope.account.Balance) {
        $scope.order.listing = 'my';
      }
    }, true);

    $scope.back = function () {
      $scope.mode = "trade";
    };

    $scope.place_order = function () {
      $scope.mode = "confirm";
      if ($scope.order.type === 'buy') {
        $scope.order.sell_amount = $scope.order.second_amount;
        $scope.order.buy_amount = $scope.order.first_amount;
      } else {
        $scope.order.sell_amount = $scope.order.first_amount;
        $scope.order.buy_amount = $scope.order.second_amount;
      }
    };

    $scope.cancel_order = function ()
    {
      var tx = $network.remote.transaction();
      tx.offer_cancel($id.account, this.entry.seq);
      tx.on('success', function () {
        $scope.$apply(function () {
        });
      });
      tx.on('error', function () {
        $scope.$apply(function () {
          $scope.mode = "error";
        });
      });
      tx.submit();

      this.cancelled = true;
    };

    $scope.order_confirmed = function ()
    {
      var tx = $network.remote.transaction();
      tx.offer_create($id.account, $scope.order.buy_amount, $scope.order.sell_amount);

      // Sets a tfSell flag. This is the only way to distinguish sell offers from buys.
      if ($scope.order.type == 'sell')
        tx.set_flags('Sell');

      tx.on('success', function (res) {
        $scope.$apply(function () {
          setEngineStatus(res, false);
          $scope.done(tx.hash);

          // Remember pair and increase order
          var found;

          for (var i = 0; i < $scope.pairs_all.length; i++) {
            if ($scope.pairs_all[i].name == $scope.order.currency_pair) {
              $scope.pairs_all[i].order++;
              found = true;
              break;
            }
          }

          if (!found) {
            $scope.pairs_all.push({
{'fraction_non_alphanumeric': 0.1568, 'fraction_numerical': 0.0018461538461538461, 'mean_word_length': 2.3344275748871564, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1835907', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2536, 'n_words': 500}