PositioningController.js

Summary

No overview generated for 'PositioningController.js'


Class Summary
TKPositioningController  

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

TKPositioningController.inherits = TKController;
TKPositioningController.synthetizes = ['positioningViewData'];

function TKPositioningController (data) {
  // set up the positioning view
  this.positioningView = new TKPositioningView();
  this.positioningView.delegate = this;
  // set up default pointers for arrows
  this._previousButton = null;
  this._nextButton = null;
  //
  this.callSuper(data);
};

TKPositioningController.prototype.processView = function () {
  this.callSuper();
  // add the positioning view
  this._view.appendChild(this.positioningView.element);
  // wire up actions if we have a previous and next button wired
  if (this.previousButton !== null) {
    this._previousButton = this.view.querySelector(this.previousButton);
    if (this._previousButton !== null) {
      this._previousButton.addEventListener('click', this, false);
    }
  }
  if (this.nextButton !== null) {
    this._nextButton = this.view.querySelector(this.nextButton);
    if (this._nextButton !== null) {
      this._nextButton.addEventListener('click', this, false);
    }
  }
  this.addKeyboardElement(this._view);
  this.syncPageButtons();
};

TKPositioningController.prototype.setPositioningViewData = function (data) {
  // set up the data source if we have .elements on the data object
  if (!TKUtils.objectIsUndefined(data.elements)) {
    this.positioningView.dataSource = new TKPositioningViewDataSourceHelper(data.elements);
    // FIXME: huh?
    //delete data.element;
  }
  // copy properties
  TKUtils.copyPropertiesFromSourceToTarget(data, this.positioningView);
  // init our view
  this.positioningView.init();
  this.syncPageButtons();
};

TKPositioningController.prototype.syncPageButtons = function () {
  if (this._previousButton !== null) {
    this._previousButton[(this.positioningView.activeElementIndex <= 0 ? 'add' : 'remove') + 'ClassName']('inactive');
  }
  if (this._nextButton !== null) {
    this._nextButton[(this.positioningView.activeElementIndex >= this.positioningView.numberOfElements - 1 ? 'add' : 'remove') + 'ClassName']('inactive');
  }
};


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

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

TKPositioningController.prototype.wantsCustomHandlingOfKeyboardEvent = function (event) {
  var key = event.keyCode;
  return (
    (key == KEYBOARD_LEFT && this.positioningView.activeElementIndex > 0) ||
    (key == KEYBOARD_RIGHT && this.positioningView.activeElementIndex < this.positioningView.numberOfElements - 1)
  );
};

TKPositioningController.prototype.handleKeydown = function (event) {
  if (!this.wantsCustomHandlingOfKeyboardEvent(event)) {
    this.callSuper(event);
    return;
  }
  switch (event.keyCode) {
    case KEYBOARD_RIGHT:
      this.positioningView.activeElementIndex++;
      break;
    case KEYBOARD_LEFT:
      this.positioningView.activeElementIndex--;
      break;
  }
};

TKPositioningController.prototype.handleEvent = function (event) {
  this.callSuper(event);
  // see if we hit one of the previous / next buttons
  if (event.currentTarget === this._previousButton) {
    this.positioningView.activeElementIndex--;
  }
  else if (event.currentTarget === this._nextButton) {
    this.positioningView.activeElementIndex++;
  }
};

/* ==================== TKPositioningView Protocol ==================== */

TKPositioningController.prototype.positioningViewDidFocusElementAtIndex = function (view, index) {
  // update the states of previous and next buttons
  this.syncPageButtons();
};

TKPositioningController.prototype.positioningViewDidBlurElementAtIndex = function (view, index) {};

TKPositioningController.prototype.positioningViewDidSelectActiveElement = function (view, index) {
  this.elementWasSelected(index);
};

// placeholders to be over-riden by instance
TKPositioningController.prototype.elementWasSelected = function (index) {};

TKClass(TKPositioningController);


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