// Copyright Neil Ramsden 2004. All rights reserved.

// Action classes
// Neil Ramsden
// Action.js
// 16 Jul 04 -> 27 Aug 04

//---------------------------------------------------------------------------

/*Action classes

An Action encapsulates a decision or instruction in a flowchart.
Clients can deduce the current position in the flowchart, eg. via the
choice.active property.
Each action class has an associated viewer class.
*/

/*Advice - provides a simple textual instruction
*/

function Advice(message)
//create an Advice object
{
  //advice text
  this.message = message;

  //view creation
  this.makeView =
    function(presentation) { return presentation.makeAdviceView(this); };
}


/*Choice - provides a sequence of available options

Each option is a pair of form {text: 'text string', action: action_object}
*/

function getActiveAction()
//get active option action
//  return -> [none] null; [exists] current chosen action object
{
  if (this._activeIndex == null)
    return null;
  else
    return this.options[this._activeIndex].action;
}

function getActiveChoice()
//get active option index
//  return -> index for active option
{
  return this._activeIndex;
}

function setActiveChoice(index)
//set active option index
//  index -> index for active option
{
  this._activeIndex = index;
}

function Choice(optionArray)
//create a Choice object
//  optionArray - array of option pairs
{
  //available options
  this.options = optionArray;

  //current choice
  this._activeIndex = null;
  this.activeIndex /*getter*/ = function() { return this._activeIndex; };
  this.active /*getter*/ = getActiveAction;
  this.getActiveValue = getActiveChoice;
  this.setActiveValue = setActiveChoice;

  //view creation
  this.makeView =
    function(presentation) { return presentation.makeChoiceView(this); };
}
