PageSliderController.js

Summary

No overview generated for 'PageSliderController.js'


Class Summary
TKPageSliderController  

/**
 *  Copyright © 2009 Apple Inc.  All rights reserved.
 *
 *  @class
 **/

const TKPageSliderControllerContainerCSSClass = 'tk-page-slider-controller-view';

TKPageSliderController.inherits = TKController;
TKPageSliderController.synthetizes = ['slidingViewData', 'pageControlData', 'sliderHasHighlight'];

function TKPageSliderController (data) {
  // public properties
  this.previousPageButton = null;
  this.nextPageButton = null;
  // set up the sliding view
  this.slidingView = new TKSlidingView();
  this.slidingView.delegate = this;
  // set up the page control
  this.pageControl = new TKPageControl();
  this.pageControl.delegate = this;
  // set up default pointers for arrows
  this._previousPageButton = null;
  this._nextPageButton = null;
  //
  this.callSuper(data);
};

TKPageSliderController.prototype.processView = function () {
  this.callSuper();
  // wire up actions if we have a previous and next button wired
  if (this.previousPageButton !== null) {
    this._previousPageButton = this.view.querySelector(this.previousPageButton);
    if (this._previousPageButton !== null) {
      this._previousPageButton.addEventListener('click', this, false);
    }
  }
  if (this.nextPageButton !== null) {
    this._nextPageButton = this.view.querySelector(this.nextPageButton);
    if (this._nextPageButton !== null) {
      this._nextPageButton.addEventListener('click', this, false);
    }
  }
  // add the sliding view and page control containers
  this.container = this._view.appendChild(document.createElement('div'));
  this.container.addClassName(TKPageSliderControllerContainerCSSClass);
  this.container.appendChild(this.slidingView.element);
  this.container.appendChild(this.pageControl.element);
  this.container.addEventListener('highlight', this, false);
  this.highlightedElement = this.container;
  this.addKeyboardElement(this.container);
  // ensure our first page gets told about its being highlighted
  this.pageWasHighlighted(0);
  this.syncPageButtons();
};

TKPageSliderController.prototype.setSlidingViewData = function (data) {
  // set up the data source if we have .elements on the data object
  if (!TKUtils.objectIsUndefined(data.elements)) {
    this.slidingView.dataSource = new TKSlidingViewDataSourceHelper(data.elements);
    delete data.element;
  }
  // copy properties
  TKUtils.copyPropertiesFromSourceToTarget(data, this.slidingView);
  // init our view
  this.slidingView.init();
  //
  this.syncPageButtons();
};

TKPageSliderController.prototype.setPageControlData = function (data) {
  // set up the data source
  this.pageControl.dataSource = new TKPageControlDataSourceHelper(data);
  // copy properties
  TKUtils.copyPropertiesFromSourceToTarget(data, this.pageControl);
  // init our control
  this.pageControl.init();
};

/* ==================== Event Handling ==================== */

TKPageSliderController.prototype.getSliderHasHighlight = function () {
  return (this.highlightedElement === this.container);
};

TKPageSliderController.prototype.metricsForElement = function (element) {
  if (element !== this.container) {
    return this.callSuper(element);
  }
  var view_metrics = this._view.getBounds();
  var union = TKRect.rectFromUnionOfRects([
    this.slidingView.element.getBounds(),
    this.pageControl.element.getBounds()
  ]);
  union.width = Math.min(union.width, view_metrics.width);
  union.height = Math.min(union.width, view_metrics.height);
  return union;
};

TKPageSliderController.prototype.wantsToHandleKeyboardEvent = function (event) {
  return (this.wantsCustomHandlingOfKeyboardEvent(event) || this.callSuper(event));
};

TKPageSliderController.prototype.wantsCustomHandlingOfKeyboardEvent = function (event) {
  var key = event.keyCode;
  return (
    (key == KEYBOARD_LEFT && (this.slidingView.activeElementIndex > 0 || this.slidingView.loops)) ||
    (key == KEYBOARD_RIGHT && (this.slidingView.activeElementIndex < this.slidingView.numberOfElements || this.slidingView.loops)) ||
    (key == KEYBOARD_RETURN)
  );
};

TKPageSliderController.prototype.handleKeydown = function (event) {
  if (!this.wantsCustomHandlingOfKeyboardEvent(event) || !this.sliderHasHighlight) {
    this.callSuper(event);
    return;
  }
  switch (event.keyCode) {
    case KEYBOARD_RIGHT:
      this.slidingView.activeElementIndex++;
      break;
    case KEYBOARD_LEFT:
      this.slidingView.activeElementIndex--;
      break;
    case KEYBOARD_RETURN:
      this.pageWasSelected(this.slidingView.activeElementIndex);
      break;
  }
};

TKPageSliderController.prototype.handleEvent = function (event) {
  this.callSuper(event);
  if (event.currentTarget === this._previousPageButton) {
    this.slidingView.activeElementIndex--;
  }
  else if (event.currentTarget === this._nextPageButton) {
    this.slidingView.activeElementIndex++;
  }
};

TKPageSliderController.prototype.syncPageButtons = function () {
  // nothing to do if the sliding view is looping
  if (this.slidingView.loops) {
    return;
  }
  // check if the previous page button needs hiding
  if (this._previousPageButton !== null) {
    this._previousPageButton[(this.slidingView.activeElementIndex <= 0 ? 'add' : 'remove') + 'ClassName']('inactive');
  }
  // check if the next page button needs hiding
  if (this._nextPageButton !== null) {
    this._nextPageButton[(this.slidingView.activeElementIndex >= this.slidingView.numberOfElements - 1 ? 'add' : 'remove') + 'ClassName']('inactive');
  }
};

/* ==================== TKSlidingView Protocol ==================== */

TKPageSliderController.prototype.slidingViewDidFocusElementAtIndex = function (view, index) {
  this.pageControl.currentPage = index;
  this.pageWasHighlighted(index);
  // update the states of previous and next buttons
  this.syncPageButtons();
};

TKPageSliderController.prototype.slidingViewDidBlurElementAtIndex = function (view, index) {};

TKPageSliderController.prototype.slidingViewDidSelectActiveElement = function (view, index) {
  this.pageWasSelected(index);
};

TKPageSliderController.prototype.slidingViewStyleForItemAtIndex = function (view, index) {
  return this.styleForPageAtIndex(index);
};

TKPageSliderController.prototype.slidingViewDidHoverElementAtIndex = function (view, index) {
  this.pageWasHovered(index);
};

TKPageSliderController.prototype.slidingViewDidUnhoverElementAtIndex = function (view, index) {
  this.pageWasUnhovered(index);
};

// placeholders to be over-riden by instance
TKPageSliderController.prototype.pageWasHighlighted = function (index) {};
TKPageSliderController.prototype.pageWasSelected = function (index) {};
TKPageSliderController.prototype.pageWasHovered = function (index) {};
TKPageSliderController.prototype.pageWasUnhovered = function (index) {};
TKPageSliderController.prototype.styleForPageAtIndex = function (index) {
  return [];
};

/* ==================== TKPageControl Protocol ==================== */

TKPageSliderController.prototype.pageControlDidUpdateCurrentPage = function (control, newPageIndex) {
  this.slidingView.activeElementIndex = newPageIndex;
  this.pageControl.updateCurrentPageDisplay();
};

TKClass(TKPageSliderController);


Documentation generated by JSDoc on Tue Sep 15 21:24:36 2009