<commit_before>'use strict';

function OmnibarController($q, $scope, $timeout, $rootScope, UISessionService, ItemsService, NotesService, TasksService, AnalyticsService) {

  $scope.omnibarText = {};
  $scope.omnibarVisible = false;
  $scope.searchText = {};
  $scope.omnibarKeywords = {isVisible: false};
  $scope.isItemEditMode = false;
  $scope.isItemAddMode = false;

  var omnibarInputFocusCallbackFunction;

  var omnibarFeatures = {
    search: {
      inputPlaceholder: 'save / recall',
      footerSaveText: 'save to inbox',
      saveItemLocation: 'inbox',
      itemSaveMethod: ItemsService.saveItem.bind(ItemsService),
      persistentItemValues: ['title'],
      isActive: true  // default feature
    },
    task: {
      inputPlaceholder: 'add task',
      footerSaveText: 'save',
      saveItemLocation: 'tasks',
      itemSaveMethod: TasksService.saveTask.bind(TasksService),
      persistentItemValues: ['title', 'description']
    },
    note: {
      inputPlaceholder: 'add note',
      footerSaveText: 'save',
      saveItemLocation: 'notes',
      itemSaveMethod: $scope.saveNote,
      initializeItemFuncion: $scope.initializeOmnibarNote,
      persistentItemValues: ['title', 'content']
    }
  };

  var omnibarActionsHeight = 44, omnibarContainerHeight = 156, keyboardHeight = 0;
  function getOmnibarStaticContentHeight() {
    return omnibarContainerHeight - ($scope.isItemEditMode || $scope.isItemAddMode ? omnibarActionsHeight : 0);
  }

  $scope.getOmnibarFeatureHeight = function getOmnibarFeatureHeight() {
    if ($scope.currentHeight <= 769 || $scope.currentWidth <= 1025) {
      return $scope.currentHeight - getOmnibarStaticContentHeight() - keyboardHeight;
    }
    return 770 - getOmnibarStaticContentHeight() - keyboardHeight;
  };

  $scope.$watch('softKeyboard.height', function(newValue){
    if (newValue){
      keyboardHeight = newValue;
    } else {
      keyboardHeight = 0;
    }
  });

  $scope.getOmnibarVisibilityClass = function getOmnibarVisibilityClass() {
    return $scope.omnibarVisible ? 'omnibar-visible' : 'omnibar-hidden';
  };

  $scope.closeOmnibar = function closeOmnibar() {
    if ($scope.omnibarVisible) $scope.omnibarVisible = false;
  };

  $scope.omnibarHasText = function omnibarHasText() {
    return $scope.omnibarText.title && $scope.omnibarText.title.length !== 0;
  };

  $scope.isSearchActive = function isSearchActive() {
    return $scope.searchText.delayed && $scope.searchText.delayed.length > 1;
  };
  $scope.$watch('omnibarText.title', function(newTitle/*, oldTitle*/) {
    $scope.searchText.current = newTitle;
    if (newTitle && newTitle.length > 1){
      // Use a delayed update for search
      $timeout(function(){
        if ($scope.searchText.current === newTitle){
          $scope.searchText.delayed = newTitle;
        }
      }, 700);
    } else {
      $scope.searchText.delayed = undefined;
    }
  });

  // TODO analytics visit omnibar
  $scope.openOmnibar = function openOmnibar(feature) {
    if (!$scope.onboardingInProgress){
      AnalyticsService.visit('omnibar');
      initializeNewItemFromOmnibarText(feature);
      $scope.setOmnibarFeatureActive(feature);
      $scope.omnibarVisible = true;
    }
  };

  $scope.editItemInOmnibar = function editItemInOmnibar(item, feature) {
    $scope.isItemEditMode = true;
    $scope.omnibarText = item;
    $scope.openOmnibar(feature);
  };

  $scope.addItemInOmnibar = function addItemInOmnibar(item, feature) {
    $scope.isItemAddMode = true;
    $scope.omnibarText = item;
    $scope.openOmnibar(feature);
  };

  $scope.omnibarKeyDown = function omnibarKeyDown(event) {
    if (event.keyCode === 27) $scope.clearAndHideOmnibar();
  };

  // For empty omnibar search placeholder
  $scope.hideEmptyOmnibar = function hideEmptyOmnibar() {
    $scope.clearAndHideOmnibar();
  };

  $scope.saveOmnibarText = function saveOmnibarText() {
    var activeOmnibarFeature;
    if ($scope.omnibarHasText() && !$scope.isLoading) {
      activeOmnibarFeature = $scope.getActiveOmnibarFeature();
      omnibarFeatures[activeOmnibarFeature].itemSaveMethod($scope[activeOmnibarFeature], UISessionService.getActiveUUID()).then(function() {
        if (!$scope.isItemEditMode && !$scope.isItemAddMode){
          UISessionService.setToasterNotification(omnibarFeatures[activeOmnibarFeature].saveItemLocation);
        }
        if (!$scope.isItemEditMode || $scope.isItemAddMode){
          if ($scope.getOnboardingPhase() === 'itemAdded' || $scope.getOnboardingPhase() === 'sortingStarted'){
            $scope.setOnboardingPhase('secondItemAdded');
          }
        }
        $scope.clearAndHideOmnibar();
      });
    }
  };

  $scope.isOmnibarFeatureActive = function isOmnibarFeatureActive(feature) {
    return omnibarFeatures[feature].isActive;
  };

  $scope.setOmnibarFeatureActive = function setOmnibarFeatureActive(newActiveFeature) {<commit_msg>Fixed enter not saving omnibar text<commit_after>'use strict';

function OmnibarController($q, $scope, $timeout, $rootScope, UISessionService, ItemsService, NotesService, TasksService, AnalyticsService) {

  $scope.omnibarText = {};
  $scope.omnibarVisible = false;
  $scope.searchText = {};
  $scope.omnibarKeywords = {isVisible: false};
  $scope.isItemEditMode = false;
  $scope.isItemAddMode = false;

  var omnibarInputFocusCallbackFunction;

  var omnibarFeatures = {
    search: {
      inputPlaceholder: 'save / recall',
      footerSaveText: 'save to inbox',
      saveItemLocation: 'inbox',
      itemSaveMethod: ItemsService.saveItem.bind(ItemsService),
      persistentItemValues: ['title'],
      isActive: true  // default feature
    },
    task: {
      inputPlaceholder: 'add task',
      footerSaveText: 'save',
      saveItemLocation: 'tasks',
      itemSaveMethod: TasksService.saveTask.bind(TasksService),
      persistentItemValues: ['title', 'description']
    },
    note: {
      inputPlaceholder: 'add note',
      footerSaveText: 'save',
      saveItemLocation: 'notes',
      itemSaveMethod: $scope.saveNote,
      initializeItemFuncion: $scope.initializeOmnibarNote,
      persistentItemValues: ['title', 'content']
    }
  };

  var omnibarActionsHeight = 44, omnibarContainerHeight = 156, keyboardHeight = 0;
  function getOmnibarStaticContentHeight() {
    return omnibarContainerHeight - ($scope.isItemEditMode || $scope.isItemAddMode ? omnibarActionsHeight : 0);
  }

  $scope.getOmnibarFeatureHeight = function getOmnibarFeatureHeight() {
    if ($scope.currentHeight <= 769 || $scope.currentWidth <= 1025) {
      return $scope.currentHeight - getOmnibarStaticContentHeight() - keyboardHeight;
    }
    return 770 - getOmnibarStaticContentHeight() - keyboardHeight;
  };

  $scope.$watch('softKeyboard.height', function(newValue){
    if (newValue){
      keyboardHeight = newValue;
    } else {
      keyboardHeight = 0;
    }
  });

  $scope.getOmnibarVisibilityClass = function getOmnibarVisibilityClass() {
    return $scope.omnibarVisible ? 'omnibar-visible' : 'omnibar-hidden';
  };

  $scope.closeOmnibar = function closeOmnibar() {
    if ($scope.omnibarVisible) $scope.omnibarVisible = false;
  };

  $scope.omnibarHasText = function omnibarHasText() {
    return $scope.omnibarText.title && $scope.omnibarText.title.length !== 0;
  };

  $scope.isSearchActive = function isSearchActive() {
    return $scope.searchText.delayed && $scope.searchText.delayed.length > 1;
  };
  $scope.$watch('omnibarText.title', function(newTitle/*, oldTitle*/) {
    $scope.searchText.current = newTitle;
    if (newTitle && newTitle.length > 1){
      // Use a delayed update for search
      $timeout(function(){
        if ($scope.searchText.current === newTitle){
          $scope.searchText.delayed = newTitle;
        }
      }, 700);
    } else {
      $scope.searchText.delayed = undefined;
    }
  });

  // TODO analytics visit omnibar
  $scope.openOmnibar = function openOmnibar(feature) {
    if (!$scope.onboardingInProgress){
      AnalyticsService.visit('omnibar');
      initializeNewItemFromOmnibarText(feature);
      $scope.setOmnibarFeatureActive(feature);
      $scope.omnibarVisible = true;
    }
  };

  $scope.editItemInOmnibar = function editItemInOmnibar(item, feature) {
    $scope.isItemEditMode = true;
    $scope.omnibarText = item;
    $scope.openOmnibar(feature);
  };

  $scope.addItemInOmnibar = function addItemInOmnibar(item, feature) {
    $scope.isItemAddMode = true;
    $scope.omnibarText = item;
    $scope.openOmnibar(feature);
  };

  $scope.omnibarKeyDown = function omnibarKeyDown(event) {
    if (event.keyCode === 27){
      $scope.clearAndHideOmnibar();
    }else if (event.keyCode === 13){
      // Enter in omnibar saves, no line breaks allowed
      event.preventDefault();
      event.stopPropagation();
      $scope.saveOmnibarText();
    }
  };

  // For empty omnibar search placeholder
  $scope.hideEmptyOmnibar = function hideEmptyOmnibar() {
    $scope.clearAndHideOmnibar();
  };

  $scope.saveOmnibarText = function saveOmnibarText() {
    var activeOmnibarFeature;
    if ($scope.omnibarHasText() && !$scope.isLoading) {
      activeOmnibarFeature = $scope.getActiveOmnibarFeature();
      omnibarFeatures[activeOmnibarFeature].itemSaveMethod($scope[activeOmnibarFeature], UISessionService.getActiveUUID()).then(function() {
        if (!$scope.isItemEditMode && !$scope.isItemAddMode){
          UISessionService.setToasterNotification(omnibarFeatures[activeOmnibarFeature].saveItemLocation);
        }
        if (!$scope.isItemEditMode || $scope.isItemAddMode){
          if ($scope.getOnboardingPhase() === 'itemAdded' || $scope.getOnboardingPhase() === 'sortingStarted'){
            $scope.setOnboardingPhase('secondItemAdded');
          }
        }
        $scope.clearAndHideOmnibar();
      });
    }
  };

  $scope.isOmnibarFeatureActive = function isOmnibarFeatureActive(feature) {
    return omnibarFeatures[feature].isActive;
  };

  $scope.setOmnibarFeatureActive = function setOmnibarFeatureActive(newActiveFeature) {
{'fraction_non_alphanumeric': 0.12531178289933154, 'fraction_numerical': 0.005387608500448967, 'mean_word_length': 3.768791627021884, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '937899', 'n_tokens_mistral': 3334, 'n_tokens_neox': 3225, 'n_words': 516}