From 0d5958c7296190bb9a814048529502b3ebf90bc2 Mon Sep 17 00:00:00 2001
From: Robert Schilling <rschilling@student.tugraz.at>
Date: Thu, 11 Sep 2014 18:19:49 +0200
Subject: [PATCH] Close Zen mode by ESC, foward/backward

---
 CHANGELOG                                     |  1 +
 app/assets/javascripts/application.js.coffee  |  1 +
 app/assets/javascripts/dispatcher.js.coffee   |  8 ++-
 app/assets/javascripts/zen_mode.js.coffee     | 51 +++++++++++++++++++
 .../merge_requests/_new_submit.html.haml      |  2 +-
 5 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 app/assets/javascripts/zen_mode.js.coffee

diff --git a/CHANGELOG b/CHANGELOG
index 6021da42422..49bf983eb5e 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -26,6 +26,7 @@ v 7.3.0
   - Don't allow edit of system notes
   - Project wiki search (Ralf Seidler)
   - Enabled Shibboleth authentication support (Matus Banas)
+  - Zen mode (fullscreen) for issues/MR/notes (Robert Schilling)
 
 v 7.2.1
   - Delete orphaned labels during label migration (James Brooks)
diff --git a/app/assets/javascripts/application.js.coffee b/app/assets/javascripts/application.js.coffee
index 86ccd8c21ed..9add1304dc7 100644
--- a/app/assets/javascripts/application.js.coffee
+++ b/app/assets/javascripts/application.js.coffee
@@ -34,6 +34,7 @@
 #= require dropzone
 #= require semantic-ui/sidebar
 #= require mousetrap
+#= require mousetrap/pause
 #= require shortcuts
 #= require shortcuts_navigation
 #= require shortcuts_dashboard_navigation
diff --git a/app/assets/javascripts/dispatcher.js.coffee b/app/assets/javascripts/dispatcher.js.coffee
index ae4cf577179..086c09f196e 100644
--- a/app/assets/javascripts/dispatcher.js.coffee
+++ b/app/assets/javascripts/dispatcher.js.coffee
@@ -24,18 +24,22 @@ class Dispatcher
       when 'projects:issues:show'
         new Issue()
         shortcut_handler = new ShortcutsIssueable()
+        new ZenMode()
       when 'projects:milestones:show'
         new Milestone()
-      when 'projects:issues:new'
+      when 'projects:issues:new','projects:issues:edit'
         GitLab.GfmAutoComplete.setup()
         shortcut_handler = new ShortcutsNavigation()
-      when 'projects:merge_requests:new'
+        new ZenMode()
+      when 'projects:merge_requests:new', 'projects:merge_requests:edit'
         GitLab.GfmAutoComplete.setup()
         new Diff()
         shortcut_handler = new ShortcutsNavigation()
+        new ZenMode()
       when 'projects:merge_requests:show'
         new Diff()
         shortcut_handler = new ShortcutsIssueable()
+        new ZenMode()
       when "projects:merge_requests:diffs"
         new Diff()
       when 'projects:merge_requests:index'
diff --git a/app/assets/javascripts/zen_mode.js.coffee b/app/assets/javascripts/zen_mode.js.coffee
new file mode 100644
index 00000000000..aea707d8550
--- /dev/null
+++ b/app/assets/javascripts/zen_mode.js.coffee
@@ -0,0 +1,51 @@
+class @ZenMode
+  @fullscreen_prefix = 'fullscreen_'
+  @ESC = 27
+
+  constructor: ->
+    @active_zen_area = null
+    @active_checkbox = null
+
+    $('body').on 'change', '.zennable input[type=checkbox]', (e) =>
+      checkbox = e.currentTarget;
+      if checkbox.checked
+        Mousetrap.pause()
+        @udpateActiveZenArea(checkbox)
+      else
+        @exitZenMode()
+
+    $(document).on 'keydown', (e) =>
+      console.log("esc")
+      if e.keyCode is ZenMode.ESC
+        @exitZenMode()
+
+    $(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')
+  
+  exitZenMode: =>
+    if @active_zen_area isnt null
+      Mousetrap.unpause()
+      @active_checkbox.prop('checked', false)
+      @active_zen_area = null
+      @active_checkbox = null
+      window.location.hash = ''
+
+  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
+      @exitZenMode()      
diff --git a/app/views/projects/merge_requests/_new_submit.html.haml b/app/views/projects/merge_requests/_new_submit.html.haml
index 248f6a00522..657a77eb758 100644
--- a/app/views/projects/merge_requests/_new_submit.html.haml
+++ b/app/views/projects/merge_requests/_new_submit.html.haml
@@ -24,7 +24,7 @@
         .zennable
           %input#zen-toggle-comment{ tabindex: '-1', type: 'checkbox' }
           .zen-backdrop
-            = f.text_area :description, class: 'form-control js-gfm-input markdown-area mousetrap', rows: 10, placeholder: 'Leave a comment'
+            = f.text_area :description, class: 'form-control js-gfm-input markdown-area', rows: 10, placeholder: 'Leave a comment'
             %label{ for: 'zen-toggle-comment', class: 'expand' } Edit in fullscreen
             %label{ for: 'zen-toggle-comment', class: 'collapse' }
         .clearfix.hint
-- 
GitLab