Name

sn_mab_api.ConditionalEvaluator

Description

Used to evaluate conditionals

Script

var ConditionalEvaluator = Class.create();
ConditionalEvaluator.prototype = {
  initialize: function() {
      this.errorHandler = new sn_mab_api.ErrorHandler();
  },

  type: 'ConditionalEvaluator'
};

ConditionalEvaluator.getEvaluator = function(conditionalType, conditionalValue) {
  if (conditionalType === 'childHasKey')
  	return new ChildHasKeyEvaluator(conditionalValue);
  else if (conditionalType === 'parentHasKey')
  	return new ParentHasKeyEvaluator(conditionalValue);
  else if (conditionalType === 'aParentHasKey')
  	return new AParentHasKeyEvaluator(conditionalValue);
  else if (conditionalType === 'noParentHasKey')
  	return new NoParentHasKeyEvaluator(conditionalValue);
  else if (conditionalType === 'aParentHasAnyOfKeys')
      return new AParentHasAnyOfKeysEvaluator(conditionalValue);
  else if (conditionalType === 'noParentHasAnyOfKeys')
      return new NoParentHasAnyOfKeys(conditionalValue);
  else if (conditionalType === 'isRoot')
  	return new IsRootEvaluator();
  else
  	new ErrorHandler().throwBadConfigError('Invalid conditionalType received: ' + conditionalType);
};

ConditionalEvaluator.evaluate = function(conditional, configKeyStack, childConfigMetadata) {
  if (!conditional) {
      this.errorHandler.throwInternalError('condition must be provided');
  }
  var matches = true;

  Object.keys(conditional).forEach(function (conditionalType) {
  	var evaluator = ConditionalEvaluator.getEvaluator(conditionalType, conditional[conditionalType]);
  	if (evaluator instanceof ChildHasKeyEvaluator)
  		matches &= evaluator.evaluate(childConfigMetadata);
  	else
  		matches &= evaluator.evaluate(configKeyStack);
  });

  return matches;
};

function ChildHasKeyEvaluator(conditionalValue) {
  this.conditionalValue = conditionalValue;
}
ChildHasKeyEvaluator.prototype.evaluate = function(childConfigMetadata) {
  return childConfigMetadata && this.conditionalValue == childConfigMetadata.getConfigKey();
};

function ParentHasKeyEvaluator(conditionalValue) {
  this.conditionalValue = conditionalValue;
}
ParentHasKeyEvaluator.prototype.evaluate = function(configKeyStack) {
  return configKeyStack.length && configKeyStack[configKeyStack.length - 1].configKey == this.conditionalValue;
};

function AParentHasKeyEvaluator(conditionalValue) {
  this.conditionalValue = conditionalValue;
}
AParentHasKeyEvaluator.prototype.evaluate = function(configKeyStack) {
  var localConditionalValue = this.conditionalValue;
  var hasKey = false;
  if (configKeyStack && configKeyStack.length) {
  	configKeyStack.forEach(function(currConfigKeyEntry) {
  		hasKey = hasKey || (currConfigKeyEntry.configKey == localConditionalValue);
  	});
  }

  return hasKey;
};

function NoParentHasKeyEvaluator(conditionalValue) {
  AParentHasKeyEvaluator.call(this, conditionalValue);
}
// extend AParentHasKeyEvaluator
NoParentHasKeyEvaluator.prototype = new AParentHasKeyEvaluator();
NoParentHasKeyEvaluator.prototype.evaluate = function(configKeyStack) {
  var aParentHasKey = AParentHasKeyEvaluator.prototype.evaluate.call(this, configKeyStack);
  return !aParentHasKey;
};

function AParentHasAnyOfKeysEvaluator(conditionalValues) {
  this.conditionalValues = conditionalValues;
}

AParentHasAnyOfKeysEvaluator.prototype.evaluate = function(configKeyStack) {
  var localConditionalValues = this.conditionalValues;
  var hasKey = false;
  if (configKeyStack && configKeyStack.length) {
      configKeyStack.forEach(function(currConfigKeyEntry) {
          hasKey = hasKey || (localConditionalValues.indexOf(currConfigKeyEntry.configKey) > -1);
      });
  }

  return hasKey;
};

function NoParentHasAnyOfKeys(conditionalValues) {
  AParentHasAnyOfKeysEvaluator.call(this, conditionalValues);
}

// extend AParentHasKeyOfEvaluator
NoParentHasAnyOfKeys.prototype = new AParentHasAnyOfKeysEvaluator();

NoParentHasAnyOfKeys.prototype.evaluate = function(configKeyStack) {
  var aParentHasAnyOfKeys = AParentHasAnyOfKeysEvaluator.prototype.evaluate.call(this, configKeyStack);
  return !aParentHasAnyOfKeys;
};

function IsRootEvaluator() {}
IsRootEvaluator.prototype.evaluate = function(configKeyStack) {
  // If we are the rootnode we are the only thing on the configKeyStack
  return configKeyStack.length && configKeyStack.length == 1;
};

Sys ID

a6055b5053762010c722ddeeff7b127d

Offical Documentation

Official Docs: