public
Description: Simple javascript hotkey plugin for Rails
Homepage: http://codefluency.com
Clone URL: git://github.com/bruce/hotkey.git
Search Repo:
Updated 1.1 release, exported from subversion
bruce (author)
Sun Mar 09 19:41:41 -0700 2008
commit  0423a73262c531ca6b0bc26decb09797a3bb49c0
tree    c347205112817ee063967dc22753353c36d4d66b
...
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
0
@@ -0,0 +1,7 @@
0
+* 2008-03-09
0
+
0
+Version 1.1, checked on Rails 2.0.2.
0
+
0
+* 2006-08-22
0
+
0
+Version 0.5, Hotkey plugin first released.
0
\ No newline at end of file
0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
0
@@ -0,0 +1,73 @@
0
+Hotkey
0
+======
0
+
0
+== ABOUT
0
+
0
+Plugin to add easy support for javascript hotkeys to a Rails application.
0
+
0
+== INSTALLATION
0
+
0
+The hotkey.js file should be copied to public/javascripts upon plugin installation. If it's not been installed, run the available rake task to update:
0
+
0
+ rake hotkey:update
0
+
0
+The hotkey.js file is automatically added to the javascript include defaults, so if you're using javascript_include_tag(:defaults), inclusion of the necessary javascript will be handled automatically. If you're not using :defaults, you can include the necessary JavaScript manually using javascript_include_tag('hotkey'). Please note that Prototype is a dependency.
0
+
0
+IMPORTANT: To activate hotkeys for a given layout, you must add the following to the _body_ tag:
0
+
0
+ onkeydown="Hotkey.process(event)"
0
+
0
+Note: You could also attach Hotkey.process in an onload handler unobtrusively. I'd recommend you do this if you have the javascript chops.
0
+
0
+== USAGE
0
+
0
+While it's recommended that your hotkey definitions are made in a .js vs directly in a view whenever possible, the plugin includes a number of helpers to help you define hotkeys dynamically from Ruby.
0
+
0
+Please note that the all hotkeys defined require the pressing of a modifier key as well; due to the spotty nature of javascript character code handling, the modifier key that it is bound may differ from platform-to-platform and browser-to-browser.
0
+
0
+Note: If you'd like to define hotkeys directly in your javascript files, look at the provided helpers for examples of how to use Hotkey.add in the javascript API. The
0
+
0
+=== Binding a function
0
+
0
+Use hotkey_to_function similar to how you'd use link_to_function (without html options, obviously):
0
+
0
+ <%= hotkey_to_function(:h, "$('help-sidebar').show();") %>
0
+
0
+This will bind 'h' (with a modifier key)
0
+
0
+=== Binding a remote function (AJAX)
0
+
0
+Use hotkey_to_remote similar to how you'd use link_to_remote (without html options, obviously)
0
+
0
+ <%= hotkey_to_remote(:s, :url => {:controller=>'foo', :action => 'bar'})
0
+
0
+This will bind 's' (with a modifier key)
0
+
0
+== CREDITS
0
+
0
+MIT License
0
+Copyright (c) 2006 - 2008 Bruce Williams (http://codefluency.com)
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+'Software'), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sub-license, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
+
0
+
0
+
0
+
0
+
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+require 'hotkey_helper'
0
+
0
+ActionController::Base.send(:helper, :hotkey)
0
+ActionView::Helpers::AssetTagHelper.register_javascript_include_default('hotkey')
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
0
@@ -0,0 +1,8 @@
0
+# Install hook code here
0
+require 'fileutils'
0
+FileUtils.cp(
0
+ File.join(File.dirname(__FILE__), 'public/javascripts/hotkey.js'),
0
+ File.join(File.dirname(__FILE__), '../../../public/javascripts/hotkey.js')
0
+)
0
+puts File.read(File.join(File.dirname(__FILE__), 'README'))
0
+
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -0,0 +1,19 @@
0
+module HotkeyHelper
0
+
0
+ def hotkey_to_remote(character, *args)
0
+ javascript_tag %|Hotkey.add('#{escape_javascript character.to_s}', function(){ #{remote_function(*args)} });|
0
+ end
0
+
0
+ def hotkey_to_function(character, content)
0
+ javascript_tag %|Hotkey.add('#{escape_javascript character.to_s}', function(){ #{content} });|
0
+ end
0
+
0
+ def hotkey_to(character, url)
0
+ hotkey_to_function character, %|window.location = "#{escape_javascript(url_for(url))}";|
0
+ end
0
+
0
+ def remove_hotkey(character)
0
+ javascript_tag %|Hotkey.remove("#{escape_javascript character.to_s}");|
0
+ end
0
+
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
0
@@ -0,0 +1,35 @@
0
+Hotkey = Class.create();
0
+Hotkey.list = $A();
0
+Hotkey.prototype = {
0
+ character: null,
0
+ action: function(){},
0
+ initialize: function(character, action) {
0
+ this.character = character;
0
+ this.action = action;
0
+ },
0
+ matches: function(character) {
0
+ return (this.character == character);
0
+ }
0
+}
0
+Hotkey.add = function(character, action) {
0
+ Hotkey.list.unshift(new Hotkey(character, action));
0
+}
0
+Hotkey.remove = function(character) {
0
+ Hotkey.list.each(function(h,idx){
0
+ if(h.character == character)
0
+ Hotkey.list.splice(idx, 1);
0
+ });
0
+}
0
+Hotkey.process = function(event) {
0
+ event = event || window.event;
0
+ if (event) {
0
+ // Require modifier (Note: different by platform/browser)
0
+ if (event.ctrlKey || event.altKey || event.metaKey) {
0
+ var code = event.charCode || event.which || event.keyCode;
0
+ var character = String.fromCharCode(code).toLowerCase();
0
+ if (event.shiftKey) character = character.toUpperCase();
0
+ if(hotkey = Hotkey.list.detect(function(hk){ return hk.matches(character); }))
0
+ hotkey.action();
0
+ }
0
+ }
0
+}
...
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -0,0 +1,12 @@
0
+namespace :hotkey do
0
+
0
+ desc "Install assets for hotkey plugin"
0
+ task :update do
0
+ cp (
0
+ File.join(File.dirname(__FILE__), '../public/javascripts/hotkey.js'),
0
+ File.join(RAILS_ROOT, 'public', 'javascripts')
0
+ )
0
+ end
0
+
0
+end
0
+
0
\ No newline at end of file
...
 
 
0
...
1
2
3
0
@@ -0,0 +1,2 @@
0
+require 'fileutils'
0
+FileUtils.unlink File.join(File.dirname(__FILE__), '../../../public/javascript/hotkey.js')
0
\ No newline at end of file

Comments

    No one has commented yet.