<commit_before>
#http://www.kryogenix.org/code/browser/custom-drag-image.html
#http://www.html5rocks.com/en/tutorials/dnd/basics/#toc-examples
class window.Editor extends Backbone.View
  el: '#editor-container'

  events: ->
    'keyup .note-editable': 'copyToFocusedElement'
    #'keypress #mail-editor': 'copyToTextArea'
    'drag .blocks li a' : 'drag'

    'drag .tpl-block-controls a' : 'drag'
    'dragstart .tpl-block-controls a': 'setDraggedEl'

    #"dragover #bodyTable" : "displaySections"
    "dragleave #bodyTable" : "hideSections"
    "drop #bodyTable" : "hideSections"
    #"dragover .tpl-container" : "displaySections"
    "dragover .tpl-block" : "displayItemOver"
    "dragover .mojoMcContainerEmptyMessage": "displayItemOver"

    #"dragover .tpl-container" : "allowDrop"

    #"dragover #templateBody" : "allowDrop"
    #"dragover #templatePreheader" : "allowDrop"
    #"dragover #templateHeader" : "allowDrop"
    #"dragover #templateBody" : "allowDrop"
    #"dragover #templateFooter" : "allowDrop"

    'drop #templateBody': "drop"
    'drop #templatePreheader': "drop"
    'drop #templateHeader': "drop"
    'drop #templateBody': "drop"
    "drop #templateFooter" : "drop"

    "click .tpl-block": "setFocus"
    "click #editor-controls #save" : "saveAndClose"
    "click .imagePlaceholder .button-small" : "displayUploaderList"
    "click .tpl-block-delete": "deleteBloc"

  initialize: ->
    @textarea = $(@el).find('#campaign_html_content')

  copyToEditor: (ev)->
    $this = $(ev.currentTarget);
    window.setTimeout ()=>
      $(@el).find('#mail-editor').html($this.val());
    , 0

  copyToTextArea: ()->
    $this = $("#mail-editor");
    window.setTimeout ()=>
      $(@el).find('textarea').val($this.html());
    , 0

  copyToFocusedElement: (ev)->
    @currentFocused().find('.mcnTextContent').html($(ev.currentTarget).html())
    @copyToTextArea()

  template: ->
    '<p>dsfsd</p>'

  render: ->
    #$(@el).find('#mail-editor').html(@template())
    #$(@el).find('#mail-editor').html(@textarea.val()); #init from saved content
    $(@el).find('#mail-editor').html(@baseTemplate()) #init from base js tamplarte

  displaySections: (ev)->

    #console.log($(ev.currentTarget))
    #console.log($(ev.currentTarget).hasClass("tpl-container"))
    container = $(ev.currentTarget).parent(".tpl-container")

    _.each $('.tpl-container'), (n)->
      return if $(n).find(".legend").length > 0
      $(n).append("<div class='legend default'>#{$(n).attr("mc:container")}</div>")

    container.addClass("default")

    if container.hasClass("tpl-container")
      container.addClass("over").removeClass("default")
      container.find(".legend").addClass("over").removeClass("default")

    ev.preventDefault()

  setDraggedEl: (e)->
    #debugger
    crt = $(e.currentTarget).parents(".tpl-block")[0].cloneNode(true) #.clone()
    crt.style.backgroundColor = "red";
    crt.style.position = "absolute"; crt.style.top = "0px"; crt.style.right = "0px";
    document.body.appendChild(crt);
    e.dataTransfer = e.originalEvent.dataTransfer;
    e.dataTransfer.setDragImage(crt, 0, 0);

  displayItemOver: (ev)->
    @displaySections(ev)

    $('.tpl-block').removeClass("dojoDndItemBefore")
    if $(ev.currentTarget).hasClass("tpl-block")
      $(ev.currentTarget).addClass("dojoDndItemBefore")

    ev.preventDefault()

  hideSections: ()->
    $('.tpl-container').removeClass("default").removeClass("over")
    _.each $('.tpl-container'), (n)->
      $(n).find(".legend").remove()


  drag: (ev)->
    #console.log(ev.currentTarget)
    @dragged = $(ev.currentTarget)

  allowDrop: (ev)->
    #console.log("allos drop")
    ev.preventDefault()

  drop: (ev)->
    ev.preventDefault()
    #console.log($(ev.dataTransfer.getData("text")))
    #console.log(@dragged.data('block'))
    console.log ev.target.id
    #data = $(ev.dataTransfer.getData("text"));
    #ev.target.appendChild(data);
    container = $(ev.currentTarget)

    #debugger
    if @dragged.attr('data-block')
      tmpl = @handleBlock @dragged.data('block')
      @dropBlock(container, tmpl)
    else
      to_drop = this.dragged.parents(".tpl-block")
      @dropBlock(container, to_drop)

    @copyToTextArea()

    @releaseBeforeItem()

  dropBlock: (container, tmpl)->
    #drop on before item or in tpl-container
    if $(".tpl-block.dojoDndItemBefore").length > 0
      container.find(".tpl-block.dojoDndItemBefore").before(tmpl)
    else
      container.find(".tpl-container").append(tmpl)
      container.find('.mojoMcContainerEmptyMessage').hide()

  releaseBeforeItem: ()->
    $(".tpl-block").removeClass("dojoDndItemBefore")

  displayWysiwyg: ->
    $('.block-settings').show()
    $('.main-settings').hide()
    @initWysiwyg()
    false

  displayBlockButtons: ->
    $('.block-settings').hide()
    $('.main-settings').show()
    false

  saveAndClose: ->
    @copyToTextArea()
    @displayBlockButtons()
    false
<commit_msg>hide blocks when drop<commit_after>
#http://www.kryogenix.org/code/browser/custom-drag-image.html
#http://www.html5rocks.com/en/tutorials/dnd/basics/#toc-examples
class window.Editor extends Backbone.View
  el: '#editor-container'

  events: ->
    'keyup .note-editable': 'copyToFocusedElement'
    #'keypress #mail-editor': 'copyToTextArea'
    'drag .blocks li a' : 'drag'

    'drag .tpl-block-controls a' : 'drag'
    'dragstart .tpl-block-controls a': 'setDraggedEl'

    #"dragover #bodyTable" : "displaySections"
    "dragleave #bodyTable" : "hideSections"
    "drop #bodyTable" : "hideSections"
    #"dragover .tpl-container" : "displaySections"
    "dragover .tpl-block" : "displayItemOver"
    "dragover .mojoMcContainerEmptyMessage": "displayItemOver"

    #"dragover .tpl-container" : "allowDrop"

    #"dragover #templateBody" : "allowDrop"
    #"dragover #templatePreheader" : "allowDrop"
    #"dragover #templateHeader" : "allowDrop"
    #"dragover #templateBody" : "allowDrop"
    #"dragover #templateFooter" : "allowDrop"

    'drop #templateBody': "drop"
    'drop #templatePreheader': "drop"
    'drop #templateHeader': "drop"
    'drop #templateBody': "drop"
    "drop #templateFooter" : "drop"

    "click .tpl-block": "setFocus"
    "click #editor-controls #save" : "saveAndClose"
    "click .imagePlaceholder .button-small" : "displayUploaderList"
    "click .tpl-block-delete": "deleteBloc"

  initialize: ->
    @textarea = $(@el).find('#campaign_html_content')

  copyToEditor: (ev)->
    $this = $(ev.currentTarget);
    window.setTimeout ()=>
      $(@el).find('#mail-editor').html($this.val());
    , 0

  copyToTextArea: ()->
    $this = $("#mail-editor");
    window.setTimeout ()=>
      $(@el).find('textarea').val($this.html());
    , 0

  copyToFocusedElement: (ev)->
    @currentFocused().find('.mcnTextContent').html($(ev.currentTarget).html())
    @copyToTextArea()

  template: ->
    '<p>dsfsd</p>'

  render: ->
    #$(@el).find('#mail-editor').html(@template())
    #$(@el).find('#mail-editor').html(@textarea.val()); #init from saved content
    $(@el).find('#mail-editor').html(@baseTemplate()) #init from base js tamplarte

  displaySections: (ev)->

    #console.log($(ev.currentTarget))
    #console.log($(ev.currentTarget).hasClass("tpl-container"))
    container = $(ev.currentTarget).parent(".tpl-container")

    _.each $('.tpl-container'), (n)->
      return if $(n).find(".legend").length > 0
      $(n).append("<div class='legend default'>#{$(n).attr("mc:container")}</div>")

    container.addClass("default")

    if container.hasClass("tpl-container")
      container.addClass("over").removeClass("default")
      container.find(".legend").addClass("over").removeClass("default")

    ev.preventDefault()

  setDraggedEl: (e)->
    crt = $(e.currentTarget).parents(".tpl-block")[0].cloneNode(true) #.clone()
    crt.style.backgroundColor = "white";
    crt.style.position = "absolute";
    crt.style.border = "1px solid #666";
    crt.style.boxShadow = "1px 2px 2px #adadad"
    crt.style.top = "0px";
    crt.style.right = "0px";
    crt.style.opacity = 0.9;
    document.body.appendChild(crt);
    e.dataTransfer = e.originalEvent.dataTransfer;
    e.dataTransfer.setDragImage(crt, 0, 0);

  displayItemOver: (ev)->
    @displaySections(ev)

    $('.tpl-block').removeClass("dojoDndItemBefore")
    if $(ev.currentTarget).hasClass("tpl-block")
      $(ev.currentTarget).addClass("dojoDndItemBefore")

    ev.preventDefault()

  hideSections: ()->
    $('.tpl-container').removeClass("default").removeClass("over")
    _.each $('.tpl-container'), (n)->
      $(n).find(".legend").remove()

  drag: (ev)->
    #console.log(ev.currentTarget)
    @dragged = $(ev.currentTarget)

  allowDrop: (ev)->
    #console.log("allos drop")
    ev.preventDefault()

  displayEmptyBlocks: ->
    _.each $('.tpl-container'), (container)->
      empty_message = $(container).find('.mojoMcContainerEmptyMessage')
      if $(container).find(".tpl-block").length > 0
        empty_message.hide()
      else
        empty_message.show()

  drop: (ev)->
    ev.preventDefault()
    #console.log($(ev.dataTransfer.getData("text")))
    #console.log(@dragged.data('block'))
    console.log ev.target.id
    #data = $(ev.dataTransfer.getData("text"));
    #ev.target.appendChild(data);
    container = $(ev.currentTarget)

    if @dragged.attr('data-block')
      tmpl = @handleBlock @dragged.data('block')
      @dropBlock(container, tmpl)
    else
      to_drop = this.dragged.parents(".tpl-block")
      @dropBlock(container, to_drop)

    @copyToTextArea()

    @releaseBeforeItem()

    @displayEmptyBlocks()

  dropBlock: (container, tmpl)->
    #drop on before item or in tpl-container
    if $(".tpl-block.dojoDndItemBefore").length > 0
      container.find(".tpl-block.dojoDndItemBefore").before(tmpl)
      container.find('.mojoMcContainerEmptyMessage').hide()
    else
      container.find(".tpl-container").append(tmpl)
      #container.find('.mojoMcContainerEmptyMessage').hide()

  releaseBeforeItem: ()->
    $(".tpl-block").removeClass("dojoDndItemBefore")

  displayWysiwyg: ->
    $('.block-settings').show()
    $('.main-settings').hide()
    @initWysiwyg()
    false

  displayBlockButtons: ->
    $('.block-settings').hide()
    $('.main-settings').show()
    false

  saveAndClose: ->
    @copyToTextArea()
    @displayBlockButtons()
    false

{'fraction_non_alphanumeric': 0.18945841392649904, 'fraction_numerical': 0.0029013539651837525, 'mean_word_length': 3.6686230248307, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '6078518', 'n_tokens_mistral': 3479, 'n_tokens_neox': 3440, 'n_words': 534}