Sha256: d16c586fda8128b678efa11deda14759d676988a410e99f60c7a7c8e458af110

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

angular.module("EssayApp.services").factory "utils", ['$timeout', '$window', ($timeout, $window) ->
  api =
    scrollTop: (offset) ->
      _offset = parseInt(offset || 0, 10)
      _top = $('html > body').offset().top - _offset

      $('html, body').animate({scrollTop: _top}, 600)

    redirect: (url)->
      return unless url?
      $window.location.replace(url)

    navigate: (url)->
      return unless url?
      $window.open(url, '_self')

    scrollToElement: (selector, offset = 200) ->
      $timeout (->
        dom = $(selector)
        if dom.length > 0
          _top = dom.offset().top - offset
          api.scrollTop(-_top)
      ), 100, false

    findOrUseDefault: (id, array, default_value = undefined, id_key = 'id')->
      unless angular.isUndefined(id)
        for item in array
          # return founded item
          return item if id == item?[id_key]
      # item not found, return default
      if angular.isUndefined(default_value)
        # return first if no default_value defined
        array?[0]
      else
        default_value

    inGroups: (items, count, fill = undefined)->
      if angular.isArray(items) && (count = parseInt(count)) > 0
        ans = []
        per = ~~(items.length / count)
        ext = items.length % count
        cursor = 0

        for col in [0...count]
          chunk = per + if ext > 0 && col < ext then 1 else 0
          ans.push items.slice(cursor, (cursor = cursor + chunk))

          if (ext > 0) && (col >= ext) && angular.isDefined(fill)
            least = per + 1 - ans[col].length
            ans[col].push(fill) for cell in [0...least]

        ans
      else
        items
  api
]

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/assets/javascripts/app/services/utils.js.coffee