gitlab-org--gitlab-foss/app/assets/javascripts/zen_mode.js.coffee

68 lines
2.1 KiB
CoffeeScript
Raw Normal View History

2014-09-11 12:19:49 -04:00
class @ZenMode
@fullscreen_prefix = 'fullscreen_'
constructor: ->
@active_zen_area = null
@active_checkbox = null
@scroll_position = 0
$(window).scroll =>
if not @active_checkbox
@scroll_position = window.pageYOffset
2014-09-11 12:19:49 -04:00
$('body').on 'click', '.zen-enter-link', (e) =>
e.preventDefault()
$(e.currentTarget).closest('.zennable').find('.zen-toggle-comment').prop('checked', true)
$('body').on 'click', '.zen-leave-link', (e) =>
e.preventDefault()
$(e.currentTarget).closest('.zennable').find('.zen-toggle-comment').prop('checked', false)
$('body').on 'change', '.zen-toggle-comment', (e) =>
2014-09-19 11:32:23 -04:00
checkbox = e.currentTarget
2014-09-11 12:19:49 -04:00
if checkbox.checked
# Disable other keyboard shortcuts in ZEN mode
2014-09-11 12:19:49 -04:00
Mousetrap.pause()
@udpateActiveZenArea(checkbox)
else
@exitZenMode()
$(document).on 'keydown', (e) =>
2014-09-19 11:32:23 -04:00
if e.keyCode is $.ui.keyCode.ESCAPE
2014-09-11 12:19:49 -04:00
@exitZenMode()
e.preventDefault()
2014-09-11 12:19:49 -04:00
$(window).on 'hashchange', @updateZenModeFromLocationHash
udpateActiveZenArea: (checkbox) =>
@active_checkbox = $(checkbox)
@active_checkbox.prop('checked', true)
@active_zen_area = @active_checkbox.parent().find('textarea')
@active_zen_area.focus()
window.location.hash = ZenMode.fullscreen_prefix + @active_checkbox.prop('id')
2014-09-19 11:32:23 -04:00
2014-09-11 12:19:49 -04:00
exitZenMode: =>
if @active_zen_area isnt null
Mousetrap.unpause()
@active_checkbox.prop('checked', false)
@active_zen_area = null
@active_checkbox = null
window.location.hash = ''
window.scrollTo(window.pageXOffset, @scroll_position)
2014-10-01 10:40:30 -04:00
# Enable dropzone when leaving ZEN mode
Dropzone.forElement('.div-dropzone').enable()
2014-09-11 12:19:49 -04:00
checkboxFromLocationHash: (e) ->
id = $.trim(window.location.hash.replace('#' + ZenMode.fullscreen_prefix, ''))
if id
return $('.zennable input[type=checkbox]#' + id)[0]
else
return null
updateZenModeFromLocationHash: (e) =>
checkbox = @checkboxFromLocationHash()
if checkbox
@udpateActiveZenArea(checkbox)
else
2014-09-19 11:32:23 -04:00
@exitZenMode()