<commit_before>Ext.ns('Rwiki');

Rwiki.rootFolderName = '.';
Rwiki.currentPageName = null;

Ext.onReady(function() {
  Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

  var model = new Rwiki.Model();

  var treePanel = new Rwiki.TreePanel();
  treePanel.relayEvents(model, [model.PAGE_LOADED]);
  
  var tabPanel = new Rwiki.TabPanel();
  tabPanel.relayEvents(model, [model.PAGE_LOADED, model.PAGE_UPDATED]);

  var editorPanel = new Rwiki.EditorPanel();
  editorPanel.relayEvents(model, [model.PAGE_LOADED]);

  treePanel.on(model.PAGE_LOADED, function(data) {
    if (!data.success) return;

    var pageName = data.pageName;
    var node = treePanel.findNodeByPageName(pageName);
    node.select();
  });

  editorPanel.on(model.PAGE_LOADED, function(data) {
    if (!data.success) return;
    editorPanel.getEditor().setContent(data.raw);
  });

  tabPanel.on(model.PAGE_LOADED, function(data) {
    if (!data.success) return;

    var pageName = data.pageName;
    var tab = tabPanel.updateOrAddPageTab(pageName, data.html);
    tab.show();
  });

  // Event: node has been loaded
  model.on(model.PAGE_LOADED, function(data) {
    if (!data.success) {
      Ext.MessageBox.alert("Error!", "Can't load the page content.");
    }
  });

  // Event: folder has been created
  model.on(model.FOLDER_CREATED, function(parentFolderName, newFolderName) {
    var parentNode = treePanel.findNodeByPageName(parentFolderName);
    parentNode.reload();
  });

  // Event: page has been created
  model.on(model.NODE_CREATED, function(parentFolderName, newPageName) {
    var parentNode = treePanel.findNodeByPageName(parentFolderName);

    parentNode.reload(function() {
      // slect a new node and open a new page
      var node = treePanel.findNodeByPageName(newPageName, parentNode);
      node.select();
      tabPanel.updateOrAddPageTab(newPageName);
    });
  });

  // Event: node has been moved
  model.on(model.NODE_MOVED, function(oldNodeName, destFolderName) {
    var node = treePanel.findNodeByPageName(oldNodeName);
    var destNode = treePanel.findNodeByPageName(destFolderName);

    tabPanel.closeRelatedTabs(node);
    destNode.reload();
  });

  // Event: node has been renamed
  model.on(model.NODE_RENAMED, function(data) {
    if (data.success) {
      var node = treePanel.findNodeByPageName(data.oldNodeName);
      if (node == null) return;

      tabPanel.closeRelatedTabs(node);
      node.parentNode.reload();
    } else {
      Ext.MessageBox.alert("Error!", "Can't rename node.");
    }
  });

  // Event: node has been deleted
  model.on(model.NODE_DELETED, function(nodeName) {
    var node = treePanel.findNodeByPageName(nodeName);
    
    tabPanel.closeRelatedTabs(node);
    node.remove();
  });

  // Event: editor content changed
  editorPanel.on('contentChanged', function(content) {
    var currentTab = tabPanel.getActiveTab();
    var pageName = currentTab.getPageName();

    model.updatePage(pageName, content);
  });

  tabPanel.on(model.PAGE_UPDATED, function(data) {
    var pageName = data.pageName;
    var tab = tabPanel.findTabByPageName(pageName);
    tab.setContent(data.html);
  });

  // Event: tab has changed
  tabPanel.on('tabchange', function(tabPanel, tab) {
    var lastTabClosed = !tab;

    if (!lastTabClosed) {
      var pageName = tab.getPageName();
      model.loadPage(pageName);
    } else {
      editorPanel.editor.clearContent();
    }
  });

  // Event: TreePanel, click on a node
  treePanel.on('click', function(node, e) {
    if (node.isLeaf()) {
      var pageName = node.id;
      model.loadPage(pageName);
    }
  });

  // Event: TreePanel, a node has been moved
  treePanel.on('movenode', function(tree, node, oldParent, newParent, position) {
    var nodeName = node.id;
    var destFolderName = newParent.id;
    
    model.moveNode(nodeName, destFolderName);
  });

  // Attach listeners to the tree context menu
  var treeContextMenu = treePanel.getContextMenu();

  // Event: context menu, create folder
  treeContextMenu.on('createFolder', function(node) {
    if (node.cls == 'file') return;

    var callback = function(button, folderBaseName) {
      if (button != 'ok') return;

      var parentFolderName = node.id;
      model.createNode(parentFolderName, folderBaseName, true);
    };

    Ext.MessageBox.prompt('Create folder', 'New folder name:', callback);
  });

  // Event: context menu, create page
  treeContextMenu.on('createPage', function(node) {
    if (node.cls == 'file') return;<commit_msg>Fix problem with double event.<commit_after>Ext.ns('Rwiki');

Rwiki.rootFolderName = '.';
Rwiki.currentPageName = null;

Ext.onReady(function() {
  Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

  var model = new Rwiki.Model();

  var treePanel = new Rwiki.TreePanel();
  treePanel.relayEvents(model, [model.PAGE_LOADED]);
  
  var tabPanel = new Rwiki.TabPanel();
  tabPanel.relayEvents(model, [model.PAGE_LOADED, model.PAGE_UPDATED]);

  var editorPanel = new Rwiki.EditorPanel();
  editorPanel.relayEvents(model, [model.PAGE_LOADED]);

  treePanel.on(model.PAGE_LOADED, function(data) {
    if (!data.success) return;

    var pageName = data.pageName;
    var node = treePanel.findNodeByPageName(pageName);
    node.select();
  });

  editorPanel.on(model.PAGE_LOADED, function(data) {
    if (!data.success) return;
    editorPanel.getEditor().setContent(data.raw);
  });

  tabPanel.on(model.PAGE_LOADED, function(data) {
    if (!data.success) return;

    var pageName = data.pageName;
    var tab = tabPanel.updateOrAddPageTab(pageName, data.html);
    tab.show();
  });

  // Event: node has been loaded
  model.on(model.PAGE_LOADED, function(data) {
    if (!data.success) {
      Ext.MessageBox.alert("Error!", "Can't load the page content.");
    }
  });

  // Event: folder has been created
  model.on(model.FOLDER_CREATED, function(parentFolderName, newFolderName) {
    var parentNode = treePanel.findNodeByPageName(parentFolderName);
    parentNode.reload();
  });

  // Event: page has been created
  model.on(model.NODE_CREATED, function(parentFolderName, newPageName) {
    var parentNode = treePanel.findNodeByPageName(parentFolderName);

    parentNode.reload(function() {
      // slect a new node and open a new page
      var node = treePanel.findNodeByPageName(newPageName, parentNode);
      node.select();
      tabPanel.updateOrAddPageTab(newPageName);
    });
  });

  // Event: node has been moved
  model.on(model.NODE_MOVED, function(oldNodeName, destFolderName) {
    var node = treePanel.findNodeByPageName(oldNodeName);
    var destNode = treePanel.findNodeByPageName(destFolderName);

    tabPanel.closeRelatedTabs(node);
    destNode.reload();
  });

  // Event: node has been renamed
  model.on(model.NODE_RENAMED, function(data) {
    if (data.success) {
      var node = treePanel.findNodeByPageName(data.oldNodeName);
      if (node == null) return;

      tabPanel.closeRelatedTabs(node);
      node.parentNode.reload();
    } else {
      Ext.MessageBox.alert("Error!", "Can't rename node.");
    }
  });

  // Event: node has been deleted
  model.on(model.NODE_DELETED, function(nodeName) {
    var node = treePanel.findNodeByPageName(nodeName);
    
    tabPanel.closeRelatedTabs(node);
    node.remove();
  });

  // Event: editor content changed
  editorPanel.on('contentChanged', function(content) {
    var currentTab = tabPanel.getActiveTab();
    var pageName = currentTab.getPageName();

    model.updatePage(pageName, content);
  });

  tabPanel.on(model.PAGE_UPDATED, function(data) {
    var pageName = data.pageName;
    var tab = tabPanel.findTabByPageName(pageName);
    tab.setContent(data.html);
  });

  // Event: tab has changed
  tabPanel.on('tabchange', function(tabPanel, tab) {
    var lastTabClosed = !tab;

    if (!lastTabClosed) {
      var pageName = tab.getPageName();
      model.loadPage(pageName);
    } else {
      editorPanel.editor.clearContent();
    }
  });

  // Event: TreePanel, click on a node
  treePanel.on('click', function(node, e) {
    var pageName = node.id;
    tabPanel.updateOrAddPage(pageName);
  });

  // Event: TreePanel, a node has been moved
  treePanel.on('movenode', function(tree, node, oldParent, newParent, position) {
    var nodeName = node.id;
    var destFolderName = newParent.id;
    
    model.moveNode(nodeName, destFolderName);
  });

  // Attach listeners to the tree context menu
  var treeContextMenu = treePanel.getContextMenu();

  // Event: context menu, create folder
  treeContextMenu.on('createFolder', function(node) {
    if (node.cls == 'file') return;

    var callback = function(button, folderBaseName) {
      if (button != 'ok') return;

      var parentFolderName = node.id;
      model.createNode(parentFolderName, folderBaseName, true);
    };

    Ext.MessageBox.prompt('Create folder', 'New folder name:', callback);
  });

  // Event: context menu, create page
  treeContextMenu.on('createPage', function(node) {
    if (node.cls == 'file') return;
{'fraction_non_alphanumeric': 0.13309671694764863, 'fraction_numerical': 0.0, 'mean_word_length': 3.6455435342606903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6616801', 'n_tokens_mistral': 2948, 'n_tokens_neox': 2860, 'n_words': 591}