From 41ec250f0a376758a7f93fc517ccdede73135059 Mon Sep 17 00:00:00 2001
From: Kathrin Osswald <kathrin.osswald@uni-ulm.de>
Date: Tue, 26 Sep 2017 12:48:35 +0200
Subject: [PATCH] Improved catching shortcuts feature to prevent the catching
 when form elements are focused.

---
 CHANGES.md                      |  1 +
 amd/build/catchshortcuts.min.js |  2 +-
 amd/src/catchshortcuts.js       | 37 +++++++++++++++++++++++++++------
 3 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 2289d0b..dce59a6 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -6,6 +6,7 @@ Changes
 
 ### Unreleased
 
+* 2017-09-25 - Improved catching shortcuts feature to prevent the catching when form elements are focused.
 * 2017-09-22 - Check if user is logged in before showing the switched role information box.
 
 ### Release v3.2-r3
diff --git a/amd/build/catchshortcuts.min.js b/amd/build/catchshortcuts.min.js
index abc970c..24aa6e9 100644
--- a/amd/build/catchshortcuts.min.js
+++ b/amd/build/catchshortcuts.min.js
@@ -1 +1 @@
-define(["jquery"],function(a){"use strict";function b(b){"end"==b&&a(document).keydown(function(a){35==a.keyCode&&c(a)}),navigator.appVersion.indexOf("Mac")!=-1&&"cmdarrowdown"==b&&a(document).keydown(function(a){40==a.keyCode&&a.metaKey&&c(a)}),navigator.appVersion.indexOf("Win")!=-1&&"ctrlarrowdown"==b&&a(document).keydown(function(a){40==a.keyCode&&a.ctrlKey&&c(a)})}function c(b){b.preventDefault(),a("html, body").animate({scrollTop:a("#page-footer").offset().top-a(window).height()+50},500)}return{init:function(a){for(var c=0,d=a.length;c<d;c++)b(a[c])}}});
\ No newline at end of file
+define(["jquery"],function(a){"use strict";function b(b){"end"==b&&a(document).keydown(function(a){35==a.keyCode&&1!=d()&&c(a)}),navigator.appVersion.indexOf("Mac")!=-1&&"cmdarrowdown"==b&&a(document).keydown(function(a){40==a.keyCode&&a.metaKey&&1!=d()&&c(a)}),navigator.appVersion.indexOf("Win")!=-1&&"ctrlarrowdown"==b&&a(document).keydown(function(a){40==a.keyCode&&a.ctrlKey&&1!=d()&&c(a)})}function c(b){b.preventDefault(),a("html, body").animate({scrollTop:a("#page-footer").offset().top-a(window).height()+50},500)}function d(){var a=document.activeElement.tagName,b=!1;return"INPUT"!=a&&"TEXTAREA"!=a||(b=!0),b}return{init:function(a){for(var c=0,d=a.length;c<d;c++)b(a[c])}}});
\ No newline at end of file
diff --git a/amd/src/catchshortcuts.js b/amd/src/catchshortcuts.js
index bbfdf8f..c39543e 100644
--- a/amd/src/catchshortcuts.js
+++ b/amd/src/catchshortcuts.js
@@ -34,8 +34,11 @@ define(['jquery'], function($) {
             // Catch the end key to be able to change the behavior.
             $(document).keydown(function(e) {
                 if (e.keyCode == 35) {
-                    // Scroll only to the bottom of the course content.
-                    scrollToBottomOfCourse(e);
+                    // Additionally check no active focus in form elements.
+                    if (checkForActiveFormElement() != true) {
+                        // Scroll only to the bottom of the course content.
+                        scrollToBottomOfCourse(e);
+                    }
                 }
             });
         }
@@ -44,8 +47,11 @@ define(['jquery'], function($) {
             // Bind the cmd + arrow down shortcut to be able to change the behavior.
             $(document).keydown(function(e) {
                 if (e.keyCode == 40 && e.metaKey) {
-                    // Scroll only to the bottom of the course content.
-                    scrollToBottomOfCourse(e);
+                    // Additionally check no active focus in form elements.
+                    if (checkForActiveFormElement() != true) {
+                        // Scroll only to the bottom of the course content.
+                        scrollToBottomOfCourse(e);
+                    }
                 }
             });
         }
@@ -54,8 +60,11 @@ define(['jquery'], function($) {
             // Bind the ctrl + arrow down shortcut to be able to change the behavior.
             $(document).keydown(function(e) {
                 if (e.keyCode == 40 && e.ctrlKey) {
-                    // Scroll only to the bottom of the course content.
-                    scrollToBottomOfCourse(e);
+                    // Additionally check no active focus in form elements.
+                    if (checkForActiveFormElement() != true) {
+                        // Scroll only to the bottom of the course content.
+                        scrollToBottomOfCourse(e);
+                    }
                 }
             });
         }
@@ -75,6 +84,22 @@ define(['jquery'], function($) {
         }, 500);
     }
 
+    /**
+     * Function to check for an active form element.
+     *
+     * @return boolean
+     */
+    function checkForActiveFormElement() {
+        // Get the active Element for the current page.
+        var activeElement = document.activeElement.tagName;
+        var returnvalue = false;
+        // Check if the given active element is an input field or a textarea.
+        if (activeElement == 'INPUT' || activeElement == 'TEXTAREA') {
+            returnvalue = true;
+        }
+        return returnvalue;
+    }
+
     return {
         init: function(params) {
             for (var i = 0, len = params.length; i < len; i++) {
-- 
GitLab