<commit_before>#  Project: Flexirails
#  Description:
#  Copyright 2010 - 2013, <NAME>
#  License: MIT

(($, window) ->
  pluginName = 'flexirails'
  document = window.document

  firstPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_first" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <path d="M186.178,256.243l211.583,166.934V89.312L186.178,256.243z M112.352,422.512h66.179V89.975h-66.179V422.512z"/> </svg>'
  prevPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_left" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <polygon points="148.584,255.516 360.168,88.583 360.166,422.445 "/> </svg>'
  lastPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_last" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <path d="M111.708,424.514l211.581-166.927L111.708,90.654V424.514z M330.935,87.311v332.544h66.173V87.311H330.935z"/> </svg>'
  nextPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_right" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <polygon points="360.124,255.513 148.535,422.442 148.537,88.58 "/> </svg>'

  defaults =
    # how many items to fetch when querying for data
    limitFetchResultsTo: 50
    # how many items to display on each page
    limitDisplayPerPageTo: 25
    # per page selection options
    limitDisplayPerPageOptions: [5, 25, 50, 100, 250]

    locales:
      no_results: 'No results found'
      results:
        perPage: 'Results per Page:'
        page: 'Page '
        of: ' of '
        total: ' Results'

  extractAttribute = (obj, qualifiedName) ->
    parts = qualifiedName.split(".")
    current = obj

    for part in parts
      if current.hasOwnProperty part
        current = current[part]
      else
        break

    value = current
    attribute = parts[parts.length-1]
    if value? && typeof(value) is 'object'
      if value.hasOwnProperty(attribute)
        value = current[attribute]
      else
        value = null

    return value

  #
  class Flexirails
    t: (name) -> return extractAttribute(@options.locales, name)

    constructor: (@element, options) ->
      @options = $.extend {}, defaults, options

      @_defaults = defaults
      @_name = pluginName

      @_formatterFunctions = {}
      @_currentView = options.view
      @_url = options.url
      @_pagination = {
        last: 1,
        first: 1
      }

      @locales = options.locales if options.hasOwnProperty 'locales'
      @setupEventListeners()

      @init()

    init: ->
      @initializingView = true;

      @_currentView.totalResults or= 1
      @_currentView.currentPage or= @_currentView.current_page or 1
      @_currentView.perPage or= @_currentView.per_page or 1

      @_currentView.currentPage = if @_currentView.hasOwnProperty('currentPage') then parseInt(@_currentView.currentPage, 10) else 1
      @_currentView.perPage = if @_currentView.hasOwnProperty('perPage') then parseInt(@_currentView.perPage, 10) else @_defaults.perPage

      @initializingView = false

      @flexiContainer = $(document.createElement('div')).addClass('flexirails')

      $(@flexiTable).remove() if @flexiTable?

      @flexiTable = document.createElement('table')
      @flexiContainer.append(@flexiTable)

      navigation = $(document.createElement('div'))
      @createNavigation(navigation)
      $(@element).append(navigation.clone())

      @flexiContainer.append(@flexiTable)
      $(@element).append(@flexiContainer)

      $(@element).append(navigation)

      @invalidateView()

    setupEventListeners: ->
      $(@element).on "click",  "a[name=toFirstPage]", @paginateToFirstPage
      $(@element).on "click",  "a[name=toPrevPage]", @paginateToPrevPage
      $(@element).on "click",  "a[name=toNextPage]", @paginateToNextPage
      $(@element).on "click",  "a[name=toLastPage]", @paginateToLastPage
      $(@element).on "change", ":input[name=current_page_box]", @paginateToAnyPage
      $(@element).on "change", ":input[name=per_page]", @changePerPage

    reloadFlexidata: ->
      if (!@_url? or @dontExecuteQueries or @initializingView or @loadingData)
        return

      $(@element).find(".js-fr-from-page").attr('disabled','disabled')
      request = $.ajax({
        type: 'GET',
        url: @_url,
        data: @buildFlexiOptions(),
        dataType: 'json'
      })
      request.done @buildFlexiview

      @loadingData = true
      @appendResults = false

    buildFlexiview: (data, textStatus, XMLHttpRequest) =>
      @_currentView.totalResults = parseInt(data.total, 10) || 0

      @setFlexirailsOptions(data)

      fragment = document.createDocumentFragment()

      if (!@appendResults)
        while(@flexiTable.hasChildNodes())
          @flexiTable.removeChild(@flexiTable.firstChild);
        @loadedRows = 0

        header = document.createElement("tr")
        for col in @_currentView.cols
          th = document.createElement('th')
          th.className = col.attribute
          th.appendChild(document.createTextNode(col.title))
          header.appendChild(th)

        thead = document.createElement("thead")
        thead.appendChild(header)
        fragment.appendChild(thead)

      arr = data.rows

      @loadedRows += arr.length
      cur_req = Math.round(@loadedRows / @_defaults.limitFetchResultsTo)

      tbody = document.createElement('tbody')
      if (arr.length is 0)
        _tr = document.createElement('tr')
        _tr.className = 'no_results'

        td = document.createElement('td')
        td.className = 'center'
        td.colSpan = @_currentView.cols.length
        td.appendChild(document.createTextNode(@t('no_results')))

        _tr.appendChild(td)
        tbody.appendChild(_tr)
      else
        tbody.appendChild(@buildFlexiRow(item)) for item in arr

      fragment.appendChild tbody
      @flexiTable.appendChild(fragment.cloneNode(true))

      @setupFirstLastColumns()

      @loadingData = false

      if ((@loadedRows < @_currentView.perPage) && (@loadedRows < @_currentView.totalResults))
        @appendFlexiData()
        if (@_currentView.currentPage is @_pagination.last)
          $(@element).find(".js-fr-from-page").removeAttr('disabled')

          if window.history?
            url = window.location.pathname + "?current_page=#{@_currentView.currentPage}&per_page=#{@_currentView.perPage}"
            window.history.pushState({
              turbolinks: true
              position: Date.now()
            }, null, url)
      else
        @appendResults = false;
        $(@element).find(".js-fr-from-page").removeAttr('disabled')
        $(@element).find(".flexirails-container").trigger("complete")

        if window.history?
          url = window.location.pathname + "?current_page=#{@_currentView.currentPage}&per_page=#{@_currentView.perPage}"
          window.history.pushState({
            turbolinks: true
            position: Date.now()
          }, null, url)

    appendFlexiData: ->
      if ((@_currentView.perPage * (@_currentView.currentPage - 1) + @loadedRows) < @_currentView.totalResults)<commit_msg>update flexirails url to keep unknown fragments<commit_after>#  Project: Flexirails
#  Description:
#  Copyright 2010 - 2013, <NAME>
#  License: MIT

getUrlParts = (url) ->
  # url contains your data.
  qs = url.indexOf("?")
  return {} if qs is -1

  fr = url.indexOf("#")
  q = ""
  q = if (fr is -1) then url.substr(qs+1) else url.substr(qs+1, fr-qs-1)
  parts = q.split("&")
  vars = {}
  for part, i in parts
    p = part.split("=")
    if p[1]
      vars[decodeURIComponent(p[0])] = decodeURIComponent(p[1])
    # else
    #   vars[decodeURIComponent(p[0])] = ""

  # vars contain all the variables in an array.
  return vars;


(($, window) ->
  pluginName = 'flexirails'
  document = window.document

  firstPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_first" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <path d="M186.178,256.243l211.583,166.934V89.312L186.178,256.243z M112.352,422.512h66.179V89.975h-66.179V422.512z"/> </svg>'
  prevPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_left" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <polygon points="148.584,255.516 360.168,88.583 360.166,422.445 "/> </svg>'
  lastPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_last" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <path d="M111.708,424.514l211.581-166.927L111.708,90.654V424.514z M330.935,87.311v332.544h66.173V87.311H330.935z"/> </svg>'
  nextPageSVG = '<svg version="1.2" baseProfile="tiny" id="Navigation_right" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 512 512" overflow="inherit" xml:space="preserve"> <polygon points="360.124,255.513 148.535,422.442 148.537,88.58 "/> </svg>'

  defaults =
    # how many items to fetch when querying for data
    limitFetchResultsTo: 50
    # how many items to display on each page
    limitDisplayPerPageTo: 25
    # per page selection options
    limitDisplayPerPageOptions: [5, 25, 50, 100, 250]

    locales:
      no_results: 'No results found'
      results:
        perPage: 'Results per Page:'
        page: 'Page '
        of: ' of '
        total: ' Results'

  extractAttribute = (obj, qualifiedName) ->
    parts = qualifiedName.split(".")
    current = obj

    for part in parts
      if current.hasOwnProperty part
        current = current[part]
      else
        break

    value = current
    attribute = parts[parts.length-1]
    if value? && typeof(value) is 'object'
      if value.hasOwnProperty(attribute)
        value = current[attribute]
      else
        value = null

    return value

  #
  class Flexirails
    t: (name) -> return extractAttribute(@options.locales, name)

    constructor: (@element, options) ->
      @options = $.extend {}, defaults, options

      @_defaults = defaults
      @_name = pluginName

      @_formatterFunctions = {}
      @_currentView = options.view
      @_url = options.url
      @_pagination = {
        last: 1,
        first: 1
      }

      @locales = options.locales if options.hasOwnProperty 'locales'
      @setupEventListeners()

      @init()

    init: ->
      @initializingView = true;

      @_currentView.totalResults or= 1
      @_currentView.currentPage or= @_currentView.current_page or 1
      @_currentView.perPage or= @_currentView.per_page or 1

      @_currentView.currentPage = if @_currentView.hasOwnProperty('currentPage') then parseInt(@_currentView.currentPage, 10) else 1
      @_currentView.perPage = if @_currentView.hasOwnProperty('perPage') then parseInt(@_currentView.perPage, 10) else @_defaults.perPage

      @initializingView = false

      @flexiContainer = $(document.createElement('div')).addClass('flexirails')

      $(@flexiTable).remove() if @flexiTable?

      @flexiTable = document.createElement('table')
      @flexiContainer.append(@flexiTable)

      navigation = $(document.createElement('div'))
      @createNavigation(navigation)
      $(@element).append(navigation.clone())

      @flexiContainer.append(@flexiTable)
      $(@element).append(@flexiContainer)

      $(@element).append(navigation)

      @invalidateView()

    setupEventListeners: ->
      $(@element).on "click",  "a[name=toFirstPage]", @paginateToFirstPage
      $(@element).on "click",  "a[name=toPrevPage]", @paginateToPrevPage
      $(@element).on "click",  "a[name=toNextPage]", @paginateToNextPage
      $(@element).on "click",  "a[name=toLastPage]", @paginateToLastPage
      $(@element).on "change", ":input[name=current_page_box]", @paginateToAnyPage
      $(@element).on "change", ":input[name=per_page]", @changePerPage

    reloadFlexidata: ->
      if (!@_url? or @dontExecuteQueries or @initializingView or @loadingData)
        return

      $(@element).find(".js-fr-from-page").attr('disabled','disabled')
      request = $.ajax({
        type: 'GET',
        url: @_url,
        data: @buildFlexiOptions(),
        dataType: 'json'
      })
      request.done @buildFlexiview

      @loadingData = true
      @appendResults = false

    buildFlexiview: (data, textStatus, XMLHttpRequest) =>
      @_currentView.totalResults = parseInt(data.total, 10) || 0

      @setFlexirailsOptions(data)

      fragment = document.createDocumentFragment()

      if (!@appendResults)
        while(@flexiTable.hasChildNodes())
          @flexiTable.removeChild(@flexiTable.firstChild);
        @loadedRows = 0

        header = document.createElement("tr")
        for col in @_currentView.cols
          th = document.createElement('th')
          th.className = col.attribute
          th.appendChild(document.createTextNode(col.title))
          header.appendChild(th)

        thead = document.createElement("thead")
        thead.appendChild(header)
        fragment.appendChild(thead)

      arr = data.rows

      @loadedRows += arr.length
      cur_req = Math.round(@loadedRows / @_defaults.limitFetchResultsTo)

      tbody = document.createElement('tbody')
      if (arr.length is 0)
        _tr = document.createElement('tr')
        _tr.className = 'no_results'

        td = document.createElement('td')
        td.className = 'center'
        td.colSpan = @_currentView.cols.length
        td.appendChild(document.createTextNode(@t('no_results')))

        _tr.appendChild(td)
        tbody.appendChild(_tr)
      else
        tbody.appendChild(@buildFlexiRow(item)) for item in arr

      fragment.appendChild tbody
      @flexiTable.appendChild(fragment.cloneNode(true))

      @setupFirstLastColumns()

      @loadingData = false

      if ((@loadedRows < @_currentView.perPage) && (@loadedRows < @_currentView.totalResults))
        @appendFlexiData()
        if (@_currentView.currentPage is @_pagination.last)
          $(@element).find(".js-fr-from-page").removeAttr('disabled')
          @updateURI()
      else
        @appendResults = false;
        $(@element).find(".js-fr-from-page").removeAttr('disabled')
        $(@element).find(".flexirails-container").trigger("complete")
        @updateURI()

    updateURI: ->
      if window.history?
        params = getUrlParts(window.location.href)

        params.current_page ?= @_currentView.currentPage
        params.per_page ?= @_currentView.perPage

        url = window.location.pathname + "?" + $.param(params)

        window.history.pushState({
          turbolinks: true
          position: Date.now()
        }, null, url)

    appendFlexiData: ->
      if ((@_currentView.perPage * (@_currentView.currentPage - 1) + @loadedRows) < @_currentView.totalResults)
{'fraction_non_alphanumeric': 0.14585079631181894, 'fraction_numerical': 0.04713392223870011, 'mean_word_length': 3.1749663526244953, 'pattern_counts': {'":': 4, '<': 35, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 16, 'xml': 24}, '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': '6633005', 'n_tokens_mistral': 5455, 'n_tokens_neox': 4854, 'n_words': 934}