<commit_before>'use strict';

angular.module('mean.accounts')


/***
 * Controller a user's account page
 *
 ***/
.controller('AccountCtrl', [
  '$scope', '$rootScope', '$location', '$state', '$stateParams', 'AUTH_EVENTS', 'AuthSrv', 'AccountSrv', 'MetabarSrv', 'toaster',
  function ($scope, $rootScope, $location, $state, $stateParams, AUTH_EVENTS, AuthSrv, AccountSrv, MetabarSrv, toaster) {

  /* ---------- SETUP ----------------------------------------------------------------------------- */
  var editWatcher;               // Watch for model changes in editmode

  $scope.editMode        = false;       // edit mode is on or off
  $scope.contentLoading  = true;
  $scope.contentEdited   = false;  // models is changed or not during editmode
  $scope.userId          = $stateParams.userId;
  $scope.accountSlug     = $stateParams.slug;

  if ($state.current.data && $state.current.data.editMode) {
    $scope.editMode = true;
  }

  /* ---------- ROOTSCOPE EVENTS ------------------------------------------------------------------ */
  // Turn on or off editMode for a dataset
  $scope.$on('metabar::editMode', function(evt,mode){
    $scope.editMode = mode;
    if (mode === true){
      // turn edit mode on
      editWatcher = $scope.$watchCollection('account', function (newval, oldval) {
        if (!angular.equals(oldval, newval)) {
          $scope.contentEdited = true;
        }
      });
    } else {
      // turn watching off.
      editWatcher();
    }
  });


  /* ---------- FUNCTIONS ------------------------------------------------------------------------- */
  function getAccount(){
    // get account information of user by userSlug
    AccountSrv.get({slug: $stateParams.slug}, function(result){

      $scope.account = result.account;
      $scope.contentLoading = false;

      // send metadata to the metabar
      var item = $scope.account;
      item.postType = 'account';
      var meta = {
        postType: 'account',
        _id: $scope.account._id,
        canEdit: AuthSrv.canEdit(item)
      };
      MetabarSrv.setPostMeta(meta);

      updateHeaderImage();

    });
  }

  function initiateNewAccount(){
    $scope.account = {
      name        : '<NAME>',
      description : 'Description of the account',
      owner       : $scope.userId
    };
    $scope.contentLoading = false;
    console.log('account initiated', $scope.account);
  }

  function updateHeaderImage(){
    $scope.headerStyle={
      'background-image': 'url(' + $scope.account.headerPattern + '), url(' + $scope.account.headerImage + ')',
      '-webkit-filter': 'hue-rotate(90deg)',
      'background-blend-mode': 'multiply'
    };
  }


  /* ---------- SCOPE FUNCTIONS ------------------------------------------------------------------- */
  $scope.toggleEditMode = function(){
    $scope.editMode = !$scope.editMode;
    // send closed message to metabar
    if ($scope.editMode === false) {
      $rootScope.$broadcast('editMode::false');
    }
  };

  $scope.create = function(){
    console.log('Creating new account');
    AccountSrv.save($scope.account, function(res){
      console.log('Account result', res);
      if (res.err){
        if (res.code === 11000) {
          toaster.pop('danger', 'Sorry, that account name already exists. Please chose a different name. ');
        }
      }
      if (res._id) {
        console.log('Account result', res);
        toaster.pop('success', 'Created', 'Collection created.');
        $state.go('account.view', {slug: res.slug});
      }
    });
  };


  /* ---------- INIT ----------------------------------------------------------------------------- */
  // View existing account<commit_msg>after account creation, add account to rootscope user<commit_after>'use strict';

angular.module('mean.accounts')


/***
 * Controller a user's account page
 *
 ***/
.controller('AccountCtrl', [
  '$scope', '$rootScope', '$location', '$state', '$stateParams', 'AUTH_EVENTS', 'AuthSrv', 'AccountSrv', 'MetabarSrv', 'toaster',
  function ($scope, $rootScope, $location, $state, $stateParams, AUTH_EVENTS, AuthSrv, AccountSrv, MetabarSrv, toaster) {

  /* ---------- SETUP ----------------------------------------------------------------------------- */
  var editWatcher;               // Watch for model changes in editmode

  $scope.editMode        = false;       // edit mode is on or off
  $scope.contentLoading  = true;
  $scope.contentEdited   = false;  // models is changed or not during editmode
  $scope.userId          = $stateParams.userId;
  $scope.accountSlug     = $stateParams.slug;

  if ($state.current.data && $state.current.data.editMode) {
    $scope.editMode = true;
  }

  /* ---------- ROOTSCOPE EVENTS ------------------------------------------------------------------ */
  // Turn on or off editMode for a dataset
  $scope.$on('metabar::editMode', function(evt,mode){
    $scope.editMode = mode;
    if (mode === true){
      // turn edit mode on
      editWatcher = $scope.$watchCollection('account', function (newval, oldval) {
        if (!angular.equals(oldval, newval)) {
          $scope.contentEdited = true;
        }
      });
    } else {
      // turn watching off.
      editWatcher();
    }
  });


  /* ---------- FUNCTIONS ------------------------------------------------------------------------- */
  function getAccount(){
    // get account information of user by userSlug
    AccountSrv.get({slug: $stateParams.slug}, function(result){

      $scope.account = result.account;
      $scope.contentLoading = false;

      // send metadata to the metabar
      var item = $scope.account;
      item.postType = 'account';
      var meta = {
        postType: 'account',
        _id: $scope.account._id,
        canEdit: AuthSrv.canEdit(item)
      };
      MetabarSrv.setPostMeta(meta);

      updateHeaderImage();

    });
  }

  function initiateNewAccount(){
    $scope.account = {
      name        : '<NAME>',
      description : 'Description of the account',
      owner       : $scope.userId
    };
    $scope.contentLoading = false;
    console.log('account initiated', $scope.account);
  }

  function updateHeaderImage(){
    $scope.headerStyle={
      'background-image': 'url(' + $scope.account.headerPattern + '), url(' + $scope.account.headerImage + ')',
      '-webkit-filter': 'hue-rotate(90deg)',
      'background-blend-mode': 'multiply'
    };
  }


  /* ---------- SCOPE FUNCTIONS ------------------------------------------------------------------- */
  $scope.toggleEditMode = function(){
    $scope.editMode = !$scope.editMode;
    // send closed message to metabar
    if ($scope.editMode === false) {
      $rootScope.$broadcast('editMode::false');
    }
  };

  $scope.create = function(){
    console.log('Creating new account');
    AccountSrv.save($scope.account, function(res){
      console.log('Account result', res);
      if (res.err){
        if (res.code === 11000) {
          toaster.pop('danger', 'Sorry, that account name already exists. Please chose a different name. ');
        }
      }
      if (res._id) {
        // add to local user
        $rootScope.user.accounts.push(res);
        console.log('Account result', res);
        toaster.pop('success', 'Created', 'Collection created.');
        $state.go('account.view', {slug: res.slug});
      }
    });
  };


  /* ---------- INIT ----------------------------------------------------------------------------- */
  // View existing account
{'fraction_non_alphanumeric': 0.24680736658152977, 'fraction_numerical': 0.0018819733835192902, 'mean_word_length': 1.7917448405253282, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5388520', 'n_tokens_mistral': 2096, 'n_tokens_neox': 2041, 'n_words': 465}