Source: modules/config/AttributeTable.js

/**
 * @module config/AttributeTable.js
 * @name AttributeTable
 * @copyright 2023 3Liz
 * @author DHONT René-Luc
 * @license MPL-2.0
 */

import { BaseObjectLayerConfig, BaseObjectLayersConfig } from './BaseObject.js';

const requiredProperties = {
    'primaryKey': {type: 'string'},
    'pivot': {type: 'boolean'},
    'hideAsChild': {type: 'boolean'},
    'hideLayer': {type: 'boolean'}
};

const optionalProperties = {
    'hiddenFields': {type: 'string', default: ''},
    'export_enabled': {type: 'boolean', default: true},
    'export_formats': {type: 'array', contentType: 'string', default: []},
    'export_max_features': {type: 'number', default: -1},
};

/**
 * Class representing an attribute layer config
 * @class
 * @name AttributeLayerConfig
 * @augments BaseObjectLayerConfig
 */
export class AttributeLayerConfig extends BaseObjectLayerConfig {
    /**
     * Create an attribute layer config instance
     * @param {string} layerName - the layer name
     * @param {object} cfg       - the lizmap config object for tooltip layer
     */
    constructor(layerName, cfg) {
        super(layerName, cfg, requiredProperties, optionalProperties)
    }

    /**
     * The layer primary key
     * @type {string}
     */
    get primaryKey() {
        return this._primaryKey;
    }

    /**
     * The layer hidden fields
     * @type {string}
     */
    get hiddenFields() {
        return this._hiddenFields;
    }

    /**
     * The layer is pivot table
     * @type {boolean}
     */
    get pivot() {
        return this._pivot;
    }

    /**
     * The layer is hide as child
     * @type {boolean}
     */
    get hideAsChild() {
        return this._hideAsChild;
    }

    /**
     * The layer is hide in attribute table list
     * @type {boolean}
     */
    get hideLayer() {
        return this._hideLayer;
    }

    /**
     * The layer export is enabled
     * @type {boolean}
     */
    get exportEnabled() {
        return this._export_enabled;
    }

    /**
     * The layer export formats
     * @type {string[]}
     */
    get exportFormats() {
        return [...this._export_formats];
    }

    /**
     * The layer export max features
     * @type {number}
     */
    get exportMaxFeatures() {
        return this._export_max_features;
    }

    /**
     * The layer export is limited by max features
     * @type {boolean}
     */
    get exportLimited() {
        return (this._export_max_features > 0);
    }

    /**
     * Get the list of effective format for file export
     * @param {string[]} vectorLayerResultFormats The list of format for file export
     * @returns {string[]} The list of effective format for file export
     */
    getEffectiveExportFormats(vectorLayerResultFormats) {
        const lowerVectorLayerResultFormats = vectorLayerResultFormats.map(format => format.toLowerCase());
        const exportFormats = [].concat(
            lowerVectorLayerResultFormats.indexOf('geojson') != -1 ? ['GeoJSON'] : [],
            lowerVectorLayerResultFormats.indexOf('gml2') != -1 ||
            lowerVectorLayerResultFormats.indexOf('gml3') != -1 ? ['GML'] : [],
            vectorLayerResultFormats.filter(format => {
                return ['gml2', 'gml3', 'geojson'].indexOf(format.toLowerCase()) == -1;
            })
        );
        if (this.exportFormats.length == 0) {
            return exportFormats;
        }
        const lowerExportFormats = this.exportFormats.map(format => format.toLowerCase());
        return exportFormats.filter(format => {
            return lowerExportFormats.indexOf(format.toLowerCase()) != -1;
        });
    }
}

/**
 * Class representing an attribute layers config
 * @class
 * @augments BaseObjectLayersConfig
 */
export class AttributeLayersConfig extends BaseObjectLayersConfig {

    /**
     * Create an attribute layers config instance
     * @param {object} cfg - the lizmap attributeLayers config object
     */
    constructor(cfg) {
        super(AttributeLayerConfig, cfg)
    }
}