<commit_before>var CodeMirror = require('codemirror');
var $ = require('jquery');
var _ = require('underscore');
var queue = require('queue-async');
var jsyaml = require('js-yaml');
var patch = require('../../vendor/liquid.patch');
var Handsontable = require('handsontable');
var Papa = require('papaparse');

var ModalView = require('./modal');
var marked = require('marked');
var diff = require('diff');
var Backbone = require('backbone');
var File = require('../models/file');
var HeaderView = require('./header');
var ToolbarView = require('./toolbar');
var MetadataView = require('./metadata');
var auth = require('../config');
var util = require('../util');
var upload = require('../upload');
var cookie = require('../cookie');
var templates = require('../../templates');

module.exports = Backbone.View.extend({
  id: 'post',

  template: templates.file,

  subviews: {},

  initialize: function(options) {
    var app = options.app;
    app.loader.start();

    // Patch Liquid
    patch.apply(this);

    this.app = app;
    this.branch = options.branch || options.repo.get('default_branch');
    this.branches = options.branches;
    this.mode = options.mode;
    this.nav = options.nav;
    this.path = options.path || '';
    this.repo = options.repo;
    this.router = options.router;
    this.sidebar = options.sidebar;

    // Set the active nav element established by this.mode
    this.nav.setFileState(this.mode);

    // Events from vertical nav
    this.listenTo(this.nav, 'edit', this.edit);
    this.listenTo(this.nav, 'blob', this.blob);
    this.listenTo(this.nav, 'meta', this.meta);
    this.listenTo(this.nav, 'settings', this.settings);
    this.listenTo(this.nav, 'save', this.showDiff);

    // Events from sidebar
    this.listenTo(this.sidebar, 'destroy', this.destroy);
    this.listenTo(this.sidebar, 'toggle-editor', this.toggleEditor);
    this.listenTo(this.sidebar, 'draft', this.draft);
    this.listenTo(this.sidebar, 'cancel', this.cancel);
    this.listenTo(this.sidebar, 'confirm', this.updateFile);
    this.listenTo(this.sidebar, 'translate', this.translate);

    // Stash editor and metadataEditor content to sessionStorage on pagehide event
    $(window).on('pagehide', this.stashFile);

    // Prevent exit when there are unsaved changes
    // jQuery won't bind to 'beforeunload' event
    // e.returnValue for Firefox compatibility
    // https://developer.mozilla.org/en-US/docs/Web/Reference/Events/beforeunload
    window.onbeforeunload = (function(e) {
      if (this.dirty) {
        var message = t('actions.unsaved');
        (e || window.event).returnValue = message;

        return message;
      }
    }).bind(this);

    var _this = this;
    var error = function (model, xhr) {
      options.router.error(xhr);
    };
    options.branches.fetch({
      success: function (branches, resp) {
        var files = branches.findWhere({ name: _this.branch }).files;
        files.fetch({
          success: _this.setModel.bind(_this),
          error: error
        });
        _this.collection = files;
      },
      error: error
    });
  },

  setModel: function() {

    // Set model either by calling directly for new File models
    // or by filtering collection for existing File models
    switch(this.mode) {
      case 'edit':
      case 'blob':
        this.model = this.collection.findWhere({ path: this.path });
        break;
      case 'preview':
        this.model = this.collection.findWhere({ path: this.path });
        if (!this.model) {
          // We may be trying to preview a new file that only has
          // stashed information lets check and create a dummy model
          var previewPath = this.absolutePathFromComponents (
            this.repo.get('owner').login,
            this.repo.get('name'),
            this.branch,
            this.path
          );
          if (this.getStashForPath(previewPath)) {
            this.model = this.newEmptyFile();
          }
        }
        break;
      case 'new':
        this.model = this.newEmptyFile();
        break;
    }

    if (!this.model) {
      this.router.notify(
        t('notification.error.exists'), undefined,
        [{
          'title': t('notification.create'),
          'className': 'create',
          'link': '#'
        },
        {
          'title': t('notification.back'),
          'link': '#' + _.compact([
            this.repo.get('owner').login,
            this.repo.get('name'),
            'tree',
            this.branch,
            util.extractFilename(this.path)[0]
          ]).join('/')
        }]
      );
    }

    else {
      var defaults = this.collection.defaults;
      if (defaults) {
        this.model.set('defaults', defaults[this.nearestPath(this.model.get('path'), defaults)]);
      }
      // Render on complete to render even if model does not exist on remote yet
      this.listenToOnce(this.model, 'change', this.render);
      this.model.fetch();
    }

    // TODO set
    this.app.loader.done();
  },

  newEmptyFile: function() {
    return new File({
      branch: this.branches.findWhere({ name: this.branch }),
      collection: this.collection,
      path: this.path,
      repo: this.repo<commit_msg>Don't fetch if the file is brand new<commit_after>var CodeMirror = require('codemirror');
var $ = require('jquery');
var _ = require('underscore');
var queue = require('queue-async');
var jsyaml = require('js-yaml');
var patch = require('../../vendor/liquid.patch');
var Handsontable = require('handsontable');
var Papa = require('papaparse');

var ModalView = require('./modal');
var marked = require('marked');
var diff = require('diff');
var Backbone = require('backbone');
var File = require('../models/file');
var HeaderView = require('./header');
var ToolbarView = require('./toolbar');
var MetadataView = require('./metadata');
var auth = require('../config');
var util = require('../util');
var upload = require('../upload');
var cookie = require('../cookie');
var templates = require('../../templates');

module.exports = Backbone.View.extend({
  id: 'post',

  template: templates.file,

  subviews: {},

  initialize: function(options) {
    var app = options.app;
    app.loader.start();

    // Patch Liquid
    patch.apply(this);

    this.app = app;
    this.branch = options.branch || options.repo.get('default_branch');
    this.branches = options.branches;
    this.mode = options.mode;
    this.nav = options.nav;
    this.path = options.path || '';
    this.repo = options.repo;
    this.router = options.router;
    this.sidebar = options.sidebar;

    // Set the active nav element established by this.mode
    this.nav.setFileState(this.mode);

    // Events from vertical nav
    this.listenTo(this.nav, 'edit', this.edit);
    this.listenTo(this.nav, 'blob', this.blob);
    this.listenTo(this.nav, 'meta', this.meta);
    this.listenTo(this.nav, 'settings', this.settings);
    this.listenTo(this.nav, 'save', this.showDiff);

    // Events from sidebar
    this.listenTo(this.sidebar, 'destroy', this.destroy);
    this.listenTo(this.sidebar, 'toggle-editor', this.toggleEditor);
    this.listenTo(this.sidebar, 'draft', this.draft);
    this.listenTo(this.sidebar, 'cancel', this.cancel);
    this.listenTo(this.sidebar, 'confirm', this.updateFile);
    this.listenTo(this.sidebar, 'translate', this.translate);

    // Stash editor and metadataEditor content to sessionStorage on pagehide event
    $(window).on('pagehide', this.stashFile);

    // Prevent exit when there are unsaved changes
    // jQuery won't bind to 'beforeunload' event
    // e.returnValue for Firefox compatibility
    // https://developer.mozilla.org/en-US/docs/Web/Reference/Events/beforeunload
    window.onbeforeunload = (function(e) {
      if (this.dirty) {
        var message = t('actions.unsaved');
        (e || window.event).returnValue = message;

        return message;
      }
    }).bind(this);

    var _this = this;
    var error = function (model, xhr) {
      options.router.error(xhr);
    };
    options.branches.fetch({
      success: function (branches, resp) {
        var files = branches.findWhere({ name: _this.branch }).files;
        files.fetch({
          success: _this.setModel.bind(_this),
          error: error
        });
        _this.collection = files;
      },
      error: error
    });
  },

  setModel: function() {

    // Set model either by calling directly for new File models
    // or by filtering collection for existing File models
    switch(this.mode) {
      case 'edit':
      case 'blob':
        this.model = this.collection.findWhere({ path: this.path });
        break;
      case 'preview':
        this.model = this.collection.findWhere({ path: this.path });
        if (!this.model) {
          // We may be trying to preview a new file that only has
          // stashed information lets check and create a dummy model
          var previewPath = this.absolutePathFromComponents (
            this.repo.get('owner').login,
            this.repo.get('name'),
            this.branch,
            this.path
          );
          if (this.getStashForPath(previewPath)) {
            this.model = this.newEmptyFile();
          }
        }
        break;
      case 'new':
        this.model = this.newEmptyFile();
        break;
    }

    if (!this.model) {
      this.router.notify(
        t('notification.error.exists'), undefined,
        [{
          'title': t('notification.create'),
          'className': 'create',
          'link': '#'
        },
        {
          'title': t('notification.back'),
          'link': '#' + _.compact([
            this.repo.get('owner').login,
            this.repo.get('name'),
            'tree',
            this.branch,
            util.extractFilename(this.path)[0]
          ]).join('/')
        }]
      );
    }

    else {
      var defaults = this.collection.defaults;
      if (defaults) {
        this.model.set('defaults', defaults[this.nearestPath(this.model.get('path'), defaults)]);
      }
      if (this.model.isNew()) {
        this.render();
      } else {
        this.listenToOnce(this.model, 'change', this.render);
        this.model.fetch();
      }
    }

    // TODO set
    this.app.loader.done();
  },

  newEmptyFile: function() {
    return new File({
      branch: this.branches.findWhere({ name: this.branch }),
      collection: this.collection,
      path: this.path,
      repo: this.repo
{'fraction_non_alphanumeric': 0.1517327206586253, 'fraction_numerical': 0.00019146084625694046, 'mean_word_length': 2.4638594164456236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3576608', 'n_tokens_mistral': 3075, 'n_tokens_neox': 3000, 'n_words': 712}