<commit_before>window.SmalrubyEditor = {}
window.changed = false
window.textEditor = null
window.blockMode = true

window.successMessage = (title, msg = '', selector = '#messages') ->
  html = $('<div class="alert alert-success" style="display: none">')
    .append("<h4><i class=\"icon-star\"></i>#{title}</h4>")
    .append(msg)
  $(selector).append(html)
  html.fadeIn('slow')
    .delay(3000)
    .fadeOut('slow')

window.errorMessage = (msg, selector = '#messages') ->
  html = $('<div class="alert alert-error" style="display: none">')
    .append('<button type="button" class="close" data-dismiss="alert">×</button>')
    .append('<h4><i class="icon-exclamation-sign"></i>エラー</h4>')
    .append(msg)
  $(selector).append(html)
  html.fadeIn('slow')

$ ->
  saving = false

  getSourceCodeData = ->
    window.textEditor.getSession().getDocument().getValue()

  setSourceCodeData = (data) ->
    window.textEditor.getSession().getDocument().setValue(data)


  $('a[data-toggle="tab"]').on 'shown', (e) ->
    if $(e.target).attr('href') == '#block-tab'
      window.blockMode = true
    else
      window.blockMode = false
      data = Blockly.Ruby.workspaceToCode()
      if $.trim(data).length > 0
        setSourceCodeData(data)
        window.textEditor.moveCursorTo(0, 0)
      window.textEditor.focus()

  $('#save-button').click (e) ->
    e.preventDefault()
    filename = $.trim($('#filename').val())
    if filename.length <= 0
      window.errorMessage('セーブする前にプログラムに名前をつけてね！')
      $('#filename').focus()
    else
      afterSave = ->
        window.changed = false
        window.successMessage('セーブしました')

        $.ajax
          url: '/source_codes/check'
          type: 'POST'
          data:
            source_code:
              data: getSourceCodeData()
          dataType: 'json'
          success: (data, textStatus, jqXHR) ->
            if data.length == 0
              window.successMessage('チェックしました', 'ただし、プログラムを動かすとエラーが見つかるかもしれません。')
            else
              for errorInfo in data
                do (errorInfo) ->
                  msg = "#{errorInfo.row}行"
                  if errorInfo.column > 0
                    msg += "、#{errorInfo.column}文字"
                  window.errorMessage(msg + ": #{errorInfo.message}")

      $.ajax
        url: '/source_codes/'
        type: 'POST'
        data:
          source_code:
            filename: filename
            data: getSourceCodeData()
        dataType: 'json'
        success: (data, textStatus, jqXHR) ->
          <% if Rails.env == 'standalone' %>
          $.ajax
            url: '/source_codes/write'
            type: 'DELETE'
            dataType: 'json'
            success: (data, textStatus, jqXHR) ->
              if data.source_code.error
                if confirm("前に#{filename}という名前でセーブしているけど本当にセーブしますか？\nセーブすると前に作成したプログラムは消えてしまうよ！")
                  $.ajax
                    url: '/source_codes/write?force=1'
                    type: 'DELETE'
                    dataType: 'json'
                    success: (data, textStatus, jqXHR) ->
                      afterSave()
                else
                  window.successMessage('セーブをキャンセルしました')
              else
                afterSave()
          <% else %>
          afterSave()
          saving = true
          $('#download-link').click()
          <% end %>

  $('#filename').keypress (e) ->
    e = window.event if !e
    if e.keyCode == 13
      $('#save-button').click()
      false
    else
      true

  $('#load-button').click (e) ->
    e.preventDefault()
    if window.changed
      return unless confirm('まだセーブしていないのでロードするとプログラムが消えてしまうよ！\nそれでもロードしますか？')
    $(@).parent().find('input[name="source_code[file]"]').click()

  $('#file-form').fileupload
    dataType: 'json'
    done: (e, data) ->
      info = data.result.source_code
      if info.error
        window.errorMessage(info.filename + 'は' + info.error)
      else
        $('#filename').val(info.filename)<commit_msg>XML形式でのセーブとロードを仮実装<commit_after>window.SmalrubyEditor = {}
window.changed = false
window.textEditor = null
window.blockMode = true

window.successMessage = (title, msg = '', selector = '#messages') ->
  html = $('<div class="alert alert-success" style="display: none">')
    .append("<h4><i class=\"icon-star\"></i>#{title}</h4>")
    .append(msg)
  $(selector).append(html)
  html.fadeIn('slow')
    .delay(3000)
    .fadeOut('slow')

window.errorMessage = (msg, selector = '#messages') ->
  html = $('<div class="alert alert-error" style="display: none">')
    .append('<button type="button" class="close" data-dismiss="alert">×</button>')
    .append('<h4><i class="icon-exclamation-sign"></i>エラー</h4>')
    .append(msg)
  $(selector).append(html)
  html.fadeIn('slow')

$ ->
  saving = false

  getSourceCodeData = (filename)->
    if filename && filename.match(/\.xml$/)
      xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace)
      Blockly.Xml.domToPrettyText(xmlDom)
    else
      window.textEditor.getSession().getDocument().getValue()

  setSourceCodeData = (data) ->
    window.textEditor.getSession().getDocument().setValue(data)


  $('a[data-toggle="tab"]').on 'shown', (e) ->
    if $(e.target).attr('href') == '#block-tab'
      window.blockMode = true
    else
      window.blockMode = false
      data = Blockly.Ruby.workspaceToCode()
      if $.trim(data).length > 0
        setSourceCodeData(data)
        window.textEditor.moveCursorTo(0, 0)
      window.textEditor.focus()

  $('#save-button').click (e) ->
    e.preventDefault()
    filename = $.trim($('#filename').val())
    if filename.length <= 0
      window.errorMessage('セーブする前にプログラムに名前をつけてね！')
      $('#filename').focus()
    else
      afterSave = ->
        window.changed = false
        window.successMessage('セーブしました')

        $.ajax
          url: '/source_codes/check'
          type: 'POST'
          data:
            source_code:
              data: getSourceCodeData()
          dataType: 'json'
          success: (data, textStatus, jqXHR) ->
            if data.length == 0
              window.successMessage('チェックしました', 'ただし、プログラムを動かすとエラーが見つかるかもしれません。')
            else
              for errorInfo in data
                do (errorInfo) ->
                  msg = "#{errorInfo.row}行"
                  if errorInfo.column > 0
                    msg += "、#{errorInfo.column}文字"
                  window.errorMessage(msg + ": #{errorInfo.message}")

      $.ajax
        url: '/source_codes/'
        type: 'POST'
        data:
          source_code:
            filename: filename
            data: getSourceCodeData(filename)
        dataType: 'json'
        success: (data, textStatus, jqXHR) ->
          <% if Rails.env == 'standalone' %>
          $.ajax
            url: '/source_codes/write'
            type: 'DELETE'
            dataType: 'json'
            success: (data, textStatus, jqXHR) ->
              if data.source_code.error
                if confirm("前に#{filename}という名前でセーブしているけど本当にセーブしますか？\nセーブすると前に作成したプログラムは消えてしまうよ！")
                  $.ajax
                    url: '/source_codes/write?force=1'
                    type: 'DELETE'
                    dataType: 'json'
                    success: (data, textStatus, jqXHR) ->
                      afterSave()
                else
                  window.successMessage('セーブをキャンセルしました')
              else
                afterSave()
          <% else %>
          afterSave()
          saving = true
          $('#download-link').click()
          <% end %>

  $('#filename').keypress (e) ->
    e = window.event if !e
    if e.keyCode == 13
      $('#save-button').click()
      false
    else
      true

  $('#load-button').click (e) ->
    e.preventDefault()
    if window.changed
      return unless confirm('まだセーブしていないのでロードするとプログラムが消えてしまうよ！\nそれでもロードしますか？')
    $(@).parent().find('input[name="source_code[file]"]').click()

  $('#file-form').fileupload
    dataType: 'json'
    done: (e, data) ->
      info = data.result.source_code
      if info.error
        window.errorMessage(info.filename + 'は' + info.error)
      else
        $('#filename').val(info.filename)
        if info.filename.match(/.xml$/)
          xml = Blockly.Xml.textToDom(info.data)
          Blockly.mainWorkspace.clear()
          Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml)
          info.data = Blockly.Ruby.workspaceToCode()
{'fraction_non_alphanumeric': 0.15481870229007633, 'fraction_numerical': 0.0040553435114503815, 'mean_word_length': 2.056872037914692, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2547396', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2672, 'n_words': 448}