private trellisResetAction$()

in clns-acuity-vahub/vahub/src/main/webapp/src/app/common/trellising/store/Trellising.ts [310:491]


    private trellisResetAction$(): Observable<Action> {
        return this.actions$
            .withLatestFrom(this._store.select(getTabId))
            .filter(([action, tabId]) => action.type === TRELLIS_RESET && NEW_APPROACH_TAB_LIST.contains(tabId))
            .do(([action, tabId]) => this._store.dispatch(this.trellisingActionCreator.makeLoadingAction(true, tabId)))
            // we have to get parent selection, as tabId can already be changed by the end of x-axis request completion
            .withLatestFrom(this._store.select(getParentSelections))
            .mergeMap(([[action, tabId], parentSelectionsPlot]) => {
                // TODO find a way to avoid this magic
                return this.dataService.getXAxisOptions(tabId)
                    .map(formatAxisOptions)
                    .withLatestFrom(
                        this._store.select(getCurrentNewXAxisOption),
                        this._store.select(getCurrentSubPlotNewXAxisOption),
                        this._store.select(getSubPlotTabId),
                        this._store.select(getCurrentNewYAxisOption),
                        this._store.select(getCurrentNewColorBy)
                    )
                    .map(([xAxisOptions, currentXAxisOption, currentSubPlotNewXAxisOption,
                              subPlotTabId, currentYAxisOption, currentColorBy]) => {
                        const currentNewXAxisOption = subPlotTabId && tabId === subPlotTabId ?
                            currentSubPlotNewXAxisOption : currentXAxisOption;
                        let xAxisOption;
                        if (currentYAxisOption && currentColorBy) {
                            this.trellisingDispatcher.cacheColorByForYAxis(currentYAxisOption, currentColorBy, tabId);

                        }
                        const isPresent = xAxisOptions.options.some(option => {
                            return currentNewXAxisOption ?
                                currentNewXAxisOption.get('groupByOption') === option.groupByOption :
                                false;
                        });
                        this._store.dispatch(new FetchGroupByOptions({
                            tabId,
                            groupBy: 'X_AXIS',
                            groupByOptions: xAxisOptions
                        }));

                        if (isPresent || isEmpty(xAxisOptions.options) && !isUndefined(currentNewXAxisOption)) {
                            xAxisOption = currentNewXAxisOption.toJS();
                            return {tabId, xAxisOption, parentSelectionsPlot};
                        } else {

                            xAxisOption = <GroupBySetting>DefaultAxisService.initialX(tabId, this.isOngoingDataset(), xAxisOptions);
                            this._store.dispatch(new ChangeGroupBySetting({
                                tabId,
                                groupBy: 'X_AXIS',
                                groupBySetting: xAxisOption
                            }));

                            return {tabId, xAxisOption, parentSelectionsPlot};
                        }
                    });
            })
            .mergeMap(({tabId, xAxisOption, parentSelectionsPlot}) => {
                return this.dataService.getYAxisOptions(tabId)
                    .map(formatAxisOptions)
                    .withLatestFrom(this._store.select(getCurrentNewYAxisOption),
                        this._store.select(getFiltersUpdateOnYAxisChangeRequired),
                        this._store.select(getPreserveYAxisIfItsNotPresent))
                    .map(([yAxisOptions, currentYAxisOption, updateFilters, preserveYAxis]) => {
                        let yAxisOption;

                        const isYAxisPresent = yAxisOptions.options.some((option: any) => {
                            if (currentYAxisOption) {
                                const groupByOption = currentYAxisOption.get('groupByOption');
                                preserveYAxis = preserveYAxis && groupByOption !== NO_AVAILABLE_PARAMETERS;
                                return option.groupByOption.trellisOptions
                                    ? groupByOption === option.groupByOption || option.groupByOption.trellisOptions.includes(groupByOption)
                                    : groupByOption === option.groupByOption;

                            } else {
                                return false;
                            }
                        });

                        this._store.dispatch(new FetchGroupByOptions({
                            tabId,
                            groupBy: 'Y_AXIS',
                            groupByOptions: yAxisOptions
                        }));

                        const useCurrentYAxis = currentYAxisOption && (isYAxisPresent || isEmpty(yAxisOptions.options) || preserveYAxis);
                        if (useCurrentYAxis) {
                            yAxisOption = currentYAxisOption.toJS();
                            return {tabId, xAxisOption, yAxisOption, parentSelectionsPlot};
                        } else {
                            yAxisOption = <GroupBySetting>DefaultAxisService.initialY(tabId, yAxisOptions);

                            this._store.dispatch(
                                new ChangeGroupBySetting({
                                    tabId,
                                    groupBy: 'Y_AXIS',
                                    groupBySetting: yAxisOption
                                })
                            );
                        }
                        if (updateFilters && currentYAxisOption && !isYAxisPresent && !parentSelectionsPlot) {
                            this.filtersUtils.getFilterModelById(tabId).makeFilterRequest(false);
                        }

                        return {tabId, xAxisOption, yAxisOption, parentSelectionsPlot};
                    });
            })
            .withLatestFrom(this._store.select(getTrellisingRequired))
            .mergeMap(([{tabId, xAxisOption, yAxisOption, parentSelectionsPlot}, trellisingRequired]: any) => {
                // TODO check if trellising required and add MANDATORY_TRELLIS
                if (trellisingRequired) {
                    return this.dataService.getTrellisOptions(tabId, fromJS(yAxisOption))
                        .map((trellisOptions: TrellisOptions<any>[]) => {
                            return {
                                tabId, xAxisOption, yAxisOption,
                                trellisOptions: trellisOptions.map(option => {
                                    (<any>option).category = 'MANDATORY_TRELLIS';
                                    return option;
                                }),
                                parentSelectionsPlot
                            };
                        });
                } else {
                    return Observable.of({tabId, xAxisOption, yAxisOption, trellisOptions: [], parentSelectionsPlot});
                }
            })
            .withLatestFrom(
                this._store.select(getColorByRequired),
                this._store.select(getCurrentPlotSettings)
            )
            .mergeMap(([{tabId, xAxisOption, yAxisOption, trellisOptions, parentSelectionsPlot},
                           colorByRequired, currentPlotSettings]: any) => {
                //avoid second condition
                //without second condition it will not work when we need color by in sub plot
                if (colorByRequired && !parentSelectionsPlot) {

                    return this.dataService.getColorByOptions(tabId, yAxisOption, currentPlotSettings)
                        .map((colorByOptions: TrellisOptions<any>[]) => {
                            return colorByOptions.map(option => {
                                // TODO it's a frontend-filled field, so it's absent in the backend-based type.
                                // TODO some better solution is needed here, thouch.
                                (<any>option).category = 'NON_MANDATORY_SERIES';
                                return option;
                            });
                        })
                        .map((colorByOptions: TrellisOptions<any>[]) => {
                            this.updateTrellisOptions(colorByOptions.concat(trellisOptions), '', tabId);
                            return {tabId, xAxisOption, yAxisOption, trellisOptions, parentSelectionsPlot};
                        });

                } else {
                    this.updateTrellisOptions(trellisOptions, '');
                    return Observable.of({tabId, xAxisOption, yAxisOption, trellisOptions, parentSelectionsPlot});
                }
            })
            .withLatestFrom(
                this._store.select(getCurrentLimit),
                this._store.select(getCurrentOffset),
                this._store.select(getCurrentNewColorBy),
                this._store.select(getCurrentPlotSettings),
                this._store.select(getFilterTrellisByOptions)
            )
            .mergeMap(([{
                tabId, xAxisOption, yAxisOption, trellisOptions, parentSelectionsPlot
            }, limit, offset, colorByOptions, plotSettings, filterTrellisByOptions]: any) => {
                const settings = constructDataRequestPayload(
                    xAxisOption,
                    yAxisOption,
                    trellisOptions,
                    limit,
                    offset,
                    colorByOptions,
                    plotSettings,
                    filterTrellisByOptions,
                    parentSelectionsPlot
                );
                return this.dataService.getPlotData(tabId, yAxisOption, settings)
                    .map((payload: List<IPlot>) => {
                        this.trellisingDispatcher.clearSelection(tabId);
                        this.updatePlotOptions(payload, tabId, true);
                        this._store.dispatch(TrellisingActionCreator.makeLocalInitialisingAction(true, tabId));
                        return this.trellisingActionCreator.makeLoadingAction(false, tabId);
                    });
            });
    }