<commit_before># Include routes helpers
<% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %>

all_items = JSON.parse('<%= raw Item.order(:name).select(:name).to_json %>')

items_by_category = JSON.parse(
  '<%= raw Category.order(:name).map { |category|
    [category.id, category.items.order(:name).select(:name, :id)]
  }.to_h.to_json %>'
)

@init_smooth_scrolling = ->
  # Smooth Scroll on clicking nav items
  $('nav a').click ->
    $href = $(this).attr('href')
    $('body').stop().animate { scrollTop: $($href).offset().top }, 1000
    false
  # back to top
  $('#toTop a').click ->
    $('body').animate { scrollTop: 0 }, 1000
    false
  # Parallaxing + add class active on scroll
  $(document).scroll ->
    # parallaxing
    $movebg = $(window).scrollTop() * -0.3
    $('.portion').css 'background-positionY', $movebg + 'px'
    # add class active to nav a on scroll
    scrollPos = $(document).scrollTop() + 100
    $('nav a').each ->
      currLink = $(this)
      refElement = $(currLink.attr('href'))
      if refElement.position().top <= scrollPos and refElement.position().top + refElement.height() > scrollPos
        $('nav a').removeClass 'active'
        currLink.addClass 'active'
      return
    # changing padding of nav a on scroll
    if scrollPos > 250
      $('nav a').addClass 'small'
      $('nav img').addClass 'move'
      $('nav span').removeClass 'movetext'
    else
      $('nav a').removeClass 'small'
      $('nav img').removeClass 'move'
      $('nav span').addClass 'movetext'
    return

###
Display only items belonging to the given category in the items picker.
If !force, then this founction is a noop if the given category is already displayed. Force can be
specified as a means of refreshing the items picker after an update.
###
@filter_by_category = (category_id, {force} = {}) ->
  force ?= false

  btn = $('#category' + category_id)
  if is_active(btn) and not force
    # We're already filtering by this category
    return
  clear_items()
  append_items_by_category category_id
  activate btn
  return


###
Display all items from all categories in the items picker.
###
@display_all_items = ->
  btn = $('#all_categories')
  if is_active btn
    # We're already displaying all items
    return
  clear_items()
  append_items all_items
  activate btn
  return

###
Set or unset (based on value of select) active class on all the Items within a category
###
@select_all = (select) ->
  $('#items').children('li').each ->
    if is_active($(this)) != select
      toggle_active $(this)

###
Add a new item to the items picker based on the contents of #new-item-form
###
@save_new_item = ->
  item = form_to_json $('#new-item-form')

  if !item.name
    flash_error("Please enter a name for the item.", selector: '#new-item-errors')
    return
  if !item.category_id
    flash_error("Please choose a category for the item.", selector: '#new-item-errors')
    return

  # Tell the server to create the item
  get_json '<%= items_path %>', method: 'POST', data: { item: item }
    .on_success (res) ->
      # Close the dialog
      $('#new-item').modal('hide')

      # Add the new item to the items picker (maintaining sorted order) and filter by its category
      index = _.sortedIndex(items_by_category[item.category_id], item, 'name')
      items_by_category[item.category_id].splice index, 0, item
      filter_by_category(item.category_id, force: true)

      # Scroll to the new item
      first = $('#items li').first().position().top
      elem = $("#items li[name=#{item.name}]").position().top
      $('#items').scrollTop(elem - first);

      # And select it
      activate $("#items li[name=#{item.name}")<commit_msg>Fix a JQuery attribute selector bug.<commit_after># Include routes helpers
<% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %>

all_items = JSON.parse('<%= raw Item.order(:name).select(:name).to_json %>')

items_by_category = JSON.parse(
  '<%= raw Category.order(:name).map { |category|
    [category.id, category.items.order(:name).select(:name, :id)]
  }.to_h.to_json %>'
)

@init_smooth_scrolling = ->
  # Smooth Scroll on clicking nav items
  $('nav a').click ->
    $href = $(this).attr('href')
    $('body').stop().animate { scrollTop: $($href).offset().top }, 1000
    false
  # back to top
  $('#toTop a').click ->
    $('body').animate { scrollTop: 0 }, 1000
    false
  # Parallaxing + add class active on scroll
  $(document).scroll ->
    # parallaxing
    $movebg = $(window).scrollTop() * -0.3
    $('.portion').css 'background-positionY', $movebg + 'px'
    # add class active to nav a on scroll
    scrollPos = $(document).scrollTop() + 100
    $('nav a').each ->
      currLink = $(this)
      refElement = $(currLink.attr('href'))
      if refElement.position().top <= scrollPos and refElement.position().top + refElement.height() > scrollPos
        $('nav a').removeClass 'active'
        currLink.addClass 'active'
      return
    # changing padding of nav a on scroll
    if scrollPos > 250
      $('nav a').addClass 'small'
      $('nav img').addClass 'move'
      $('nav span').removeClass 'movetext'
    else
      $('nav a').removeClass 'small'
      $('nav img').removeClass 'move'
      $('nav span').addClass 'movetext'
    return

###
Display only items belonging to the given category in the items picker.
If !force, then this founction is a noop if the given category is already displayed. Force can be
specified as a means of refreshing the items picker after an update.
###
@filter_by_category = (category_id, {force} = {}) ->
  force ?= false

  btn = $('#category' + category_id)
  if is_active(btn) and not force
    # We're already filtering by this category
    return
  clear_items()
  append_items_by_category category_id
  activate btn
  return


###
Display all items from all categories in the items picker.
###
@display_all_items = ->
  btn = $('#all_categories')
  if is_active btn
    # We're already displaying all items
    return
  clear_items()
  append_items all_items
  activate btn
  return

###
Set or unset (based on value of select) active class on all the Items within a category
###
@select_all = (select) ->
  $('#items').children('li').each ->
    if is_active($(this)) != select
      toggle_active $(this)

###
Add a new item to the items picker based on the contents of #new-item-form
###
@save_new_item = ->
  item = form_to_json $('#new-item-form')

  if !item.name
    flash_error("Please enter a name for the item.", selector: '#new-item-errors')
    return
  if !item.category_id
    flash_error("Please choose a category for the item.", selector: '#new-item-errors')
    return

  # Tell the server to create the item
  get_json '<%= items_path %>', method: 'POST', data: { item: item }
    .on_success (res) ->
      # Close the dialog
      $('#new-item').modal('hide')

      # Add the new item to the items picker (maintaining sorted order) and filter by its category
      index = _.sortedIndex(items_by_category[item.category_id], item, 'name')
      items_by_category[item.category_id].splice index, 0, item
      filter_by_category(item.category_id, force: true)

      # Scroll to the new item
      first = $('#items li').first().position().top
      elem = $("#items li[name=\"#{item.name}\"]").position().top
      $('#items').scrollTop(elem - first);

      # And select it
      activate $("#items li[name=\"#{item.name}\"]")
{'fraction_non_alphanumeric': 0.14660246120920278, 'fraction_numerical': 0.004815409309791332, 'mean_word_length': 3.3648569760653824, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, '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': '370820', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2274, 'n_words': 704}