debian-mirror-gitlab/app/assets/javascripts/zen_mode.js.coffee

55 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2015-09-11 14:41:01 +05:30
#= require dropzone
#= require mousetrap
#= require mousetrap/pause
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
class @ZenMode
2015-04-26 12:48:37 +05:30
constructor: ->
@active_zen_area = null
@active_checkbox = null
@scroll_position = 0
$(window).scroll =>
if not @active_checkbox
@scroll_position = window.pageYOffset
$('body').on 'click', '.zen-enter-link', (e) =>
e.preventDefault()
2015-09-11 14:41:01 +05:30
$(e.currentTarget).closest('.zennable').find('.zen-toggle-comment').prop('checked', true).change()
2015-04-26 12:48:37 +05:30
$('body').on 'click', '.zen-leave-link', (e) =>
e.preventDefault()
2015-09-11 14:41:01 +05:30
$(e.currentTarget).closest('.zennable').find('.zen-toggle-comment').prop('checked', false).change()
2015-04-26 12:48:37 +05:30
$('body').on 'change', '.zen-toggle-comment', (e) =>
checkbox = e.currentTarget
if checkbox.checked
# Disable other keyboard shortcuts in ZEN mode
Mousetrap.pause()
2015-09-11 14:41:01 +05:30
@updateActiveZenArea(checkbox)
2015-04-26 12:48:37 +05:30
else
@exitZenMode()
$(document).on 'keydown', (e) =>
2015-09-11 14:41:01 +05:30
if e.keyCode is 27 # Esc
2015-04-26 12:48:37 +05:30
@exitZenMode()
e.preventDefault()
2015-09-11 14:41:01 +05:30
updateActiveZenArea: (checkbox) =>
2015-04-26 12:48:37 +05:30
@active_checkbox = $(checkbox)
@active_checkbox.prop('checked', true)
@active_zen_area = @active_checkbox.parent().find('textarea')
@active_zen_area.focus()
exitZenMode: =>
if @active_zen_area isnt null
Mousetrap.unpause()
@active_checkbox.prop('checked', false)
@active_zen_area = null
@active_checkbox = null
2015-09-11 14:41:01 +05:30
@restoreScroll(@scroll_position)
2015-04-26 12:48:37 +05:30
# Enable dropzone when leaving ZEN mode
Dropzone.forElement('.div-dropzone').enable()
2015-09-11 14:41:01 +05:30
restoreScroll: (y) ->
window.scrollTo(window.pageXOffset, y)