<commit_before>saveGraph = () ->
  graph = {}
  graph.entities = getCurrentEntities()
  graph.relationships = getCurrentRelationships()
  graph.settings = getSettings()

  #encodeData = encodeURIComponent("graph=" + JSON.stringify(graph))
  encodeData = "graph=" + JSON.stringify(graph)

  # PUT update existing graph if it exists; otherwise POST a new one
  currentID = graphID()
  if currentID
    $.ajax({ url:"/graphs/"+currentID, type:"PUT", dataType:"json", data:encodeData })
  else
    $.ajax({ url:"/graphs", type:"POST", dataType:"json", data:encodeData })

getCurrentEntities = () ->
  entities = []
  $("div.ui-dialog").each (index, element) ->
    entities[index] = {}
    entities[index].name   = $(element).find("span.ui-dialog-title").html()
    entities[index].id     = $(element).find("div.entity").attr("data-id")
    entities[index].width  = Math.floor($(element).width())
    entities[index].height = Math.floor($(element).height())
    entities[index].x      = Math.floor($(element).position().left)
    entities[index].y      = Math.floor($(element).position().top)
    entities[index].attrib = $(element).find("textarea.attributes").val()
  entities

getCurrentRelationships = () ->
  relationships = []
  # Add parameterization code here once relationships are implemented
  relationships

getSettings = () ->
  settings = {}
  $("#settings").find(".field").find("input,textarea").each (index, element) ->
    setting_name = element.name.replace("graph[","").replace("]","")
    settings[setting_name] = element.value
  settings

graphID = () ->
  window.location.pathname.split('/')[2]

addRelationshipsToDOM = () ->
  for relationship, index in window.relationships.length
    paths = paths + '<path id="relationship' + index + '" stroke="black" stroke-width="2" fill="none" />'
  
  $('#relationships').html(paths)

$(document).ready ->
  $(".entity").each (index, element) ->
    entity_width  = parseInt( $(element).attr("data-width"))
    entity_height = parseInt( $(element).attr("data-height"))
    entity_x      = parseInt( $(element).attr("data-x"))
    entity_y      = parseInt( $(element).attr("data-y"))
    $(element).dialog({ width: entity_width, height: entity_height, position: [entity_x,entity_y] })

  $("#settings").hide()

  $("#save_button").click -> saveGraph()

  alerts = 0

  addRelationshipsToDOM

  $('.entity').on 'dialogdrag', (event, ui) ->

    alerts += 1
    console.log( "Moving entity " + event.currentTarget.dataset.id ) if alerts % 30 == 0
    
    x1 = ui.position.left
    y1 = ui.position.top - $('#header_bar').height()
    x2 = 0 # window.relationships[] # which entity is being dragged??
    y2 = 0 # window.relationships[]
    $('#relationship1').attr('d', "M #{x1} #{y1} L #{x2} #{y2}")

<commit_msg>Cleanup document.ready<commit_after>saveGraph = ->
  graph = {}
  graph.entities = getCurrentEntities()
  graph.relationships = getCurrentRelationships()
  graph.settings = getSettings()

  #encodeData = encodeURIComponent("graph=" + JSON.stringify(graph))
  encodeData = "graph=" + JSON.stringify(graph)

  # PUT update existing graph if it exists; otherwise POST a new one
  currentID = graphID()
  if currentID
    $.ajax({ url:"/graphs/"+currentID, type:"PUT", dataType:"json", data:encodeData })
  else
    $.ajax({ url:"/graphs", type:"POST", dataType:"json", data:encodeData })

getCurrentEntities = ->
  entities = []
  $("div.ui-dialog").each (index, element) ->
    entities[index] = {}
    entities[index].name   = $(element).find("span.ui-dialog-title").html()
    entities[index].id     = $(element).find("div.entity").attr("data-id")
    entities[index].width  = Math.floor($(element).width())
    entities[index].height = Math.floor($(element).height())
    entities[index].x      = Math.floor($(element).position().left)
    entities[index].y      = Math.floor($(element).position().top)
    entities[index].attrib = $(element).find("textarea.attributes").val()
  entities

getCurrentRelationships = ->
  relationships = []
  # Add parameterization code here once relationships are implemented
  relationships

getSettings = ->
  settings = {}
  $("#settings").find(".field").find("input,textarea").each (index, element) ->
    setting_name = element.name.replace("graph[","").replace("]","")
    settings[setting_name] = element.value
  settings

graphID = ->
  window.location.pathname.split('/')[2]

makeEntitiesDraggable = ->
  $(".entity").each (index, element) ->
    entity_width  = parseInt( $(element).attr("data-width"))
    entity_height = parseInt( $(element).attr("data-height"))
    entity_x      = parseInt( $(element).attr("data-x"))
    entity_y      = parseInt( $(element).attr("data-y"))
    $(element).dialog({ width: entity_width, height: entity_height, position: [entity_x,entity_y] })

addRelationshipsToDOM = ->
  for relationship, index in window.relationships.length
    paths = paths + '<path id="relationship' + index + '" stroke="black" stroke-width="2" fill="none" />'
  $('#relationships').html(paths)

setupEntityDragHandler = ->
  $('.entity').on 'dialogdrag', (event, ui) ->
    x1 = ui.position.left
    y1 = ui.position.top - $('#header_bar').height()
    x2 = 0 # window.relationships[] # which entity is being dragged??
    y2 = 0 # window.relationships[]
    $('#relationship1').attr('d', "M #{x1} #{y1} L #{x2} #{y2}")

$(document).ready ->
  makeEntitiesDraggable()
  addRelationshipsToDOM()
  setupEntityDragHandler()
  $("#settings").hide()
  $("#save_button").click -> saveGraph()

{'fraction_non_alphanumeric': 0.1897677793904209, 'fraction_numerical': 0.005624092888243832, 'mean_word_length': 4.137931034482759, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '2219294', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1714, 'n_words': 325}