pagescript 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +20 -0
- data/README.md +137 -0
- data/Rakefile +21 -0
- data/app/assets/config/pagescript_manifest.js +1 -0
- data/app/assets/javascripts/pagescript.js +125 -0
- data/app/assets/stylesheets/pagescript/application.css +15 -0
- data/lib/pagescript.rb +6 -0
- data/lib/pagescript/engine.rb +9 -0
- data/lib/pagescript/helpers/tags_helper.rb +60 -0
- data/lib/pagescript/version.rb +3 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +5 -0
- data/spec/dummy/app/assets/javascripts/application.js +33 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/haikus_controller.rb +14 -0
- data/spec/dummy/app/helpers/application_helper.rb +4 -0
- data/spec/dummy/app/models/haiku.rb +5 -0
- data/spec/dummy/app/views/haikus/index.html.erb +13 -0
- data/spec/dummy/app/views/haikus/new.html.erb +3 -0
- data/spec/dummy/app/views/haikus/show.html.erb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +16 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +34 -0
- data/spec/dummy/bin/update +29 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/config/application.rb +22 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +9 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +51 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/new_framework_defaults.rb +21 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/puma.rb +47 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/log/test.log +37 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/-a/-aX9Yk8ywq6ZNPFEh9x8ZXUaKG9XFVqq8Qe9pE8jYEA.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/0Q/0QUwm-186bFoV7AViyynrzoj-7yzGD5By1fEpB2uxVo.cache +2 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/2K/2KOLH54mhXL0432j0sUROyG3QXNSteLvubePSU6JKGk.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/2Y/2Yxyfmf6W4pqC3WQ6tpt5Wh8teZlZElaRzJkLfYs17M.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/44/44BQmJl1Jgw0aArQu3fgjMySa-iNLYB_GkYZX1p49Do.cache +2 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/9o/9oV17Pqvr4n5vPsYQG3ThkqetgHhyg8NpqutJZ5gAe8.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Bh/Bhc1z03rEiBD6gs8mAXjqNOibA-_pA4qmvPyjSgECuo.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/CR/CRAjazM6UdeSHQf3veWIBtbRjNfQQTFhWWJ-IJXwi0c.cache +3 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Cw/Cwdcaf1mJHhaYA36TsU77OPqZnJuwTWa1DeB5f2lk6E.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/EX/EX_WKAJGVlPMg5zwBKnMGl5U9CHYyLk2qxbCxuRPThQ.cache +2 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Et/Et0SNyP-LInBe4_tGeAtKOzgOTB_ZTrdgvEFCMIibV8.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/F7/F7djknrhxZzDPkr_GuNlLI42uZucXTil7rkXu2Y1uC4.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/FK/FK7dFJpljHVHEgfFDKbzQxc3bN1cBS6aSJ-DOMlqIKE.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Hp/HpUhFMmSUe4uCbQU6s6bG1PnrDgA1H9_9o0RrJhBgQ0.cache +2 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Jy/JyE243RJvJzq3yK-XqdyUe7zU9T6yPuY_EgZ3bWlnGA.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/La/LaIBsM-mWqVhZAmk7D_BWHCeRDXV8vg6WvlE0B2XRaY.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/NR/NR8R5f_QAUHndvPgXstl0cFg2tpLVrAsYaBuTZt_my4.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/P5/P5sQBymdtF9XBsdSsgjmCLl_ORDWblQp1mpor46xJJg.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Pw/PwjCIwNybpEUCYlZaKIZAws5YGks7lVzWV9SeHmTqac.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/QO/QOvpUM1_uH4V310ujW5C0kMeUlikClz4YXa0XSpwkwA.cache +2 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Qw/QwzTWa228RodRfDR7WXC0QDEwzOzeVnSUKmxYDPrBxE.cache +2 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/RU/RUFMlDf7ywaGVksh50Vk8_ONxwa4eYivyhfDH6J8CtA.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/V9/V9s1wLOdRtuJ74soXvCFMzbg17kLZxHa4w38rQxEpBc.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/W_/W_mQqQPd_8QBLVXKKQ10CBW7NAvJvF_-NaduHDHQOoM.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/Yw/YwwwIoIi78wfB4J-Zs2BLGOirh1dJ8_FQuyuxZGykq4.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/ap/apD-0maW7C6annZSgLs5trt4EtDUGOB116_kayPXKVI.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/bI/bIGc1lmBvbXcF9e-rFY05l-olKGgid1fK_LZ5wU7lHE.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/cD/cD6lksrcljcRHGndfCqKKDF_w8TQMxTakOGjxB0K3_8.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/el/elVa94gSE1wrKZXkRFEHPfP1x7vOc7aLY4ApIopl-DM.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/fN/fNM97zlRlEo55oJjbyozttYPHQf1Q0KCnhzcRYjfdNs.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/fQ/fQ9HtpTjfAGLh0l6oo-Xrr6GhWKlvgRRB7vPpGyjQ6Q.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/gj/gjbuFfGzO6JtMDzQwTjsKLQzM6iNnWoTO75xJexVgW4.cache +2 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/h6/h6AXhPdiVtyEEmqBLOSJKh9xt6V9LQtOVK0X1EKEfCM.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/hF/hFAc_e-m9RsUzbJ-j0znwH0gO1GabvsEk6-NIx8SoC4.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/i8/i8Pwia6e1VcMTF-76YtlYRvbj6YsIRz94H6XCG2EpcY.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/n_/n_xYqQYhwEMQknb3jFQnjlxxBE9TzMNHCdJ-bEyZFIw.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/nx/nxTv3sKVUQZADJyM3dPaVmUA78MIsMLD_K279yN_GsI.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/qv/qvBZRNrA3BgC-8ZhUkrkQeIm7uObAakyTvcIfn2Yusk.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/rN/rNflwRtdyDrdC7dBuqwIKxXkfRUPWL1YjuvazJQtrck.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/sz/sz2VK7Ev0ExN5b-fZ51PqWIaUzXqODmWw6fXlNXOKBA.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/tN/tN7UU773XH9oEu5z0jnw1uKLKV_4UG5YHTOdsAyPHiM.cache +3 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/u-/u-9psQcvMhMVXGCghlghMq5CxdjWbUfBTDsLPccAe5k.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/xA/xAwVWATNEW09m5X31uBt_T3rxHC0nStO8akIoBmmPUg.cache +1 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/xU/xUUA7mtxZIxTVdmDMa6sqgpOkubyGjAvNkeMGEB1peM.cache +0 -0
- data/spec/dummy/tmp/cache/assets/sprockets/v3.0/zJ/zJSuXh9W2t5W2MRaoTu4trOegf0HoyuaRWOTPZdavPo.cache +1 -0
- data/spec/examples.txt +22 -0
- data/spec/features/pagescript_js_spec.rb +35 -0
- data/spec/helpers/pagescript/tags_helper_spec.rb +98 -0
- data/spec/rails_helper.rb +48 -0
- data/spec/spec_helper.rb +96 -0
- metadata +226 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf788c27bc7710e84c048a762da8f5ea3de504d3
|
4
|
+
data.tar.gz: 712e21fa0c410c61db48cc8cac287424fd7831c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb896ad96e0ff850547c6919c9757c77045abcbd5f74be4db91377569ad998768eabfe1a8163c63522c1729b82253c084cc687c663f89b449a0201f85e49b939
|
7
|
+
data.tar.gz: 7c2e61a82c66b59bfaaf17324fcc61b902c608b1b6926a3f74b05d97fc672ca79711f0cd5cc0ec347f6c9f066b4299ace8f64ecd13924e25d181c38f548786ba
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Max Calabrese
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# Pagescript
|
2
|
+
|
3
|
+
So you're ready to put on the big girl/boy pants and stop using inline
|
4
|
+
script tags in your views?
|
5
|
+
|
6
|
+
Can't figure out how to hook javascript to specific actions in your Rails app?
|
7
|
+
|
8
|
+
Pagescript can help. It fires custom events based on what controller / action
|
9
|
+
was called and makes it simple to add event handlers.
|
10
|
+
|
11
|
+
Pagescript is intended for classical web applications where Rails
|
12
|
+
handles rendering views. SPA's have better mechanisms for this anyways.
|
13
|
+
|
14
|
+
It is designed to integrate seamlessly with [Turbolinks](https://github.com/turbolinks/turbolinks).
|
15
|
+
|
16
|
+
## Dependencies
|
17
|
+
- Sprockets
|
18
|
+
- jQuery
|
19
|
+
- Turbolinks (optional)
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'pagescript'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
```bash
|
30
|
+
$ bundle
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
Replace your the body tag in your `app/views/layouts/application.html.erb`
|
36
|
+
with `<%= body_tag %>`. This adds [*data* attributes](https://developer.mozilla.org/docs/Web/Guide/HTML/Using_data_attributes) that we can hook onto.
|
37
|
+
|
38
|
+
You can optionally pass the `params_as_metadata: true` option to attach
|
39
|
+
the url parameters to the body element. See [The pagescript event][#the-pagescript-event] for more details.
|
40
|
+
|
41
|
+
```javascript
|
42
|
+
//= pagescript
|
43
|
+
Pagescript.on('users#*', function(){
|
44
|
+
console.log('some action belonging to UsersController was rendered');
|
45
|
+
}) // all methods are chainable
|
46
|
+
.on('users#show', function(){
|
47
|
+
console.log('Users#show action was called');
|
48
|
+
}) // you can remove handlers as well.
|
49
|
+
.off('users#*');
|
50
|
+
|
51
|
+
// This is only required if you are not using Turbolinks
|
52
|
+
Pagescript.jumpstart();
|
53
|
+
```
|
54
|
+
|
55
|
+
Note that you should not wrap this in a `$(document).ready` callback.
|
56
|
+
|
57
|
+
## Pagescript Event types
|
58
|
+
On each page replacement (or load) Pagescript will fire multiple events that you can listen to.
|
59
|
+
`*` symbolizes a wildcard.
|
60
|
+
|
61
|
+
```JS
|
62
|
+
// A specific controller_name and action
|
63
|
+
$.on('pagescript:controller_name#action_name')
|
64
|
+
// Any action belonging to a specific controller
|
65
|
+
$.on('pagescript:controller_name#*')
|
66
|
+
// Any action matching action_name
|
67
|
+
$.on('pagescript:*#action_name')
|
68
|
+
// Any page load
|
69
|
+
$.on('pagescript:*')
|
70
|
+
```
|
71
|
+
|
72
|
+
When using the Pagescript API methods you don't need to include the `pagescript:`
|
73
|
+
"namespace".
|
74
|
+
|
75
|
+
## The Pagescript Event
|
76
|
+
The [Event object](https://api.jquery.com/category/events/event-object/) which
|
77
|
+
is passed to event handlers has the following extra properties:
|
78
|
+
|
79
|
+
- controller [String]
|
80
|
+
- action [String]
|
81
|
+
- params [object]
|
82
|
+
|
83
|
+
The params object is only populated if you have used the `params_as_metadata: true` option.
|
84
|
+
|
85
|
+
```erb
|
86
|
+
<%= body_tag(params_as_metadata: true) %>
|
87
|
+
```
|
88
|
+
|
89
|
+
The param keys are cast to `camelCase` by jQuery.
|
90
|
+
|
91
|
+
## Javascript API
|
92
|
+
All methods return the Pagescript module and can be chained.
|
93
|
+
|
94
|
+
When using the Pagescript API methods you don't need to include the `pagescript:`
|
95
|
+
"namespace" in the event name.
|
96
|
+
|
97
|
+
`on`, `off` and `one` proxy to their jQuery counterparts. Please see the jQuery
|
98
|
+
documentation for more details.
|
99
|
+
|
100
|
+
### Pagescript.kickstart
|
101
|
+
Used to start hook Pagescript to the `document.ready` event in the absense of
|
102
|
+
Turbolinks
|
103
|
+
|
104
|
+
### Pagescript.off
|
105
|
+
Removes all handlers for the given event.
|
106
|
+
See [jQuery.off](http://api.jquery.com/off/)
|
107
|
+
|
108
|
+
### Pagescript.on
|
109
|
+
Attaches an event handler to a controller, a particular action or any action
|
110
|
+
matching the name.
|
111
|
+
|
112
|
+
```JS
|
113
|
+
Pagescript.on('foo#bar', function(e){
|
114
|
+
console.log( e.controller, e.action_name, e.params );
|
115
|
+
// "foo", "bar", {}
|
116
|
+
});
|
117
|
+
```
|
118
|
+
|
119
|
+
See [jQuery.on](http://api.jquery.com/on/)
|
120
|
+
|
121
|
+
### Pagescript.one
|
122
|
+
Like `.on`, but the handler only fires once.
|
123
|
+
|
124
|
+
See [jQuery.one](http://api.jquery.com/one/)
|
125
|
+
|
126
|
+
### Pagescript.start
|
127
|
+
Adds the main event handler which Pagescript hinges on.
|
128
|
+
|
129
|
+
### Pagescript.stop
|
130
|
+
Halts any Pagescript events from being fired - does not remove user created handlers. Call `.start` to reverse the effect.
|
131
|
+
|
132
|
+
## License
|
133
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
134
|
+
|
135
|
+
## Want to help?
|
136
|
+
See [Contributing](CONTRIBUTING.md) for how you can help and [Devlopment](DEVELOPMENT.md) for instructions to setup the test suite and
|
137
|
+
what is expected of you.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
require 'bundler/gem_tasks'
|
9
|
+
require 'yard'
|
10
|
+
require 'rspec/core/rake_task'
|
11
|
+
|
12
|
+
Bundler::GemHelper.install_tasks
|
13
|
+
|
14
|
+
APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
|
15
|
+
load 'rails/tasks/engine.rake'
|
16
|
+
load 'rails/tasks/statistics.rake'
|
17
|
+
|
18
|
+
YARD::Rake::YardocTask.new
|
19
|
+
RSpec::Core::RakeTask.new(:spec)
|
20
|
+
|
21
|
+
task default: :spec
|
@@ -0,0 +1 @@
|
|
1
|
+
//= link_directory ../javascripts/pagescript .js
|
@@ -0,0 +1,125 @@
|
|
1
|
+
//= require jquery
|
2
|
+
var Pagescript = (function(ps, $){
|
3
|
+
var $doc = $(document);
|
4
|
+
|
5
|
+
// "private" methods
|
6
|
+
function namespace(str){
|
7
|
+
return str.match(/pagescript:/) ? str : 'pagescript:' + str;
|
8
|
+
}
|
9
|
+
|
10
|
+
function trigger(str, data){
|
11
|
+
return $doc.trigger({
|
12
|
+
type: namespace(str),
|
13
|
+
controller: data.controller,
|
14
|
+
action: data.action,
|
15
|
+
params: filter_params(data)
|
16
|
+
});
|
17
|
+
}
|
18
|
+
|
19
|
+
function filter_params(obj) {
|
20
|
+
var re = /^params([A-Z])(.*)/;
|
21
|
+
var filtered = {};
|
22
|
+
$.each( obj, function( key, value ) {
|
23
|
+
var matches = key.match(re);
|
24
|
+
if (matches) {
|
25
|
+
filtered[ matches[1].toLowerCase() + (matches[2] || "") ] = value;
|
26
|
+
}
|
27
|
+
});
|
28
|
+
return filtered;
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
function parseEvents(str) {
|
33
|
+
return jQuery.map(str.split(' '), function(s){
|
34
|
+
return namespace(s);
|
35
|
+
}).join(' ');
|
36
|
+
}
|
37
|
+
|
38
|
+
// Integrate with turbolinks
|
39
|
+
// 'page:change' is the turbolinks-classic api
|
40
|
+
// 'turbolinks:load' is the Turbolinks 5+ api
|
41
|
+
$doc.on('page:change turbolinks:load', function(){
|
42
|
+
$doc.trigger('pagescript:load');
|
43
|
+
});
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Adds the main event handler
|
47
|
+
* @api
|
48
|
+
* @return this [Object]
|
49
|
+
*/
|
50
|
+
ps.start = function() {
|
51
|
+
ps.stop();
|
52
|
+
$doc.on('pagescript:load', function(){
|
53
|
+
var data = $('body').data();
|
54
|
+
trigger(data.controller + "#" + data.action, data);
|
55
|
+
trigger(data.controller + "#*", data);
|
56
|
+
trigger("*#" + data.action , data);
|
57
|
+
trigger('*', data);
|
58
|
+
});
|
59
|
+
return ps;
|
60
|
+
};
|
61
|
+
/**
|
62
|
+
* Halts any Pagescript events from being fired - does not remove the handlers.
|
63
|
+
* @api
|
64
|
+
* @return this [Object]
|
65
|
+
**/
|
66
|
+
ps.stop = function(){
|
67
|
+
$doc.off('pagescript:load');
|
68
|
+
return this;
|
69
|
+
};
|
70
|
+
/**
|
71
|
+
* Used to hook Pagescript to the `document.ready`
|
72
|
+
* event in the absense of Turbolinks.
|
73
|
+
* @api
|
74
|
+
* @return this [Object]
|
75
|
+
**/
|
76
|
+
ps.kickstart = function(){
|
77
|
+
$doc.ready(function(){ $doc.trigger('pagescript:load'); });
|
78
|
+
return this;
|
79
|
+
};
|
80
|
+
/**
|
81
|
+
* Attaches an event handler to a controller, a particular action or any action.
|
82
|
+
* @api
|
83
|
+
* @param events [String]
|
84
|
+
* @param selector [String] optional
|
85
|
+
* @param data [Object] optional
|
86
|
+
* @param handler [Function]
|
87
|
+
* @return this [Object]
|
88
|
+
**/
|
89
|
+
ps.on = function( events, selector, data, handler ){
|
90
|
+
var args = $.makeArray( arguments );
|
91
|
+
args[0] = parseEvents(args[0]);
|
92
|
+
$doc.on.apply($doc, args);
|
93
|
+
return this;
|
94
|
+
};
|
95
|
+
/**
|
96
|
+
* @api
|
97
|
+
* @param events [String]
|
98
|
+
* @param selector [String] optional
|
99
|
+
* @param handler [Function]
|
100
|
+
* @return this [Object]
|
101
|
+
**/
|
102
|
+
ps.off = function( events, selector, handler ){
|
103
|
+
var args = $.makeArray( arguments );
|
104
|
+
args[0] = parseEvents(args[0]);
|
105
|
+
$doc.off.apply($doc, args);
|
106
|
+
return this;
|
107
|
+
};
|
108
|
+
/**
|
109
|
+
* @api
|
110
|
+
* @param events [String]
|
111
|
+
* @param selector [String] optional
|
112
|
+
* @param data [Object] optional
|
113
|
+
* @param handler [Function]
|
114
|
+
* @return this [Object]
|
115
|
+
**/
|
116
|
+
ps.one = function( events, selector, data, handler ){
|
117
|
+
var args = $.makeArray( arguments );
|
118
|
+
args[0] = parseEvents(args[0]);
|
119
|
+
$doc.one.apply($doc, args);
|
120
|
+
return this;
|
121
|
+
}
|
122
|
+
|
123
|
+
// Export module
|
124
|
+
return ps.start();
|
125
|
+
}(Pagescript || {}, jQuery));
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
data/lib/pagescript.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
module Pagescript
|
2
|
+
# Main Rails integration.
|
3
|
+
# Used primarily to add the gem assets to the Sprockets pipeline.
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
require 'jquery-rails'
|
6
|
+
isolate_namespace Pagescript
|
7
|
+
ActionView::Base.send :include, Pagescript::TagsHelper
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Pagescript
|
2
|
+
# A helper that creates elements with metadata attached as data attributes.
|
3
|
+
module TagsHelper
|
4
|
+
# Generates a `<body>` tag
|
5
|
+
# with HTML5 data attributes for the controller & action name.
|
6
|
+
# The keyword arguments are forwarded to tag and content_tag.
|
7
|
+
# @see http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html ActionView::Helpers::TagHelper
|
8
|
+
# @return [String]
|
9
|
+
# @yield Yields flow to an optional block.
|
10
|
+
# Using a block adds a closing </body> tag.
|
11
|
+
# @param [Boolean] params_as_metadata default: false.
|
12
|
+
# Includes params from the URL as data attributes.
|
13
|
+
# The data attributes are prefixed with `param-`.
|
14
|
+
# Underscores in the param name are converted to '-'.
|
15
|
+
# @example when called without a block
|
16
|
+
# body_tag
|
17
|
+
# => <body data-controller="foo" data-action="bar">
|
18
|
+
# @example when called with a block
|
19
|
+
# body_tag { 'hello world' }
|
20
|
+
# => <body data-controller="foo" data-action="bar">Hello World</body>
|
21
|
+
# @example when called with keyword arguments
|
22
|
+
# body_tag(foo: 'bar', data: { x: 2 })
|
23
|
+
# => <body data-controller="foo" data-action="bar" foo="" data-x="2">
|
24
|
+
# @example when params_as_metadata is true
|
25
|
+
# body_tag(params_as_metadata: true)
|
26
|
+
# => <body data-controller="foo" data-action="bar" data-params-user-id="2">
|
27
|
+
# @since 0.1.0
|
28
|
+
# @api
|
29
|
+
def body_tag(params_as_metadata: false, **kwargs)
|
30
|
+
options = kwargs.deep_merge( data: data_attrs(params_as_metadata) )
|
31
|
+
if block_given?
|
32
|
+
content_tag(:body, options) { yield }
|
33
|
+
else
|
34
|
+
tag(:body, options, true)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def data_attrs(params_as_metadata = false)
|
41
|
+
{
|
42
|
+
controller: controller_name,
|
43
|
+
action: action_name
|
44
|
+
}.tap do |hash|
|
45
|
+
if params_as_metadata
|
46
|
+
params_from_url.each do |k, v|
|
47
|
+
hash["params-#{k}"] = v
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def params_from_url
|
54
|
+
{}.tap do |h|
|
55
|
+
h.merge! request.path_parameters
|
56
|
+
h.merge! request.query_parameters
|
57
|
+
end.except(:controller, :action)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//= require jquery
|
13
|
+
//= require jquery_ujs
|
14
|
+
//= require pagescript
|
15
|
+
//= require_tree .
|
16
|
+
|
17
|
+
function log(e){
|
18
|
+
$("#logger").append('<li>'+e.type+'</li>');
|
19
|
+
}
|
20
|
+
|
21
|
+
Pagescript
|
22
|
+
.on('haikus#* *#index haikus#index', log)
|
23
|
+
.on('*#show', log)
|
24
|
+
.on('haikus#show', log)
|
25
|
+
.on('pagescript:*', function(e){
|
26
|
+
$('#test').text(JSON.stringify({
|
27
|
+
type: e.type,
|
28
|
+
params: e.params,
|
29
|
+
action: e.action,
|
30
|
+
controller: e.controller
|
31
|
+
}));
|
32
|
+
})
|
33
|
+
.kickstart();
|