BigW Consortium Gitlab

zen_mode.js.coffee 1.8 KB
Newer Older
Robert Speicher committed
1 2 3 4 5 6
# Zen Mode (full screen) textarea
#
#= provides zen_mode:enter
#= provides zen_mode:leave
#
#= require jquery.scrollTo
7 8 9
#= require dropzone
#= require mousetrap
#= require mousetrap/pause
Robert Speicher committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#
# ### Events
#
# `zen_mode:enter`
#
# Fired when the "Edit in fullscreen" link is clicked.
#
# **Synchronicity** Sync
# **Bubbles** Yes
# **Cancelable** No
# **Target** a.js-zen-enter
#
# `zen_mode:leave`
#
# Fired when the "Leave Fullscreen" link is clicked.
#
# **Synchronicity** Sync
# **Bubbles** Yes
# **Cancelable** No
# **Target** a.js-zen-leave
#
31 32
class @ZenMode
  constructor: ->
Robert Speicher committed
33 34
    @active_backdrop = null
    @active_textarea = null
35

Robert Speicher committed
36
    $(document).on 'click', '.js-zen-enter', (e) ->
37
      e.preventDefault()
Robert Speicher committed
38
      $(e.currentTarget).trigger('zen_mode:enter')
39

Robert Speicher committed
40
    $(document).on 'click', '.js-zen-leave', (e) ->
41
      e.preventDefault()
Robert Speicher committed
42 43 44 45 46 47 48 49 50
      $(e.currentTarget).trigger('zen_mode:leave')

    $(document).on 'zen_mode:enter', (e) =>
      @enter(e.target.parentNode)
    $(document).on 'zen_mode:leave', (e) =>
      @exit()

    $(document).on 'keydown', (e) ->
      if e.keyCode == 27 # Esc
51
        e.preventDefault()
Robert Speicher committed
52 53 54 55 56 57 58 59 60
        $(document).trigger('zen_mode:leave')

  enter: (backdrop) ->
    Mousetrap.pause()

    @active_backdrop = $(backdrop)
    @active_backdrop.addClass('fullscreen')

    @active_textarea = @active_backdrop.find('textarea')
61

62
    # Prevent a user-resized textarea from persisting to fullscreen
Robert Speicher committed
63 64
    @active_textarea.removeAttr('style')
    @active_textarea.focus()
65

Robert Speicher committed
66 67
  exit: ->
    if @active_textarea
68
      Mousetrap.unpause()
Robert Speicher committed
69 70 71 72 73 74 75 76

      @active_textarea.closest('.zen-backdrop').removeClass('fullscreen')

      @scrollTo(@active_textarea)

      @active_textarea = null
      @active_backdrop = null

77
      Dropzone.forElement('.div-dropzone').enable()
78

Robert Speicher committed
79 80
  scrollTo: (zen_area) ->
    $.scrollTo(zen_area, 0, offset: -150)