<commit_before>_ = require('lodash')

class RenderedMultiSelect
  constructor: (@element, @options) ->
    return if @element.data("readonly") == "true"
    @body = $('body')
    @win  = $(window)
    @inputContainer = @element.find(".rendered-multi-select-input")
    @input = @inputContainer.find(".editable-input")
    @createResultMenu()
    @registerEvents()
    @multiple = @element.data("multiple") == true
    @lastName = null
    @configureMultiple()
    @blurTimeout = null
    
  registerEvents: ->
    @element.on "keydown", ".editable-input", (event) =>
      @inputKeyDown(event)
    @element.on 'keyup', '.editable-input', _.throttle((=>
      @updateQuery()
      ), 200)
    @element.on "blur", ".editable-input", (event) =>
      # Create any partially edited item if it allows new options
      if @input.text() && (index = @resultList.find("li").map(() -> $(this).text().toLowerCase()).get().indexOf(@input.text().toLowerCase())) >= 0
        @addItem(@resultList.find("li").eq(index))
      else if @options.allowNew
        @createNewItem(@input.text())

      @blurTimeout = setTimeout =>
          @blurTimeout = null
          @input.html("")
          @hideResultMenu(true)
          @element.removeClass("rendered-multi-select-active")
        , 200
    @element.on "focus", ".editable-input", (event) =>
      clearTimeout @blurTimeout if @blurTimeout
      @element.addClass("rendered-multi-select-active")
      @lastName = null
      @updateQuery()
    @resultMenu.on "click", "li", (event) =>
      @addItem($(event.target))
      event.stopPropagation()
    @resultMenu.on "focus", (event) =>
      unless @input.is(":focus")
        clearTimeout @blurTimeout if @blurTimeout
        @input[0].focus() if @input[0]
    @resultMenu.on "mousedown", (event) =>
      false
    @element.on "click", ".rendered-multi-select-element b", (event) =>
      @deleteItem($(event.target).parent(".rendered-multi-select-element"))
      event.stopPropagation()
    @element.on "click", (event) =>
      # Focus the input when user clicks on the control.
      unless @input.is(":focus")
        clearTimeout @blurTimeout if @blurTimeout
        @input[0].focus() if @input[0]
      
    @element.on "change", (event) =>
      @configureMultiple()
      
  configureMultiple: ->
      # For non-multiple item controls hide input if an item exists.
      unless @multiple 
        if @element.find(".rendered-multi-select-element").length > 0
          @inputContainer.hide()
        else
          @inputContainer.show()
  
  createResultMenu: ->
    @resultMenu = $("<div class='rendered-multi-select-menu'><ul class='rendered-multi-select-results'></ul></div")

    if @element.attr("data-fixed-menu") == "true"
      @resultMenu.addClass('fixed')
      @body.append(@resultMenu)
    else
      @resultMenu.insertAfter(@input)

    @resultList = @resultMenu.find("ul")

  showResultMenu: ->
    return @resultMenu.show() unless @element.attr("data-fixed-menu") == "true"

    winHeight = @win.height()
    inputTop  = @inputContainer.offset().top
    elemLeft  = @element.offset().left
    scrollTop = @body.scrollTop()
    rules     = 
      display: 'block'
      left:    elemLeft - @body.scrollLeft()
      width:   @element.width()

    if winHeight / 2 < inputTop - scrollTop
      rules.bottom = winHeight - inputTop + scrollTop
    else
      rules.top = inputTop - scrollTop + @inputContainer.height()

    @resultMenu.css(rules)
    @body.css(overflow: 'hidden')
    return<commit_msg>Fix positioning bugs<commit_after>_ = require('lodash')

class RenderedMultiSelect
  constructor: (@element, @options) ->
    return if @element.data("readonly") == "true"
    @body = $('body')
    @win  = $(window)
    @inputContainer = @element.find(".rendered-multi-select-input")
    @input = @inputContainer.find(".editable-input")
    @createResultMenu()
    @registerEvents()
    @multiple = @element.data("multiple") == true
    @lastName = null
    @configureMultiple()
    @blurTimeout = null
    
  registerEvents: ->
    @element.on "keydown", ".editable-input", (event) =>
      @inputKeyDown(event)
    @element.on 'keyup', '.editable-input', _.throttle((=>
      @updateQuery()
      ), 200)
    @element.on "blur", ".editable-input", (event) =>
      # Create any partially edited item if it allows new options
      if @input.text() && (index = @resultList.find("li").map(() -> $(this).text().toLowerCase()).get().indexOf(@input.text().toLowerCase())) >= 0
        @addItem(@resultList.find("li").eq(index))
      else if @options.allowNew
        @createNewItem(@input.text())

      @blurTimeout = setTimeout =>
          @blurTimeout = null
          @input.html("")
          @hideResultMenu(true)
          @element.removeClass("rendered-multi-select-active")
        , 200
    @element.on "focus", ".editable-input", (event) =>
      clearTimeout @blurTimeout if @blurTimeout
      @element.addClass("rendered-multi-select-active")
      @lastName = null
      @updateQuery()
    @resultMenu.on "click", "li", (event) =>
      @addItem($(event.target))
      event.stopPropagation()
    @resultMenu.on "focus", (event) =>
      unless @input.is(":focus")
        clearTimeout @blurTimeout if @blurTimeout
        @input[0].focus() if @input[0]
    @resultMenu.on "mousedown", (event) =>
      false
    @element.on "click", ".rendered-multi-select-element b", (event) =>
      @deleteItem($(event.target).parent(".rendered-multi-select-element"))
      event.stopPropagation()
    @element.on "click", (event) =>
      # Focus the input when user clicks on the control.
      unless @input.is(":focus")
        clearTimeout @blurTimeout if @blurTimeout
        @input[0].focus() if @input[0]
      
    @element.on "change", (event) =>
      @configureMultiple()
      
  configureMultiple: ->
      # For non-multiple item controls hide input if an item exists.
      unless @multiple 
        if @element.find(".rendered-multi-select-element").length > 0
          @inputContainer.hide()
        else
          @inputContainer.show()
  
  createResultMenu: ->
    @resultMenu = $("<div class='rendered-multi-select-menu'><ul class='rendered-multi-select-results'></ul></div")

    if @element.attr("data-fixed-menu") == "true"
      @resultMenu.addClass('fixed')
      @body.append(@resultMenu)
    else
      @resultMenu.insertAfter(@input)

    @resultList = @resultMenu.find("ul")

  showResultMenu: ->
    return @resultMenu.show() unless @element.attr("data-fixed-menu") == "true"
    return if @inputContainer.is(":hidden")

    winHeight = @win.height()
    inputTop  = @inputContainer.offset().top
    elemLeft  = @element.offset().left
    scrollTop = @body.scrollTop()
    rules     = 
      display: 'block'
      left:    elemLeft - @body.scrollLeft()
      width:   @element.width()

    if winHeight / 2 < inputTop - scrollTop
      rules.bottom = winHeight - inputTop + scrollTop
      rules.top = ""
    else
      rules.top = inputTop - scrollTop + @inputContainer.height()
      rules.bottom = ""

    @resultMenu.css(rules)
    @body.css(overflow: 'hidden')
    return
{'fraction_non_alphanumeric': 0.16231762065095398, 'fraction_numerical': 0.003647586980920314, 'mean_word_length': 2.889252591380251, 'pattern_counts': {'":': 5, '<': 13, '<?xml version=': 0, '>': 47, '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': '4819693', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2133, 'n_words': 432}