<commit_before>define [
  'jquery'
  'marionette'
  'cs!collections/content'
  'cs!session'
  'hbs!gh-book/auth-template'
  'difflib'
  'diffview'
  'bootstrapModal'
  'bootstrapCollapse'
], ($, Marionette, allContent, session, authTemplate, difflib, diffview) ->

  return class GithubAuthView extends Marionette.ItemView
    template: authTemplate

    events:
      'click #sign-in-ok': 'signIn'
      'click #sign-in': 'signInModal'
      'click #sign-out': 'signOut'
      'click #save-content': 'saveContent'
      'click #fork-content': 'forkContent'
      'click #edit-settings': 'editSettingsModal'
      'click #edit-settings-ok': 'editSettings'
      'submit #login-form': 'signIn'
      'click #show-diffs': 'showDiffsModal'

    initialize: () ->
      # When a model has changed (triggered `dirty`) update the Save button
      @listenTo allContent, 'change:_isDirty', (model, value, options) =>
        if value
          @setDirty()
        else
          # This element may have been the only one to have the dirty bit set, and it was just cleared

          # Recalculate the dirty bit
          @isDirty = allContent.some (model) -> model.isDirty()

          @render()
      # Update the Save button when new Folder/Book/Module is created (added to `allContent`)
      @listenTo allContent, 'add remove', (model, collection, options) =>
        @setDirty() if not (options.loading or options.parse)

      @listenTo allContent, 'reset', (collection, options) =>
        # Clear the dirty bit since allContent has been reparsed
        @isDirty = allContent.some (model) -> model.isDirty()


      @listenTo @model, 'change', () => @render()

      # Bind a function to the window if the user tries to navigate away from this page
      $(window).on 'beforeunload', () ->
        return 'You have unsaved changes. Are you sure you want to leave this page?' if @isDirty

      # Since this View is reloaded all the time (whenever a route change occurs)
      # re-set the `isDirty` bit.
      @isDirty = allContent.some (model) -> model.isDirty()

    templateHelpers: () ->
      return {
        isDirty: @isDirty
        isAuthenticated: !! (@model.get('password') or @model.get('token'))
      }


    setDirty: () ->
      @isDirty = true
      @render()

    signInModal: () ->
      $modal = @$el.find('#sign-in-modal')

      # The hidden event on #login-advanced should not propagate
      $modal.find('#login-advanced').on 'hidden', (e) => e.stopPropagation()

      # attach a close listener
      $modal.on 'hidden', () => @trigger 'close'<commit_msg>Repair onunload event to warn user if they have unsaved changes.<commit_after>define [
  'jquery'
  'marionette'
  'cs!collections/content'
  'cs!session'
  'hbs!gh-book/auth-template'
  'difflib'
  'diffview'
  'bootstrapModal'
  'bootstrapCollapse'
], ($, Marionette, allContent, session, authTemplate, difflib, diffview) ->

  return class GithubAuthView extends Marionette.ItemView
    template: authTemplate

    events:
      'click #sign-in-ok': 'signIn'
      'click #sign-in': 'signInModal'
      'click #sign-out': 'signOut'
      'click #save-content': 'saveContent'
      'click #fork-content': 'forkContent'
      'click #edit-settings': 'editSettingsModal'
      'click #edit-settings-ok': 'editSettings'
      'submit #login-form': 'signIn'
      'click #show-diffs': 'showDiffsModal'

    initialize: () ->
      # When a model has changed (triggered `dirty`) update the Save button
      @listenTo allContent, 'change:_isDirty', (model, value, options) =>
        if value
          @setDirty()
        else
          # This element may have been the only one to have the dirty bit set, and it was just cleared

          # Recalculate the dirty bit
          @isDirty = allContent.some (model) -> model.isDirty()

          @render()
      # Update the Save button when new Folder/Book/Module is created (added to `allContent`)
      @listenTo allContent, 'add remove', (model, collection, options) =>
        @setDirty() if not (options.loading or options.parse)

      @listenTo allContent, 'reset', (collection, options) =>
        # Clear the dirty bit since allContent has been reparsed
        @isDirty = allContent.some (model) -> model.isDirty()


      @listenTo @model, 'change', () => @render()

      # Bind a function to the window if the user tries to navigate away from this page
      $(window).on 'beforeunload', () =>
        return 'You have unsaved changes. Are you sure you want to leave this page?' if @isDirty

      # Since this View is reloaded all the time (whenever a route change occurs)
      # re-set the `isDirty` bit.
      @isDirty = allContent.some (model) -> model.isDirty()

    templateHelpers: () ->
      return {
        isDirty: @isDirty
        isAuthenticated: !! (@model.get('password') or @model.get('token'))
      }


    setDirty: () ->
      @isDirty = true
      @render()

    signInModal: () ->
      $modal = @$el.find('#sign-in-modal')

      # The hidden event on #login-advanced should not propagate
      $modal.find('#login-advanced').on 'hidden', (e) => e.stopPropagation()

      # attach a close listener
      $modal.on 'hidden', () => @trigger 'close'
{'fraction_non_alphanumeric': 0.1279493573757913, 'fraction_numerical': 0.0, 'mean_word_length': 2.8141916605705926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '6409252', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1560, 'n_words': 485}