UxJobStepFactory.js

/*
 * @copyright 2023 Tridium, Inc. All Rights Reserved.
 * @author Sai Komaravolu
 */

/**
 * @module nmodule/batchJob/rc/baja/UxJobStepFactory
 */
define([
  'baja!',
  'lex!',
  'Promise' ], function (
  baja,
  lex,
  Promise) {

  'use strict';

  /**
   * API Status: **Development**
   *
   * You must implement this interface to create 'batchJob:UxJobStepFactory'.
   * Properties must have 'module' and 'lex' to derive 'displayName', 'description',
   * and 'icon' for the job step factory, also, one must implement method 'makeStep' to make the job step,
   * and may implement method 'editStep' to edit the job step.
   *
   * @since Niagara 4.14
   * @interface
   * @alias module:nmodule/batchJob/rc/baja/UxJobStepFactory
   */
  class UxJobStepFactory {


    /**
     * @param {Object} params
     * @param {String} params.module Module name
     * @param {String} params.lex Prefix of the lexicon key
     *
     * @example
     * //module:lex will be used to resolve description, icon and displayName of the UxJobStepFactory.
     * //myFactory.description in the 'myModule' lexicon will be used as description for the job step factory.
     * //myFactory.icon in the 'myModule' lexicon will be used as an icon for the job step factory.
     * //myFactory.displayName in the 'myModule' lexicon will be used as displayName for the job step factory.
     *
     * new UxJobStepFactory({
     *   module: 'myModule',
     *   lex: 'myFactory'
     * });
     */
    constructor(params) {
      this.$displayName = '';
      this.$description = '';
      this.$icon = '';

      if (params && params.module && params.lex) {
        const moduleWithPrefix = `${ params.module }:${ params.lex }`;
        this.$displayName = `%lexicon(${ moduleWithPrefix }.displayName)%`;
        this.$description = `%lexicon(${ moduleWithPrefix }.description)%`;
        this.$icon = `%lexicon(${ moduleWithPrefix }.icon)%`;
      }
    }

    /**
     * Returns fully configured JobStep or an array of JobStep or null.
     *
     * @param {Object} params
     * @param {baja.Component} params.deviceContainer
     * @returns {Promise.<baja.Component|Array.<baja.Component>>} resolves to a singular or array of JobStep.
     * @throws {Error} throws Error if abstract makeStep is invoked
     */
    makeStep(params) {
      throw new Error('Must override method');
    }

    /**
     * Takes an existing JobStep as input, allows the user to edit it, and returns or resolves a
     * new copy of that JobStep. The new copy will be used to replace the existing one.
     *
     * @function editStep
     * @memberOf module:nmodule/batchJob/rc/baja/UxJobStepFactory
     * @param {Object} params
     * @param {baja.Component} params.step an instance of `batchJob:JobStep`.
     * @param {baja.Component} params.deviceContainer
     * @returns {Promise.<baja.Component|undefined>|baja.Component|undefined}
     */

    /**
     * @returns {Promise.<string>} Description of the UxJobStep
     */
    toDescription() {
      return lex.format(this.$description);
    }

    /**
     * @returns {Promise.<baja.Icon>} Icon of the UxJobStep
     */
    toIcon() {
      return lex.format(this.$icon)
        .then((iconString) => baja.Icon.make(iconString));
    }

    /**
     * @returns {Promise.<string>} DisplayName of the UxJobStep
     */
    toDisplayName() {
      return lex.format(this.$displayName);
    }

    /**
     * Returns the value to true which means the step is added in the step list by default.
     * If return value is false then the step is not added in the step list.
     * Can be overridden by a subclass to provide its own value.
     *
     * @private
     * @returns {boolean} returns true by default
     */
    $isValidFor() {
      return true;
    }
  }

  return UxJobStepFactory;
});