JavaScript Extension Development API for Oracle Visual Builder Cloud Service - Classic Applications

Source: extensions.dt/js/api/CustomParameter.js

/* eslint-disable no-use-before-define */

define([], function () {

    'use strict';

    /**
     * Factory object for creating <code>CustomParameter</code> instances.
     *
     * <p>
     * Created parameters are always one of two available types:
     * <ul>
     *  <li>
     *      Key-value pair where value is plain string. Created using {@link extensions.dt/js/api/CustomParameter.createSimple CustomParameter.createSimple(..)} method.
     *  </li>
     *  <li>
     *      Key-value pair where value is always set to one of predefined values. Created using {@link extensions.dt/js/api/CustomParameter.createEnum CustomParameter.createEnum(..)} method.
     *  </li>
     * </ul>
     * </p>
     *
     * <p>
     * Note that <code>CustomParameter</code>s created here don't hold the value itself. It's rather a definition which gives Application Builder information required to build up the UI.
     * If you're interested in consuming <code>CustomParameter</code> values defined by Application Builder Business User, they will be passed within the dependencies parameter given to
     * the {@link extensions.dt/js/spi/ExtensionManager#initialise ExtensionManager.initialise()}, {@link extensions.dt/js/spi/ExtensionManager#destroy ExtensionManager.destroy()} and
     * {@link extensions.dt/js/spi/ExtensionManager#generateRuntimeCode ExtensionManager.generateRuntimeCode()} methods.
     * </p>
     *
     * @AbcsExtension stable
     * @version 17.1.1
     * @exports extensions.dt/js/api/CustomParameter
     *
     * @constructor
     * @private
     *
     * @param {String} key - Key (resp. display name) representing this <code>CustomParameter</code>.
     * @param {String} displayName - Display name representing this <code>CustomParameter</code>.
     * @param {String} [defaultValue] - Default value for this <code>CustomParameter</code>.
     *
     * @see {@link bop.dt/js/spi/BOPExtensionManager BOPExtensionManager} where you can use instances of <code>CustomParameter</code>s to define input values required for {@link bop/js/spi/BOP BOP} initialization.
     */
    var CustomParameter = function(key, displayName, defaultValue) {
        AbcsLib.checkThis(this);

        this._key = key;
        this._displayName = displayName;
        this._defaultValue = defaultValue;
    };

    /**
     * Gets the key of this <code>CustomParameter</code>.
     *
     * @returns {String}
     */
    CustomParameter.prototype.getKey = function() {
        return this._key;
    };

    /**
     * Gets the display name of this <code>CustomParameter</code>.
     *
     * @returns {String}
     */
    CustomParameter.prototype.getDisplayName = function() {
        return this._displayName;
    };

    /**
     * Gets the default value of this <code>CustomParameter</code>.
     *
     * @returns {String}
     */
    CustomParameter.prototype.getDefaultValue = function() {
        return this._defaultValue;
    };

    /**
     * Checks if this parameter is of {@link extensions.dt/js/api/CustomParameter.Enum CustomParameter.Enum} or not.
     *
     * @returns {Boolean}
     */
    CustomParameter.prototype.isEnum = function() {
        return this instanceof Enum;
    };

    /**
     * Checks if this parameter is of {@link extensions.dt/js/api/CustomParameter.System CustomParameter.System} or not.
     *
     * @returns {Boolean}
     */
    CustomParameter.prototype.isSystem = function() {
        return this instanceof System;
    };

    /**
     * Creates a simple instance representing key-value pair.
     *
     * @AbcsExtension stable
     * @version 17.1.1
     *
     * @param {String} key - Key value identifying this <code>CustomParameter</code>.
     * @param {String} displayName - Display name representing this <code>CustomParameter</code>.
     * @param {String} [defaultValue] - Default value for this <code>CustomParameter</code>.
     * @returns {extensions.dt/js/api/CustomParameter}
     *
     * @example
     * <caption>
     *  Creates <code>CustomParameter</code> which will holds simple key-value pair.
     * </caption>
     *
     * CustomParameter.createSimple('SourceURL', 'URL poiting to the source of this BOP');
     *
     * @example
     * <caption>
     *  Creates <code>CustomParameter</code> which will holds simple key-value pair with a default value pre-defined.
     * </caption>
     *
     * CustomParameter.createSimple('SourceURL', 'URL pointing to the source of this BOP', 'https://my.url.com');
     */
    CustomParameter.createSimple = function(key, displayName, defaultValue) {
        AbcsLib.checkDefined(key, 'key');
        AbcsLib.checkDefined(displayName, 'displayName');
        AbcsLib.checkDataType(key, AbcsLib.Type.STRING);
        AbcsLib.checkDataType(displayName, AbcsLib.Type.STRING);
        if (defaultValue) {
            AbcsLib.checkDataType(defaultValue, AbcsLib.Type.STRING);
        }
        AbcsLib.checkParameterCount(arguments, 2, 1);

        return new Simple(key, displayName, defaultValue);
    };

    /**
     * Creates a simple instance that allows a object as a default value.
     *
     * @param {String} key - Key value identifying this <code>CustomParameter</code>.
     * @param {String} displayName - Display name representing this <code>CustomParameter</code>.
     * @param {Object} [defaultValue] - Default value for this <code>CustomParameter</code>.
     * @returns {extensions.dt/js/api/CustomParameter}
     *
     * @example
     * <caption>
     *  Creates <code>CustomParameter</code> which will holds simple key-value pair with a default value pre-defined.
     * </caption>
     *
     * CustomParameter.createSystem('SecurityKey', 'Key for accesing remote service', { key : '...' });
     */
    CustomParameter.createSystem = function(key, displayName, defaultValue) {
        AbcsLib.checkDefined(key, 'key');
        AbcsLib.checkDefined(displayName, 'displayName');
        AbcsLib.checkDataType(key, AbcsLib.Type.STRING);
        AbcsLib.checkDataType(displayName, AbcsLib.Type.STRING);
        AbcsLib.checkParameterCount(arguments, 2, 1);

        return new System(key, displayName, defaultValue);
    };

    /**
     * Creates an enumeration instance where value can be one of prefedined values.
     *
     * @AbcsExtension stable
     * @version 17.1.1
     *
     * @param {Object} params - Object literal with all possible parameters.
     * @param {String} params.key - Key value identifying this <code>CustomParameter</code>.
     * @param {String} params.displayName - Display name representing this <code>CustomParameter</code>.
     * @param {String[]} params.values - Array of strings listing all available values this Enum provides.
     * @param {String} [params.defaultValue] - Default value for this <code>CustomParameter</code>. If no default value is provided, The first value passed
     *                                         into "values" attribute will be considered as default and Application Builder will always show it initially.
     * @returns {extensions.dt/js/api/CustomParameter}
     *
     * @example
     * <caption>
     *  Creates <code>CustomParameter</code> which will holds key-value pair where the value will always be one of three pre-defined enumeration values.
     * </caption>
     *
     * CustomParameter.createEnum({
     *     key: 'environment',
     *     displayName: 'Choose type of the environment you want to work with',
     *     values: [
     *         'Development',
     *         'Testing',
     *         'Production'
     *     ]
     * });
     *
     * @example
     * <caption>
     *  Creates <code>CustomParameter</code> enumeration with explicitly pre-defined default value.
     * </caption>
     *
     * CustomParameter.createEnum({
     *     key: 'environment',
     *     displayName: 'Choose type of the environment you want to work with',
     *     values: [
     *         'Development',
     *         'Testing',
     *         'Production'
     *     ],
     *     defaultValue: 'Testing':
     * });
     */
    CustomParameter.createEnum = function(params) {
        AbcsLib.checkDefined(params, 'params', 'You need to pass object literal with at least "key", "displayName" and "values" attributes being set');
        AbcsLib.checkDefined(params.key, 'params.key', 'Your object literal need to contain "key" attribute being set');
        AbcsLib.checkDefined(params.values, 'params.values', 'Your object literal need to contain "values" attribute being set');
        AbcsLib.checkDefined(params.displayName, 'params.displayName', 'Your object literal need to contain "key" attribute being set');
        AbcsLib.checkParameterCount(arguments, 1);
        AbcsLib.checkObjectLiteral(params, ['key', 'displayName', 'values', 'defaultValue']);
        AbcsLib.checkDataType(params.key, AbcsLib.Type.STRING);
        AbcsLib.checkDataType(params.displayName, AbcsLib.Type.STRING);
        AbcsLib.checkDataType(params.values, AbcsLib.Type.ARRAY);
        if (params.defaultValue) {
            AbcsLib.checkDataType(params.defaultValue, AbcsLib.Type.STRING);
        }

        var values = params.values;
        for (var i = 0; i < values.length; i++) {
            AbcsLib.checkDataType(values[i], AbcsLib.Type.STRING);
        }
        var defaultValue;
        if (params.defaultValue) {
            defaultValue = params.defaultValue;
        } else if (values && values.length > 0) {
            defaultValue = values[0];
        }

        return new Enum(params.key, params.displayName, values, defaultValue);
    };

    /**
     * Represents simple key-value instance of <code>CustomParameter</code>.
     *
     * @param {String} key - Key value identifying this <code>CustomParameter</code>.
     * @param {String} displayName - Display name representing this <code>CustomParameter</code>.
     * @param {String} [defaultValue] - Default value for this <code>CustomParameter</code>.
     * @returns {Simple}
     */
    var Simple = function(key, displayName, defaultValue) {
        CustomParameter.apply(this, [key, displayName, defaultValue]);
    };
    AbcsLib.extend(Simple, CustomParameter);

    /**
     * Represents simple key-value instance of <code>CustomParameter</code>.
     *
     * @param {String} key - Key value identifying this <code>CustomParameter</code>.
     * @param {String} displayName - Display name representing this <code>CustomParameter</code>.
     * @param {Object} [defaultValue] - Default value for this <code>CustomParameter</code>.
     */
    var System = function(key, displayName, defaultValue) {
        CustomParameter.apply(this, [key, displayName, defaultValue]);
    };
    AbcsLib.extend(System, CustomParameter);

    /**
     * Represents simple key-value instance of <code>CustomParameter</code>.
     *
     * @param {String} key - Key value identifying this <code>CustomParameter</code>.
     * @param {String} displayName - Display name representing this <code>CustomParameter</code>.
     * @param {String[]} values - Array of strings listing all available values this Enum provides.
     * @param {String} [defaultValue] - Default value for this <code>CustomParameter</code>.
     * @returns {Enum}
     */
    var Enum = function(key, displayName, values, defaultValue) {
        CustomParameter.apply(this, [key, displayName, defaultValue]);

        this._values = values;
    };
    AbcsLib.extend(Enum, CustomParameter);

    /**
     * Gets all values assigned to this <code>CustomParameter</code>.
     *
     * <p>
     * Calling this methods makes sense on if {@link extensions.dt/js/api/CustomParameter#isEnum CustomParameter.isEnum()} returns <code>true</code>.
     * </p>
     *
     * @returns {String[]}
     */
    Enum.prototype.getValues = function() {
        return this._values;
    };

    return CustomParameter;
});