<commit_before>﻿import ConverterFactory = require('./converter/ConverterFactory');
import Crumb = require('./config/Crumb');
import CrumbTrailManager = require('./CrumbTrailManager');
import Dialog = require('./config/Dialog');
import IDialog = require('./config/IDialog');
import HistoryAction = require('./history/HistoryAction');
import IHistoryManager = require('./history/IHistoryManager');
import NavigationData = require('./NavigationData');
import NavigationSettings = require('./NavigationSettings');
import INavigationSettings = require('./INavigationSettings');
import ReturnDataManager = require('./ReturnDataManager');
import State = require('./config/State');
import IState = require('./config/IState');
import StateContext = require('./StateContext');
import StateInfoConfig = require('./StateInfoConfig');
import Transition = require('./config/Transition');
import ITransition = require('./config/ITransition');

class StateController {
    private NAVIGATE_HANDLER_ID = 'navigateHandlerId';
    private navigateHandlerId: number = 1;
    private navigateHandlers: { [index: string]: (oldState: State, state: State, data: any) => void } = {};
    private settings: NavigationSettings = new NavigationSettings(); 
    private converterFactory: ConverterFactory = new ConverterFactory();
    stateContext: StateContext = new StateContext();
    historyManager: IHistoryManager;
    dialogs: { [index: string]: Dialog } = {};
    _dialogs: Dialog[] = [];
    
    constructor(dialogs?: IDialog<string, IState<ITransition<string>[]>[]>[], settings?: INavigationSettings) {
        if (dialogs)
            this.configure(dialogs, settings);
    }
    
    configure(dialogs: IDialog<string, IState<ITransition<string>[]>[]>[], settings?: INavigationSettings) {
        for(var setting in settings)
            this.settings[setting] = settings[setting];
        var oldHistoryManager = this.historyManager;
        this.historyManager = this.settings.historyManager;
        if (this.historyManager !== oldHistoryManager) {
            if (oldHistoryManager)
                oldHistoryManager.stop();
            this.historyManager.init(() => {
                if (this.stateContext.url === this.historyManager.getCurrentUrl())
                    return;
                this.navigateLink(this.historyManager.getCurrentUrl(), undefined, true);
            }, this.settings.applicationPath);
        }
        var config = StateInfoConfig.build(dialogs, this.settings, this.converterFactory);
        this._dialogs = config._dialogs;
        this.dialogs = config.dialogs;
        this.settings.router.addRoutes(this._dialogs);
    }

    setStateContext(state: State, url: string) {
        try {
            this.setOldStateContext();
            this.stateContext.state = state;
            this.stateContext.url = url;
            this.stateContext.dialog = state.parent;
            this.stateContext.title = state.title;
            var { data, separableData } = state.stateHandler.getNavigationData(this.settings.router, state, url);
            this.stateContext.data = this.parseData(data, state, separableData);
            this.stateContext.previousState = null;
            this.stateContext.previousDialog = null;
            this.stateContext.previousData = {};
            this.stateContext.crumbTrail = this.settings.crumbTrailPersister.load(data[this.settings.crumbTrailKey]);
            var uncombined = !!data[this.settings.previousStateIdKey];
            this.setPreviousStateContext(uncombined, data);
            CrumbTrailManager.buildCrumbTrail(this.stateContext, this.settings, this.converterFactory, this._dialogs, uncombined);
            this.stateContext.crumbs = CrumbTrailManager.getCrumbs(this.stateContext, this.settings, this.converterFactory, this._dialogs, true, this.settings.combineCrumbTrail);
        } catch (e) {
            throw new Error('The Url is invalid\n' + e.message);
        }
    }

    clearStateContext() {<commit_msg>Always re-init history manager on new config<commit_after>﻿import ConverterFactory = require('./converter/ConverterFactory');
import Crumb = require('./config/Crumb');
import CrumbTrailManager = require('./CrumbTrailManager');
import Dialog = require('./config/Dialog');
import IDialog = require('./config/IDialog');
import HistoryAction = require('./history/HistoryAction');
import IHistoryManager = require('./history/IHistoryManager');
import NavigationData = require('./NavigationData');
import NavigationSettings = require('./NavigationSettings');
import INavigationSettings = require('./INavigationSettings');
import ReturnDataManager = require('./ReturnDataManager');
import State = require('./config/State');
import IState = require('./config/IState');
import StateContext = require('./StateContext');
import StateInfoConfig = require('./StateInfoConfig');
import Transition = require('./config/Transition');
import ITransition = require('./config/ITransition');

class StateController {
    private NAVIGATE_HANDLER_ID = 'navigateHandlerId';
    private navigateHandlerId: number = 1;
    private navigateHandlers: { [index: string]: (oldState: State, state: State, data: any) => void } = {};
    private settings: NavigationSettings = new NavigationSettings(); 
    private converterFactory: ConverterFactory = new ConverterFactory();
    stateContext: StateContext = new StateContext();
    historyManager: IHistoryManager;
    dialogs: { [index: string]: Dialog } = {};
    _dialogs: Dialog[] = [];
    
    constructor(dialogs?: IDialog<string, IState<ITransition<string>[]>[]>[], settings?: INavigationSettings) {
        if (dialogs)
            this.configure(dialogs, settings);
    }
    
    configure(dialogs: IDialog<string, IState<ITransition<string>[]>[]>[], settings?: INavigationSettings) {
        for(var setting in settings)
            this.settings[setting] = settings[setting];
        if (this.historyManager)
            this.historyManager.stop();
        this.historyManager = this.settings.historyManager;
        this.historyManager.init(() => {
            if (this.stateContext.url === this.historyManager.getCurrentUrl())
                return;
            this.navigateLink(this.historyManager.getCurrentUrl(), undefined, true);
        }, this.settings.applicationPath);
        var config = StateInfoConfig.build(dialogs, this.settings, this.converterFactory);
        this._dialogs = config._dialogs;
        this.dialogs = config.dialogs;
        this.settings.router.addRoutes(this._dialogs);
    }

    setStateContext(state: State, url: string) {
        try {
            this.setOldStateContext();
            this.stateContext.state = state;
            this.stateContext.url = url;
            this.stateContext.dialog = state.parent;
            this.stateContext.title = state.title;
            var { data, separableData } = state.stateHandler.getNavigationData(this.settings.router, state, url);
            this.stateContext.data = this.parseData(data, state, separableData);
            this.stateContext.previousState = null;
            this.stateContext.previousDialog = null;
            this.stateContext.previousData = {};
            this.stateContext.crumbTrail = this.settings.crumbTrailPersister.load(data[this.settings.crumbTrailKey]);
            var uncombined = !!data[this.settings.previousStateIdKey];
            this.setPreviousStateContext(uncombined, data);
            CrumbTrailManager.buildCrumbTrail(this.stateContext, this.settings, this.converterFactory, this._dialogs, uncombined);
            this.stateContext.crumbs = CrumbTrailManager.getCrumbs(this.stateContext, this.settings, this.converterFactory, this._dialogs, true, this.settings.combineCrumbTrail);
        } catch (e) {
            throw new Error('The Url is invalid\n' + e.message);
        }
    }

    clearStateContext() {
{'fraction_non_alphanumeric': 0.12409408773045137, 'fraction_numerical': 0.0002542911633820725, 'mean_word_length': 3.5051546391752577, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '346720', 'n_tokens_mistral': 2062, 'n_tokens_neox': 2031, 'n_words': 402}