breakpoints.coffee 1016 Bytes
Newer Older
1
class @Breakpoints
Phil Hughes's avatar
Phil Hughes committed
2
  instance = null;
3

Phil Hughes's avatar
Phil Hughes committed
4 5
  class BreakpointInstance
    BREAKPOINTS = ["xs", "sm", "md", "lg"]
6

Phil Hughes's avatar
Phil Hughes committed
7 8
    constructor: ->
      @setup()
9

Phil Hughes's avatar
Phil Hughes committed
10 11 12 13
    setup: ->
      allDeviceSelector = BREAKPOINTS.map (breakpoint) ->
        ".device-#{breakpoint}"
      return if $(allDeviceSelector.join(",")).length
14

Phil Hughes's avatar
Phil Hughes committed
15
      # Create all the elements
16 17 18
      els = $.map BREAKPOINTS, (breakpoint) ->
        "<div class='device-#{breakpoint} visible-#{breakpoint}'></div>"
      $("body").append els.join('')
19

20
    visibleDevice: ->
Phil Hughes's avatar
Phil Hughes committed
21 22
      allDeviceSelector = BREAKPOINTS.map (breakpoint) ->
        ".device-#{breakpoint}"
23
      $(allDeviceSelector.join(",")).filter(":visible")
Phil Hughes's avatar
Phil Hughes committed
24

25 26 27 28 29 30
    getBreakpointSize: ->
      $visibleDevice = @visibleDevice
      # the page refreshed via turbolinks
      if not $visibleDevice().length
        @setup()
      $visibleDevice = @visibleDevice()
Phil Hughes's avatar
Phil Hughes committed
31 32 33 34 35
      return $visibleDevice.attr("class").split("visible-")[1]

  @get: ->
    return instance ?= new BreakpointInstance

36 37
$ =>
  @bp = Breakpoints.get()