<commit_before>angular.module('Directory.account.controllers', ['stripe', 'Directory.account.models', 'Directory.usage.models', 'ngPlayer'])
.config(function () {
  Stripe.setPublishableKey('<%= ENV["STRIPE_PUBLISHABLE_KEY"] %>');
})
.controller('AccountCtrl', ['$scope', 'Me', '$modal', '$q', 'Usage', 'Loader', function ($scope, Me, $modal, $q, Usage, Loader) {
  Me.authenticated(function (me) {
    $scope.me = me;
  });

  $scope.errors = {};
  $scope.usageDetails = {};

  $scope.saveCC = function(status, response) {
    if (response.error) {
      var e = {};
      e[response.error.param] = response.error.message;
      angular.copy(e, $scope.errors);
    } else {
      Me.authenticated(function (me) {

      });
    }
  }

  $scope.changeCreditCard = function () {
    $scope.pendingPlan = $scope.me.plan;
    $modal({template: '/assets/account/credit_card.html', persist: true, show: true, backdrop: 'static', scope: $scope});
  }

  $scope.toggleDetails = function($event) {
    var btn = $($event.target);
    // make sure we actually have the button and not the icon
    var btnTag = btn.prop('tagName').toLowerCase();
    //console.log('btnTag:', btnTag);
    if (btnTag != 'button') {
      btn = btn.parent();
    }
    var tbody = btn.parents('.usage-month-group');
    var period = tbody.data('period');

    // if not already loaded for period, load w/promise
    if (!$scope.me.usageDetailsByMonth(period)) {
      Loader(Usage.get(period)).then( function(usage) {
        var transcriptDetails = usage[period];
        //console.log(transcriptDetails);
        $scope.me.usage.transcripts[period] = transcriptDetails;
        if (usage.organization && $scope.me.organization) {
          $scope.me.organization.usage.transcripts[period] = usage.organization[period];
        }
        $scope.me.buildUsageSummary();
      });
    }

    //console.log('toggle ', period);
    if (!$scope.usageDetails[period]) {
      $scope.usageDetails[period] = true;
      btn.html('<i class="icon-large icon-collapse-alt"></i> Details');
    }
    else {
      $scope.usageDetails[period] = false;
      btn.html('<i class="icon-large icon-expand-alt"></i> Details');
    }
  }

  $scope.usageDetailsVisible = function(period) {
    return $scope.usageDetails[period]; 
  }

}])

.filter('shortInterval', function () {
  return function (interval) {
    if (!interval || interval.toLowerCase() == 'month') {
      return 'mo';
    } else {
      return 'yr';
    }
  }
})

.controller('AccountPlanCtrl', ['$scope', 'Me', 'Plan', '$modal', '$location', '$window', 'SampleAudio', 'Subscribe', function ($scope, Me, Plan, $modal, $location, $window, SampleAudio, Subscribe) {

  Me.authenticated(function (me) {
    $scope.me = me;
    $scope.pendingPlan = me.plan.id;
    $scope.interval = me.plan.interval;
    console.log($scope.me);
  });

  $scope.interval = 'month';

  $scope.isCurrentPlan = function (plan) {
    return plan && $scope.me && $scope.me.plan && plan.id == $scope.me.plan.id;
    console.log($scope.me.plan.id);
  };

  $scope.hasSpecialPlan = function () {
    if (!$scope.me) { return false };
    var special = true;
    angular.forEach($scope.plans, function (plan) {
      if (plan.id == $scope.me.plan.id) {
        special = false;
      }
    });
    return special;
  };
  
  subscribe = function (plan) {
    $scope.plan = plan;
    $modal({template: '/assets/account/signup_verify.html', persist: true, show: true, backdrop: 'static', scope: $scope});
  };

  $scope.completeSubscribe = function (plan) {
    $scope.pendingPlanObj = plan;
    if ($scope.me.creditCard || $scope.pendingPlanObj.amount == 0) {
      $scope.me.plan.id = $scope.pendingPlanObj.id;
      $scope.me.subscribe($scope.pendingPlanObj.id, $scope.offer.code).then(function () {
        $location.path('/account');
      });
    } else {
      var unregister = $scope.$on('creditCardSubmit', function () {
        unregister();
        $scope.me.plan.id = $scope.pendingPlanObj.id;
        $scope.me.subscribe($scope.pendingPlanObj.id, $scope.offer.code).then(function () {
          $location.path('/account');
        });
      });
      $modal({template: '/assets/account/credit_card.html', persist: true, show: true, backdrop: 'static', scope: $scope});
    }
  };
}])
.controller('CreditCardCtrl', ['$scope', function ($scope) {
  var currentUser = $scope.currentUser || $scope.me;
  $scope.errors = {};
  $scope.creditCard = currentUser.creditCard;
  $scope.saving = false;
  $scope.save = function(status, response) {
    if (response.error) {
      var e = {};
      e[response.error.param] = response.error.message;
      angular.copy(e, $scope.errors);
    } else {
      $scope.saving = true;
      currentUser.updateCreditCard(response.id).then(function (data) {
        $scope.$emit('creditCardSubmit', data);
        if ($scope.onDemandRequiresCC) {
          $scope.$emit('userHasValidCC', data);
        }
        $scope.hide();
      });
    }
  }
}])
.filter('monthOrYear', function () {
  var plans_ = [];
  return function monthOrYear (plans, monthOrYear) {
    if(angular.isArray(plans)) {
      plans_.length = 0;
      angular.forEach(plans, function (plan) {
        plan[monthOrYear].frequency = (plan[monthOrYear].frequency || monthOrYear);
        plan[monthOrYear].hours = (plan[monthOrYear].hours || plan.level);
        plans_.push(plan[monthOrYear]);
      });
      plans = plans_;
    }
    return plans;
  }<commit_msg>Close credit card modal after submit<commit_after>angular.module('Directory.account.controllers', ['stripe', 'Directory.account.models', 'Directory.usage.models', 'ngPlayer'])
.config(function () {
  Stripe.setPublishableKey('<%= ENV["STRIPE_PUBLISHABLE_KEY"] %>');
})
.controller('AccountCtrl', ['$scope', 'Me', '$modal', '$q', 'Usage', 'Loader', function ($scope, Me, $modal, $q, Usage, Loader) {
  Me.authenticated(function (me) {
    $scope.me = me;
  });

  $scope.errors = {};
  $scope.usageDetails = {};

  $scope.saveCC = function(status, response) {
    if (response.error) {
      var e = {};
      e[response.error.param] = response.error.message;
      angular.copy(e, $scope.errors);
    } else {
      Me.authenticated(function (me) {

      });
    }
  }

  $scope.changeCreditCard = function () {
    $scope.pendingPlan = $scope.me.plan;
    $scope.showCreditModal();
  }

  var creditCardModal = $modal({template: '/assets/account/credit_card.html', persist: true, show: false, backdrop: 'static', scope: $scope});

  $scope.showCreditModal = function () {
    creditCardModal.$promise.then(creditCardModal.show);
  }

  $scope.hideCreditModal = function () {
    creditCardModal.$promise.then(creditCardModal.hide);
  }

  $scope.$on('creditCardSubmit', function( event){
    $scope.hideCreditModal();
  });

  $scope.$on('openModal', function( event){
    console.log("open modal");
    $scope.showCreditModal();
  });

  $scope.toggleDetails = function($event) {
    var btn = $($event.target);
    // make sure we actually have the button and not the icon
    var btnTag = btn.prop('tagName').toLowerCase();
    //console.log('btnTag:', btnTag);
    if (btnTag != 'button') {
      btn = btn.parent();
    }
    var tbody = btn.parents('.usage-month-group');
    var period = tbody.data('period');

    // if not already loaded for period, load w/promise
    if (!$scope.me.usageDetailsByMonth(period)) {
      Loader(Usage.get(period)).then( function(usage) {
        var transcriptDetails = usage[period];
        //console.log(transcriptDetails);
        $scope.me.usage.transcripts[period] = transcriptDetails;
        if (usage.organization && $scope.me.organization) {
          $scope.me.organization.usage.transcripts[period] = usage.organization[period];
        }
        $scope.me.buildUsageSummary();
      });
    }

    //console.log('toggle ', period);
    if (!$scope.usageDetails[period]) {
      $scope.usageDetails[period] = true;
      btn.html('<i class="icon-large icon-collapse-alt"></i> Details');
    }
    else {
      $scope.usageDetails[period] = false;
      btn.html('<i class="icon-large icon-expand-alt"></i> Details');
    }
  }

  $scope.usageDetailsVisible = function(period) {
    return $scope.usageDetails[period]; 
  }

}])

.filter('shortInterval', function () {
  return function (interval) {
    if (!interval || interval.toLowerCase() == 'month') {
      return 'mo';
    } else {
      return 'yr';
    }
  }
})

.controller('AccountPlanCtrl', ['$scope', 'Me', 'Plan', '$modal', '$location', '$window', 'SampleAudio', 'Subscribe', function ($scope, Me, Plan, $modal, $location, $window, SampleAudio, Subscribe) {

  Me.authenticated(function (me) {
    $scope.me = me;
    $scope.pendingPlan = me.plan.id;
    $scope.interval = me.plan.interval;
    console.log($scope.me);
  });

  $scope.interval = 'month';

  $scope.isCurrentPlan = function (plan) {
    return plan && $scope.me && $scope.me.plan && plan.id == $scope.me.plan.id;
    console.log($scope.me.plan.id);
  };

  $scope.hasSpecialPlan = function () {
    if (!$scope.me) { return false };
    var special = true;
    angular.forEach($scope.plans, function (plan) {
      if (plan.id == $scope.me.plan.id) {
        special = false;
      }
    });
    return special;
  };
  
  subscribe = function (plan) {
    $scope.plan = plan;
    $modal({template: '/assets/account/signup_verify.html', persist: true, show: true, backdrop: 'static', scope: $scope});
  };

  $scope.completeSubscribe = function (plan) {
    $scope.pendingPlanObj = plan;
    if ($scope.me.creditCard || $scope.pendingPlanObj.amount == 0) {
      $scope.me.plan.id = $scope.pendingPlanObj.id;
      $scope.me.subscribe($scope.pendingPlanObj.id, $scope.offer.code).then(function () {
        $location.path('/account');
      });
    } else {
      var unregister = $scope.$on('creditCardSubmit', function () {
        unregister();
        $scope.me.plan.id = $scope.pendingPlanObj.id;
        $scope.me.subscribe($scope.pendingPlanObj.id, $scope.offer.code).then(function () {
          $location.path('/account');
        });
      });
      $scope.$broadcast('openModal');
    }
  };
}])
.controller('CreditCardCtrl', ['$scope', function ($scope) {
  var currentUser = $scope.currentUser || $scope.me;
  $scope.errors = {};
  $scope.creditCard = currentUser.creditCard;
  $scope.saving = false;
  $scope.save = function(status, response) {
    if (response.error) {
      var e = {};
      e[response.error.param] = response.error.message;
      angular.copy(e, $scope.errors);
    } else {
      $scope.saving = true;
      currentUser.updateCreditCard(response.id).then(function (data) {
        $scope.$emit('creditCardSubmit', data);
        if ($scope.onDemandRequiresCC) {
          $scope.$emit('userHasValidCC', data);
        }
      });
    }
  }
}])
.filter('monthOrYear', function () {
  var plans_ = [];
  return function monthOrYear (plans, monthOrYear) {
    if(angular.isArray(plans)) {
      plans_.length = 0;
      angular.forEach(plans, function (plan) {
        plan[monthOrYear].frequency = (plan[monthOrYear].frequency || monthOrYear);
        plan[monthOrYear].hours = (plan[monthOrYear].hours || plan.level);
        plans_.push(plan[monthOrYear]);
      });
      plans = plans_;
    }
    return plans;
  }
{'fraction_non_alphanumeric': 0.1782870288444523, 'fraction_numerical': 0.00035391966023712616, 'mean_word_length': 3.1057028695968034, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1881519', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3576, 'n_words': 648}