You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@guacamole.apache.org by jm...@apache.org on 2016/06/30 05:37:01 UTC

[07/32] incubator-guacamole-client git commit: GUACAMOLE-55: Clean up event handlers after guacClipboard directive is destroyed.

GUACAMOLE-55: Clean up event handlers after guacClipboard directive is destroyed.


Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/commit/0a0933a2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/tree/0a0933a2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/diff/0a0933a2

Branch: refs/heads/master
Commit: 0a0933a23e935e80622b37ac292f06cdf3720a76
Parents: 65897fd
Author: Michael Jumper <mj...@apache.org>
Authored: Wed Jun 22 01:46:32 2016 -0700
Committer: Michael Jumper <mj...@apache.org>
Committed: Sun Jun 26 20:44:56 2016 -0700

----------------------------------------------------------------------
 .../app/client/directives/guacClipboard.js      | 39 +++++++++++++-------
 1 file changed, 26 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/0a0933a2/guacamole/src/main/webapp/app/client/directives/guacClipboard.js
----------------------------------------------------------------------
diff --git a/guacamole/src/main/webapp/app/client/directives/guacClipboard.js b/guacamole/src/main/webapp/app/client/directives/guacClipboard.js
index 4f0354e..bd050c9 100644
--- a/guacamole/src/main/webapp/app/client/directives/guacClipboard.js
+++ b/guacamole/src/main/webapp/app/client/directives/guacClipboard.js
@@ -63,6 +63,7 @@ angular.module('client').directive('guacClipboard', [function guacClipboard() {
 
         // Required services
         var $rootScope       = $injector.get('$rootScope');
+        var $window          = $injector.get('$window');
         var clipboardService = $injector.get('clipboardService');
 
         /**
@@ -122,26 +123,38 @@ angular.module('client').directive('guacClipboard', [function guacClipboard() {
         });
 
         /**
-         * Checks whether the clipboard data has changed, firing a new
-         * "guacClipboard" event if it has.
+         * Checks whether the clipboard data has changed, updating the stored
+         * clipboard data if it has. If this function is being called due to a
+         * DOM event, that event should be passed to this function such that the
+         * context of the call can be taken into account. Focus events, in
+         * particular, need to be considered only in the context of the window.
+         *
+         * @param {Event} [e]
+         *     The event currently being handled, if any.
          */
-        var checkClipboard = function checkClipboard() {
+        var checkClipboard = function checkClipboard(e) {
+
+            // Ignore focus events for anything except the window
+            if (e && e.type === 'focus' && e.target !== $window)
+                return;
+
             clipboardService.getLocalClipboard().then(function clipboardRead(data) {
                 $scope.data = data;
             });
+
         };
 
         // Attempt to read the clipboard if it may have changed
-        window.addEventListener('load',  checkClipboard, true);
-        window.addEventListener('copy',  checkClipboard, true);
-        window.addEventListener('cut',   checkClipboard, true);
-        window.addEventListener('focus', function focusGained(e) {
-
-            // Only recheck clipboard if it's the window itself that gained focus
-            if (e.target === window)
-                checkClipboard();
-
-        }, true);
+        $window.addEventListener('copy',  checkClipboard, true);
+        $window.addEventListener('cut',   checkClipboard, true);
+        $window.addEventListener('focus', checkClipboard, true);
+
+        // Clean up on destruction
+        $scope.$on('$destroy', function destroyClipboard() {
+            $window.removeEventListener('copy',  checkClipboard);
+            $window.removeEventListener('cut',   checkClipboard);
+            $window.removeEventListener('focus', checkClipboard);
+        });
 
         // Perform initial clipboard check
         checkClipboard();