TabController.js

Summary

No overview generated for 'TabController.js'


Class Summary
TKTabController  

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


const TKTabControllerWillShowController = 'tabControllerWillShowController';  
const TKTabControllerDidShowController = 'tabControllerDidShowController';  

TKTabController.inherits = TKController;
TKTabController.synthetizes = ['selectedController', 'selectedIndex'];

function TKTabController (data) {
  this.tabs = null;
  this._selectedController = null;
  this.controllers = [];
  this.delegate = null;
  this.tabNavigator = null;
  this.previousController = null;
  //
  this.busy = false;
  //
  this.callSuper(data);
};

/* ==================== Additional View Processing ==================== */

TKTabController.prototype.loadView = function () {
  this.callSuper();
  //
  this.host = this._view.appendChild(document.createElement('div'));
};

TKTabController.prototype.processView = function () {
  this.callSuper();
  // get tabs
  var tabs = this._view.querySelectorAll(this.tabsSelector);
  // nothing to do if we don't have tabs
  if (tabs.length < 1) {
    return;
  }
  //
  var container = tabs[0].parentNode;
  container.id = (container.id == '') ? (this.id + '-tabs-navigator') : container.id;
  this.tabNavigator = new TKTabNavigationController({
    view : container,
    tabs : tabs
  });
  this.tabNavigator.delegate = this;
  this.tabNavigator.parentController = this;
  this.addKeyboardElement(this.tabNavigator.view);
  // track highlight on the tabs container
  this.tabNavigator.view.addEventListener('highlight', this, false);
  // highlight the first tab
  this.highlightElement(this.tabNavigator.view);
  // track our navigator as our highlight view
  this.metricsView = this.tabNavigator.view;
};

/* ==================== Keyboard Navigation ==================== */

TKTabController.prototype.handleKeydown = function (event) {
  var selected_controller = this.selectedController;
  // pass the event down to the selected controller if we did not know what to do with the event
  if (this.tabNavigator.view.hasClassName(TKControllerHighlightCSSClass) && selected_controller.wantsToHandleKeyboardEvent(event)) {
    selected_controller.handleKeydown(event);
  }
  // otherwise just do the usual
  else {
    this.callSuper(event);
  }
};

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

TKTabController.prototype.getSelectedIndex = function () {
  return (this._selectedController === null) ? -1 : this.controllers.indexOf(this._selectedController);
};

TKTabController.prototype.setSelectedIndex = function (index) {
  if (index >= 0 && index < this.controllers.length) {
    this.selectedController = this.controllers[index];
  }
};

TKTabController.prototype.setSelectedController = function (controller) {
  // do nothing if we're busy, or if we don't know about such a controller, or we're already on this controller
  if (this.busy || controller === this._selectedController || this.controllers.indexOf(controller) == -1) {
    return;
  }
  //
  TKTransaction.begin();
  // get pointers to object we'll manipulate
  var previous_controller = this._selectedController;
  var next_view = controller.view;
  // fire delegate saying we're moving to a new controller
  if (TKUtils.objectHasMethod(this.delegate, TKTabControllerWillShowController)) {
    this.delegate[TKTabControllerWillShowController](this, controller);
  }
  // track this is our newly selected controller
  this._selectedController = controller;
  // notify of upcoming change and update tabs styling
  if (previous_controller !== null) {
    previous_controller.viewWillDisappear();
  }
  controller.viewWillAppear();
  // add it to the tree
  this.host.appendChild(controller.view);
  // transition
  if (previous_controller !== null) {
    this.transitionToController(previous_controller, controller);
  }
  else {
    this.transitionDidComplete();
  }
  //
  TKTransaction.commit();
};

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

TKTabController.prototype.transitionToController = function (previous_controller, top_controller) {
  // mark that a transition is now in progress
  this.busy = true;
  TKController.busyControllers++;
  // 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);
};

TKTabController.prototype.transitionDidComplete = function (transition) {
  if (!this.busy) {
    return;
  }
  // update the highlightable items and notify of completed change
  if (this.previousController !== null) {
    this.host.removeChild(this.previousController.view);
    this.removeKeyboardElement(this.previousController.view);
    this.previousController.parentController = null;
    this.previousController.viewDidDisappear();
  }
  this.addKeyboardElement(this._selectedController.view);
  this._selectedController.parentController = this;
  this._selectedController.viewDidAppear();
  // fire delegate saying we've moved to a new controller
  if (TKUtils.objectHasMethod(this.delegate, TKTabControllerDidShowController)) {
    this.delegate[TKTabControllerDidShowController](this, this._selectedController);
  }
  //
  this.busy = false;
  TKController.busyControllers--;
};

/* ==================== TKTabNavigationController Delegation ==================== */

TKTabController.prototype.tabNavigationControllerDidSelectTab = function (tabNavigationController, tabIndex) {
  this.selectedIndex = tabIndex;
};

TKClass(TKTabController);


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