<commit_before>class Loader
  @show: ->
    @_instance().show()
  @hide: ->
    @_instance().hide()
  @_instance: ->
    unless @__instance
      @__instance = new Loader()
    @__instance
  constructor: ->
    @loader = $('body > div#lazy-loader')
    if @loader.length == 0
      @loader = $('<div>')
        .attr('id', 'lazy-loader')
        .css(display: 'none')
        .appendTo 'body'
  show: ->
    @loader.fadeIn()
  hide: ->
    @loader.fadeOut()


class Sortable
  constructor: (options = {})->
    return if $('ul.items-list.sortable').length == 0

    url = '' + $('ul.items-list.sortable').first().data('url')

    if url.length == 0
      throw "Element ul.sortable must have data-url."

    if url.indexOf(':id') < 0 || url.indexOf(':target_id') < 0
      throw "Url '#{url}' has to have :id and :target_id placeholders."

    $("ul.items-list.sortable li:not(.header)").draggable appendTo: 'body',
      revert: 'invalid',
      cursor: 'move',
      cancel: '[data-context-button]'

    $("ul.items-list.sortable li:not(.header)").droppable hoverClass: 'ui-state-hover',
      greedy: true,
      drop: (event, ui) ->

        object = $(ui.draggable)
        id = object.find('input[type=checkbox]').val()

        target = $(this)
        target_id = target.find('input[type=checkbox]').val()

        action_url = url
          .replace(':id', id)
          .replace(':target_id', target_id)

        Loader.show()

        $.post(action_url, (data) ->
          object.insertAfter(target)
        )
        .fail( (jqXHR, textStatus) ->
          text = if jqXHR.responseJSON?
            jqXHR.responseJSON.join('. ')
          else
            "Network error"

          $('#notice')
            .html(text)
            .show()
          setTimeout ->
            $('#notice').fadeOut(3000);
          , 3000
        )
        .always( ->
          object.css(top: '', left: '')
          Loader.hide()
        )

class LazyAdmin
  constructor: ->
    @_sortable_list()
    @_selection_actions()
    # @_custom_select()
    @_custom_file_input()
    @_locales_tabs()
    @_fileupload()
    @_init_data_picker()

    $(document).on 'opened', '[data-dropdown-content]', (event, dropdown, target) ->
      if dropdown.hasClass('axis-right')
        _left = dropdown.position().left + target.outerWidth() - dropdown.outerWidth()
        dropdown.css(left: _left)
    @submenu_hidden_buttons()
    @set_main_content_height()

    $('.items-list')
      .on 'jsp-initialised jsp-scroll-y', (event) ->
        window.context_menu && window.context_menu.close_all_context_menus()
        jsp = $(event.target).data('jsp')
        if !jsp? || jsp.isAboutEnd(50)
          $('.show-more a', event.target).click()
        if jsp?
          $('.items-list-header').width(jsp.getContentPane().outerWidth())
      .jScrollPane()

    $('.left-menu ul.root').jScrollPane()

    $(window).on 'resize', (event) =>
      @set_main_content_height()
      @submenu_hidden_buttons()

    $(document).on 'click', '.left-menu li.has-submenu > a', (event) ->
      event.preventDefault()
      $(event.currentTarget).parent().toggleClass('opened')

    $(document).on 'append-next-page', (event, content) =>
      element = $(event.target)
      element.find('.show-more').replaceWith $(content)
      @set_main_content_height()


  set_main_content_height: ->
    _height = $(window).innerHeight() - $('header.panel-header').outerHeight()
    $('.main-content').height(_height)
    menu = $('.left-menu ul.root')
    (mjsp = menu.data('jsp')) && mjsp.reinitialise()
    if (list = $('.main-content .items-list')).length > 0
      _list_height = _height - list.position().top
      list.height(_list_height)
      (jsp = list.data('jsp')) && jsp.reinitialise()

  submenu_hidden_buttons: ->
    $('.submenu + ul[data-dropdown-content] li')
      .detach()
      .appendTo('.submenu')

    elements = $('.submenu li:not(.show-more)')
      .filter (idx) ->
        $(this).position().top > 0
      .detach()
      .appendTo('.submenu + ul[data-dropdown-content]')

    $('.submenu li.show-more').toggle(elements.length > 0)



  loader: ->
    Loader

  action_on_selected: (url, options = {}) ->
    console.log("Not used any more")
    return

  _init_data_picker: ->
    _init_each_datepicker = ->
      $('.input-calendar').each ->
        input = $('input[type=text]', this)
        return if input.hasClass('hasDatepicker')
        button = $('a.button', this)
        input.datepicker()
        button.click ->
          input.datepicker('show')

    $(document)
      .on 'click', '.ui-datepicker, a[data-handler], button[data-handler]', (event) ->
        event.stopPropagation()
        event.preventDefault()
      .ajaxComplete(_init_each_datepicker)

    $.datepicker.setDefaults($.datepicker.regional['pl']);
    $.datepicker.setDefaults(dateFormat: 'dd/mm/yy');
    _init_each_datepicker()


  _selection_actions: ->
    $('form select#selection_action').on 'change', (event) ->
      ids = []
      for obj in $(this.form).serializeArray()
        if obj.name == 'selection[ids][]'
          ids.push obj.value
      if ids.length > 0 and $(this).val().length > 0
        $(this.form).submit()<commit_msg>Fix items list equalizer<commit_after>class Loader
  @show: ->
    @_instance().show()
  @hide: ->
    @_instance().hide()
  @_instance: ->
    unless @__instance
      @__instance = new Loader()
    @__instance
  constructor: ->
    @loader = $('body > div#lazy-loader')
    if @loader.length == 0
      @loader = $('<div>')
        .attr('id', 'lazy-loader')
        .css(display: 'none')
        .appendTo 'body'
  show: ->
    @loader.fadeIn()
  hide: ->
    @loader.fadeOut()


class Sortable
  constructor: (options = {})->
    return if $('ul.items-list.sortable').length == 0

    url = '' + $('ul.items-list.sortable').first().data('url')

    if url.length == 0
      throw "Element ul.sortable must have data-url."

    if url.indexOf(':id') < 0 || url.indexOf(':target_id') < 0
      throw "Url '#{url}' has to have :id and :target_id placeholders."

    $("ul.items-list.sortable li:not(.header)").draggable appendTo: 'body',
      revert: 'invalid',
      cursor: 'move',
      cancel: '[data-context-button]'

    $("ul.items-list.sortable li:not(.header)").droppable hoverClass: 'ui-state-hover',
      greedy: true,
      drop: (event, ui) ->

        object = $(ui.draggable)
        id = object.find('input[type=checkbox]').val()

        target = $(this)
        target_id = target.find('input[type=checkbox]').val()

        action_url = url
          .replace(':id', id)
          .replace(':target_id', target_id)

        Loader.show()

        $.post(action_url, (data) ->
          object.insertAfter(target)
        )
        .fail( (jqXHR, textStatus) ->
          text = if jqXHR.responseJSON?
            jqXHR.responseJSON.join('. ')
          else
            "Network error"

          $('#notice')
            .html(text)
            .show()
          setTimeout ->
            $('#notice').fadeOut(3000);
          , 3000
        )
        .always( ->
          object.css(top: '', left: '')
          Loader.hide()
        )

class LazyAdmin
  constructor: ->
    @_sortable_list()
    @_selection_actions()
    # @_custom_select()
    @_custom_file_input()
    @_locales_tabs()
    @_fileupload()
    @_init_data_picker()

    $(document).on 'opened', '[data-dropdown-content]', (event, dropdown, target) ->
      if dropdown.hasClass('axis-right')
        _left = dropdown.position().left + target.outerWidth() - dropdown.outerWidth()
        dropdown.css(left: _left)
    @submenu_hidden_buttons()
    @set_main_content_height()

    $('.items-list')
      .on 'jsp-initialised jsp-scroll-y', (event) ->
        window.context_menu && window.context_menu.close_all_context_menus()
        jsp = $(event.target).data('jsp')
        if !jsp? || jsp.isAboutEnd(50)
          $('.show-more a', event.target).click()
        if jsp?
          $('.items-list-header').width(jsp.getContentPane().outerWidth())
      .jScrollPane()

    $('.left-menu ul.root').jScrollPane()

    $(window).on 'resize', (event) =>
      @set_main_content_height()
      @submenu_hidden_buttons()

    $(document).on 'click', '.left-menu li.has-submenu > a', (event) ->
      event.preventDefault()
      $(event.currentTarget).parent().toggleClass('opened')

    $(document).on 'append-next-page', (event, content) =>
      element = $(event.target)
      element.find('.show-more').replaceWith $(content)
      element.foundation()
      @set_main_content_height()


  set_main_content_height: ->
    _height = $(window).innerHeight() - $('header.panel-header').outerHeight()
    $('.main-content').height(_height)
    menu = $('.left-menu ul.root')
    (mjsp = menu.data('jsp')) && mjsp.reinitialise()
    if (list = $('.main-content .items-list')).length > 0
      _list_height = _height - list.position().top
      list.height(_list_height)
      (jsp = list.data('jsp')) && jsp.reinitialise()

  submenu_hidden_buttons: ->
    $('.submenu + ul[data-dropdown-content] li')
      .detach()
      .appendTo('.submenu')

    elements = $('.submenu li:not(.show-more)')
      .filter (idx) ->
        $(this).position().top > 0
      .detach()
      .appendTo('.submenu + ul[data-dropdown-content]')

    $('.submenu li.show-more').toggle(elements.length > 0)

  loader: ->
    Loader

  action_on_selected: (url, options = {}) ->
    console.log("Not used any more")
    return

  _init_data_picker: ->
    _init_each_datepicker = ->
      $('.input-calendar').each ->
        input = $('input[type=text]', this)
        return if input.hasClass('hasDatepicker')
        button = $('a.button', this)
        input.datepicker()
        button.click ->
          input.datepicker('show')

    $(document)
      .on 'click', '.ui-datepicker, a[data-handler], button[data-handler]', (event) ->
        event.stopPropagation()
        event.preventDefault()
      .ajaxComplete(_init_each_datepicker)

    lang = $('html').attr('lang') ? 'pl'
    $.datepicker.setDefaults($.datepicker.regional[lang]);
    $.datepicker.setDefaults(dateFormat: 'dd/mm/yy');
    _init_each_datepicker()


  _selection_actions: ->
    $('form select#selection_action').on 'change', (event) ->
      ids = []
      for obj in $(this.form).serializeArray()
        if obj.name == 'selection[ids][]'
          ids.push obj.value
      if ids.length > 0 and $(this).val().length > 0
        $(this.form).submit()
{'fraction_non_alphanumeric': 0.17976994010837533, 'fraction_numerical': 0.0038026428367715563, 'mean_word_length': 2.4719471947194718, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 79, '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': '7476887', 'n_tokens_mistral': 3486, 'n_tokens_neox': 3324, 'n_words': 596}