From 57c928f1957901367cb63fbc6b0c636103fa6b77 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 16 Jan 2014 23:28:58 +0100 Subject: [PATCH] Add esc/enter support to cancel or accept popups. --- js/AddShapeDialog.js | 3 +++ js/Keyboard.js | 12 ++++++++++++ js/Popup.js | 30 ++++++++++++++++++++++-------- js/WordArt.js | 7 +++++-- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/js/AddShapeDialog.js b/js/AddShapeDialog.js index bf2adee..286e904 100644 --- a/js/AddShapeDialog.js +++ b/js/AddShapeDialog.js @@ -7,7 +7,10 @@ function initShapeDialog() { $("#btnShapePlus").on("onButtonHold",onShapePlus); $("#btnShapeMin").on("onButtonHold",onShapeMin); updateShapePreview(); + shapePopup = new Popup($("#popupShape"), $("#popupMask")); + shapePopup.setEscapeKeyHandler(onShapeCancel); + shapePopup.setEnterKeyHandler(onShapeOk); } function showShapeDialog() { diff --git a/js/Keyboard.js b/js/Keyboard.js index 93e15df..334f147 100644 --- a/js/Keyboard.js +++ b/js/Keyboard.js @@ -1,4 +1,5 @@ var keyboardShortcutsEnabled = true; +var keyboardEscapeEnterEnabled = false; var wordBuffer = ""; var wordFuncs = { @@ -14,6 +15,17 @@ function initKeyboard() { $(document).keypress(function(event) { + if (keyboardEscapeEnterEnabled) { + switch (event.keyCode) { + case 13: + $(document).trigger("onEnterKey"); + break; + case 27: + $(document).trigger("onEscapeKey"); + break; + } + } + if (!keyboardShortcutsEnabled) return; if (event.ctrlKey && event.altKey && ! event.metaKey) processWords(event); if (event.altKey || event.ctrlKey || event.metaKey) return; //ignore key presses with modifier keys except shift diff --git a/js/Popup.js b/js/Popup.js index 7b4f2a1..f29f013 100644 --- a/js/Popup.js +++ b/js/Popup.js @@ -1,20 +1,34 @@ -function Popup(element,mask) { - +function Popup(element, mask) { var self = this; + var escapeKeyHandler = null; + var enterKeyHandler = null; - this.open = function(complete,disableMaskClick) { + this.open = function(complete, disableMaskClick) { mask.fadeIn(POPUP_SHOW_DURATION); element.fadeIn(POPUP_SHOW_DURATION, complete); - keyboardShortcutsEnabled=false; - document.body.removeEventListener('touchmove',prevent,false); + + keyboardShortcutsEnabled = false; + keyboardEscapeEnterEnabled = true; + + document.body.removeEventListener('touchmove', prevent, false); mask.bind("onButtonClick", function() { self.close() }); + if (escapeKeyHandler) $(document).bind("onEscapeKey", escapeKeyHandler); + if (enterKeyHandler) $(document).bind("onEnterKey", enterKeyHandler); } + this.close = function(complete) { mask.fadeOut(POPUP_SHOW_DURATION); - element.fadeOut(POPUP_SHOW_DURATION,complete); + element.fadeOut(POPUP_SHOW_DURATION, complete); - keyboardShortcutsEnabled=true; - document.body.addEventListener('touchmove',prevent,false); + keyboardShortcutsEnabled = true; + keyboardEscapeEnterEnabled = false; + + document.body.addEventListener('touchmove', prevent, false); mask.unbind("onButtonClick"); + if (escapeKeyHandler) $(document).unbind("onEscapeKey"); + if (enterKeyHandler) $(document).unbind("onEnterKey"); } + + this.setEscapeKeyHandler = function(hnd) { escapeKeyHandler = hnd; } + this.setEnterKeyHandler = function(hnd) { enterKeyHandler = hnd; } } \ No newline at end of file diff --git a/js/WordArt.js b/js/WordArt.js index 5c4425a..105b4cd 100644 --- a/js/WordArt.js +++ b/js/WordArt.js @@ -1,11 +1,14 @@ var wordArtPopup; function initWordArt() { - $("body").append(''); - $("#svgfont").load("img/font.svg?"); + $("body").append(''); + $("#svgfont").load("img/font.svg?"); $("#btnWordArtOk").on("onButtonClick",onWordArtOk); $("#btnWordArtCancel").on("onButtonClick",onWordArtCancel); + wordArtPopup = new Popup($("#popupWordArt"),$("#popupMask")); + wordArtPopup.setEscapeKeyHandler(onWordArtCancel); + wordArtPopup.setEnterKeyHandler(onWordArtOk); } function showWordArtDialog() {