<commit_before>#= require _history

class Wiselinks
  constructor: () ->
    # check that JQuery or Zepto.js are available
    throw "Load JQuery or Zepto.js to use Wiselinks" unless window.JQuery? || window.Zepto?

    self = this

    History.Adapter.bind(
      window,
      "statechange"
      (event, data) ->
        return if (!History.ready)
  
        state = History.getState()         
        self._call(state.url, state.data.target, state.data.slide)  

        return false
    )
    
    $(document).on(
      "submit", "form[data-push], form[data-replace]"
      (event) ->      
        self._process_form($(this))
        
        event.preventDefault()
        return false
    )
  
    $(document).on(
      "click", "a[data-push], a[data-replace]"
      (event) ->      
        if (event.ctrlKey || event.metaKey)
          return true;
        
        self._process_link($(this))

        event.preventDefault()
        return false
    )
  
  load: (url, target, slide = 'template') ->
    History.ready = true
    History.pushState({ timestamp: (new Date().getTime()), slide: slide, target: target }, document.title, decodeURI(url) )    

  reload: () ->    
    History.ready = true
    History.replaceState({ timestamp: (new Date().getTime()), slide: 'template' }, document.title, decodeURI(History.getState().url) )    

  _call: (url, target, slide = 'template') ->    
    $(document).trigger('wiselinks:loading')

    $.ajax(
      url: url      
      headers:
        'X-Slide': slide  
      success: (data) ->                
        $(document).trigger('wiselinks:success', data)
        # window.app.managers.data.set(data, target)
        # window.app.managers.analytics.hit()

      error: ->        
        $(document).trigger('wiselinks:error')
        # window.app.managers.data.loaded()
      # statusCode:
      #   401: ->
      #     window.location = ROOT_PATH + "auth/login"
      dataType: "html"
    )
  
  _process_form: ($form) ->    
    self = this
    
    $disable = $form.find(':input[value=""]')
    $disable.attr('disabled', true);      
    
    params = {}
    for item in $form.serializeArray()
      if item.name != 'utf8'
        name = if item.name.ends_with('[]')
          item.name.substr(0, item.name.length - 2)
        else
          item.name

        if params[name]?
          params[name] = params[name] + ",#{item.value}"
        else
          params[name] = item.value  

    serialized = []
    for key in params
      serialized.push("#{params[key]}=key")

    serialized = serialized.join('&').replace(/%|!/g, '')
    <commit_msg>First working release<commit_after>#= require _history

class Wiselinks
  constructor: (@$target = $('body'), options = {}) ->
    # check that JQuery or Zepto.js are available
    throw "Load JQuery or Zepto.js to use Wiselinks" unless window.JQuery? || window.Zepto?

    self = this

    History.Adapter.bind(
      window,
      "statechange"
      (event, data) ->
        return if (!History.ready)
  
        state = History.getState()         
        self._call(state.url, state.data.target, state.data.slide)  

        return false
    )
    
    $(document).on(
      "submit", "form[data-push], form[data-replace]"
      (event) ->      
        self._process_form($(this))
        
        event.preventDefault()
        return false
    )
  
    $(document).on(
      "click", "a[data-push], a[data-replace]"
      (event) ->      
        if (event.ctrlKey || event.metaKey)
          return true;
        
        self._process_link($(this))

        event.preventDefault()
        return false
    )
  
  load: (url, target, slide = 'template') ->
    History.ready = true
    History.pushState({ timestamp: (new Date().getTime()), slide: slide, target: target }, document.title, decodeURI(url) )

  reload: () ->
    History.ready = true
    History.replaceState({ timestamp: (new Date().getTime()), slide: 'template' }, document.title, decodeURI(History.getState().url) )

  _call: (url, target, slide = 'template') ->
    self = this
    $(document).trigger('wiselinks:loading')

    console.log @$target

    $.ajax(
      url: url
      headers:
        'X-Slide': slide
      success: (data) ->
        $target = if target? then $(target) else self.$target
        $target.html(data)

        $(document).trigger('wiselinks:success', data)
      error: (xhr)->        
        $(document).trigger('wiselinks:error', xhr)
      dataType: "html"
    )
  
  _process_form: ($form) ->    
    self = this
    
    $disable = $form.find(':input[value=""]')
    $disable.attr('disabled', true);      
    
    params = {}
    for item in $form.serializeArray()
      if item.name != 'utf8'
        name = if item.name.ends_with('[]')
          item.name.substr(0, item.name.length - 2)
        else
          item.name

        if params[name]?
          params[name] = params[name] + ",#{item.value}"
        else
          params[name] = item.value  

    serialized = []
    for key in params
      serialized.push("#{params[key]}=key")

    serialized = serialized.join('&').replace(/%|!/g, '')
    
{'fraction_non_alphanumeric': 0.15228821811100293, 'fraction_numerical': 0.0017526777020447906, 'mean_word_length': 2.0607866507747317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '6708306', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1569, 'n_words': 332}