<commit_before>import Morph from 'src/components/widgets/lively-morph.js';
import focalStorage from 'src/external/focalStorage.js';

export default class LivelyCacheViewer extends Morph {
  async initialize() {
    this.windowTitle = "LivelyCacheViewer";
    
    // Register listener to receive data from serviceworker
    window.serviceWorkerMessageHandlers['cacheViewer'] = (event) => {
      const message = event.data;
      
      // Only handle notifications here
      if (message.type === 'dataResponse') {
        this._receiveFromServiceWorker(message.command, message.data);
      }
    };
    
    // Set up search
    this._currentSearch = '';
    var searchInput = this.get("#search");
    $(searchInput).keyup(event => {
      if (event.keyCode == 13) { // ENTER
        this._currentSearch = searchInput.value;
        this._showUpdatedCacheKeys();
      }
    });
    
    // Set up mode selection
    const instanceName = lively4url.split("/").pop();
    const cacheModeKey = `${instanceName}-cacheMode`;
    let modeSelect = this.get('#modeSelect');
    focalStorage.getItem(cacheModeKey).then(
      (cacheMode) => {
        if (cacheMode !== null) {
          modeSelect.selectedIndex = parseInt(cacheMode);
        }
      }
    )
    $(modeSelect).change((event) => {
      let value = event.target.selectedIndex;
      focalStorage.setItem(cacheModeKey, value).then(() => {
        if (value == 3) {
          this._showLoadingScreen(true);
          this._requestFromServiceWorker('preloadFull');
        }
      });
    })
    
    // Keep a copy so we don't have to ask the serviceworker for every search
    this._cacheKeys = [];
    
    lively.html.registerButtons(this);
    this._requestFromServiceWorker('cacheKeys');
  }
  
  /*
   * Component callbacks
   */
  onRefreshButton() {
    this._showLoadingScreen(true);
    this._requestFromServiceWorker('cacheKeys');
  }
  
  /*
   * Methods to update UI
   */
  
  _showLoadingScreen(visible) {
    let overlay = this.get('#overlay');
    if (visible) {<commit_msg>Change cache viewer window title, refactor<commit_after>import Morph from 'src/components/widgets/lively-morph.js';
import focalStorage from 'src/external/focalStorage.js';

export default class LivelyCacheViewer extends Morph {
  async initialize() {
    this.windowTitle = "Lively Cache Viewer";
    // Keep a copy so we don't have to ask the serviceworker for every search
    this._cacheKeys = [];
    lively.html.registerButtons(this);
    
    // Register listener to receive data from serviceworker
    window.serviceWorkerMessageHandlers['cacheViewer'] = (event) => {
      const message = event.data;
      
      // Only handle notifications here
      if (message.type === 'dataResponse') {
        this._receiveFromServiceWorker(message.command, message.data);
      }
    };
    
    this._setUpSearch();
    this._setUpModeSelection();    
    this._requestFromServiceWorker('cacheKeys');
  }
  
  /**
   * Set up search
   */
  _setUpSearch() {
    this._currentSearch = '';
    var searchInput = this.get("#search");
    $(searchInput).keyup(event => {
      if (event.keyCode != 13) return; // ENTER
      
      this._currentSearch = searchInput.value;
      this._showUpdatedCacheKeys();
    });
  }
  
  /**
   * Set up mode selection
   */
  _setUpModeSelection() {
    const instanceName = lively4url.split("/").pop();
    const cacheModeKey = `${instanceName}-cacheMode`;
    let modeSelect = this.get('#modeSelect');
    focalStorage.getItem(cacheModeKey).then(
      (cacheMode) => {
        if (cacheMode !== null) {
          modeSelect.selectedIndex = parseInt(cacheMode);
        }
      }
    )
    $(modeSelect).change((event) => {
      let value = event.target.selectedIndex;
      focalStorage.setItem(cacheModeKey, value).then(() => {
        if (value == 3) {
          this._showLoadingScreen(true);
          this._requestFromServiceWorker('preloadFull');
        }
      });
    })
  }
  
  /**
   * Component callbacks
   */
  onRefreshButton() {
    this._showLoadingScreen(true);
    this._requestFromServiceWorker('cacheKeys');
  }
  
  /**
   * Methods to update UI
   */
  _showLoadingScreen(visible) {
    let overlay = this.get('#overlay');
    if (visible) {
{'fraction_non_alphanumeric': 0.12969924812030076, 'fraction_numerical': 0.0018796992481203006, 'mean_word_length': 2.7017391304347824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4206732', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1230, 'n_words': 246}