GUACAMOLE-55: Only the guacClipboard directive should fire guacClipboard events.

This commit is contained in:
Michael Jumper
2016-06-22 00:26:46 -07:00
parent 0eed6c32ae
commit 0edc730308
4 changed files with 90 additions and 86 deletions

View File

@@ -23,7 +23,9 @@
* cannot be directly accessed, the user can at least directly copy/paste data
* within the field provided by this directive. The contents of this clipboard
* directive, whether retrieved from the local or manipulated manually by the
* user, are exposed via the "data" attribute.
* user, are exposed via the "data" attribute. In addition to updating the
* "data" attribute, changes to clipboard data will be broadcast on the scope
* via "guacClipboard" events.
*/
angular.module('client').directive('guacClipboard', [function guacClipboard() {
@@ -59,7 +61,87 @@ angular.module('client').directive('guacClipboard', [function guacClipboard() {
config.controller = ['$scope', '$injector', '$element',
function guacClipboardController($scope, $injector, $element) {
// STUB
// Required services
var $rootScope = $injector.get('$rootScope');
var clipboardService = $injector.get('clipboardService');
/**
* Map of all currently pressed keys by keysym. If a particular key is
* currently pressed, the value stored under that key's keysym within
* this map will be true. All keys not currently pressed will not have entries
* within this map.
*
* @type Object.<Number, Boolean>
*/
var keysCurrentlyPressed = {};
/**
* Map of all currently pressed keys (by keysym) to the clipboard
* contents received while those keys were pressed. All keys not
* currently pressed will not have entries within this map.
*
* @type Object.<Number, String>
*/
var clipboardDataFromKey = {};
// Watch clipboard for new data, associating it with any pressed keys
$scope.$watch('data', function clipboardChanged(data) {
// Associate new clipboard data with any currently-pressed key
for (var keysym in keysCurrentlyPressed)
clipboardDataFromKey[keysym] = data;
// Notify of updated clipboard data
$rootScope.$broadcast('guacClipboard', 'text/plain', data);
});
// Track pressed keys
$scope.$on('guacKeydown', function keydownListener(event, keysym, keyboard) {
// Record key as pressed
keysCurrentlyPressed[keysym] = true;
});
// Update pressed keys as they are released, synchronizing the clipboard
// with any data that appears to have come from those key presses
$scope.$on('guacKeyup', function keyupListener(event, keysym, keyboard) {
// Sync local clipboard with any clipboard data received while this
// key was pressed (if any)
var clipboardData = clipboardDataFromKey[keysym];
if (clipboardData) {
clipboardService.setLocalClipboard(clipboardData);
delete clipboardDataFromKey[keysym];
}
// Mark key as released
delete keysCurrentlyPressed[keysym];
});
/**
* Checks whether the clipboard data has changed, firing a new
* "guacClipboard" event if it has.
*/
var checkClipboard = function checkClipboard() {
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);
}];