<commit_before>/* Copyright 2013-2016 Extended Mind Technologies Oy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /* global console */
 'use strict';

 function EditorController($rootScope, $scope, $timeout, $q,
                           BackendClientService, ConvertService, ItemsService, ListsService, NotesService,
                           PlatformService, SwiperService, TagsService, TasksService, UISessionService,
                           URLService, UserSessionService) {

  // OPENING, INITIALIZING, CLOSING

  var dataInEdit;
  var exitAppOnBack = false;
  var reinitializeEditorCallback;
  $scope.initializeEditor = function(editorType, data, mode){
    // Reset $scope variables. These may exist from previous editor.
    $scope.task = undefined;
    $scope.note = undefined;
    $scope.list = undefined;
    $scope.item = undefined;
    $scope.tag = undefined;
    $scope.iterableItems = undefined;
    dataInEdit = data;

    $scope.editorType = editorType;
    $scope.editorVisible = true;
    $scope.mode = mode;
    var overrideTitleFocus = false;
    if (editorType === 'task'){
      $scope.task = dataInEdit;
      if (mode == 'title') overrideTitleFocus = true;
      initializeEditorVisibilityAndPermission(dataInEdit);
    }else if (editorType === 'note'){
      $scope.note = dataInEdit;
      initializeEditorVisibilityAndPermission(dataInEdit);
      if (mode === 'advanced'){
        SwiperService.setInitialSlidePath('noteEditor', 'noteEditor/advanced');
      }else if (mode == 'expand'){
        $scope.toggleExpandEditor();
      }
    }else if (editorType === 'list'){
      var list = dataInEdit.list || dataInEdit;
      $scope.list = list;
      if (mode == 'title') overrideTitleFocus = true;
      initializeEditorVisibilityAndPermission(list);
    }else if (editorType === 'item'){
      $scope.item = dataInEdit;
      initializeEditorVisibilityAndPermission(dataInEdit);
      exitAppOnBack = mode === 'share';
    }else if (editorType === 'tag'){
      $scope.tag = dataInEdit;
    }else if (editorType === 'user'){
      $scope.user = dataInEdit || {};
    }else if (editorType === 'recurring') {
      $scope.iterableItems = dataInEdit;
      initializeEditorVisibilityAndPermission(dataInEdit[0]);
    }
    if (angular.isFunction(reinitializeEditorCallback)) reinitializeEditorCallback();

    // Handle editor opened straight away when there is no animation for editor opening
    if ($scope.columns === 3){
      handleEditorOpened(overrideTitleFocus);
    }
    if ($scope.columns === 3 || $scope.isEditorVisible()){
      // Broadcast an adjust that will hopefully fix title textarea not containing last words in mobile
      // when editor is already open, and also for larger screens when editor is opened
      window.requestAnimationFrame(function(){
        $scope.$broadcast('elastic:adjust');
      });
    }
    if (!$scope.$$phase && !$rootScope.$$phase) $scope.$digest();
  };

  // Re-initializing, this is fired only on the second go, as the above method is fired before
  // this can be called in child editors.
  $scope.registerReinitializeEditorCallback = function(callback){
    reinitializeEditorCallback = callback;
  };
  $scope.unregisterReinitializeEditorCallback = function(){
    reinitializeEditorCallback = undefined;
  };

  function initializeEditorVisibilityAndPermission(item) {

    function isCollectiveOwner(ownerUUID) {
      return $scope.collectives && $scope.collectives[ownerUUID];
    }

    function isSharedListOwner(ownerUUID) {
      return $scope.sharedLists && $scope.sharedLists[ownerUUID];
    }

    var ownerUUID = item.trans.owner;
    var userUUID = UserSessionService.getUserUUID();
    var activeUUID = UISessionService.getActiveUUID();

    if (ownerUUID === userUUID && ownerUUID === activeUUID) {
      $scope.fullEditor = true;
      $scope.readOnly = false;
    } else if (isCollectiveOwner(ownerUUID)) {
      $scope.fullEditor = ownerUUID === activeUUID;
      $scope.readOnly = $scope.isCollectiveReadOnly(ownerUUID);
    } else if (isSharedListOwner(ownerUUID)) {
      $scope.fullEditor = false;
      if (item.trans.itemType === 'list') {
        $scope.readOnly = true;
      }else{
        var listUUID;
        if (item.trans.list) {
          listUUID = item.trans.list.trans.uuid;
        }
        $scope.readOnly = listUUID && $scope.isSharedListReadOnly(ownerUUID, listUUID);
      }
    }
  }

  function evaluateExitApp(){
    if (exitAppOnBack){
      exitAppOnBack = false;
      if (navigator && navigator.Backbutton && navigator.Backbutton.goBack){
        navigator.Backbutton.goBack(undefined, function(){
          // Try to go home if there is no going back
          navigator.Backbutton.goHome(undefined, function(){
            // Just exit app, if can't go home either
            if (navigator.app && navigator.app.exitApp){
              navigator.app.exitApp();
            }
          });
        });
      }
    }
  }

  function editorAboutToClose() {
    var featureActionBeforeExitPromise;
    if (typeof featureEditorAboutToCloseCallback === 'function'){
      featureActionBeforeExitPromise = featureEditorAboutToCloseCallback(exitAppOnBack);
    }
    if (featureActionBeforeExitPromise){
      featureActionBeforeExitPromise.then(function(){
        evaluateExitApp();
      });
    }else{
      evaluateExitApp();
    }
  }

  function handleEditorOpened(overrideTitleFocus){
    if (featureEditorOpenedCallback) featureEditorOpenedCallback();
    var autoFocusTitle =
      overrideTitleFocus ||
      $scope.editorType === 'omnibar' ||
      ($scope.editorType === 'note' && !$scope.note.uuid);
    if (autoFocusTitle && (!dataInEdit || !dataInEdit.deleted)){
      // Focus on title
      setFocusOnTitlebarInput();
    }
  }

  // Callback from Snap.js, outside of AngularJS event loop
  function editorOpened() {
    // Handle editor opened from here only when there is an animation for editor
    // opening, as there is for below three columns
    if ($scope.columns < 3){
      handleEditorOpened();
      // Broadcast an adjust, that will hopefully fix textarea problems for mobile devices,
      // initializeEditor broadcasts this for larger screens that don't have an animation,
      // and also in situations where there is a change of editor, say from omnibar to note
      $scope.$broadcast('elastic:adjust');
    }
  }<commit_msg>Fixed non-synced note getting title autofocus all the time.<commit_after>/* Copyright 2013-2016 Extended Mind Technologies Oy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /* global console */
 'use strict';

 function EditorController($rootScope, $scope, $timeout, $q,
                           BackendClientService, ConvertService, ItemsService, ListsService, NotesService,
                           PlatformService, SwiperService, TagsService, TasksService, UISessionService,
                           URLService, UserSessionService) {

  // OPENING, INITIALIZING, CLOSING

  var dataInEdit;
  var exitAppOnBack = false;
  var reinitializeEditorCallback;
  $scope.initializeEditor = function(editorType, data, mode){
    // Reset $scope variables. These may exist from previous editor.
    $scope.task = undefined;
    $scope.note = undefined;
    $scope.list = undefined;
    $scope.item = undefined;
    $scope.tag = undefined;
    $scope.iterableItems = undefined;
    dataInEdit = data;

    $scope.editorType = editorType;
    $scope.editorVisible = true;
    $scope.mode = mode;
    var overrideTitleFocus = false;
    if (editorType === 'task'){
      $scope.task = dataInEdit;
      if (mode == 'title') overrideTitleFocus = true;
      initializeEditorVisibilityAndPermission(dataInEdit);
    }else if (editorType === 'note'){
      $scope.note = dataInEdit;
      initializeEditorVisibilityAndPermission(dataInEdit);
      if (mode === 'advanced'){
        SwiperService.setInitialSlidePath('noteEditor', 'noteEditor/advanced');
      }else if (mode == 'expand'){
        $scope.toggleExpandEditor();
      }
    }else if (editorType === 'list'){
      var list = dataInEdit.list || dataInEdit;
      $scope.list = list;
      if (mode == 'title') overrideTitleFocus = true;
      initializeEditorVisibilityAndPermission(list);
    }else if (editorType === 'item'){
      $scope.item = dataInEdit;
      initializeEditorVisibilityAndPermission(dataInEdit);
      exitAppOnBack = mode === 'share';
    }else if (editorType === 'tag'){
      $scope.tag = dataInEdit;
    }else if (editorType === 'user'){
      $scope.user = dataInEdit || {};
    }else if (editorType === 'recurring') {
      $scope.iterableItems = dataInEdit;
      initializeEditorVisibilityAndPermission(dataInEdit[0]);
    }
    if (angular.isFunction(reinitializeEditorCallback)) reinitializeEditorCallback();

    // Handle editor opened straight away when there is no animation for editor opening
    if ($scope.columns === 3){
      handleEditorOpened(overrideTitleFocus);
    }
    if ($scope.columns === 3 || $scope.isEditorVisible()){
      // Broadcast an adjust that will hopefully fix title textarea not containing last words in mobile
      // when editor is already open, and also for larger screens when editor is opened
      window.requestAnimationFrame(function(){
        $scope.$broadcast('elastic:adjust');
      });
    }
    if (!$scope.$$phase && !$rootScope.$$phase) $scope.$digest();
  };

  // Re-initializing, this is fired only on the second go, as the above method is fired before
  // this can be called in child editors.
  $scope.registerReinitializeEditorCallback = function(callback){
    reinitializeEditorCallback = callback;
  };
  $scope.unregisterReinitializeEditorCallback = function(){
    reinitializeEditorCallback = undefined;
  };

  function initializeEditorVisibilityAndPermission(item) {

    function isCollectiveOwner(ownerUUID) {
      return $scope.collectives && $scope.collectives[ownerUUID];
    }

    function isSharedListOwner(ownerUUID) {
      return $scope.sharedLists && $scope.sharedLists[ownerUUID];
    }

    var ownerUUID = item.trans.owner;
    var userUUID = UserSessionService.getUserUUID();
    var activeUUID = UISessionService.getActiveUUID();

    if (ownerUUID === userUUID && ownerUUID === activeUUID) {
      $scope.fullEditor = true;
      $scope.readOnly = false;
    } else if (isCollectiveOwner(ownerUUID)) {
      $scope.fullEditor = ownerUUID === activeUUID;
      $scope.readOnly = $scope.isCollectiveReadOnly(ownerUUID);
    } else if (isSharedListOwner(ownerUUID)) {
      $scope.fullEditor = false;
      if (item.trans.itemType === 'list') {
        $scope.readOnly = true;
      }else{
        var listUUID;
        if (item.trans.list) {
          listUUID = item.trans.list.trans.uuid;
        }
        $scope.readOnly = listUUID && $scope.isSharedListReadOnly(ownerUUID, listUUID);
      }
    }
  }

  function evaluateExitApp(){
    if (exitAppOnBack){
      exitAppOnBack = false;
      if (navigator && navigator.Backbutton && navigator.Backbutton.goBack){
        navigator.Backbutton.goBack(undefined, function(){
          // Try to go home if there is no going back
          navigator.Backbutton.goHome(undefined, function(){
            // Just exit app, if can't go home either
            if (navigator.app && navigator.app.exitApp){
              navigator.app.exitApp();
            }
          });
        });
      }
    }
  }

  function editorAboutToClose() {
    var featureActionBeforeExitPromise;
    if (typeof featureEditorAboutToCloseCallback === 'function'){
      featureActionBeforeExitPromise = featureEditorAboutToCloseCallback(exitAppOnBack);
    }
    if (featureActionBeforeExitPromise){
      featureActionBeforeExitPromise.then(function(){
        evaluateExitApp();
      });
    }else{
      evaluateExitApp();
    }
  }

  function handleEditorOpened(overrideTitleFocus){
    if (featureEditorOpenedCallback) featureEditorOpenedCallback();
    var autoFocusTitle =
      overrideTitleFocus ||
      $scope.editorType === 'omnibar' ||
      ($scope.editorType === 'note' && (!$scope.note.uuid && !$scope.note.mod));
    if (autoFocusTitle && (!dataInEdit || !dataInEdit.deleted)){
      // Focus on title
      setFocusOnTitlebarInput();
    }
  }

  // Callback from Snap.js, outside of AngularJS event loop
  function editorOpened() {
    // Handle editor opened from here only when there is an animation for editor
    // opening, as there is for below three columns
    if ($scope.columns < 3){
      handleEditorOpened();
      // Broadcast an adjust, that will hopefully fix textarea problems for mobile devices,
      // initializeEditor broadcasts this for larger screens that don't have an animation,
      // and also in situations where there is a change of editor, say from omnibar to note
      $scope.$broadcast('elastic:adjust');
    }
  }
{'fraction_non_alphanumeric': 0.1083453237410072, 'fraction_numerical': 0.002302158273381295, 'mean_word_length': 3.1261501929355893, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6641902', 'n_tokens_mistral': 3813, 'n_tokens_neox': 3772, 'n_words': 1053}