From 135f6e54ad21213573dcd88dc689a1069fa6cdca Mon Sep 17 00:00:00 2001 From: Dirk Kelly Date: Tue, 11 Jan 2011 21:42:47 +0800 Subject: [PATCH] added a $.cmd("key", {}) method, is now used by shortcut.js --- config/assets.yml | 1 + public/javascripts/admin/plugins/cmd.js | 13 +++++++++++++ public/javascripts/admin/plugins/shortcut.js | 3 +-- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 public/javascripts/admin/plugins/cmd.js diff --git a/config/assets.yml b/config/assets.yml index 68753938..aec6a93b 100644 --- a/config/assets.yml +++ b/config/assets.yml @@ -12,6 +12,7 @@ javascripts: - public/javascripts/admin/jquery.ui.js - public/javascripts/admin/rails.js - public/javascripts/admin/utils.js + - public/javascripts/admin/plugins/cmd.js - public/javascripts/admin/plugins/subscribe.js - public/javascripts/admin/plugins/shortcut.js - public/javascripts/admin/plugins/toggle.js diff --git a/public/javascripts/admin/plugins/cmd.js b/public/javascripts/admin/plugins/cmd.js new file mode 100644 index 00000000..7971670e --- /dev/null +++ b/public/javascripts/admin/plugins/cmd.js @@ -0,0 +1,13 @@ +$.cmd = function(key, callback, args) { + var isCtrl = false; + $(document).keydown(function(e) { + if(!args) args=[]; // IE barks when args is null + if(e.metaKey) isCtrl = true; + if(e.keyCode == key.charCodeAt(0) && isCtrl) { + callback.apply(this, args); + return false; + } + }).keyup(function(e) { + if(e.ctrlKey) isCtrl = false; + }); +}; \ No newline at end of file diff --git a/public/javascripts/admin/plugins/shortcut.js b/public/javascripts/admin/plugins/shortcut.js index 6ae11956..4a0544d3 100644 --- a/public/javascripts/admin/plugins/shortcut.js +++ b/public/javascripts/admin/plugins/shortcut.js @@ -43,8 +43,7 @@ jQuery.fn.saveWithShortcut = function() { return this.each(function() { var form = jQuery(this); - jQuery(document).bind('keypress.shortcut', function(event) { - if (!(event.which == 115 && (event.ctrlKey || event.metaKey))) return true; + $.cmd('S', function() { updateFromCodeMirror(); save(form); event.preventDefault();