<commit_before>'use strict';

function swiperContainerDirective(SwiperService) {

  return {
    restrict: 'A',
    scope: {
      swiperPath: '@swiperContainer',
      swiperType: '@swiperType',
      expectedSlides: '=?expectedSlides'
    },
    controller: function($scope, $element) {
      var swiperSlideInfos = [];
      var initializeSwiperCalled = false;

      this.setExpectedSlides = function(expectedSlides){
        $scope.expectedSlides = expectedSlides;
      };

      // Registers the path of the slide to the swiper
      // and sets up listeners for element, if needed
      this.registerSlide = function(path, element, index) {
        // Slides from DOM (AngularJS directive) are not necessarily registered in desired order.
        // Slide array of objects is sorted later, during swiper initialization.
        if (index || index === 0) {
          swiperSlideInfos.push({slidePath: path, slideIndex: index});
        } else {
          swiperSlideInfos.push(path);
        }
        // For vertical page swiping, we need to the register touch elements
        // to decide whether events should propagate to the underlying horizontal
        // swiper or not.
        if ($scope.swiperType === 'page'){
          // We're expecting an slide, which has "inner-slide-content-container", which has section
          element[0].firstElementChild.firstElementChild.addEventListener('touchstart', slideTouchStart, false);
          element[0].firstElementChild.firstElementChild.addEventListener('touchmove', slideTouchMove, false);
        }

        // if ($scope.swiperType === 'main') {
          // element[0].addEventListener('touchstart', mainSlideTouchStart, false);
          // element[0].addEventListener('touchmove', mainSlideTouchMove, false);
          // element[0].addEventListener('touchend', mainSlideTouchEnd, false);
        // }
        
        // (Re)inializes the swiper after the digest to make sure the whole
        // DOM is ready before this is done. Otherwise Swiper does not register
        // the slides. It is in the registerSlide function to prevent the DOM 
        // from being incomplete before the swiper is created.
        //
        // To get this to work, we have to know the expected slide count. 
        // Using $scope.$evalAsync should have done the right thing, but it is 
        // fired way too early - only the first slides is evaluated before it 
        // is fired. Using $timeout causes flickering and slows down everything.
        // https://groups.google.com/forum/#!topic/angular/SCc45uVhTt9
        // http://stackoverflow.com/a/17303759/2659424
        if (!initializeSwiperCalled){
          if ($scope.expectedSlides === swiperSlideInfos.length){
            sortAndFlattenSlideInfos();

            SwiperService.initializeSwiper(
              $element[0],
              $scope.swiperPath,
              $scope.swiperType,
              swiperSlideInfos,
              onSlideChangeEndCallback);
            initializeSwiperCalled = true;
          }
        }
        function sortAndFlattenSlideInfos() {
          // does array contain slide objects
          if (swiperSlideInfos[0].slidePath) {
            swiperSlideInfos.sort(function(a, b) {
              return a.slideIndex - b.slideIndex;
            });
            // flatten
            for (var i = 0, len = swiperSlideInfos.length; i < len; i++) {
              swiperSlideInfos[i] = swiperSlideInfos[i].slidePath;
            }
          }
        }
      };

      function onSlideChangeEndCallback() {
        SwiperService.onSlideChangeEnd($scope, $scope.swiperPath);
      }

      // Overlapping swipers, should stopPropagation be called?

      var up = false;
      var down = false;
      var startX, startY, distX, distY;

      // var touchStartTime, touchEndTime, touchTime, touchStartPosition, touchEndPosition;

      // function mainSlideTouchStart(event) {
      //   touchStartTime = Date.now();
      //   touchStartPosition = event.changedTouches[0].pageX;
      // }

      // function mainSlideTouchMove(event) {
      //   var currentTouchPosition = event.changedTouches[0].pageX;
      //   var touchDelta = currentTouchPosition - touchStartPosition;
      //   // this.style.webkitTransform = 'translate3d(' + touchDelta + 'px, 0, 0)';
      // }

      // function mainSlideTouchEnd(event) {

      //   touchEndTime = Date.now();
      //   touchEndPosition = event.changedTouches[0].pageX;

      //   touchTime = touchEndTime - touchStartTime;
      //   var touchDistance = touchStartPosition - touchEndPosition;
      //   var velocity = touchDistance / touchTime;
      //   var acceleration = velocity < 0 ? 0.0005 : -0.0005;
      //   var displacement = (velocity * velocity) / (2 * acceleration);
      //   var time = - velocity / acceleration;

      //   this.style.webkitTransition = '-webkit-transform ' + time + 'ms cubic-bezier(0.33, 0.66, 0.66, 1)';
      //   this.style.webkitTransform = 'translate3d(' + displacement + 'px, 0, 0)';
      // }

      function slideTouchStart() {
        var touchobj = event.changedTouches[0];
        startX = touchobj.pageX;
        startY = touchobj.pageY;
      }

      function slideTouchMove() {
        /*jshint validthis: true */
        var touchobj = event.changedTouches[0];
        distX = touchobj.pageX - startX;
        distY = touchobj.pageY - startY;

        // http://www.javascriptkit.com/javatutors/touchevents2.shtml
        if (Math.abs(distX) > Math.abs(distY)) { // horizontal
          return;
        } else if (Math.abs(distX) < Math.abs(distY)) { // vertical
          if (distY < 0) {
            down = true;
            up = false;
          } else {
            down = false;
            up = true;
          }

          // https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollHeight#Determine_if_an_element_has_been_totally_scrolled
          if (((this.scrollHeight - this.scrollTop) <= this.clientHeight) && down) {
            // bottom
          } else if ((this.scrollTop <= 0) && up) {
            // top
          } else {
            event.stopPropagation();
          }
        }
      }
    }
  };
}
angular.module('em.directives').directive('swiperContainer', swiperContainerDirective);
swiperContainerDirective.$inject = ['SwiperService'];
<commit_msg>Re-init swiperSlideInfos array for new slide set.<commit_after>'use strict';

function swiperContainerDirective(SwiperService) {

  return {
    restrict: 'A',
    scope: {
      swiperPath: '@swiperContainer',
      swiperType: '@swiperType',
      expectedSlides: '=?expectedSlides'
    },
    controller: function($scope, $element) {
      var swiperSlideInfos = [];
      var initializeSwiperCalled = false;

      this.setExpectedSlides = function(expectedSlides){
        $scope.expectedSlides = expectedSlides;
      };

      // Registers the path of the slide to the swiper
      // and sets up listeners for element, if needed
      this.registerSlide = function(path, element, index) {
        // Slides from DOM (AngularJS directive) are not necessarily registered in desired order.
        // Slide array of objects is sorted later, during swiper initialization.
        if (index || index === 0) {
          swiperSlideInfos.push({slidePath: path, slideIndex: index});
        } else {
          // Re-init slide info array for new slide set, eg. date slides.
          if (swiperSlideInfos.length >= $scope.expectedSlides) {
            swiperSlideInfos.length = 0;
          }
          swiperSlideInfos.push(path);
        }
        // For vertical page swiping, we need to the register touch elements
        // to decide whether events should propagate to the underlying horizontal
        // swiper or not.
        if ($scope.swiperType === 'page'){
          // We're expecting an slide, which has "inner-slide-content-container", which has section
          element[0].firstElementChild.firstElementChild.addEventListener('touchstart', slideTouchStart, false);
          element[0].firstElementChild.firstElementChild.addEventListener('touchmove', slideTouchMove, false);
        }

        // if ($scope.swiperType === 'main') {
          // element[0].addEventListener('touchstart', mainSlideTouchStart, false);
          // element[0].addEventListener('touchmove', mainSlideTouchMove, false);
          // element[0].addEventListener('touchend', mainSlideTouchEnd, false);
        // }
        
        // (Re)inializes the swiper after the digest to make sure the whole
        // DOM is ready before this is done. Otherwise Swiper does not register
        // the slides. It is in the registerSlide function to prevent the DOM 
        // from being incomplete before the swiper is created.
        //
        // To get this to work, we have to know the expected slide count. 
        // Using $scope.$evalAsync should have done the right thing, but it is 
        // fired way too early - only the first slides is evaluated before it 
        // is fired. Using $timeout causes flickering and slows down everything.
        // https://groups.google.com/forum/#!topic/angular/SCc45uVhTt9
        // http://stackoverflow.com/a/17303759/2659424
        if (!initializeSwiperCalled){
          if ($scope.expectedSlides === swiperSlideInfos.length){
            sortAndFlattenSlideInfos();

            SwiperService.initializeSwiper(
              $element[0],
              $scope.swiperPath,
              $scope.swiperType,
              swiperSlideInfos,
              onSlideChangeEndCallback);
            initializeSwiperCalled = true;
          }
        } else {
          if ($scope.expectedSlides === swiperSlideInfos.length){
            SwiperService.reInit($scope.swiperPath);
          }
        }
        function sortAndFlattenSlideInfos() {
          // does array contain slide objects
          if (swiperSlideInfos[0].slidePath) {
            swiperSlideInfos.sort(function(a, b) {
              return a.slideIndex - b.slideIndex;
            });
            // flatten
            for (var i = 0, len = swiperSlideInfos.length; i < len; i++) {
              swiperSlideInfos[i] = swiperSlideInfos[i].slidePath;
            }
          }
        }
      };

      function onSlideChangeEndCallback() {
        SwiperService.onSlideChangeEnd($scope, $scope.swiperPath);
      }

      // Overlapping swipers, should stopPropagation be called?

      var up = false;
      var down = false;
      var startX, startY, distX, distY;

      // var touchStartTime, touchEndTime, touchTime, touchStartPosition, touchEndPosition;

      // function mainSlideTouchStart(event) {
      //   touchStartTime = Date.now();
      //   touchStartPosition = event.changedTouches[0].pageX;
      // }

      // function mainSlideTouchMove(event) {
      //   var currentTouchPosition = event.changedTouches[0].pageX;
      //   var touchDelta = currentTouchPosition - touchStartPosition;
      //   // this.style.webkitTransform = 'translate3d(' + touchDelta + 'px, 0, 0)';
      // }

      // function mainSlideTouchEnd(event) {

      //   touchEndTime = Date.now();
      //   touchEndPosition = event.changedTouches[0].pageX;

      //   touchTime = touchEndTime - touchStartTime;
      //   var touchDistance = touchStartPosition - touchEndPosition;
      //   var velocity = touchDistance / touchTime;
      //   var acceleration = velocity < 0 ? 0.0005 : -0.0005;
      //   var displacement = (velocity * velocity) / (2 * acceleration);
      //   var time = - velocity / acceleration;

      //   this.style.webkitTransition = '-webkit-transform ' + time + 'ms cubic-bezier(0.33, 0.66, 0.66, 1)';
      //   this.style.webkitTransform = 'translate3d(' + displacement + 'px, 0, 0)';
      // }

      function slideTouchStart() {
        var touchobj = event.changedTouches[0];
        startX = touchobj.pageX;
        startY = touchobj.pageY;
      }

      function slideTouchMove() {
        /*jshint validthis: true */
        var touchobj = event.changedTouches[0];
        distX = touchobj.pageX - startX;
        distY = touchobj.pageY - startY;

        // http://www.javascriptkit.com/javatutors/touchevents2.shtml
        if (Math.abs(distX) > Math.abs(distY)) { // horizontal
          return;
        } else if (Math.abs(distX) < Math.abs(distY)) { // vertical
          if (distY < 0) {
            down = true;
            up = false;
          } else {
            down = false;
            up = true;
          }

          // https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollHeight#Determine_if_an_element_has_been_totally_scrolled
          if (((this.scrollHeight - this.scrollTop) <= this.clientHeight) && down) {
            // bottom
          } else if ((this.scrollTop <= 0) && up) {
            // top
          } else {
            event.stopPropagation();
          }
        }
      }
    }
  };
}
angular.module('em.directives').directive('swiperContainer', swiperContainerDirective);
swiperContainerDirective.$inject = ['SwiperService'];

{'fraction_non_alphanumeric': 0.11144278606965174, 'fraction_numerical': 0.009720627631075392, 'mean_word_length': 2.137094837935174, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 6, 'https://': 4, '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': '2655417', 'n_tokens_mistral': 3791, 'n_tokens_neox': 3635, 'n_words': 877}