BigW Consortium Gitlab

zen_mode.js.coffee 1.71 KB
Newer Older
1 2 3 4
#= require dropzone
#= require mousetrap
#= require mousetrap/pause

5 6 7 8
class @ZenMode
  constructor: ->
    @active_zen_area = null
    @active_checkbox = null
9 10 11 12 13
    @scroll_position = 0

    $(window).scroll =>
      if not @active_checkbox
        @scroll_position = window.pageYOffset
14

15 16
    $('body').on 'click', '.zen-enter-link', (e) =>
      e.preventDefault()
17
      $(e.currentTarget).closest('.zennable').find('.zen-toggle-comment').prop('checked', true).change()
18 19 20

    $('body').on 'click', '.zen-leave-link', (e) =>
      e.preventDefault()
21
      $(e.currentTarget).closest('.zennable').find('.zen-toggle-comment').prop('checked', false).change()
22 23

    $('body').on 'change', '.zen-toggle-comment', (e) =>
24
      checkbox = e.currentTarget
25
      if checkbox.checked
26
        # Disable other keyboard shortcuts in ZEN mode
27
        Mousetrap.pause()
28
        @updateActiveZenArea(checkbox)
29 30 31 32
      else
        @exitZenMode()

    $(document).on 'keydown', (e) =>
33
      if e.keyCode is 27 # Esc
34
        @exitZenMode()
35
        e.preventDefault()
36

37
  updateActiveZenArea: (checkbox) =>
38 39 40
    @active_checkbox = $(checkbox)
    @active_checkbox.prop('checked', true)
    @active_zen_area = @active_checkbox.parent().find('textarea')
41 42
    # Prevent a user-resized textarea from persisting to fullscreen
    @active_zen_area.removeAttr('style')
43
    @active_zen_area.focus()
44

45 46 47 48 49 50
  exitZenMode: =>
    if @active_zen_area isnt null
      Mousetrap.unpause()
      @active_checkbox.prop('checked', false)
      @active_zen_area = null
      @active_checkbox = null
51
      @restoreScroll(@scroll_position)
52 53
      # Enable dropzone when leaving ZEN mode
      Dropzone.forElement('.div-dropzone').enable()
54 55 56

  restoreScroll: (y) ->
    window.scrollTo(window.pageXOffset, y)