<commit_before>var $ = require('jquery-browserify');
var _ = require('underscore');
var Backbone = require('backbone');
var utils = require('.././util');

module.exports = Backbone.View.extend({
    className: 'application',

    events: {
      'click .post-views .edit': 'edit',
      'click .post-views .preview': 'preview',
      'click .post-views .settings': 'settings',
      'click .post-views .meta': 'meta',
      'click .auth .logout': 'logout',
      'click a.item.removed': 'restoreFile',
      'click a.save': 'save',
      'click a.save.confirm': 'updateFile',
      'click a.save-action': 'updateFile',
      'click a.cancel': 'cancelSave',
      'click a.delete': 'deleteFile',
      'click a.translate': 'translate',
      'keypress input.filepath': 'saveFilePath'
    },

    initialize: function(options) {

      // Key Binding support accross the application.
      if (!window.shortcutsRegistered) {
        key('j, k, enter, o, ctrl+s', _.bind(function(e, handler) {
          if (!app.state.mode || app.state.mode === 'tree') {
            // We are in any navigation view
            if (handler.key === 'j' || handler.key === 'k') {
              utils.pageListing(handler.key);
            } else {
              utils.goToFile();
            }
          } else {
            // We are in state of the application
            // where we can edit a file
            if (handler.key === 'ctrl+s') {
              this.updateFile();
            }
          }
        }, this));

        window.shortcutsRegistered = true;
      }

      app.state = {
        user: '',
        repo: '',
        mode: '',
        branch: '',
        path: ''
      };

      this.eventRegister = app.eventRegister;

      _.bindAll(this, 'documentTitle', 'headerContext', 'sidebarContext', 'recentFiles', 'updateSaveState');
      this.eventRegister.bind('documentTitle', this.documentTitle);
      this.eventRegister.bind('headerContext', this.headerContext);
      this.eventRegister.bind('sidebarContext', this.sidebarContext);
      this.eventRegister.bind('recentFiles', this.recentFiles);
      this.eventRegister.bind('updateSaveState', this.updateSaveState);
    },

    render: function(options) {
      var tmpl = _(window.app.templates.app).template();
      var isJekyll = false;
      var errorPage = false;
      if (options && options.jekyll) isJekyll = options.jekyll;
      if (options && options.error) errorPage = options.error;

      $(this.el).empty().append(tmpl(_.extend(this.model, app.state, {
        jekyll: isJekyll,
        error: errorPage
      })));

      // When the sidebar should be open.
      // Fix this in re-factor, could be much tighter
      if (app.state.mode === 'tree') {
        $('#prose').toggleClass('open', true);
      } else if (app.state.mode === '' && window.authenticated && app.state.user !== '') {
        $('#prose').toggleClass('open', true);
      } else {
        $('#prose').toggleClass('open', false);
      }

      return this;
    },

    documentTitle: function(title) {
      document.title = title + ' · Prose';
    },

    headerContext: function(data) {
      var heading = _(window.app.templates.heading).template();
      $('#heading').empty().append(heading(data));
    },

    sidebarContext: function(data) {
      var sidebarTmpl;

      if (app.state.mode === 'tree') {
        sidebarTmpl = _(app.templates.sidebarProject).template();
      } else if (data.file) {
        this.writeable = data.writeable;
        sidebarTmpl = _(app.templates.settings).template();
      }

      $('#drawer', this.el)
        .empty()
        .append(sidebarTmpl(data));

      // Branch Switching
      $('.chzn-select').chosen().change(function() {
          router.navigate($(this).val(), true);
      });
    },

    recentFiles: function(data) {
      var sidebarTmpl = _(window.app.templates.recentFiles).template();
      $('#drawer', this.el).empty().append(sidebarTmpl(data));
    },

    // Event Triggering to other files
    edit: function(e) {
      this.viewing = 'edit';
      this.eventRegister.trigger('edit', e);
      return false;
    },

    preview: function(e) {
      if ($(e.target).data('jekyll')) {
        this.eventRegister.trigger('preview', e);
      } else {
        this.viewing = 'preview';
        this.eventRegister.trigger('preview', e);
        // Cancel propagation
        return false;
      }
    },

    // Event Triggering to other files
    meta: function(e) {
      this.viewing = 'meta';
      this.eventRegister.trigger('meta', e);
      return false;
    },

    settings: function(e) {
      $navItems = $('.navigation a', this.el);

      if ($(e.target, this.el).hasClass('active')) {
        $navItems.removeClass('active');
        $('.navigation .' + this.viewing, this.el).addClass('active');
      } else {
        $navItems.removeClass('active');
        $(e.target, this.el).addClass('active');
      }

      $('#prose').toggleClass('open');
      return false;
    },

    restoreFile: function(e) {<commit_msg>If this.viewing is not set, default to app.state.mode<commit_after>var $ = require('jquery-browserify');
var _ = require('underscore');
var Backbone = require('backbone');
var utils = require('.././util');

module.exports = Backbone.View.extend({
    className: 'application',

    events: {
      'click .post-views .edit': 'edit',
      'click .post-views .preview': 'preview',
      'click .post-views .settings': 'settings',
      'click .post-views .meta': 'meta',
      'click .auth .logout': 'logout',
      'click a.item.removed': 'restoreFile',
      'click a.save': 'save',
      'click a.save.confirm': 'updateFile',
      'click a.save-action': 'updateFile',
      'click a.cancel': 'cancelSave',
      'click a.delete': 'deleteFile',
      'click a.translate': 'translate',
      'keypress input.filepath': 'saveFilePath'
    },

    initialize: function(options) {

      // Key Binding support accross the application.
      if (!window.shortcutsRegistered) {
        key('j, k, enter, o, ctrl+s', _.bind(function(e, handler) {
          if (!app.state.mode || app.state.mode === 'tree') {
            // We are in any navigation view
            if (handler.key === 'j' || handler.key === 'k') {
              utils.pageListing(handler.key);
            } else {
              utils.goToFile();
            }
          } else {
            // We are in state of the application
            // where we can edit a file
            if (handler.key === 'ctrl+s') {
              this.updateFile();
            }
          }
        }, this));

        window.shortcutsRegistered = true;
      }

      app.state = {
        user: '',
        repo: '',
        mode: '',
        branch: '',
        path: ''
      };

      this.eventRegister = app.eventRegister;

      _.bindAll(this, 'documentTitle', 'headerContext', 'sidebarContext', 'recentFiles', 'updateSaveState');
      this.eventRegister.bind('documentTitle', this.documentTitle);
      this.eventRegister.bind('headerContext', this.headerContext);
      this.eventRegister.bind('sidebarContext', this.sidebarContext);
      this.eventRegister.bind('recentFiles', this.recentFiles);
      this.eventRegister.bind('updateSaveState', this.updateSaveState);
    },

    render: function(options) {
      var tmpl = _(window.app.templates.app).template();
      var isJekyll = false;
      var errorPage = false;
      if (options && options.jekyll) isJekyll = options.jekyll;
      if (options && options.error) errorPage = options.error;

      $(this.el).empty().append(tmpl(_.extend(this.model, app.state, {
        jekyll: isJekyll,
        error: errorPage
      })));

      // When the sidebar should be open.
      // Fix this in re-factor, could be much tighter
      if (app.state.mode === 'tree') {
        $('#prose').toggleClass('open', true);
      } else if (app.state.mode === '' && window.authenticated && app.state.user !== '') {
        $('#prose').toggleClass('open', true);
      } else {
        $('#prose').toggleClass('open', false);
      }

      return this;
    },

    documentTitle: function(title) {
      document.title = title + ' · Prose';
    },

    headerContext: function(data) {
      var heading = _(window.app.templates.heading).template();
      $('#heading').empty().append(heading(data));
    },

    sidebarContext: function(data) {
      var sidebarTmpl;

      if (app.state.mode === 'tree') {
        sidebarTmpl = _(app.templates.sidebarProject).template();
      } else if (data.file) {
        this.writeable = data.writeable;
        sidebarTmpl = _(app.templates.settings).template();
      }

      $('#drawer', this.el)
        .empty()
        .append(sidebarTmpl(data));

      // Branch Switching
      $('.chzn-select').chosen().change(function() {
          router.navigate($(this).val(), true);
      });
    },

    recentFiles: function(data) {
      var sidebarTmpl = _(window.app.templates.recentFiles).template();
      $('#drawer', this.el).empty().append(sidebarTmpl(data));
    },

    // Event Triggering to other files
    edit: function(e) {
      this.viewing = 'edit';
      this.eventRegister.trigger('edit', e);
      return false;
    },

    preview: function(e) {
      if ($(e.target).data('jekyll')) {
        this.eventRegister.trigger('preview', e);
      } else {
        this.viewing = 'preview';
        this.eventRegister.trigger('preview', e);
        // Cancel propagation
        return false;
      }
    },

    // Event Triggering to other files
    meta: function(e) {
      this.viewing = 'meta';
      this.eventRegister.trigger('meta', e);
      return false;
    },

    settings: function(e) {
      $navItems = $('.navigation a', this.el);
      if (!this.viewing) this.viewing = app.state.mode;

      if ($(e.target, this.el).hasClass('active')) {
        $navItems.removeClass('active');
        $('.navigation .' + this.viewing, this.el).addClass('active');
      } else {
        $navItems.removeClass('active');
        $(e.target, this.el).addClass('active');
      }

      $('#prose').toggleClass('open');
      return false;
    },

    restoreFile: function(e) {
{'fraction_non_alphanumeric': 0.16712463199214916, 'fraction_numerical': 0.0, 'mean_word_length': 2.308766233766234, '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': '1270101', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2973, 'n_words': 619}