2

I'm trying to implement a custom scroll pane component using an AngularJS directive. in the following jsfiddle example I have an example of the basic prototype.

here is a schema of my idea: enter image description here

Here is the directive code:

    myApp.directive('lpScrollPane', function factory() {
    return {
        restrict: 'A',
        replace: true,
        transclude: true,
        template: '<div class="scrollPaneWrapper"><div class="scrollPane" ng-transclude></div><div class="thumbTrack" ></div></div>',
        compile: function (tElement, tAttrs) {
            var minHeight = 30;
            return function (scope, iElement, iAttrs) {
                var thumbTrack = angular.element(iElement.children()[1]);

                scope.onScrollHeight = function () {
                    console.log(iElement.children()[0].scrollHeight);

                    var H1 = iElement[0].offsetHeight;
                    var H2 = iElement.children()[0].scrollHeight;
                    if (H2 > H1) {
                        var trackHeight = Math.round(minHeight + (H1 - minHeight) * (1 - Math.pow((H2 - H1) / H2, 0.8)));
                        thumbTrack.css({
                            display: "block",
                            height: trackHeight + "px"
                        });
                        console.log(H2, H1, trackHeight);
                    } else {
                        thumbTrack.css({
                            display: "none"
                        });
                    }
                };

                scope.$watch(function () {
                    scope.onScrollHeight();
                    //setTimeout(scope.onScrollHeight, 100)
                });


            }
        }
    };
});

Basically there are 2 dives 1 with overflow hidden and one with overflow scroll and another div to mimic the thumb tracker.

My Goal is to monitor the scrollHeight property and then change the tracker height accordingly. the issue is the $watch gets fired before the DOM is rendered so there is a delay in showing and calculating the tracker. For now I used setTimeout on the watch function and it works fine (un-comment line 35 and comment 34 to see it in action).

What would be the right way to do it?

Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186

2 Answers2

6

See is there a post render callback for Angular JS directive?

Unfortunately, there is no way to determine when rendering is complete (e.g., there is no event). Using $timeout seems to be the best workaround available.

In the link above, @Nik mentioned in a comment that he was checking $('tr').length > 3, for his particular scenario, to determine when rendering was complete. Maybe there is something you could periodically examine in the DOM to determine that rendering is complete.

Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
4

Two observations:

  • you don't need compile imho, but link function
  • you should wait until the element is ready, instead of using timeout

So:

myApp.directive('lpScrollPane', function factory() {
  return {
    restrict: 'A',
    replace: true,
    transclude: true,
    template: '<div class="scrollPaneWrapper"><div class="scrollPane" ng-transclude></div><div class="thumbTrack" ></div></div>',
    link: function (scope, iElement, iAttrs) {
      var minHeight = 30;
      var thumbTrack = angular.element(iElement.children()[1]);

      scope.onScrollHeight = function () {
        console.log(iElement.children()[0].scrollHeight);

        var H1 = iElement[0].offsetHeight;
        var H2 = iElement.children()[0].scrollHeight;
        if (H2 > H1) {
          var trackHeight = Math.round(minHeight + (H1 - minHeight) * (1 - Math.pow((H2 - H1) / H2, 0.8)));
          thumbTrack.css({
            display: "block",
            height: trackHeight + "px"
          });
          console.log(H2, H1, trackHeight);
        } else {
          thumbTrack.css({
            display: "none"
          });
        }
      };

      iElement.ready(function () {  
         scope.$watch(function () {
           scope.onScrollHeight();
         });
      });        
    }
  };
});

See jsFiddle.

Edit:

Because 2 images say more than 1000 words, here are two screenshots: last element still viewable gets inserted first element not viewable gets inserted

asgoth
  • 35,552
  • 12
  • 89
  • 98
  • Thanks for the reply, but as you can see in the updated fiddle (http://jsfiddle.net/neuTA/136/) it does not solve the issue. You can see that the thumbTrack is created in a delay of one $digest step (wait until the content overflows at the bottom) – Shlomi Schwartz Jan 09 '13 at 14:03
  • The blue bar appears from the moment the values overflows. That's not how it should be? – asgoth Jan 09 '13 at 14:08
  • did you look at the updated fiddle? (jsfiddle.net/neuTA/136) because that's not what I see, the blue bar appears on the second line overflow – Shlomi Schwartz Jan 09 '13 at 14:46
  • do you mean the (half) blue bar at the beginning with appears for a half second? – asgoth Jan 09 '13 at 14:52
  • No, this is just a CSS issue (fixed http://jsfiddle.net/neuTA/139/), I see a normal overflow scroll and then when the other line is inserted the blue bar appears (I use chrome) – Shlomi Schwartz Jan 09 '13 at 14:54
  • I've added two screenshots. The first is when the last viewable element gets inserted. The second is when the first non viewable element gets inserted. – asgoth Jan 09 '13 at 18:20
  • Thanks for the effort, but I see it different. I noticed that you use mac, could there be a difference between chrome for Mac and pc? – Shlomi Schwartz Jan 09 '13 at 21:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22482/discussion-between-shlomi-schwartz-and-asgoth) – Shlomi Schwartz Jan 09 '13 at 21:26