<commit_before>define [
  "jquery"
  "underscore"
  "moment"
  "chaplin"
  "lib/view_helpers"
  "templates/notification"
], ($, _, moment, Chaplin, viewHelpers, notification) ->
  "use strict"

  utils = Chaplin.utils.beget Chaplin.utils

  _(utils).extend viewHelpers,

    apiCall: (func, data = {}, method = "GET", options = {}) ->
      deferred = $.Deferred()
      promise = deferred.promise()

      data.login = utils.getLoginKey() if utils.isLogged()
      jqxhr = $.ajax
        url: @bnwUrl "/api/#{func}"
        type: method
        data: data
        # This should actually be auto-detectable (backend do return
        # correct Content-Type header) but jQuery sucks.
        dataType: "json"
      jqxhr.done (data) =>
        if data.ok
          @showAlert data.desc, "info" if options.verbose
          deferred.resolve data
        # Must be 4xx HTTP code actually, but @stiletto fucked it up.
        else
          @showAlert data.desc
          deferred.reject data
      jqxhr.fail (err) =>
        text = "Ошибка AJAX: #{err.status} #{err.statusText}"
        @showAlert text
        deferred.reject()
      promise

    showAlert: (text, type = "error", autoHide = true) ->
      alert = $(@renderTemplate(notification, text: text, type: type))
      $("#notifications").append(alert)
      if autoHide
        hide = ->
          alert.fadeOut("slow", -> alert.remove())
        setTimeout hide, 5000

    # Helpers around apiCall

    get: (func, data = {}, verbose = false) ->
      @apiCall func, data, "GET", {verbose}

    post: (func, data = {}, verbose = false) ->
      @apiCall func, data, "POST", {verbose}

    gotoUrl: (url, force = true) ->
      Chaplin.mediator.publish "!router:route", url, forceStartup: force

    # Get and set login info via cookies bakend.
    # Note that we reread login key and user login info from cookies
    # on the each action requires auth. It _could_ be inefficient but
    # I don't think so.

    setCookie: (key, value) ->
      $.cookie key, value, expires: 365, path: "/"

    getLoginKey: ->
      $.cookie "login"

    setLoginKey: (loginKey) ->
      @setCookie "login", loginKey

    setUser: (user) ->
      @setCookie "user", user

    clearAuth: ->
      $.removeCookie "login"
      $.removeCookie "user"

    # Global vars emulation. Doesn't seems like a perfect solution
    # but it works.

    getGlobal: (key) ->
      window.MEOW_GLOBALS?[key]

    setGlobal: (key, value) ->
      window.MEOW_GLOBALS ?= {}
      window.MEOW_GLOBALS[key] = value

    pluralForm: (n, [p1, p2, p5]) ->
      _pluralForm = ->
        if 10 < n < 15
          return p5
        n = n % 10
        if n == 1
          p1
        else if 1 < n < 5
          p2
        else<commit_msg>Fix logout<commit_after>define [
  "jquery"
  "underscore"
  "moment"
  "chaplin"
  "lib/view_helpers"
  "templates/notification"
], ($, _, moment, Chaplin, viewHelpers, notification) ->
  "use strict"

  utils = Chaplin.utils.beget Chaplin.utils

  _(utils).extend viewHelpers,

    apiCall: (func, data = {}, method = "GET", options = {}) ->
      deferred = $.Deferred()
      promise = deferred.promise()

      data.login = utils.getLoginKey() if utils.isLogged()
      jqxhr = $.ajax
        url: @bnwUrl "/api/#{func}"
        type: method
        data: data
        # This should actually be auto-detectable (backend do return
        # correct Content-Type header) but jQuery sucks.
        dataType: "json"
      jqxhr.done (data) =>
        if data.ok
          @showAlert data.desc, "info" if options.verbose
          deferred.resolve data
        # Must be 4xx HTTP code actually, but @stiletto fucked it up.
        else
          @showAlert data.desc
          deferred.reject data
      jqxhr.fail (err) =>
        text = "Ошибка AJAX: #{err.status} #{err.statusText}"
        @showAlert text
        deferred.reject()
      promise

    showAlert: (text, type = "error", autoHide = true) ->
      alert = $(@renderTemplate(notification, text: text, type: type))
      $("#notifications").append(alert)
      if autoHide
        hide = ->
          alert.fadeOut("slow", -> alert.remove())
        setTimeout hide, 5000

    # Helpers around apiCall

    get: (func, data = {}, verbose = false) ->
      @apiCall func, data, "GET", {verbose}

    post: (func, data = {}, verbose = false) ->
      @apiCall func, data, "POST", {verbose}

    gotoUrl: (url, force = true) ->
      Chaplin.mediator.publish "!router:route", url, forceStartup: force

    # Get and set login info via cookies bakend.
    # Note that we reread login key and user login info from cookies
    # on the each action requires auth. It _could_ be inefficient but
    # I don't think so.

    setCookie: (key, value) ->
      $.cookie key, value, expires: 365, path: "/"

    removeCookie: (key) ->
      $.removeCookie key, path: "/"

    getLoginKey: ->
      $.cookie "login"

    setLoginKey: (loginKey) ->
      @setCookie "login", loginKey

    setUser: (user) ->
      @setCookie "user", user

    clearAuth: ->
      @removeCookie "login"
      @removeCookie "user"

    # Global vars emulation. Doesn't seems like a perfect solution
    # but it works.

    getGlobal: (key) ->
      window.MEOW_GLOBALS?[key]

    setGlobal: (key, value) ->
      window.MEOW_GLOBALS ?= {}
      window.MEOW_GLOBALS[key] = value

    pluralForm: (n, [p1, p2, p5]) ->
      _pluralForm = ->
        if 10 < n < 15
          return p5
        n = n % 10
        if n == 1
          p1
        else if 1 < n < 5
          p2
        else
{'fraction_non_alphanumeric': 0.12744570095135524, 'fraction_numerical': 0.008257045413749775, 'mean_word_length': 2.1498021481062746, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 42, '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': '776143', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1734, 'n_words': 519}