Booklet.js

Summary

No overview generated for 'Booklet.js'


Class Summary
ITunesTrackEmulator  

Method Summary
static void init()
          

/**
 *  Copyright © 2009 Apple Inc.  All rights reserved.
 *
 *  @class
 **/
 
var bookletController = new TKObject();

bookletController.init = function () {
  this.playbackHasStarted = false;

  // stop current playback
  window.iTunes.stop();
  
  // set up audio loop
  this.startAudioLoop();
  
  // determine play position if this a movie
  if (appData.feature.XID){
    var feature = this.getTrack(appData.feature);
    if (feature && feature.bookmark != 0){
      this.playbackHasStarted = true;
      this.dispatchDisplayUpdates();
    }
  }
  
  // add event listeners
  window.addEventListener("play", this, false);
  window.addEventListener("pause", this, false);
  window.addEventListener("videoclosed", this, false);
  
  // create our navigation controller and add the home controller as the root controller
  this.navigation = new TKNavigationController({
    id : 'navigation',
    rootController : homeController,
    delegate : this
  });
};

bookletController.handleEvent = function(event) {
  switch (event.type) {
    case "play":
      // video has started to play - stop the audio loop
      this.stopAudioLoop();
      //this.dispatchDisplayUpdates();
      break;
    case "pause":
      // video has paused
      //this.dispatchDisplayUpdates();
      break;
    case "videoclosed":
      // video has stopped - restart audio loop
      this.startAudioLoop();
      this.dispatchDisplayUpdates();
      // stub to check what chapter we are on for sticky chapters
      debug(iTunes.currentChapter);
      break;
    default:
      debug("Unknown event type in bookletController : " + event.type);
  }
};

bookletController.startAudioLoop = function() {
  
  // if the loop exists already, we're being asked to resume
  // check if we're set to do so, and if not, exit without starting playback
  if (this.audioLoop && appData.audioLoop && !appData.audioLoop.loop) {
    this.audioLoop.pause();
    this.audioLoop.volume = 0;
    return;
  }
  
  // create the loop if it doesn't exist yet
  if (appData.audioLoop && !this.audioLoop) {
    this.audioLoop = new Audio();
    this.audioLoop.src = appData.audioLoop.src;
    // make sure this background audio is never displayed
    this.audioLoop.style.display = "none";
    // add it to the document so that iTunes will notice it is there
    document.body.appendChild(this.audioLoop);
    this.audioLoop.volume = 0;
  }
  
  if (this.audioLoop) {
    this.audioLoop.loop = appData.audioLoop.loop;
    this.audioLoop.volume = Math.min(1, window.iTunes.volume);
    this.audioLoop.play();
  }
};

bookletController.stopAudioLoop = function() {
  if (this.audioLoop) {
    this.audioLoop.pause();
    this.audioLoop.loop = false;
    this.audioLoop.currentTime = 0; // reset to beginning
  }
};

bookletController.play = function (trackObj) {
  var track = this.getTrack(trackObj);
  
  if (track != null){
    track.play();
  }
};

bookletController.playFeature = function (){
  var feature = bookletController.getTrack(appData.feature);
  
  if (feature != null){
    feature.play();
    if (!this.playbackHasStarted){
      this.playbackHasStarted = true;
    }
  }
};

bookletController.resumeFeature = function(){
  bookletController.playFeature();
};

bookletController.playChapter = function (index){
  var feature = this.getTrack(appData.feature);
  
  if (feature != null){
    feature.play({startChapterIndex : index});
    if (!this.playbackHasStarted){
      this.playbackHasStarted = true;
    }
  }
};

bookletController.getChapter = function (){
  var feature = this.getTrack(appData.feature);
  
  if (feature){
    if (iTunes.currentChapter == 0 && feature.bookmark != 0){
      debug("estimating");
      var estimatedChapter = Math.floor((feature.bookmark / feature.duration) * feature.chapters.length);
      var actualChapter = -1;

      if ((feature.chapters[estimatedChapter].startOffsetTime / 1000) == feature.bookmark){
      } else if ((feature.chapters[estimatedChapter].startOffsetTime / 1000) < feature.bookmark){
        while (estimatedChapter < feature.chapters.length && (feature.chapters[estimatedChapter].startOffsetTime / 1000) < feature.bookmark){
          estimatedChapter++;
        }
      } else if ((feature.chapters[estimatedChapter].startOffsetTime / 1000) > feature.bookmark){
        while (estimatedChapter >= 0 && (feature.chapters[estimatedChapter].startOffsetTime / 1000) > feature.bookmark){
          estimatedChapter--;
        }
      }
      actualChapter = estimatedChapter;
      return actualChapter;
    } else {
      debug("itunes query");
      return iTunes.currentChapter;
    }
  } else {
    return -1;
  }
}

bookletController.buildPlaylist = function (tracks){
  var tracklistObj = iTunes.createTempPlaylist();
  var tracklist = [];
  
  for (var i = 0; i < tracks.length; i++){
    var track = this.getTrack(tracks[i]);
    if (track != null){
      tracklist.push(track);
    }
  }
  
  tracklistObj.addTracks(tracklist);
  debug("added " + tracklist.length + " of " + tracks.length + " tracks successfully.");
  return tracklistObj;
};

bookletController.buildNonLibraryPlaylist = function (tracks){
  var tracklistObj = iTunes.createTempPlaylist();
  var tracklist = [];
  
  for (var i = 0; i < tracks.length; i++){
    var track = {};
    track.url = tracks[i].src;
    track.title = tracks[i].string;
    track.artist = appData.feature.artist;
    track.album = appData.feature.title;
    debug("adding: " + track.title + " (" + track.url + ")");
    tracklist.push(track);
  }
  
  debug("pushing to tracklistOb");
  tracklistObj.addURLs(tracklist);
  return tracklistObj;
};

bookletController.trackDuration = function (trackObj){
  var track = this.getTrack(trackObj);
  if (track != null){
    debug("querying duration");
    return track.durationAsString;
  } else {
    return "0:00";
  }
};

bookletController.trackNumber = function (trackObj){
  var track = this.getTrack(trackObj);
  if (track != null){
    debug("querying track number");
    return track.trackNumber;
  } else {
    return "0";
  }
};

bookletController.getTrack = function (trackObj){
  if (trackObj.XID){
    debug("searching by XID: " + trackObj.XID);
    var iTunesTrack = window.iTunes.findTracksByXID(trackObj.XID);
    if (iTunesTrack.length > 0){
      debug("found by XID");
      return iTunesTrack[0];
    } else {
      debug("XID not found in library");
      return null;
    }
  } else {
    debug("no XID");
    return null;
  }
};

bookletController.playNonLibraryContent = function (trackObj){
  debug("videos/" + trackObj.src);
  debug({title : trackObj.string, artist : appData.feature.artist, album : appData.feature.title});
  window.iTunes.play("videos/" + trackObj.src, {title : trackObj.string, artist : appData.feature.artist, album : appData.feature.title});
};

bookletController.childControllers = [];

bookletController.registerForDisplayUpdates = function (childController) {
  if (this.childControllers.indexOf(childController) == -1) {
    this.childControllers.push(childController);
  }
};

bookletController.dispatchDisplayUpdates = function () {
  for (var i=0; i < this.childControllers.length; i++) {
    if (TKUtils.objectIsFunction(this.childControllers[i].updateDisplay)) {
      this.childControllers[i].updateDisplay();
    }
  }
};

/* ================= iTunes Emulation ======================== */

var iTunesEmulator = {
  volume : 1,
  platform : 'Emulator',
  version : '-1'
};

iTunesEmulator.play = function (mediaLocation) {
  debug("iTunesEmulator - play: " + mediaLocation);
};

iTunesEmulator.stop = function () {
  debug("iTunesEmulator - stop");
};

iTunesEmulator.findTracksByStoreID = function (storeID) {
  debug("iTunesEmulator - findTracksByStoreID: " + storeID);
  return [new ITunesTrackEmulator()];
};

iTunesEmulator.findTracksByXID = function (xID) {
  debug("iTunesEmulator - findTracksByXID: " + xID);
  return [new ITunesTrackEmulator()];
};

function ITunesTrackEmulator() {
}

ITunesTrackEmulator.prototype.play = function (params) {
  debug("iTunesTrackEmulator - play: " + params);
  // fake the play event to the window
  var event = document.createEvent("HTMLEvents");
  event.initEvent("play", false, false);
  window.dispatchEvent(event);
  setTimeout(function() {
    debug("iTunesTrackEmulator - coming back from playback");
    event = document.createEvent("HTMLEvents");
    event.initEvent("videoclosed", false, false);
    window.dispatchEvent(event);
  }, 5000);
};

/* ================= Initialisation ======================== */

// indicates whether we support keyboard navigation
var SUPPORTS_KEYBOARD_NAVIGATION;

function init () {
  
  // override select start
  document.onselectstart = function() { return false; };
  
  // check for iTunes object, create a dummy if it doesn't exist
  if (!window.iTunes) {
    window.iTunes = iTunesEmulator;
  }
  // set up the keyboard navigation support global 
  SUPPORTS_KEYBOARD_NAVIGATION = (parseInt(window.iTunes.version, 0) < 9);

  bookletController.init();
};

window.addEventListener('load', init, false);


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