1 /*
  2  *  Copyright © 2009 Apple Inc. All rights reserved.
  3  */
  4 
  5 TKComicController.inherits = TKController;
  6 TKComicController.synthetizes = ['comicViewData'];
  7 
  8 function TKComicController (data) {
  9   // set up the comic view
 10   this.comicView = new TKComicView();
 11   this.comicView.delegate = this;
 12   // set up default pointers for arrows
 13   this._previousButton = null;
 14   this._nextButton = null;
 15   //
 16   this.callSuper(data);
 17 };
 18 
 19 TKComicController.prototype.processView = function () {
 20   this.callSuper();
 21   // add the positioning view
 22   this._view.appendChild(this.comicView.element);
 23   // wire up actions if we have a previous and next button wired
 24   if (this.previousButton !== null) {
 25     this._previousButton = this.view.querySelector(this.previousButton);
 26     if (this._previousButton !== null) {
 27       this._previousButton.addEventListener('click', this, false);
 28     }
 29   }
 30   if (this.nextButton !== null) {
 31     this._nextButton = this.view.querySelector(this.nextButton);
 32     if (this._nextButton !== null) {
 33       this._nextButton.addEventListener('click', this, false);
 34     }
 35   }
 36   this.addNavigableElement(this._view);
 37 };
 38 
 39 TKComicController.prototype.setComicViewData = function (data) {
 40   // set up the data source if we have .elements on the data object
 41   if (!TKUtils.objectIsUndefined(data.elements)) {
 42     this.comicView.dataSource = new TKComicViewDataSourceHelper(data.elements);
 43   }
 44   // copy properties
 45   TKUtils.copyPropertiesFromSourceToTarget(data, this.comicView);
 46   // init our view
 47   this.comicView.init();
 48   this.syncPageButtons();
 49 };
 50 
 51 TKComicController.prototype.syncPageButtons = function () {
 52   if (this._previousButton !== null) {
 53     this._previousButton[(this.comicView.currentPanelIndex <= 0 ? 'add' : 'remove') + 'ClassName']('inactive');
 54   }
 55   if (this._nextButton !== null) {
 56     this._nextButton[(this.comicView.currentPanelIndex >= this.comicView.numberOfPanels - 1 ? 'add' : 'remove') + 'ClassName']('inactive');
 57   }
 58 };
 59 
 60 /* ==================== Event Handling ==================== */
 61 
 62 TKComicController.prototype.wantsToHandleKeyboardEvent = function (event) {
 63   return (this.wantsCustomHandlingOfKeyboardEvent(event) || this.callSuper(event));
 64 };
 65 
 66 TKComicController.prototype.wantsCustomHandlingOfKeyboardEvent = function (event) {
 67   return false;
 68   // FIXME: actually implement this
 69   var key = event.keyCode;
 70   return (
 71     (key == KEYBOARD_LEFT && this.positioningView.activeElementIndex > 0) ||
 72     (key == KEYBOARD_RIGHT && this.positioningView.activeElementIndex < this.positioningView.numberOfElements - 1)
 73   );
 74 };
 75 
 76 TKComicController.prototype.handleKeydown = function (event) {
 77   if (!this.wantsCustomHandlingOfKeyboardEvent(event)) {
 78     this.callSuper(event);
 79     return;
 80   }
 81   switch (event.keyCode) {
 82     case KEYBOARD_RIGHT:
 83       this.positioningView.activeElementIndex++;
 84       break;
 85     case KEYBOARD_LEFT:
 86       this.positioningView.activeElementIndex--;
 87       break;
 88   }
 89 };
 90 
 91 TKComicController.prototype.handleEvent = function (event) {
 92   this.callSuper(event);
 93   // see if we hit one of the previous / next buttons
 94   if (event.currentTarget === this._previousButton) {
 95     this.comicView.showPreviousPanel();
 96   }
 97   else if (event.currentTarget === this._nextButton) {
 98     this.comicView.showNextPanel();
 99   }
100 };
101 
102 /* ==================== TKComicView Protocol ==================== */
103 
104 TKComicController.prototype.comicViewDidShowPanelAtIndex = function (view, index) {
105   this.panelWasShown(index);
106   // update the states of previous and next buttons
107   this.syncPageButtons();
108 };
109 
110 TKComicController.prototype.panelWasShown = function (index) {};
111 
112 TKClass(TKComicController);
113