<commit_before>Spine = require('spine')
List = require('models/list')
$ = Spine.$

class TaskItem extends Spine.Controller
  template: require('views/task')
  ENTER_KEY = 13
  ESCAPE_KEY = 27

  elements:
    '.name': 'name'
    '.notes': 'notes'

  events:
    'click .delete': 'remove'
    'click .prioritybtn': 'prioritize'
    'click .checkbox': 'toggleStatus'
    'click .tag': 'tagClick'

    # Editing the actual task
    'click': 'expand'
    'blur .name': 'endEdit'
    'keypress .name': 'endEditOnEnter'

    # Make notes editable
    'focus .notes': 'notesEdit'
    'blur .notes': 'notesSave'


  constructor: ->
    super
    throw "@task required" unless @task
    @task.bind 'update', @update
    @task.bind 'destroy', @release
    @task.bind 'change', @change

  render: =>
    @replace @template @task
    @el.draggable
      revert: "invalid"
      revertDuration: 200
      distance: 10
      scroll: false
      cursorAt:
        top: 15
        right: 30
      helper: =>
        $("body").append("<div class=\"helper\">#{ @task.name }</div>")
        $(".helper")
    @

  # Remove task if it is no longer in the current list
  change: (task) =>
    if List.current.id isnt "all" and task.list isnt List.current.id
      @release()

  update: =>
    @name.text @task.name
    @el.toggleClass "completed", @task.completed

  # Delete Button
  remove: ->
    @task.destroy()

  prioritize: ->
    # There's a better way of doing this - tell me how george
    # Change the priority
    if @task.priority is 1
      @task.updateAttribute "priority", 2
    else if @task.priority is 2
      @task.updateAttribute "priority", 3
    else if @task.priority is 3
      @task.updateAttribute "priority", 1

    @el.removeClass("p0 p1 p2 p3").addClass("p" + @task.priority)

  toggleStatus: ->
    @task.completed = !@task.completed
    @task.save()

  expand: (e) ->
    if e.target.className isnt "checkbox"
      @el.parent().find(".expanded").removeClass("expanded")
      @el.addClass("expanded animout")
      @el.draggable({ disabled: true })

  endEdit: ->
    @el.draggable({ disabled: false })
    val = @name.text()
    if val then @task.updateAttribute("name", val) else @task.destroy()
    # Makes tags clickable because shit.
    @name.html(val.replace(new RegExp("\\s#([^ ]*)", "ig"), ' <span class="tag">#$1</span>'))

  endEditOnEnter: (e) =>
    if e.which is ENTER_KEY
      e.preventDefault()
      @name.blur()

  notesEdit: =>
    if @notes.text() is "Notes:" then @notes.text("")
    @notes.removeClass("placeholder")
    @notes.focus()

  notesSave: =>
    text = @notes.text()
    if text is ""
      @notes.text("Notes:")
      @notes.addClass("placeholder")
    else
      @task.updateAttribute "notes", text

  tagClick: (e) =>
    e.stopPropagation()
    List.trigger "changeList",
      name: "Tagged with " + $(e.currentTarget).text()
      id: "search"
      tasks: Task.tag($(e.currentTarget).text().substr(1))
      disabled: yes<commit_msg>Fixed Notes infinite loop & change to "Notes"<commit_after>Spine = require('spine')
List = require('models/list')
$ = Spine.$

class TaskItem extends Spine.Controller
  template: require('views/task')
  ENTER_KEY = 13
  ESCAPE_KEY = 27

  elements:
    '.name': 'name'
    '.notes': 'notes'

  events:
    'click .delete': 'remove'
    'click .prioritybtn': 'prioritize'
    'click .checkbox': 'toggleStatus'
    'click .tag': 'tagClick'

    # Editing the actual task
    'click': 'expand'
    'blur .name': 'endEdit'
    'keypress .name': 'endEditOnEnter'

    # Make notes editable
    'focus .notes': 'notesEdit'
    'blur .notes': 'notesSave'


  constructor: ->
    super
    throw "@task required" unless @task
    @task.bind 'update', @update
    @task.bind 'destroy', @release
    @task.bind 'change', @change

  render: =>
    @replace @template @task
    @el.draggable
      revert: "invalid"
      revertDuration: 200
      distance: 10
      scroll: false
      cursorAt:
        top: 15
        right: 30
      helper: =>
        $("body").append("<div class=\"helper\">#{ @task.name }</div>")
        $(".helper")
    @

  # Remove task if it is no longer in the current list
  change: (task) =>
    if List.current.id isnt "all" and task.list isnt List.current.id
      @release()

  update: =>
    @name.text @task.name
    @el.toggleClass "completed", @task.completed

  # Delete Button
  remove: ->
    @task.destroy()

  prioritize: ->
    # There's a better way of doing this - tell me how george
    # Change the priority
    if @task.priority is 1
      @task.updateAttribute "priority", 2
    else if @task.priority is 2
      @task.updateAttribute "priority", 3
    else if @task.priority is 3
      @task.updateAttribute "priority", 1

    @el.removeClass("p0 p1 p2 p3").addClass("p" + @task.priority)

  toggleStatus: ->
    @task.completed = !@task.completed
    @task.save()

  expand: (e) ->
    if e.target.className isnt "checkbox"
      @el.parent().find(".expanded").removeClass("expanded")
      @el.addClass("expanded animout")
      @el.draggable({ disabled: true })

  endEdit: ->
    @el.draggable({ disabled: false })
    val = @name.text()
    if val then @task.updateAttribute("name", val) else @task.destroy()
    # Makes tags clickable because shit.
    @name.html(val.replace(new RegExp("\\s#([^ ]*)", "ig"), ' <span class="tag">#$1</span>'))

  endEditOnEnter: (e) =>
    if e.which is ENTER_KEY
      e.preventDefault()
      @name.blur()

  notesEdit: =>
    if @notes.text() is "Notes" then @notes.text("")
    @notes.removeClass("placeholder")

  notesSave: =>
    text = @notes.text()
    if text is ""
      @notes.text("Notes")
      @notes.addClass("placeholder")
    else
      @task.updateAttribute "notes", text

  tagClick: (e) =>
    e.stopPropagation()
    List.trigger "changeList",
      name: "Tagged with " + $(e.currentTarget).text()
      id: "search"
      tasks: Task.tag($(e.currentTarget).text().substr(1))
      disabled: yes
{'fraction_non_alphanumeric': 0.1583347266343421, 'fraction_numerical': 0.008359806052499582, 'mean_word_length': 2.7481203007518795, 'pattern_counts': {'":': 0, '<': 11, '<?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': '7227678', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1940, 'n_words': 508}