'use strict';

angular.module('sgApp')
  .directive('sgSection', ['$rootScope', '$window', '$timeout', function($rootScope, $window, $timeout) {
    return {
      replace: true,
      restrict: 'A',
      templateUrl: 'views/partials/section.html',
      link: function(scope, element, attrs) {
        function updateCurrentReference() {
          var topOffset = element[0].offsetTop,
            bottomOffset = element[0].offsetTop + element[0].offsetHeight,
            buffer = 100;

          if (this.pageYOffset > topOffset - buffer && this.pageYOffset < bottomOffset - buffer) {
            if ($rootScope.currentReference.section.reference !== scope.section.reference) {
              $rootScope.currentReference.section = scope.section;
              if (!scope.$$phase) {
                $rootScope.$apply();
              }
            }
          }
        }

        // Init markup visibility based on global setting
        scope.section.showMarkup = scope.markupSection.isVisible;
        // By default do not show CSS markup
        scope.section.showCSS = false;

        // Listen to scroll events and update currentReference if this section is currently focused
        angular.element($window).bind('scroll', function() {
          updateCurrentReference();
        });

        scope.$watch('search.$', function(newVal, oldVal) {
          // Search is not processed completely yet
          // We want to run updateCurrentReference after digest is complete
          $timeout(function() {
            updateCurrentReference();
          });
        });

        // Section location will change still after initialzation
        // We want to run updateCurrentReference after digest is complete
        $timeout(function() {
          updateCurrentReference();
        });
      }
    };
  }]);

@@ -671,24 +671,278 @@
 eference) %7B%0A
+              // Add focus to current section%0A              scope.section.focused = true;%0A              // Remove focus from previous section%0A              $rootScope.currentReference.section.focused = false;%0A%0A              // Assign new current section%0A
             

Mark current section with flag