<commit_before>/* Copyright 2013-2014 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.
 */
 'use strict';

 function rootViewDirective($injector, $rootScope, $templateCache, $window,
                            AnalyticsService, BackendClientService, SynchronizeService, UISessionService,
                            UUIDService, UserSessionService, packaging) {

  return {
    restrict: 'A',
    replace: 'true',
    templateUrl: $rootScope.urlBase + 'app/root/root.html',
    controller: function($scope, $attrs) {

      // BASIC DIMENSIONS TO ROOT SCOPE
      // NOTE: For some reason this had to be 567 before,
      // because editor disappeared after animation was ready.
      $rootScope.CONTAINER_MASTER_MAX_WIDTH = 568;
      $rootScope.CONTAINER_MASTER_MAX_HEIGHT = 769;
      $rootScope.MAX_HEIGHT = 769;
      $rootScope.MENU_WIDTH = 260;
      $rootScope.EDITOR_FOOTER_HEIGHT = 44;
      $rootScope.TOOLBAR_BUTTON_WIDTH = 80;
      $rootScope.TOOLBAR_HEADING_MAX_WIDTH = 350;
      $rootScope.TOOLBAR_HEIGHT = 66;
      $rootScope.LIST_FOOTER_HEIGHT = 44;
      $rootScope.LOADING_ANIMATION_HEIGHT = 19;

      // ANIMATION
      $rootScope.KEYBOARD_ANIMATION_TIME = 300;
      // NOTE: These need to be in sync with their counterparts in LESS and/or JS.
      $rootScope.MENU_ANIMATION_SPEED = 200;
      $rootScope.EDITOR_ANIMATION_SPEED = 350;
      $rootScope.EDITOR_CLOSED_FAILSAFE_TIME = 2*$rootScope.EDITOR_ANIMATION_SPEED;
      $rootScope.CHECKBOX_CHECKING_ANIMATION_TIME = 3000;
      $rootScope.LIST_ITEM_LEAVE_ANIMATION_SPEED = 1000;

      // Sticky inputs
      var useStickyInputs = false;
      if (packaging.endsWith('cordova')){
        useStickyInputs = true;
      }

      $scope.getEditableFieldSticky = function(){
        if (useStickyInputs) return 'sticky';
      };

      // Online/offline status, optimistic default
      $scope.online = true;
      var onlineStatusCallback = function(online) {
        $scope.online = online;
      };
      BackendClientService.registerOnlineStatusCallback(onlineStatusCallback);

      var exiting = false;
      $rootScope.redirectToEntry = function() {
        exiting = true;
        UISessionService.reset();
        SynchronizeService.clearData();
        UserSessionService.clearUser();
        $rootScope.synced = $rootScope.syncState = undefined;
        // $location can not be injected directly presumably because this directive
        // is defined above ng-view
        var $location = $injector.get('$location');
        $location.url('/');

        // NOTE:  Without this swiper breaks after logout/login, because
        //        window.getComputedStyle(el, null).getPropertyValue('width');
        //        in swiper.js starts returning roughly half the real value. This turned out
        //        to be because of AngularJS template caching: the first ng-include workds, but
        //        not the second.

        // Tried also this in swiperContainer after initialization:
        //
        //      if ($rootScope.horizontalSwipersNeedResize){
        //        $element[0].style.visibility = 'hidden';
        //        setTimeout(function(){
        //          SwiperService.resizeFixSwiper($scope.swiperPath);
        //          $element[0].style.visibility = 'visible';
        //        });
        //      }
        //
        // which almost worked, but on menu open, failed. In app that always broke.
        $templateCache.removeAll();
      };

      // EVENTS - user interactions, exceptions

      // Listen to interactions emitted to $rootScope.
      var unbindEmInteraction = $rootScope.$on('emInteraction', function(name, interaction) {
        if (interaction.type === 'onlineRequired') {
          var params = {
            messageHeading: 'no online connection',
            messageText: 'please connect to the internet and press retry to access your information',
            confirmText: 'retry',
            confirmTextDeferred: 'retrying\u2026',
            confirmActionDeferredFn: interaction.value.retry,
            confirmActionDeferredParam: interaction.value.retryParam,
            confirmActionPromiseFn: interaction.value.promise,
            confirmActionPromiseParam: interaction.value.promiseParam
          };

          $scope.showModal(undefined, params);
        }
      });

      // Listen to exceptions emitted to $rootScope.
      var unbindEmException = $rootScope.$on('emException', function(name, exception) {

        if (exception.type === 'http' && exception.value.status === 403) {
          // Redirect thrown 403 Forbidden exception to the login page
          AnalyticsService.error('forbidden', JSON.stringify(exception));
          $rootScope.redirectToEntry();
        } else if (exception.type === 'session') {
          if (!exiting) {
            // Redirect session errors to the login page
            AnalyticsService.error('session', exception.value);
            $rootScope.redirectToEntry();
          }
        // TODO: Type 'response' for offline responses!
      } else if (exception.type === 'redirectToEntry') {
        $rootScope.redirectToEntry();<commit_msg>Shorter text for online that won't use two lines<commit_after>/* Copyright 2013-2014 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.
 */
 'use strict';

 function rootViewDirective($injector, $rootScope, $templateCache, $window,
                            AnalyticsService, BackendClientService, SynchronizeService, UISessionService,
                            UUIDService, UserSessionService, packaging) {

  return {
    restrict: 'A',
    replace: 'true',
    templateUrl: $rootScope.urlBase + 'app/root/root.html',
    controller: function($scope, $attrs) {

      // BASIC DIMENSIONS TO ROOT SCOPE
      // NOTE: For some reason this had to be 567 before,
      // because editor disappeared after animation was ready.
      $rootScope.CONTAINER_MASTER_MAX_WIDTH = 568;
      $rootScope.CONTAINER_MASTER_MAX_HEIGHT = 769;
      $rootScope.MAX_HEIGHT = 769;
      $rootScope.MENU_WIDTH = 260;
      $rootScope.EDITOR_FOOTER_HEIGHT = 44;
      $rootScope.TOOLBAR_BUTTON_WIDTH = 80;
      $rootScope.TOOLBAR_HEADING_MAX_WIDTH = 350;
      $rootScope.TOOLBAR_HEIGHT = 66;
      $rootScope.LIST_FOOTER_HEIGHT = 44;
      $rootScope.LOADING_ANIMATION_HEIGHT = 19;

      // ANIMATION
      $rootScope.KEYBOARD_ANIMATION_TIME = 300;
      // NOTE: These need to be in sync with their counterparts in LESS and/or JS.
      $rootScope.MENU_ANIMATION_SPEED = 200;
      $rootScope.EDITOR_ANIMATION_SPEED = 350;
      $rootScope.EDITOR_CLOSED_FAILSAFE_TIME = 2*$rootScope.EDITOR_ANIMATION_SPEED;
      $rootScope.CHECKBOX_CHECKING_ANIMATION_TIME = 3000;
      $rootScope.LIST_ITEM_LEAVE_ANIMATION_SPEED = 1000;

      // Sticky inputs
      var useStickyInputs = false;
      if (packaging.endsWith('cordova')){
        useStickyInputs = true;
      }

      $scope.getEditableFieldSticky = function(){
        if (useStickyInputs) return 'sticky';
      };

      // Online/offline status, optimistic default
      $scope.online = true;
      var onlineStatusCallback = function(online) {
        $scope.online = online;
      };
      BackendClientService.registerOnlineStatusCallback(onlineStatusCallback);

      var exiting = false;
      $rootScope.redirectToEntry = function() {
        exiting = true;
        UISessionService.reset();
        SynchronizeService.clearData();
        UserSessionService.clearUser();
        $rootScope.synced = $rootScope.syncState = undefined;
        // $location can not be injected directly presumably because this directive
        // is defined above ng-view
        var $location = $injector.get('$location');
        $location.url('/');

        // NOTE:  Without this swiper breaks after logout/login, because
        //        window.getComputedStyle(el, null).getPropertyValue('width');
        //        in swiper.js starts returning roughly half the real value. This turned out
        //        to be because of AngularJS template caching: the first ng-include workds, but
        //        not the second.

        // Tried also this in swiperContainer after initialization:
        //
        //      if ($rootScope.horizontalSwipersNeedResize){
        //        $element[0].style.visibility = 'hidden';
        //        setTimeout(function(){
        //          SwiperService.resizeFixSwiper($scope.swiperPath);
        //          $element[0].style.visibility = 'visible';
        //        });
        //      }
        //
        // which almost worked, but on menu open, failed. In app that always broke.
        $templateCache.removeAll();
      };

      // EVENTS - user interactions, exceptions

      // Listen to interactions emitted to $rootScope.
      var unbindEmInteraction = $rootScope.$on('emInteraction', function(name, interaction) {
        if (interaction.type === 'onlineRequired') {
          var params = {
            messageHeading: 'not online',
            messageText: 'please connect to the internet and press retry to access your information',
            confirmText: 'retry',
            confirmTextDeferred: 'retrying\u2026',
            confirmActionDeferredFn: interaction.value.retry,
            confirmActionDeferredParam: interaction.value.retryParam,
            confirmActionPromiseFn: interaction.value.promise,
            confirmActionPromiseParam: interaction.value.promiseParam
          };

          $scope.showModal(undefined, params);
        }
      });

      // Listen to exceptions emitted to $rootScope.
      var unbindEmException = $rootScope.$on('emException', function(name, exception) {

        if (exception.type === 'http' && exception.value.status === 403) {
          // Redirect thrown 403 Forbidden exception to the login page
          AnalyticsService.error('forbidden', JSON.stringify(exception));
          $rootScope.redirectToEntry();
        } else if (exception.type === 'session') {
          if (!exiting) {
            // Redirect session errors to the login page
            AnalyticsService.error('session', exception.value);
            $rootScope.redirectToEntry();
          }
        // TODO: Type 'response' for offline responses!
      } else if (exception.type === 'redirectToEntry') {
        $rootScope.redirectToEntry();
{'fraction_non_alphanumeric': 0.09683083135808972, 'fraction_numerical': 0.01229040470546923, 'mean_word_length': 2.5291201982651796, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4769903', 'n_tokens_mistral': 3226, 'n_tokens_neox': 3040, 'n_words': 824}