mousetrap-rails 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.0.2
2
+
3
+ * add rails generator for mousetrap
4
+
1
5
  ## v0.0.1
2
6
 
3
7
  * initial release
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
- # Mousetrap::Rails
1
+ # Mousetrap::Rails [![endorse](http://api.coderwall.com/kugaevsky/endorsecount.png)](http://coderwall.com/kugaevsky)
2
2
 
3
3
  [Moustrap](https://github.com/ccampbell/mousetrap) is a javascript library for handling keyboard shortcuts in your web applications written by [Craig Campbell](http://craig.is/).
4
4
 
5
5
  The `mousetrap-rails` gem integrates Moustrap javascript library with Rails Asset Pipeline.
6
6
 
7
+
7
8
  ## Installation
8
9
 
9
- ### Install mousetrap-rails gem
10
+ ### Add mousetrap-rails gem to app
10
11
 
11
12
  Add this line to your application's Gemfile:
12
13
 
@@ -16,26 +17,77 @@ And then execute:
16
17
 
17
18
  $ bundle install
18
19
 
19
- ### Include moustrap to asset pipeline
20
+ ### Run generator
20
21
 
21
- Add to your `app/assets/javascripts/application.js` file
22
+ $ rails generate mousetrap:install
22
23
 
23
- //= require mousetrap
24
+ It will create sample `keybindings.js.coffee` file in `app/assets/javascripts` and add `//= require mousetrap` to `application.js` manifest.
24
25
 
25
26
  Voila!
26
27
 
28
+ ### Latest (may be unstable) version
29
+
30
+ Instead of `gem 'mousetrap-rails'` add to your Gemfile
31
+
32
+ gem 'mousetrap-rails', git: 'git://github.com/kugaevsky/mousetrap-rails.git'
33
+
34
+
27
35
  ## Usage
28
36
 
29
- Now you can use Moustrap library features in your rails application. To test it out create coffeescript file in your `app/assets/javascripts` directory and add to it this code
37
+ ### Via data-attributes
38
+
39
+ You can add keyboard navigation to your links by using `data-keybinding` attribute.
40
+
41
+ = link_to 'Homepage', root_path, data: { keybinding: 'h' } # Press 'h' to navigate to homepage
42
+
43
+ You can jump to input
44
+
45
+ = text_field_tag 'Username', nil, data: { keybinding: 'u' } # Press 'u' to focus username input field
46
+
47
+ ### Via javascript
30
48
 
31
- # app/assets/javascripts/test_hotkeys.js.coffee
32
- Mousetrap.bind 's', -> console.log 's pressed!'
49
+ Any javascript function can be called with mousetrap
33
50
 
34
- Run application and press `s` on your keyboard. You should see `s pressed!` message in your javascript console.
51
+ ```coffeescript
52
+ Mousetrap.bind 'f', (e) -> alert 'My perfect function called'
53
+ ```
54
+
55
+ ### More examples (from official guide)
56
+
57
+ ```coffeescript
58
+ # single keys
59
+ Mousetrap.bind '4', -> alert '4 pressed!'
60
+ Mousetrap.bind 'x', (-> alert 'x pressed!'), 'keyup'
61
+
62
+ # combinations
63
+ Mousetrap.bind 'command+shift+k', ->
64
+ alert 'command+shift+k pressed!'
65
+ false
66
+
67
+ Mousetrap.bind ['command+k', 'ctrl+k'], ->
68
+ alert 'command+k or ctrl+k pressed!'
69
+ false
70
+
71
+ # gmail style sequences
72
+ Mousetrap.bind 'g i', -> console.log 'g i sequence pressed!'
73
+ Mousetrap.bind '* a', -> console.log '* a sequence pressed!'
74
+
75
+ # konami code!
76
+ Mousetrap.bind 'up up down down left right left right b a enter', -> console.log 'You WIN!'
77
+ ```
35
78
 
36
79
  You can find full documentation on [Moustrap library page](http://craig.is/killing/mice). Really, look there – there are plenty examples of using this awesome library.
37
80
 
81
+
82
+ ## TODO
83
+
84
+ * Generators
85
+ * Helpers
86
+ * Keyboard navigation for links
87
+ * Keyboard navigation for inputs
88
+
89
+
38
90
  ## Authors
39
91
 
40
92
  * mousetrap-rails gem by [Nick Kugaevsky](http://kugaevsky.ru)
41
- * original moustrap library by [Craig Campbell](http://craig.is/).
93
+ * original moustrap library by [Craig Campbell](http://craig.is/)
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Mousetrap generator installs mousetrap javascript library in your Rails application
3
+
4
+ Example:
5
+ rails generate mousetrap:install
6
+
7
+ This will:
8
+ Create `keybindings.js.coffee' file in your `app/assets/javascripts' directory
9
+ And add `//= require mousetrap' line to `application.js' file to include library to Rails Asset Pipeline
@@ -0,0 +1,20 @@
1
+ module Mousetrap
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ desc "Copy Mousetrap default files"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def copy_mousetrap
8
+ copy_file "keybindings.js.coffee", "app/assets/javascripts/keybindings.js.coffee"
9
+ end
10
+
11
+ def add_assets
12
+ if File.exist?('app/assets/javascripts/application.js')
13
+ insert_into_file "app/assets/javascripts/application.js", "//= require mousetrap\n", :after => "jquery_ujs\n"
14
+ else
15
+ copy_file "application.js", "app/assets/javascripts/application.js"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require mousetrap
10
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ $ ->
2
+ # Hotkey binding to links with 'data-keybindings' attribute
3
+ # Navigate link when hotkey pressed
4
+ $('a[data-keybinding]').each (i, el) ->
5
+ Mousetrap.bind $(el).data('keybinding'), (e) -> el.click()
6
+
7
+ # Hotkey binding to inputs with 'data-keybindings' attribute
8
+ # Focus input when hotkey pressed
9
+ $('input[data-keybinding]').each (i, el) ->
10
+ Mousetrap.bind $(el).data('keybinding'), (e) ->
11
+ el.focus()
12
+ if e.preventDefault
13
+ e.preventDefault()
14
+ else
15
+ e.returnValue = false
@@ -1,5 +1,5 @@
1
1
  module Mousetrap
2
2
  module Rails
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mousetrap-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-22 00:00:00.000000000 Z
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Mousetrap is a javascript library for handling keyboard shortcuts in
15
15
  your web applications. This gem integrates Mousetrap with Rails asset pipeline for
@@ -26,6 +26,10 @@ files:
26
26
  - LICENSE.txt
27
27
  - README.md
28
28
  - Rakefile
29
+ - lib/generators/mousetrap/install/USAGE
30
+ - lib/generators/mousetrap/install/install_generator.rb
31
+ - lib/generators/mousetrap/install/templates/application.js
32
+ - lib/generators/mousetrap/install/templates/keybindings.js.coffee
29
33
  - lib/mousetrap-rails.rb
30
34
  - lib/mousetrap-rails/engine.rb
31
35
  - lib/mousetrap-rails/railtie.rb