NavigationController.js

Summary

No overview generated for 'NavigationController.js'


Class Summary
TKNavigationController  

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

const TKNavigationControllerWillShowController = 'navigationControllerWillShowController';  
const TKNavigationControllerDidShowController = 'navigationControllerDidShowController';  

TKNavigationController.inherits = TKController;
TKNavigationController.includes = [TKEventTriage];
TKNavigationController.synthetizes = ['topController'];

TKNavigationController.sharedNavigation = null;

function TKNavigationController (data) {
  this.controllers = [];
  this.delegate = null;
  this.rootController = null;
  this.previousController = null;
  //
  this.busy = false;
  //
  this.callSuper(data);
  // see if we have a root set up
  if (this.rootController !== null) {
    this.pushController(this.rootController);
  }
  // register for keyboard events if we're running outside of the iTunes app
  if (SUPPORTS_KEYBOARD_NAVIGATION) {
    window.addEventListener('keydown', this, true);
  }
  //
  TKNavigationController.sharedNavigation = this;
};

/* ==================== Keyboard Handling ==================== */

// routed thanks to TKEventTriage
TKNavigationController.prototype.handleKeydown = function (event) {
  // see if we pressed esc. so we can pop to previous controller
  if (event.keyCode == KEYBOARD_BACKSPACE) {
    this.popController();
    event.preventDefault();
    return;
  }
  // pass that on to our top controller if it cares about it
  var top_controller = this.topController;
  if (TKUtils.objectHasMethod(top_controller, 'handleKeydown')) {
    top_controller.handleKeydown(event);
  }
};

/* ==================== Controllers ==================== */

TKNavigationController.prototype.getTopController = function () {
  return (this.controllers.length > 0) ? this.controllers[this.controllers.length - 1] : null;
};

TKNavigationController.prototype.pushController = function (controller) {
  // do nothing if we're busy
  if (this.busy) {
    return;
  }
  //
  TKTransaction.begin();
  // get pointers to object we'll manipulate
  var previous_controller = this.topController;
  var next_view = controller.view;
  // fire delegate saying we're moving to a new controller
  if (TKUtils.objectHasMethod(this.delegate, TKNavigationControllerWillShowController)) {
    this.delegate[TKNavigationControllerWillShowController](this, controller);
  }
  // put the controller in our array
  this.controllers.push(controller);
  // notify of upcoming change
  if (previous_controller !== null) {
    previous_controller.viewWillDisappear();
  }
  controller.viewWillAppear();
  // add it to the tree
  this.view.appendChild(controller.view);
  //
  if (previous_controller !== null) {
    this.transitionToController(previous_controller, controller);
  }
  else {
    this.busy = true;
    this.transitionDidComplete();
  }
  //
  TKTransaction.commit();
};

TKNavigationController.prototype.popController = function () {
  // do nothing if we're busy or if there's nothing to pop
  if (this.busy || this.controllers.length < 2) {
    return;
  }
  TKTransaction.begin();
  // fire delegate saying we're moving to a new controller
  if (TKUtils.objectHasMethod(this.delegate, TKNavigationControllerWillShowController)) {
    this.delegate[TKNavigationControllerWillShowController](this, this.controllers[this.controllers.length - 2]);
  }
  // update stack
  var previous_controller = this.controllers.pop();
  var top_controller = this.topController;
  // notify of upcoming change
  previous_controller.viewWillDisappear();
  top_controller.viewWillAppear();
  // add it to the tree
  this.view.appendChild(top_controller.view);
  // transition
  this.transitionToController(previous_controller, top_controller);
  //
  TKTransaction.commit();
};

/* ==================== Transition ==================== */

TKNavigationController.prototype.transitionToController = function (previous_controller, top_controller) {
  // mark that a transition is now in progress
  this.busy = true;
  // record some parameters that we will need at the end of the transition
  this.previousController = previous_controller;
  // figure out transitions
  if (previous_controller !== null) {
    previous_controller.view.applyTransition(previous_controller.becomesInactiveTransition, false);
  }
  var top_controller_transition = top_controller.becomesActiveTransition;
  top_controller_transition.delegate = this;
  top_controller.view.applyTransition(top_controller_transition, false);
};

TKNavigationController.prototype.transitionDidComplete = function (transition) {
  if (!this.busy) {
    return;
  }
  // remove the old screen
  if (this.previousController !== null) {
    this.view.removeChild(this.previousController.view);
    this.previousController.viewDidDisappear();
  }
  // notify of completed change
  this.topController.viewDidAppear();
  // fire delegate saying we've moved to a new controller
  if (TKUtils.objectHasMethod(this.delegate, TKNavigationControllerDidShowController)) {
    this.delegate[TKNavigationControllerDidShowController](this, this.topController);
  }
  //
  this.busy = false;
  //
  var controller = this.topController;
  setTimeout(function () {
    TKFocusManager.focusElement(controller.highlightedElement);
  }, 0);
};

TKClass(TKNavigationController);


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