Files
ionic-framework/js/angular/controller/slideBoxController.js
Andrew 7ef9ad74cf fix(slidebox): refactor for performance and stability
Closes #2336. Closes #2317. Closes #2290. Closes #2228. Closes #2067.
Closes #1890. Closes #1865. Closes #1850. Closes #1755. Closes #1688.
Closes #1578. Closes #1501. Closes #1353. Closes #1342. Closes #782.
Closes #416. Closes #2288.

BREAKING CHANGE: The slideBox's API has undergone many changes.

- **`<ion-slide-box>`** attributes have changed (see
  [documentation](http://ionicframework.com/docs/api/directive/ionSlideBox)):

  * `active-slide` has changed to `selected`. Change your code from
  this:

    ```html
    <ion-slide-box active-slide="activeSlideIndex"></ion-slide-box>
    ```

    To this:

    ```html
    <ion-slide-box selected="activeSlideIndex"></ion-slide-box>
    ```

  * `does-continue` has changed to `loop`.  Change your code from this:

    ```html
    <ion-slide-box does-continue="shouldLoop"></ion-slide-box>
    ```

    To this:

    ```html
    <ion-slide-box loop="shouldLoop"></ion-slide-box>
    ```

  * `auto-play` and `slide-interval` have been merged into `auto-play`.
  Change your code from this:

    ```html
    <!-- autoPlay is on -->
    <ion-slide-box auto-play="true" slide-interval="1000">
    </ion-slide-box>
    <!-- autoPlay is off -->
    <ion-slide-box auto-play="false" slide-interval="1000">
    </ion-slide-box>
    ```

    To this:

    ```html
    <!-- autoPlay is on -->
    <ion-slide-box auto-play="1000"></ion-slide-box>
    <!-- autoPlay is off -->
    <ion-slide-box auto-play="false"></ion-slide-box>
    ```

  * `show-pager` and `pager-click` have been removed. Use
  a child `<ion-slide-pager>` element. See the [`ion-slide-pager`
  documentation](http://ionicframework.com/docs/api/directive/ionSlidePager).
  Change your code from this:

  ```html
  <!-- pager using default click action -->
  <ion-slide-box show-pager="true">
  </ion-slide-box>
  <!-- pager with custom click action -->
  <ion-slide-box show-pager="true" pager-click="doSomething(index)">
  </ion-slide-box>
  ```

  To this:

  ```html
  <ion-slide-box>
    <!-- pager using default click action -->
    <ion-slide-pager></ion-slide-pager>
  </ion-slide-box>
  <ion-slide-box>
    <!-- pager with custom click action -->
    <ion-slide-pager ng-click="doSomething(index)"></ion-slide-pager>
  </ion-slide-box>
  ```

- **`$ionicSlideBoxDelegate`** methods have changed (see
  [documentation](http://ionicframework.com/docs/api/service/$ionicSlideBoxDelegate)):

  - `update()` has been removed. slideBox updates on its own now.

  - `stop()` has been removed. See `autoPlay()` below.

  - `start()` hass been removed. See `autoPlay()` below.

  - `slide(newIndex[, speed])` has been renamed to `select(newIndex[,
    speed]);

  - `currentIndex()` has been renamed to `selected()`.

  - `slidesCount()` has been renamed to `count()`.

  - New method `$ionicSlideBoxDelegate.autoPlay()`. Change your code
    from this:

    ```js
    // stop auto sliding
    $ionicSlideBoxDelegate.stop();
    // later... start auto sliding
    $ionicSlideBoxDelegate.start();
    ```

    To this:

    ```js
    var autoPlaySpeed = 3000; //wait 3000 seconds between changing slide
    // stop auto sliding
    $ionicSlideBoxDelegate.autoPlay(false);
    // later... start auto sliding
    $ionicSlideBoxDelegate.autoPlay(autoPlaySpeed);
    ```

  - `previous()` now returns the index of the previous slide and does
    not select. Change your code from this:

    ```js
    // select previous slide
    $ionicSlideBoxDelegate.previous();
    ```

    To this:

    ```js
    // select previous slide
    $ionicSlideBoxDelegate.select( $ionicSlideBoxDelegate.previous() );
    ```
  - `next()` now returns the index of the next slide and does
    not select. Change your code from this:

    ```js
    // select next slide
    $ionicSlideBoxDelegate.next();
    ```

    To this:

    ```js
    // select next slide
    $ionicSlideBoxDelegate.select( $ionicSlideBoxDelegate.next() );
    ```
2014-10-08 11:09:15 -06:00

264 lines
7.5 KiB
JavaScript

IonicModule
.controller('$ionSlideBox', [
'$scope',
'$element',
'$$ionicAttachDrag',
'$interval',
/*
* This can be abstracted into a controller that will work for views, tabs, and
* slidebox.
*/
function(scope, element, $$ionicAttachDrag, $interval) {
var self = this;
var slideList = ionic.Utils.list([]);
var selectedIndex = -1;
var slidesParent = angular.element(element[0].querySelector('.slider-slides'));
// Successful slide requires velocity to be greater than this amount
var SLIDE_SUCCESS_VELOCITY = (1 / 4); // pixels / ms
var SLIDE_TRANSITION_DURATION = 250; //ms
$$ionicAttachDrag(scope, element, {
getDistance: function() { return slidesParent.prop('offsetWidth'); },
onDrag: onDrag,
onDragEnd: onDragEnd
});
self.element = element;
self.isRelevant = isRelevant;
self.previous = previous;
self.next = next;
// Methods calling straight back to Utils.list
self.at = slideList.at;
self.count = slideList.count;
self.indexOf = slideList.indexOf;
self.isInRange = slideList.isInRange;
self.loop = slideList.loop;
self.delta = slideList.delta;
self.enableSlide = enableSlide;
self.autoPlay = autoPlay;
self.add = add;
self.remove = remove;
self.move = move;
self.selected = selected;
self.select = select;
self.onDrag = onDrag;
self.onDragEnd = onDragEnd;
// ***
// Public Methods
// ***
// Gets whether the given index is relevant to selected
// That is, whether the given index is previous, selected, or next
function isRelevant(index) {
return slideList.isRelevant(index, selectedIndex);
}
// Gets the index to the previous of the given slide, default selectedIndex
function previous(index) {
index = arguments.length ? index : selectedIndex;
// If we only have two slides and loop is enabled, we cannot have a previous
// because previous === next. In this case, return -1.
if (self.loop() && self.count() === 2) {
return -1;
}
return slideList.previous(index);
}
// Gets the index to the next of the given slide, default selectedIndex
function next(index) {
index = arguments.length ? index : selectedIndex;
return slideList.next(index);
}
function enableSlide(isEnabled) {
if (arguments.length) {
self.dragDisabled = !isEnabled;
}
return !!self.dragDisabled;
}
function autoPlay(newInterval) {
$interval.cancel(self.autoPlayTimeout);
if (angular.isNumber(newInterval) && newInterval > 0) {
self.autoPlayTimeout = $interval(function() {
self.select(self.next());
}, newInterval);
}
}
/*
* Add/remove/move slides
*/
function add(slide, index) {
var newIndex = slideList.add(slide, index);
slide.onAdded(slidesParent);
if (selectedIndex === -1) {
self.select(newIndex);
} else if (newIndex === self.previous() || newIndex === self.next()) {
// if the new slide is adjacent to selected, refresh the selection
enqueueRefresh();
}
}
function remove(slide) {
var index = self.indexOf(slide);
if (index === -1) return;
var isSelected = self.selected() === index;
slideList.remove(index);
slide.onRemoved();
if (isSelected) {
self.select( self.isInRange(selectedIndex) ? selectedIndex : selectedIndex - 1 );
}
}
function move(slide, targetIndex) {
var index = self.indexOf(slide);
if (index === -1) return;
// If the slide is current, next, or previous, save so we can re-select after moving.
var isRelevant = self.isRelevant(targetIndex);
slideList.remove(index);
slideList.add(slide, targetIndex);
if (isRelevant) {
enqueueRefresh();
}
}
function selected() {
return selectedIndex;
}
/*
* Select and change slides
*/
function select(newIndex, transitionDuration) {
if (!self.isInRange(newIndex)) return;
var delta = self.delta(selectedIndex, newIndex);
slidesParent.css(
ionic.CSS.TRANSITION_DURATION,
(transitionDuration || SLIDE_TRANSITION_DURATION) + 'ms'
);
selectedIndex = newIndex;
if (self.isInRange(selectedIndex) && Math.abs(delta) > 1) {
// if the new slide is > 1 away, then it is currently not attached to the DOM.
// Attach it in the position from which it will slide in.
self.at(newIndex).setState(delta > 1 ? 'next' : 'previous');
// Wait one frame so the new slide can 'settle' in its new place and
// be ready to properly transition in
ionic.requestAnimationFrame(doSelect);
} else {
doSelect();
}
function doSelect() {
// If a new selection has happened before this frame, abort.
if (selectedIndex !== newIndex) return;
scope.$evalAsync(function() {
if (selectedIndex !== newIndex) return;
arrangeSlides(newIndex);
});
}
}
// percent is negative 0-1 for backward slide
// positive 0-1 for forward slide
function onDrag(percent) {
if (self.dragDisabled) return;
var target = self.at(percent > 0 ? self.next() : self.previous());
var current = self.at(self.selected());
target && target.transform(percent);
current && current.transform(percent);
}
function onDragEnd(percent, velocity) {
var nextIndex = -1;
if (Math.abs(percent) > 0.5 || velocity > SLIDE_SUCCESS_VELOCITY) {
nextIndex = percent > 0 ? self.next() : self.previous();
}
var transitionDuration = Math.min(
slidesParent.prop('offsetWidth') / (3 * velocity),
SLIDE_TRANSITION_DURATION
);
// Select a new slide if it's avaiable
self.select(
self.isInRange(nextIndex) ? nextIndex : self.selected(),
transitionDuration
);
}
// ***
// Private Methods
// ***
var oldSlides;
function arrangeSlides(newShownIndex) {
var newSlides = {
previous: self.at(self.previous(newShownIndex)),
selected: self.at(newShownIndex),
next: self.at(self.next(newShownIndex))
};
newSlides.previous && newSlides.previous.setState('previous');
newSlides.selected && newSlides.selected.setState('selected');
newSlides.next && newSlides.next.setState('next');
if (oldSlides) {
var oldShown = oldSlides.selected;
var delta = self.delta(self.indexOf(oldSlides.selected), self.indexOf(newSlides.selected));
if (Math.abs(delta) > 1) {
// If we're changing by more than one slide, we need to manually transition
// the current slide out and then put it into its new state.
oldShown.setState(delta > 1 ? 'previous' : 'next').then(function() {
oldShown.setState(
newSlides.previous === oldShown ? 'previous' :
newSlides.next === oldShown ? 'next' :
'detached'
);
});
} else {
detachIfUnused(oldSlides.selected);
}
//Additionally, we need to detach both of the old slides.
detachIfUnused(oldSlides.previous);
detachIfUnused(oldSlides.next);
}
function detachIfUnused(oldSlide) {
if (oldSlide && oldSlide !== newSlides.previous &&
oldSlide !== newSlides.selected &&
oldSlide !== newSlides.next) {
oldSlide.setState('detached');
}
}
oldSlides = newSlides;
}
// When adding/moving slides, we sometimes need to refresh
// the currently selected slides to reflect new data.
// We don't want to refresh more than once per digest cycle,
// so we do this.
function enqueueRefresh() {
if (!enqueueRefresh.queued) {
enqueueRefresh.queued = true;
scope.$$postDigest(function() {
self.select(selectedIndex);
enqueueRefresh.queued = false;
});
}
}
}]);