<commit_before>declare const System: any;
import { UIRouter, Glob } from '@uirouter/core';

import { bootstrapModule } from './bootstrap.module';
import { paramChangedHelper } from 'core/bootstrap';

/**
 * Toggles the @uirouter/visualizer based on query parameter `vis` changing
 * Type javascript:vis() in the browser url or add `&vis=true` to the spinnaker URL
 */
bootstrapModule.run(($uiRouter: UIRouter) => {
  'ngInject';

  const launchVisualizer = () => {
    // Auto-collapse certain states with lots of children
    const collapseGlobs = ['home.*', 'home.*.application.*', 'home.*.application.insight.*'].map(globStr => new Glob(globStr));
    const collapsedStates = $uiRouter.stateRegistry.get().filter(state => collapseGlobs.some(glob => glob.matches(state.name)));
    collapsedStates.forEach(state => (state.$$state() as any)._collapsed = true);

    return System.import('@uirouter/visualizer').then((vis: any) => $uiRouter.plugin(vis.Visualizer));
  };

  const toggleVisualizer = (enabled: boolean) => {
    if (enabled) {
      return launchVisualizer();
    } else {
      const plugin = $uiRouter.getPlugin('visualizer');
      plugin && $uiRouter.dispose(plugin);
    }
  };

  (window as any).vis = launchVisualizer;
  $uiRouter.transitionService.onBefore({}, paramChangedHelper('vis', toggleVisualizer));
});
<commit_msg>fix(core/vis): Fix visualizer toggle to clean up old copies of the visualizer (#4672)<commit_after>declare const System: any;
import { UIRouter, Glob, UIRouterPlugin } from '@uirouter/core';

import { bootstrapModule } from './bootstrap.module';
import { paramChangedHelper } from 'core/bootstrap';

/**
 * Toggles the @uirouter/visualizer based on query parameter `vis` changing
 * Type javascript:vis() in the browser url or add `&vis=true` to the spinnaker URL
 */
bootstrapModule.run(($uiRouter: UIRouter) => {
  'ngInject';

  let visualizerEnabled = false;
  let VisualizerPlugin: { new(): UIRouterPlugin } = null;

  const loadVisualizer = () => {
    // Auto-collapse certain states with lots of children
    const collapseGlobs = ['home.*', 'home.*.application.*', 'home.*.application.insight.*'].map(globStr => new Glob(globStr));
    const collapsedStates = $uiRouter.stateRegistry.get().filter(state => collapseGlobs.some(glob => glob.matches(state.name)));
    collapsedStates.forEach(state => (state.$$state() as any)._collapsed = true);

    return System.import('@uirouter/visualizer')
      .then((vis: any) => VisualizerPlugin = vis.Visualizer)
      .then(createVisualizer);
  };

  const createVisualizer = () => {
    if (!visualizerEnabled) {
      return;
    }

    // Cleanup any current visualizer first
    destroyVisualizer();

    if (VisualizerPlugin) {
      $uiRouter.plugin(VisualizerPlugin);
    } else {
      loadVisualizer();
    }
  };

  const destroyVisualizer = () => {
    const plugin = $uiRouter.getPlugin('visualizer');
    plugin && $uiRouter.dispose(plugin);
  };

  const toggleVisualizer = (enabled: boolean) => {
    if (enabled === visualizerEnabled) {
      return;
    }
    visualizerEnabled = enabled;

    if (enabled) {
      return createVisualizer();
    } else {
      return destroyVisualizer();
    }
  };

  (window as any).vis = createVisualizer;
  $uiRouter.transitionService.onBefore({}, paramChangedHelper('vis', toggleVisualizer));
});

{'fraction_non_alphanumeric': 0.1480597014925373, 'fraction_numerical': 0.0011940298507462687, 'mean_word_length': 4.039097744360903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '7590233', 'n_tokens_mistral': 1011, 'n_tokens_neox': 979, 'n_words': 236}