Source: widget/whatsnew/project/WNTours.js

(function() {
    /**
     * this backend connector is able to handle tours for WN mode
     * @augments Help4.widget.help.project._Special
     */
    Help4.widget.help.project.WNTours = class extends Help4.widget.help.project._Special {
        static NAME = 'WNTours';

        static data = /** @type {{pub: Object[], head: Object[]}} */ {pub: [], head: []};
        static lastUpdate = 0;

        /**
         * @param {Help4.widget.help.ProjectTile[]} output
         * @param {string} catalogueKey
         */
        static getTiles(output, catalogueKey) {
            const tiles = (/** @type {Object[]} */ this.getData(catalogueKey))
            .map(data => ({...data, _catalogueKey: catalogueKey}));

            output.push(...tiles);
        }

        /**
         * delivers a dummy project; real data is added in {@link Help4.widget.help.Data.prototype.getHelpTiles}
         * @param {string} projectId - project ID
         * @param {Help4.typedef.SystemConfiguration} config - the system configuration
         * @param {Help4.widget.help.Data} data
         * @returns {Help4.widget.help.Projects}
         */
        static load(projectId, config, data) {
            const {WHATSNEW_SCREEN_ID} = Help4.widget.help.CatalogueBackend;

            const result = super.load(projectId, config, data);
            const {pub, head} = result;
            pub.screen += WHATSNEW_SCREEN_ID;
            head.screen += WHATSNEW_SCREEN_ID;
            return result;
        }

        /** @returns {Promise<void>} */
        static async clearData() {
            if (!this.hasData()) return;

            this.data = {pub: [], head: []};
            this.lastUpdate = Date.now();
            await this._refreshWidget();
        }

        /**
         * @param {{pub: Help4.widget.help.CatalogueProject[], head: Help4.widget.help.CatalogueProject[]}} data
         * @returns {Promise<void>}
         */
        static async setData(data) {
            this.data = {pub: [], head: []};
            await this.addData(data);
        }

        /**
         * @param {{pub: Help4.widget.help.CatalogueProject[], head: Help4.widget.help.CatalogueProject[]}} data
         * @returns {Promise<void>}
         */
        static async addData({pub, head}) {
            const {[this.NAME]: catalogue} = Help4.widget.help.catalogues;
            const {ID: _projectId} = catalogue;

            const toTile = project => ({
                catalogueProject: {...project},
                type: 'tour',
                id: project.id,
                title: project.title,
                hidden: project.hidden,
                published: project.published,
                language: project.language,
                _dataType: project._dataType,
                _catalogueType: project._catalogueType,
                _projectId
            });

            pub = /** @type {Object[]} */ pub.map(toTile);
            head = /** @type {Object[]} */ head.map(toTile);

            const {pub: dataPub, head: dataHead} = this.data;
            dataPub.push(...pub);
            dataHead.push(...head);
            this.lastUpdate = Date.now();

            await this._refreshWidget();
        }

        /**
         * @param {?Help4.widget.help.CatalogueKeys} [catalogueKey = null]
         * @returns {{pub: Object[], head: Object[]}}
         */
        static getData(catalogueKey = null) {
            return catalogueKey
                ? this.data[catalogueKey]
                : this.data;
        }

        /**
         * @param {?Help4.widget.help.CatalogueKeys} [catalogueKey = null]
         * @returns {boolean}
         */
        static hasData(catalogueKey = null) {
            if (catalogueKey) {
                const {[catalogueKey]: data} = this.data;
                return data.length > 0;
            } else {
                const {pub, head} = this.data;
                return pub.length > 0 || head.length > 0;
            }
        }

        /**
         * @protected
         * @returns {Promise<void>}
         */
        static async _refreshWidget() {
            const widget = /** @type {?Help4.widget.help.Widget} */ Help4.widget.getInstance('whatsnew');
            await widget?.redraw();
        }
    }
})();