<commit_before>/*global $,cyberDojo*/

var cyberDojo = (function(cd, $) {
  "use strict";

  var makeInput = function(name, filename) {
    var input = $('<input>', {
      type: 'text',
      id: name+'-filename',
      'name': name+'-filename',
      value: filename
    });
    if (name == 'delete') {
      input.attr('disabled', 'disabled');
    }
    return input;
  };

  // - - - - - - - - - - - - -

  var openDialog = function(title, okDisabled, okClicked, input, avatar) {
    var ok = {
      text: 'ok',
      id: 'file-ok',
      disabled: okDisabled(),
      click: function() {
        okClicked();
        $(this).remove();
      }
    };
    var cancel = {
      text: 'cancel',
      click: function() { $(this).remove(); }
    };
    var img = $('<img>', {
        'src': '/images/avatars/' + avatar + '.jpg',
      'style': 'width:214px'
    });
    var div = $('<div>');
    div.append(input);
    div.append(img);
    var dialog = div.dialog({
      closeOnEscape: true,
      close: function() { $(this).remove(); },
  	  title: cd.dialogTitle(title + '&nbsp;' + 'file'),
    	autoOpen: false,
      width: 250,
  	  modal: true,
      buttons: [ ok, cancel ]
    });

  	input.keyup(function(event) {
      var ok = $('#file-ok');
      var newFilename = $.trim(input.val());
      event.preventDefault();
      if (cd.isValidFilename(newFilename))  {
        ok.button('enable');
        if (event.keyCode == $.ui.keyCode.ENTER) {
          okClicked();
          ok.closest('.ui-dialog').remove();
        }
      } else {
        ok.button('disable');
      }
    });

    dialog.dialog('open');
  };

  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // delete file
  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  cd.deleteFile = function(avatar) {
    var filename = cd.currentFilename();
    var input = makeInput('delete', filename);
    var okClicked = function() { cd.doDelete(filename); };
    var okInitiallyDisabled = function() { return false; };
    openDialog('delete', okInitiallyDisabled, okClicked, input, avatar);
  };

  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // new file
  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  cd.newFile = function(avatar) {
    var newFilename = 'filename' + cd.extensionFilename();
    var input = makeInput('new', newFilename);
    var okInitiallyDisabled = function() { return !cd.isValidFilename(newFilename); };
    var okClicked = function() {
      var newFilename = $.trim(input.val());
      cd.newFileContent(newFilename, '');
    };
    openDialog('new', okInitiallyDisabled, okClicked, input, avatar);
    input[0].setSelectionRange(0, newFilename.length);
  };

  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // rename file
  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  cd.renameFile = function(avatar) {
    var oldFilename = cd.currentFilename();
    var input = makeInput('rename', oldFilename);
    var okInitiallyDisabled = function() { return !cd.isValidFilename(oldFilename); };
    var okClicked = function() {
      var newFilename = $.trim(input.val());
      cd.renameFileFromTo(oldFilename, newFilename);
    };
    openDialog('rename', okInitiallyDisabled, okClicked, input, avatar);

    var end = oldFilename.lastIndexOf('.');
    if (end == -1) {
      end = oldFilename.length;
    }
    input[0].setSelectionRange(0, end);
  };

  // -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

  cd.renameFileFromTo = function(oldFilename, newFilename) {
    var oldFile = cd.fileContentFor(oldFilename);
    var content = oldFile.val();
    var scrollTop = oldFile.scrollTop();
    var scrollLeft = oldFile.scrollLeft();
    var caretPos = oldFile.caret();
    $(oldFile).closest('tr').remove();

    cd.newFileContent(newFilename, content);
    cd.rebuildFilenameList();
    cd.loadFile(newFilename);
    var newFile = cd.fileContentFor(newFilename);
    // Note that doing the seemingly equivalent
    //   fc.scrollTop(top);
    //   fc.scrollLeft(left);
    // here does _not_ work. I use animate instead with a
    // very fast duration==1 and that does work!
    newFile.animate({scrollTop:scrollTop, scrollLeft:scrollLeft}, 1);
    newFile.caret(caretPos);
  };<commit_msg>file new/rename/delete refactoring<commit_after>/*global $,cyberDojo*/

var cyberDojo = (function(cd, $) {
  "use strict";

  var makeInput = function(name, filename) {
    var input = $('<input>', {
      type: 'text',
      id: name+'-filename',
      'name': name+'-filename',
      value: filename
    });
    if (name == 'delete') {
      input.attr('disabled', 'disabled');
    }
    return input;
  };

  // - - - - - - - - - - - - -

  var openDialog = function(name, initialFilename, okInitiallyDisabled, okClicked, avatar) {
    var input = makeInput(name, initialFilename);
    var ok = {
      text: 'ok',
      id: 'file-ok',
      disabled: okInitiallyDisabled,
      click: function() {
        var newFilename = $.trim(input.val());
        okClicked(newFilename);
        $(this).remove();
      }
    };
    var cancel = {
      text: 'cancel',
      click: function() { $(this).remove(); }
    };
    var img = $('<img>', {
        'src': '/images/avatars/' + avatar + '.jpg',
      'style': 'width:214px'
    });
    var div = $('<div>');
    div.append(input);
    div.append(img);
    var dialog = div.dialog({
      closeOnEscape: true,
      close: function() { $(this).remove(); },
  	  title: cd.dialogTitle(name + '&nbsp;' + 'file'),
    	autoOpen: false,
      width: 250,
  	  modal: true,
      buttons: [ ok, cancel ]
    });

  	input.keyup(function(event) {
      var ok = $('#file-ok');
      var newFilename = $.trim(input.val());
      event.preventDefault();
      if (cd.isValidFilename(newFilename))  {
        ok.button('enable');
        if (event.keyCode == $.ui.keyCode.ENTER) {
          okClicked(newFilename);
          ok.closest('.ui-dialog').remove();
        }
      } else {
        ok.button('disable');
      }
    });

    var end = initialFilename.lastIndexOf('.');
    if (end == -1) {
      end = initialFilename.length;
    }
    input[0].setSelectionRange(0, end);

    dialog.dialog('open');
  };


  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // delete file
  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  cd.deleteFile = function(avatar) {
    var initialFilename = cd.currentFilename();
    var okInitiallyDisabled = false;
    var okClicked = function(filename) { cd.doDelete(filename); };
    openDialog('delete', initialFilename, okInitiallyDisabled, okClicked, avatar);
  };

  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // new file
  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  cd.newFile = function(avatar) {
    var initialFilename = 'filename' + cd.extensionFilename();
    var okInitiallyDisabled = !cd.isValidFilename(initialFilename);
    var okClicked = function(newFilename) { cd.newFileContent(newFilename, ''); };
    openDialog('new', initialFilename, okInitiallyDisabled, okClicked, avatar);
  };

  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  // rename file
  //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  cd.renameFile = function(avatar) {
    var oldFilename = cd.currentFilename();
    var okInitiallyDisabled = true;
    var okClicked = function(newFilename) { cd.renameFileFromTo(oldFilename, newFilename); };
    openDialog('rename', oldFilename, okInitiallyDisabled, okClicked, avatar);
  };

  // -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

  cd.renameFileFromTo = function(oldFilename, newFilename) {
    var oldFile = cd.fileContentFor(oldFilename);
    var content = oldFile.val();
    var scrollTop = oldFile.scrollTop();
    var scrollLeft = oldFile.scrollLeft();
    var caretPos = oldFile.caret();
    $(oldFile).closest('tr').remove();

    cd.newFileContent(newFilename, content);
    cd.rebuildFilenameList();
    cd.loadFile(newFilename);
    var newFile = cd.fileContentFor(newFilename);
    // Note that doing the seemingly equivalent
    //   fc.scrollTop(top);
    //   fc.scrollLeft(left);
    // here does _not_ work. I use animate instead with a
    // very fast duration==1 and that does work!
    newFile.animate({scrollTop:scrollTop, scrollLeft:scrollLeft}, 1);
    newFile.caret(caretPos);
  };
{'fraction_non_alphanumeric': 0.19522006625650734, 'fraction_numerical': 0.002839564600094652, 'mean_word_length': 1.915833045877889, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2979224', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2831, 'n_words': 501}