<commit_before>class PagesController
  constructor: ->

  init: ->
    @$content = $('#page_content')
    @edited = false

    @preview()

    autosize(@$content)
    $('#page_parent_id').select2()

    @_initDropzone()
    @_initMousetrap()

  _initDropzone: ->
    $container = $('.js-content-container')
    $container.dropzone
      url: $container.data('attachmentsPath')
      paramName: 'attachment[data]'
      previewsContainer: false
      clickable: '.js-upload'
      params:
        authenticity_token: $('meta[name=csrf-token]').prop('content')
        'attachment[page_id]': $container.data('pageId')
      success: (file, json) =>
        text = if json.is_image
          "[![](#{json.path})](#{json.path})"
        else
          "[#{json.file_name}](#{json.path})"
        @insert(text)
        @preview()

  _initMousetrap: ->
    Mousetrap.bind 'e', ->
      Turbolinks.visit $('.js-edit-page').prop('href')

    Mousetrap.bindGlobal 'mod+s', (e) ->
      if /wiki_title|wiki_content|wiki_slug|page_title|page_content/.test(e.target.id)
        e.preventDefault()
        $('.js-submit-page').click()

  preview: ->
    return unless @$content.length

    path = @$content.data('previewPath')
    params =
      page:
        content: @$content.val()
    $.ajax
      method: 'POST'
      dataType: 'json'
      url: path
      data: params
      success: (data) ->
        $('.js-preview').html(data.html)
      error: (jqXHR, textStatus, errorThrown) ->
        console.error(jqXHR, textStatus, errorThrown)
        alert(errorThrown)

  insert: (text, pos, mode = 'before') ->
    @$content.selection('insert', { text: text, mode: mode })

    evt = document.createEvent('Event')
    evt.initEvent('autosize:update', true, false)
    @$content.get(0).dispatchEvent(evt)

    @preview()

    if pos
      end = @$content.selection('getPos').end + pos
      @$content.selection('setPos', { start: end, end: end })

   edit: ->
     @edited = true

controller = new PagesController()

$ ->
  controller.init()

$(document).on 'keyup', '#page_content', _.throttle ->
  controller.preview()
, 1000

$(document).on 'keyup', '#page_title, #page_content, #wiki_title, #wiki_content, #wiki_slug', ->
  controller.edit()

$(document).on 'click', '.js-insert', (e) ->
  e.preventDefault()
  $elm = $(this)

  text = $elm.data('text')
  pos = $elm.data('pos') || 0

  controller.insert(text.replace(/\\n/g, '\n'), pos)

$(document).on 'click', '.js-upload', (e) ->
  e.preventDefault()

$(window).on 'beforeunload', ->
  if controller.edited
    ' '
<commit_msg>Fix beforeunload<commit_after>class PagesController
  constructor: ->

  init: ->
    @$content = $('#page_content')
    @edited = false

    @preview()

    autosize(@$content)
    $('#page_parent_id').select2()

    @_initDropzone()
    @_initMousetrap()

  _initDropzone: ->
    $container = $('.js-content-container')
    $container.dropzone
      url: $container.data('attachmentsPath')
      paramName: 'attachment[data]'
      previewsContainer: false
      clickable: '.js-upload'
      params:
        authenticity_token: $('meta[name=csrf-token]').prop('content')
        'attachment[page_id]': $container.data('pageId')
      success: (file, json) =>
        text = if json.is_image
          "[![](#{json.path})](#{json.path})"
        else
          "[#{json.file_name}](#{json.path})"
        @insert(text)
        @preview()

  _initMousetrap: ->
    Mousetrap.bind 'e', ->
      Turbolinks.visit $('.js-edit-page').prop('href')

    Mousetrap.bindGlobal 'mod+s', (e) ->
      if /wiki_title|wiki_content|wiki_slug|page_title|page_content/.test(e.target.id)
        e.preventDefault()
        $('.js-submit-page').click()

  preview: ->
    return unless @$content.length

    path = @$content.data('previewPath')
    params =
      page:
        content: @$content.val()
    $.ajax
      method: 'POST'
      dataType: 'json'
      url: path
      data: params
      success: (data) ->
        $('.js-preview').html(data.html)
      error: (jqXHR, textStatus, errorThrown) ->
        console.error(jqXHR, textStatus, errorThrown)
        alert(errorThrown)

  insert: (text, pos, mode = 'before') ->
    @$content.selection('insert', { text: text, mode: mode })

    evt = document.createEvent('Event')
    evt.initEvent('autosize:update', true, false)
    @$content.get(0).dispatchEvent(evt)

    @preview()

    if pos
      end = @$content.selection('getPos').end + pos
      @$content.selection('setPos', { start: end, end: end })

controller = new PagesController()

$ ->
  controller.init()

$(document).on 'keyup', '#page_content', _.throttle ->
  controller.preview()
, 1000

$(document).on 'keyup', '#page_title, #page_content, #wiki_title, #wiki_content, #wiki_slug', ->
  controller.edited = true

$(document).on 'click', '.js-insert', (e) ->
  e.preventDefault()
  $elm = $(this)

  text = $elm.data('text')
  pos = $elm.data('pos') || 0

  controller.insert(text.replace(/\\n/g, '\n'), pos)

$(document).on 'click', '.js-upload', (e) ->
  e.preventDefault()

$(window).on 'beforeunload', ->
  if controller.edited
    ' '

$(document).on 'click', '.js-submit-page', (e) ->
  controller.edited = false
  true

{'fraction_non_alphanumeric': 0.1871042026482441, 'fraction_numerical': 0.0026866244482824796, 'mean_word_length': 2.9335849056603776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '802170', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1751, 'n_words': 316}