sjwilliams / scrollstory

A jQuery plugin for building scroll-based stories
http://sjwilliams.github.io/scrollstory/
MIT License
282 stars 31 forks source link

.next() and .previous() should account for filtered items #32

Closed sjwilliams closed 6 years ago

sjwilliams commented 6 years ago

Should we have .next() and .previous() skip an item if it's filtered?

Currently they blindly this.index(this.index() + 1);

If so, something like this could work:

    /**
     * Convenience method to navigate to next item
     *
     * @param  {Number} _index -- an optional index. Used to recursively find unflitered item 
     */
    next: function(_index) {
      var currentIndex = _index || this.index();
      var nextItem;

      if (typeof currentIndex === 'number') {
        nextItem = this.getItemByIndex(currentIndex + 1);

        // valid index and item
        if (nextItem) {

          // proceed if not filtered. if filtered try the one after that.
          if (!nextItem.filtered) {
            this.index(currentIndex + 1);
          } else {
            this.next(currentIndex + 1);
          }
        }
      }
    },

    /**
     * Convenience method to navigate to previous item
     *
     * @param  {Number} _index -- an optional index. Used to recursively find unflitered item 
     */
    previous: function(_index) {
      var currentIndex = _index || this.index();
      var previousItem;

      if (typeof currentIndex === 'number') {
        previousItem = this.getItemByIndex(currentIndex - 1);

        // valid index and item
        if (previousItem) {

          // proceed if not filtered. if filtered try the one before that.
          if (!previousItem.filtered) {
            this.index(currentIndex - 1);            
          } else {
            this.previous(currentIndex - 1);
          }
        }
      }
    }
sjwilliams commented 6 years ago

closed by https://github.com/sjwilliams/scrollstory/commit/dfe6ec726cdcdcea233ee56adeb103160ca1a9e7