kmc
kmc

Reputation: 658

Sencha DataView scrollTo() not working initially

I'm trying to get a dataview to scroll to specific places. I'm testing this in Chrome. I have a scrollToSelected() method in my dataview which calculates where to scroll to and then calls:

var scroller = this.getScrollable().getScroller();
scroller.scrollTo(0, y, true);

This works when I select list items that trigger my scrollToSelected() method. But at the point of showing the dataview for the first time I select(id), which also triggers scrollToSelected(). The variable y is calculated properly, but the scrolling doesn't happen. So I added a scroller.refresh() to the code before the scrollTo(), and now it works. But ONLY when Chrome's development toolbar is open. If I open the page when the developer toolbar is not there, the scroll never happens. Any ideas?

Edit: Some more code if it helps. Please forgive any glaring architecture faults - Sencha is quite a different coding paradigm for me. That said, suggestions welcome.

Ext.define('NC.controller.Cook', {
  extend: 'Ext.app.Controller',

  config: {
    control: {
      stepsPanel: {
        swipe: 'stepSwipe',     // this will eventually call goToStep
        selectStep: 'goToStep' // if the user directly selects a step (by tap), this will be fired
      },
      cookPage: {
        show: 'setup'
      }
    },
    refs: {
      cookPage: 'cookpage',
      stepsPanel: 'stepspanel',
    }
  },

  launch: function() {
    this.current = 0;
    this.last = 0;
  },

  setup: function() {
    var sp = this.getStepsPanel();    
    sp.select(this.current);
  },

  stepSwipe: function(dataview, event, node, options, eOpts) {
    this.last = this.current;

    if (event.direction == 'up') {
      if (this.current < dataview.getStore().data.length-1) {
        this.current += 1;
      }
    } else if (event.direction == 'down') {
      if (this.current > 0) { this.current -= 1; }
    }

    // Select the current step. It will trigger the dataView's select
    // event which will end up calling goToStep.
    dataview.select(this.current);
  },

  goToStep: function(dataview, record) {
    dataview.scrollToSelected();
  }
});


Ext.define('NC.view.cook.Steps', {
  extend: 'Ext.DataView',
  id: 'steps',
  xtype: 'stepspanel',

  config: {
    flex: 1,
    activeItem: 1,
    itemTpl: '{text}',
    selectedCls: 'current',
    itemCls: 'step',
    scrollable: true,
    zIndex: 10,
    listeners: {
      // this is if the user doesn't swipe but directly selects one of the steps
      select: function(dataview, record, eOpts) { dataview.fireEvent('selectStep', dataview, record); }
    }
  },

  initialize: function() {
    // set scrolling behaviours off without turning scrollable off
    this.getScrollable().getScroller().onDragStart = null;

    this.callParent();
    var that = this;
    this.element.on('swipe', function(event, node, options, eOpts) {
      that.fireEvent('swipe', that, event, node, options, eOpts);
    });
  },

  scrollToSelected: function() {    
    //var toTop = Ext.get(this.element).down(".current").getY();
    var thisEl = Ext.get(this.element);
    var current = thisEl.down(".current");
    var prev = current;
    var y = 0;

    do {
      prev = prev.prev('.step');
      if (prev) { y += prev.getHeight(); }
    }
    while(prev);

    y = y - ((thisEl.getHeight()-current.getHeight())/3) + 5000;

    var scroller = this.getScrollable().getScroller();
    scroller.refresh();
    scroller.scrollTo(0, y, false);
  }
})

Edit 2:

Given the suggestion of "a timing issue," I wrapped the select() in the setup function with a timer, like so:

setTimeout(function() { sp.select(0); }, 10);

Seems to be working now!

Upvotes: 3

Views: 3983

Answers (2)

andy
andy

Reputation: 550

I used the 'maxpositionchanged' event for this, which fires whenever the Lists' scrollbar's height is changed (thus when the list is loaded)

me.getList().getScrollable().getScroller().on('maxpositionchange', function(s, max) {
            if (!initialScrollDone && max.y >= me.ROW_HEIGHT*9) {
                s.scrollTo(0, 120);
                initialScrollDone = true;
            }
        });

ROW_HEIGHT is something I specified myself.

Upvotes: 3

kmc
kmc

Reputation: 658

Given the suggestion of "a timing issue" by rdougan, I wrapped the select() in the setup function with a timer, like so:

setTimeout(function() { sp.select(0); }, 10);

It works now, but I don't know how robust it will be.

Upvotes: -2

Related Questions