Source: src/main/javascript/oracle/oj/ojvalidation/DateRestrictionValidator.js

Oracle® JavaScript Extension Toolkit (JET)
1.1.2

E65298-01

/**
 * Copyright (c) 2014, Oracle and/or its affiliates.
 * All rights reserved.
 */
 
/**
 * Constructs a DateRestrictionValidator that ensures the value provided is not in a disabled entry of dayMetaData
 * @param {Object=} options an object literal used to provide the following properties
 * @param {Function=} options.dayFormatter - Additional info to be used when rendering the day. This 
 * should be a JavaScript Function callback which accepts as its argument the following JSON format 
 * <code class="prettyprint">{fullYear: Date.getFullYear(), month: Date.getMonth()+1, date: Date.getDate()}</code>
 * and returns <code class="prettyprint">null</code> or all or partial JSON data of the form 
 * <code class="prettyprint">{disabled: true|false, className: "additionalCSS", tooltip: 'Stuff to display'}</code>
 * @param {string=} options.messageSummary - an optional custom error message summarizing the 
 * error. When not present, the default message summary is the resource defined with the key 
 * <code class="prettyprint">oj-validator.restriction.date.messageSummary</code>.
 * Tokens: {value} - value entered by user<p>. 
 * Example:<br/>
 * "Value {value} is disabled."<br/>
 * <p>
 * @param {string=} options.messageDetail - a custom error message used for creating detail part 
 * of message. When not present, the default message detail is the 
 * resource defined with the key <code class="prettyprint">oj-validator.restriction.date.messageDetail</code>.
 * Tokens: {value} - value entered by user<p>. 
 * Example:<br/>
 * "Value {value} is a disabled entry. Please select a different date."<br/>
 * </p>
 * @export
 * @constructor
 * @since 0.6
 */
oj.DateRestrictionValidator = function _DateRestrictionValidator(options)
{
  this.Init(options);
};

// Subclass from oj.Object 
oj.Object.createSubclass(oj.DateRestrictionValidator, oj.Validator, "oj.DateRestrictionValidator");

/**
 * Initializes validator instance with the set options
 * @param {Object=} options
 * @memberof oj.DateRestrictionValidator
 * @instance
 * @export
 */
oj.DateRestrictionValidator.prototype.Init = function (options)
{
  oj.DateRestrictionValidator.superclass.Init.call(this);
  this._dayFormatter = options["dayFormatter"];
  this._converter = oj.IntlConverterUtils.getConverterInstance(options["converter"]);
  if (options)
  {
    this._messageSummary = options['messageSummary'] || null;
    this._messageDetail = options['messageDetail'] || null;
  }
};

/**
 * Validates whether the date provided is part of disabled date
 *
 * @private
 * @ignore
 * @param {Date} valueDate that is being validated
 * @returns {boolean} boolean of whether it is a disabled date
 */
oj.DateRestrictionValidator.prototype._inDisabled = function(valueDate) 
{
  var dayFormatter = this._dayFormatter;
  
  if(dayFormatter) {
    var fullYear = valueDate.getFullYear(),
        month = valueDate.getMonth() + 1, //request to start from 1 rather than 0
        date = valueDate.getDate(),
        metaData = dayFormatter({"fullYear": fullYear, "month": month, "date": date});
    
    return metaData && metaData.disabled;
  }
  
  return false;
};

/**
 * Validates whether the date provided is part of disabled date
 *
 * @param {string} value that is being validated
 * @returns {string} original if validation was successful
 *
 * @throws {Error} when there is no match
 * @memberof oj.DateRestrictionValidator
 * @instance
 * @export
 */
oj.DateRestrictionValidator.prototype.validate = function (value)
{
  var summary = "", 
      detail = "", 
      translations = oj.Translations, 
      messageSummary = this._messageSummary,
      messageDetail = this._messageDetail,
      valueStr = value ? this._converter['format'](value) : value,
      valueDate = value ? oj.IntlConverterUtils.isoToLocalDate(value) : null;
  
  if(value === null) 
  {
    return value;
  }
  
  if(this._inDisabled(valueDate)) {
    
    summary = messageSummary ? translations.applyParameters(messageSummary, {"value": valueStr}) : 
                translations.getTranslatedString('oj-validator.restriction.date.messageSummary', {"value": valueStr});
    detail = messageDetail ? translations.applyParameters(messageDetail, {"value": valueStr}) : 
                translations.getTranslatedString('oj-validator.restriction.date.messageDetail', {"value": valueStr});
    throw new oj.ValidatorError(summary, detail);
  }
  
  return value;
};