<commit_before>(function () {
  'use strict';

  angular
    .module('deck')
    .controller('DeckController', DeckController);

  /** @ngInject */
  function DeckController($stateParams, $state, BackendService, $log, DeckService, $mdDialog, $translate, $rootScope) {
    var vm = this;
    vm.selectedDeck = new BackendService.Deck();
    vm.load = false;
    vm.getDecks = getDecks;
    vm.selectedDeckChange = selectedDeckChange;
    vm.changeDeckName = changeDeckName;
    vm.textChange = textChange;
    vm.createDeck = createDeck;
    vm.selectDeck = selectDeck;
    vm.selectCard = selectCard;
    vm.removeCard = removeCard;
    vm.lostNetworkConnection = lostNetworkConnection;
    vm.clear = clear;
    vm.deckAccess = 'private';
    vm.emptyNameError = DeckService.getEmptyNameError();


    vm.access = $stateParams.access;

    function getDecks(query) {
      //for not loading list of deck on page init
      if (vm.load) {
        if (!vm.decks) {
          //create request for deck list
          vm.decks = BackendService.getDecks(vm.access);
        }
        return vm.decks
          .then(function (result) {
            var list = query ? result.filter(queryFilter(query)) : result;
            if (query){
              list.push({name:query});
            }
            return list;
          });
      } else {
        vm.load = true;
      }
    }

    function textChange(text) {
      DeckService.setNewDeckName(text)
    }

    function selectedDeckChange(deck) {
      if(deck === true){
        return changeDeckName();
      }
      if (deck) {
        if (deck.id){
          selectDeck(deck);
        } else {
          deck.access = vm.deckAccess;
          createDeck(deck);
        }
      }
    }

    function createDeck(deck) {
      DeckService.setDeckObj(deck);
      if($stateParams.deckId){
        $stateParams.deckId = null;
        $stateParams.cardId = null;
        initDeck(null);
      }
    }

    function selectDeck(deck) {
      DeckService.setDeckObj(deck);
      if (deck.id != $stateParams.deckId){
        $stateParams.deckId = deck.id;
        $stateParams.cardId = null;
        initDeck(deck.id);
      }
    }

    function changeDeckName() {
      if (!vm.selectedDeck){return}
      vm.selectedDeck.changeName(vm.searchText, vm.deckAccess)
      .then(function success() {
        $state.go("deck.addCard", {deckId: vm.selectedDeck.id});
        $state.reload("deck");
      },
      function error() {
        var message = 'I cant update Deck name';
        alert(message);
        throw message;
      })
    }

    function selectCard(card) {
      DeckService.setCardObj(card);
      //for selecting on ui (ng-repeat)
      if(card.id !=vm.selectedCardId) {
        vm.selectedCardId = card.id;
        $state.go("deck.addCard", {cardId: card.id}, {notify:true});
      } else {
        vm.selectedCardId = false;
        $state.go("deck.addCard", {cardId: null}, {notify:true});
      }
    }

    function removeCard(cardId){
      deleteCardDialog(cardId, vm.cards.length );
    }

    //LOCAL FUNCTIONS
    function queryFilter(query) {
      var lowercaseQuery = angular.lowercase(query);
      return function filterFn(deck) {
        if(deck.name){
          return (deck.name.toLowerCase().indexOf(lowercaseQuery) === 0);
        }
      };
    }

    function clear() {
      vm.searchText = null;
    }

    function getCards() {
      vm.selectedDeck.getFlashcards()
        .then(function (result) {
          vm.cards = result;
        }, function (e) {
          $log.error(e);
        });
    }

    //init current deck
    function initDeck(value) {
      if(value){
        BackendService.getDeckById(value)
          .then(function (result) {
            vm.selectedDeck = result;
            vm.selectedItem = vm.selectedDeck;
            DeckService.setNewDeckName(vm.selectedDeck.name);
            getCards();
          }, function (e) {
            $log.error(e);
          });
      } else {
        vm.selectedDeck = DeckService.getDeckObj();
        vm.selectedItem = vm.selectedDeck;
        vm.cards=[];
      }
      //clean card field
      if (!$stateParams.deckId){
        DeckService.setNewDeckName(null);
        vm.selectedDeck = DeckService.setDeckObj(null);
        vm.selectedItem = vm.selectedDeck;
      }
      $stateParams.cardId = null;
      if($state.$current == 'deck.addCard'){
        $state.reload('deck.addCard');
      }
    }
    initDeck($stateParams.deckId);

    function lostNetworkConnection(){
      $mdDialog.show(
          $mdDialog.alert()
            .clickOutsideToClose(false)
            .title($translate.instant('networkAlert-WARNING'))
            .textContent($translate.instant('deck-OFFLINE_REMOVE_CARD_MODAL'))
            .ariaLabel('Alert Dialog')
            .ok($translate.instant('networkAlert-AGREE'))
      );
    }

    //DELETE CARD DIALOG
    function deleteCardDialog(cardId, cardNo) {
        var content = $translate.instant("deck-REMOVE_CARD_MODAL");
        //info for last card
        if (cardNo < 2) {
          content = ($translate.instant("deck-REMOVE_LAST_CARD_MODAL"));
        }
        var confirm = $mdDialog.confirm()
          .title($translate.instant("deck-REMOVE_CARD"))
          .textContent(content)
          .ok($translate.instant("deck-YES"))
          .cancel($translate.instant("deck-NO"));
      $mdDialog.show(confirm)
        .then(function () {
          //delete card
          return vm.selectedDeck.removeFlashcard(cardId)
        }, function (e) {
          $log.error(e);
        })
        .then(function (result) {
          //delete deck if last card
          if (cardNo < 2) {
            $log.warn('last one flashcard');
            vm.selectedDeck.remove().then(function () {
              $state.go('decks');
            });
          } else {
            $state.go("deck.addCard", {deckId: vm.selectedDeck.id, cardId: null});
            getCards();
            $log.log(result);
          }
        }, function (e) {
          $log.error(e);
        });
    }
  }
})();
<commit_msg>fic STUDYBOX-263/264<commit_after>(function () {
  'use strict';

  angular
    .module('deck')
    .controller('DeckController', DeckController);

  /** @ngInject */
  function DeckController($stateParams, $state, BackendService, $log, DeckService, $mdDialog, $translate, $rootScope) {
    var vm = this;
    vm.selectedDeck = new BackendService.Deck();
    vm.load = false;
    vm.getDecks = getDecks;
    vm.selectedDeckChange = selectedDeckChange;
    vm.changeDeckName = changeDeckName;
    vm.textChange = textChange;
    vm.createDeck = createDeck;
    vm.selectDeck = selectDeck;
    vm.selectCard = selectCard;
    vm.removeCard = removeCard;
    vm.lostNetworkConnection = lostNetworkConnection;
    vm.clear = clear;
    vm.deckAccess = 'private';
    vm.emptyNameError = DeckService.getEmptyNameError();


    vm.access = $stateParams.access;

    function getDecks(query) {
      //for not loading list of deck on page init
      if (vm.load) {
        if (!vm.decks) {
          //create request for deck list
          vm.decks = BackendService.getDecks(vm.access);
        }
        return vm.decks
          .then(function (result) {
            var list = query ? result.filter(queryFilter(query)) : result;
            if (query){
              list.push({name:query});
            }
            return list;
          });
      } else {
        vm.load = true;
      }
    }

    function textChange(text) {
      DeckService.setNewDeckName(text)
    }

    function selectedDeckChange(deck) {
      if(deck === true){
        return changeDeckName();
      }
      if (deck) {
        if (deck.id){
          selectDeck(deck);
        } else {
          deck.access = vm.deckAccess;
          createDeck(deck);
        }
      }
    }

    function createDeck(deck) {
      DeckService.setDeckObj(deck);
      if($stateParams.deckId){
        $stateParams.deckId = null;
        $stateParams.cardId = null;
        initDeck(null);
      }
    }

    function selectDeck(deck) {
      DeckService.setDeckObj(deck);
      if (deck.id != $stateParams.deckId){
        $stateParams.deckId = deck.id;
        $stateParams.cardId = null;
        initDeck(deck.id);
      }
    }

    function changeDeckName() {
      if (!vm.selectedDeck){return}
      vm.selectedDeck.changeName(vm.searchText, vm.deckAccess)
      .then(function success() {
        $state.go("deck.addCard", {deckId: vm.selectedDeck.id});
        $state.reload("deck");
      },
      function error() {
        var message = 'I cant update Deck name';
        alert(message);
        throw message;
      })
    }

    function selectCard(card) {
      DeckService.setCardObj(card);
      //for selecting on ui (ng-repeat)
      if(card.id !=vm.selectedCardId) {
        vm.selectedCardId = card.id;
        $state.go("deck.addCard", {cardId: card.id}, {notify:true});
      } else {
        vm.selectedCardId = false;
        $state.go("deck.addCard", {cardId: null}, {notify:true});
      }
    }

    function removeCard(cardId){
      deleteCardDialog(cardId, vm.cards.length );
    }

    //LOCAL FUNCTIONS
    function queryFilter(query) {
      var lowercaseQuery = angular.lowercase(query);
      return function filterFn(deck) {
        if(deck.name){
          return (deck.name.toLowerCase().indexOf(lowercaseQuery) === 0);
        }
      };
    }

    function clear() {
      vm.searchText = null;
    }

    function getCards() {
      vm.selectedDeck.getFlashcards()
        .then(function (result) {
          vm.cards = result;
        }, function (e) {
          $log.error(e);
        });
    }

    //init current deck
    function initDeck(value) {
      if(value){
        BackendService.getDeckById(value)
          .then(function (result) {
            vm.selectedDeck = result;
            vm.selectedItem = vm.selectedDeck;
            DeckService.setNewDeckName(vm.selectedDeck.name);
            getCards();
          }, function (e) {
            $log.error(e);
          });
      } else {
        vm.selectedDeck = DeckService.getDeckObj();
        vm.selectedItem = vm.selectedDeck;
        vm.cards=[];
      }
      //clean card field
      if (!$stateParams.deckId){
        DeckService.setNewDeckName(null);
        vm.selectedDeck = DeckService.setDeckObj(null);
        vm.selectedItem = vm.selectedDeck;
      }
      $stateParams.cardId = null;
      if($state.$current == 'deck.addCard'){
        $state.reload('deck.addCard');
      }
    }
    initDeck($stateParams.deckId);

    function lostNetworkConnection(){
      $mdDialog.show(
          $mdDialog.alert()
            .clickOutsideToClose(false)
            .title($translate.instant('networkAlert-WARNING'))
            .textContent($translate.instant('deck-OFFLINE_REMOVE_CARD_MODAL'))
            .ariaLabel('Alert Dialog')
            .ok($translate.instant('networkAlert-AGREE'))
      );
    }

    //DELETE CARD DIALOG
    function deleteCardDialog(cardId, cardNo) {
      var content = $translate.instant("deck-REMOVE_CARD_MODAL");
      //info for last card
      if (cardNo < 2) {
        content = ($translate.instant("deck-REMOVE_LAST_CARD_MODAL"));
      }
      var confirm = $mdDialog.confirm()
        .title($translate.instant("deck-REMOVE_CARD"))
        .textContent(content)
        .ok($translate.instant("deck-YES"))
        .cancel($translate.instant("deck-NO"));
      $mdDialog.show(confirm)
        .then(function () {
          //delete card
          return vm.selectedDeck.removeFlashcard(cardId)
        }, function (e) {
          $log.error(e);
        })
        .then(function (result) {
          //delete deck if last card
          if (cardNo < 2) {
            $log.warn('last one flashcard');
            vm.selectedDeck.remove().then(function () {
              $state.go('decks');
            });
          } else {
            $state.go("deck.addCard", {deckId: vm.selectedDeck.id, cardId: null});
            getCards();
            $log.log(result);
          }
        }, function (e) {
          $log.error(e);
        });
    }
  }
})();

{'fraction_non_alphanumeric': 0.13339912821778108, 'fraction_numerical': 0.0009869232667160128, 'mean_word_length': 1.9935992122107336, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7225845', 'n_tokens_mistral': 3783, 'n_tokens_neox': 3717, 'n_words': 635}