stateful_link 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,175 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+ })();
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "stateful_link/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "stateful_link"
7
+ s.version = StatefulLink::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = "Helper to generate stateful navigation links."
10
+ s.description = "Helper to generate stateful navigation links."
11
+ s.email = "gzigzigzeo@gmail.com"
12
+ s.authors = ["Victor Sokolov"]
13
+ s.homepage = "http://github.com/gzigzigzeo/stateful_link"
14
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency(%q<rails>, [">= 3"])
22
+ s.add_development_dependency(%q<rspec-rails>, "~> 2.4")
23
+ s.add_development_dependency(%q<capybara>)
24
+ s.add_development_dependency(%q<sqlite3-ruby>)
25
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stateful_link
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
4
+ prerelease:
5
+ version: 0.0.3
11
6
  platform: ruby
12
7
  authors:
13
8
  - Victor Sokolov
@@ -15,125 +10,63 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-01-21 00:00:00 +03:00
13
+ date: 2011-03-22 00:00:00 +03:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
22
- prerelease: false
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 5
29
- segments:
30
- - 3
31
- version: "3"
32
17
  name: rails
33
- requirement: *id001
34
- type: :runtime
35
- - !ruby/object:Gem::Dependency
36
18
  prerelease: false
37
- version_requirements: &id002 !ruby/object:Gem::Requirement
19
+ requirement: &id001 !ruby/object:Gem::Requirement
38
20
  none: false
39
21
  requirements:
40
22
  - - ">="
41
23
  - !ruby/object:Gem::Version
42
- hash: 1
43
- segments:
44
- - 0
45
- - 3
46
- - 9
47
- version: 0.3.9
48
- name: capybara
49
- requirement: *id002
50
- type: :runtime
51
- - !ruby/object:Gem::Dependency
52
- prerelease: false
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
54
- none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
62
- name: sqlite3-ruby
63
- requirement: *id003
24
+ version: "3"
64
25
  type: :runtime
26
+ version_requirements: *id001
65
27
  - !ruby/object:Gem::Dependency
66
- prerelease: false
67
- version_requirements: &id004 !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 31098209
73
- segments:
74
- - 2
75
- - 0
76
- - 0
77
- - beta
78
- version: 2.0.0.beta
79
28
  name: rspec-rails
80
- requirement: *id004
81
- type: :runtime
82
- - !ruby/object:Gem::Dependency
83
29
  prerelease: false
84
- version_requirements: &id005 !ruby/object:Gem::Requirement
30
+ requirement: &id002 !ruby/object:Gem::Requirement
85
31
  none: false
86
32
  requirements:
87
- - - ">="
33
+ - - ~>
88
34
  - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
- version: "0"
93
- name: childprocess
94
- requirement: *id005
35
+ version: "2.4"
95
36
  type: :development
37
+ version_requirements: *id002
96
38
  - !ruby/object:Gem::Dependency
39
+ name: capybara
97
40
  prerelease: false
98
- version_requirements: &id006 !ruby/object:Gem::Requirement
41
+ requirement: &id003 !ruby/object:Gem::Requirement
99
42
  none: false
100
43
  requirements:
101
44
  - - ">="
102
45
  - !ruby/object:Gem::Version
103
- hash: 49
104
- segments:
105
- - 0
106
- - 10
107
- - 3
108
- version: 0.10.3
109
- name: ruby-debug
110
- requirement: *id006
46
+ version: "0"
111
47
  type: :development
48
+ version_requirements: *id003
112
49
  - !ruby/object:Gem::Dependency
50
+ name: sqlite3-ruby
113
51
  prerelease: false
114
- version_requirements: &id007 !ruby/object:Gem::Requirement
52
+ requirement: &id004 !ruby/object:Gem::Requirement
115
53
  none: false
116
54
  requirements:
117
55
  - - ">="
118
56
  - !ruby/object:Gem::Version
119
- hash: 7
120
- segments:
121
- - 3
122
- - 0
123
- - 0
124
- version: 3.0.0
125
- name: rails
126
- requirement: *id007
127
- type: :runtime
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
128
60
  description: Helper to generate stateful navigation links.
129
61
  email: gzigzigzeo@gmail.com
130
62
  executables: []
131
63
 
132
64
  extensions: []
133
65
 
134
- extra_rdoc_files:
135
- - README.rdoc
66
+ extra_rdoc_files: []
67
+
136
68
  files:
69
+ - .gitignore
137
70
  - Gemfile
138
71
  - Gemfile.lock
139
72
  - MIT-LICENSE
@@ -143,11 +76,17 @@ files:
143
76
  - lib/stateful_link/action_any_of.rb
144
77
  - lib/stateful_link/helper.rb
145
78
  - lib/stateful_link/railtie.rb
79
+ - lib/stateful_link/version.rb
146
80
  - spec/controllers/action_any_of_spec.rb
81
+ - spec/dummy/Rakefile
147
82
  - spec/dummy/app/controllers/application_controller.rb
148
83
  - spec/dummy/app/helpers/application_helper.rb
84
+ - spec/dummy/app/views/application/index.html.erb
85
+ - spec/dummy/app/views/layouts/application.html.erb
86
+ - spec/dummy/config.ru
149
87
  - spec/dummy/config/application.rb
150
88
  - spec/dummy/config/boot.rb
89
+ - spec/dummy/config/database.yml
151
90
  - spec/dummy/config/environment.rb
152
91
  - spec/dummy/config/environments/development.rb
153
92
  - spec/dummy/config/environments/production.rb
@@ -157,9 +96,23 @@ files:
157
96
  - spec/dummy/config/initializers/mime_types.rb
158
97
  - spec/dummy/config/initializers/secret_token.rb
159
98
  - spec/dummy/config/initializers/session_store.rb
99
+ - spec/dummy/config/locales/en.yml
160
100
  - spec/dummy/config/routes.rb
101
+ - spec/dummy/public/404.html
102
+ - spec/dummy/public/422.html
103
+ - spec/dummy/public/500.html
104
+ - spec/dummy/public/favicon.ico
105
+ - spec/dummy/public/javascripts/application.js
106
+ - spec/dummy/public/javascripts/controls.js
107
+ - spec/dummy/public/javascripts/dragdrop.js
108
+ - spec/dummy/public/javascripts/effects.js
109
+ - spec/dummy/public/javascripts/prototype.js
110
+ - spec/dummy/public/javascripts/rails.js
111
+ - spec/dummy/public/stylesheets/.gitkeep
112
+ - spec/dummy/script/rails
161
113
  - spec/helpers/stateful_link_spec.rb
162
114
  - spec/spec_helper.rb
115
+ - stateful_link.gemspec
163
116
  has_rdoc: true
164
117
  homepage: http://github.com/gzigzigzeo/stateful_link
165
118
  licenses: []
@@ -174,32 +127,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
127
  requirements:
175
128
  - - ">="
176
129
  - !ruby/object:Gem::Version
177
- hash: 3
178
- segments:
179
- - 0
180
130
  version: "0"
181
131
  required_rubygems_version: !ruby/object:Gem::Requirement
182
132
  none: false
183
133
  requirements:
184
134
  - - ">="
185
135
  - !ruby/object:Gem::Version
186
- hash: 3
187
- segments:
188
- - 0
189
136
  version: "0"
190
137
  requirements: []
191
138
 
192
139
  rubyforge_project:
193
- rubygems_version: 1.3.7
140
+ rubygems_version: 1.5.3
194
141
  signing_key:
195
142
  specification_version: 3
196
143
  summary: Helper to generate stateful navigation links.
197
144
  test_files:
198
145
  - spec/controllers/action_any_of_spec.rb
146
+ - spec/dummy/Rakefile
199
147
  - spec/dummy/app/controllers/application_controller.rb
200
148
  - spec/dummy/app/helpers/application_helper.rb
149
+ - spec/dummy/app/views/application/index.html.erb
150
+ - spec/dummy/app/views/layouts/application.html.erb
151
+ - spec/dummy/config.ru
201
152
  - spec/dummy/config/application.rb
202
153
  - spec/dummy/config/boot.rb
154
+ - spec/dummy/config/database.yml
203
155
  - spec/dummy/config/environment.rb
204
156
  - spec/dummy/config/environments/development.rb
205
157
  - spec/dummy/config/environments/production.rb
@@ -209,6 +161,19 @@ test_files:
209
161
  - spec/dummy/config/initializers/mime_types.rb
210
162
  - spec/dummy/config/initializers/secret_token.rb
211
163
  - spec/dummy/config/initializers/session_store.rb
164
+ - spec/dummy/config/locales/en.yml
212
165
  - spec/dummy/config/routes.rb
166
+ - spec/dummy/public/404.html
167
+ - spec/dummy/public/422.html
168
+ - spec/dummy/public/500.html
169
+ - spec/dummy/public/favicon.ico
170
+ - spec/dummy/public/javascripts/application.js
171
+ - spec/dummy/public/javascripts/controls.js
172
+ - spec/dummy/public/javascripts/dragdrop.js
173
+ - spec/dummy/public/javascripts/effects.js
174
+ - spec/dummy/public/javascripts/prototype.js
175
+ - spec/dummy/public/javascripts/rails.js
176
+ - spec/dummy/public/stylesheets/.gitkeep
177
+ - spec/dummy/script/rails
213
178
  - spec/helpers/stateful_link_spec.rb
214
179
  - spec/spec_helper.rb