turbograft 0.0.10 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1adee05ec0b9f2e394fd907aa38f75574715bcd1
4
- data.tar.gz: 4d7a8bcaf0016e552e684b38dd0323a7f455b71f
3
+ metadata.gz: 472e904d027a454a6c8030eaa4001dcb66bb1735
4
+ data.tar.gz: 341be652819520a0548b3792448fe459180722b8
5
5
  SHA512:
6
- metadata.gz: 41bfd3ae74adb0c3f62c7cefb2ee566ea1193cb2398a37b11186fd9a57289a6772b16b58de58c6237989e1196f65544a6ee931aacebabebfd663648b4075e744
7
- data.tar.gz: 1cd3bc34fd7021780ae31120991cd16bba9cafc8e6f10ea0812f272ebb0816fb0e6a200001810bd16b82f909af901755af953ae42f2843b7a5dd7e21e0579452
6
+ metadata.gz: 35734f8091358c45068f4c58e6f364d384b97df8fecf0669b2f76294f9179316f4df2bc5e843ea0de062c2e48457995aeda97f9464be3e351b554e0a27a42f71
7
+ data.tar.gz: 06b8163fef4a85f09cbf1deab300f93e870f9d79bfabe9ac0f76400860a2e4872f6e621cf94fbd1807069ae3393ea1787c4697ba280f59a426bbf3b73da05fe2
@@ -22,15 +22,15 @@ TurboGraft.handlers.partialGraftClickHandler = (ev) ->
22
22
 
23
23
  TurboGraft.handlers.remoteMethodHandler = (ev) ->
24
24
  target = ev.target
25
- httpRequestType = target.getAttribute('remote-method')
25
+ httpRequestType = target.getAttribute('tg-remote')
26
26
  return unless httpRequestType
27
27
  ev.preventDefault()
28
28
  httpUrl = target.getAttribute('href')
29
- throw new Error("Turbograft developer error: You did not provide a URL for remote-method") unless httpUrl
29
+ throw new Error("Turbograft developer error: You did not provide a URL ('href' attribute) for tg-remote") unless httpUrl
30
30
 
31
31
  if target.getAttribute("remote-once")
32
32
  target.removeAttribute("remote-once")
33
- target.removeAttribute("remote-method")
33
+ target.removeAttribute("tg-remote")
34
34
 
35
35
  options =
36
36
  httpRequestType: httpRequestType
@@ -44,19 +44,17 @@ TurboGraft.handlers.remoteMethodHandler = (ev) ->
44
44
  options.fullRefresh = true
45
45
 
46
46
  remote = new TurboGraft.Remote(options, null, target)
47
+ remote.submit()
47
48
  return
48
49
 
49
50
  TurboGraft.handlers.remoteFormHandler = (ev) ->
50
51
  target = ev.target
51
- return unless target.getAttribute('remote-form')?
52
+ return unless target.getAttribute('tg-remote')?
52
53
  ev.preventDefault()
53
54
  httpUrl = target.getAttribute('action')
54
- httpRequestType = target.getAttribute('method')
55
- throw new Error("Turbograft developer error: You did not provide a request type for remote-method") unless httpRequestType
56
- throw new Error("Turbograft developer error: You did not provide a URL for remote-method") unless httpUrl
55
+ throw new Error("Turbograft developer error: You did not provide a URL ('action' attribute) for tg-remote") unless httpUrl
57
56
 
58
57
  options =
59
- httpRequestType: httpRequestType
60
58
  httpUrl: httpUrl
61
59
  fullRefresh: target.getAttribute('full-refresh')?
62
60
  refreshOnSuccess: target.getAttribute('refresh-on-success')
@@ -67,6 +65,7 @@ TurboGraft.handlers.remoteFormHandler = (ev) ->
67
65
  options.fullRefresh = true
68
66
 
69
67
  remote = new TurboGraft.Remote(options, target, target)
68
+ remote.submit()
70
69
  return
71
70
 
72
71
  documentListenerForButtons = (eventType, handler, useCapture = false) ->
@@ -1,11 +1,11 @@
1
1
  class TurboGraft.Remote
2
2
  constructor: (@opts, form, target) ->
3
- formData = if form then new FormData(form) else new FormData()
4
- @initiator = target || form
5
3
 
6
- actualRequestType = if @opts.httpRequestType.toLowerCase() == 'get' then 'GET' else 'POST'
4
+ @initiator = form || target
7
5
 
8
- formData.append("_method", @opts.httpRequestType)
6
+ @formData = @createPayload(form)
7
+
8
+ actualRequestType = if @opts.httpRequestType?.toLowerCase() == 'get' then 'GET' else 'POST'
9
9
 
10
10
  @refreshOnSuccess = @opts.refreshOnSuccess.split(" ") if @opts.refreshOnSuccess
11
11
  @refreshOnError = @opts.refreshOnError.split(" ") if @opts.refreshOnError
@@ -13,8 +13,11 @@ class TurboGraft.Remote
13
13
 
14
14
  xhr = new XMLHttpRequest
15
15
  xhr.open(actualRequestType, @opts.httpUrl, true)
16
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
16
17
  xhr.setRequestHeader('Accept', 'text/html, application/xhtml+xml, application/xml')
17
- triggerEventFor('turbograft:remote:init', @initiator, xhr: xhr)
18
+ xhr.setRequestHeader("Content-Type", @contentType) if @contentType
19
+
20
+ triggerEventFor('turbograft:remote:init', @initiator, {xhr: xhr, initiator: @initiator})
18
21
 
19
22
  xhr.addEventListener 'loadstart', =>
20
23
  triggerEventFor 'turbograft:remote:start', @initiator,
@@ -28,15 +31,52 @@ class TurboGraft.Remote
28
31
  @onError(event)
29
32
 
30
33
  xhr.addEventListener 'loadend', =>
34
+ @opts.done?()
31
35
  triggerEventFor 'turbograft:remote:always', @initiator,
36
+ initiator: @initiator
32
37
  xhr: xhr
33
38
 
34
- xhr.send(formData)
35
- xhr
39
+ @xhr = xhr
40
+
41
+ submit: ->
42
+ @xhr.send(@formData)
43
+
44
+ createPayload: (form) ->
45
+ if form
46
+ if form.querySelectorAll("[type='file']").length > 0
47
+ formData = new FormData(form)
48
+ else # for much smaller payloads
49
+ formData = @uriEncodeForm(form)
50
+ else
51
+ formData = new FormData()
52
+
53
+ if formData instanceof FormData
54
+ formData.append("_method", @opts.httpRequestType) if @opts.httpRequestType
55
+ else
56
+ @contentType = "application/x-www-form-urlencoded; charset=UTF-8"
57
+
58
+ formData
59
+
60
+ uriEncodeForm: (form) ->
61
+ formData = ""
62
+ inputs = form.querySelectorAll("input:not([type='reset']):not([type='button']):not([type='submit']):not([type='image']), select, textarea")
63
+ for input in inputs
64
+ inputEnabled = !input.disabled
65
+ radioOrCheck = (input.type == 'checkbox' || input.type == 'radio')
66
+
67
+ if inputEnabled && input.name
68
+ if (radioOrCheck && input.checked) || !radioOrCheck
69
+ formData += "#{encodeURI(input.name)}=#{encodeURI(input.value)}&"
70
+
71
+ formData = formData.slice(0,-1) if formData.charAt(formData.length - 1) == "&"
72
+ formData
36
73
 
37
74
  onSuccess: (ev) ->
75
+ @opts.success?()
76
+
38
77
  xhr = ev.target
39
78
  triggerEventFor 'turbograft:remote:success', @initiator,
79
+ initiator: @initiator
40
80
  xhr: xhr
41
81
 
42
82
  if redirect = xhr.getResponseHeader('X-Next-Redirect')
@@ -53,8 +93,11 @@ class TurboGraft.Remote
53
93
  onlyKeys: @refreshOnSuccess
54
94
 
55
95
  onError: (ev) ->
96
+ @opts.fail?()
97
+
56
98
  xhr = ev.target
57
99
  triggerEventFor 'turbograft:remote:fail', @initiator,
100
+ initiator: @initiator
58
101
  xhr: xhr
59
102
 
60
103
  if @refreshOnError || @refreshOnErrorExcept
@@ -1,3 +1,3 @@
1
1
  module TurboGraft
2
- VERSION = '0.0.10'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbograft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Plettenberg-Dussault
@@ -13,174 +13,174 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2014-12-02 00:00:00.000000000 Z
16
+ date: 2014-12-03 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: coffee-rails
20
20
  requirement: !ruby/object:Gem::Requirement
21
21
  requirements:
22
- - - '>='
22
+ - - ">="
23
23
  - !ruby/object:Gem::Version
24
24
  version: '0'
25
25
  type: :runtime
26
26
  prerelease: false
27
27
  version_requirements: !ruby/object:Gem::Requirement
28
28
  requirements:
29
- - - '>='
29
+ - - ">="
30
30
  - !ruby/object:Gem::Version
31
31
  version: '0'
32
32
  - !ruby/object:Gem::Dependency
33
33
  name: bundler
34
34
  requirement: !ruby/object:Gem::Requirement
35
35
  requirements:
36
- - - '>='
36
+ - - ">="
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  type: :development
40
40
  prerelease: false
41
41
  version_requirements: !ruby/object:Gem::Requirement
42
42
  requirements:
43
- - - '>='
43
+ - - ">="
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: rake
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  requirements:
50
- - - '>='
50
+ - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  type: :development
54
54
  prerelease: false
55
55
  version_requirements: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - '>='
57
+ - - ">="
58
58
  - !ruby/object:Gem::Version
59
59
  version: '0'
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rails
62
62
  requirement: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - '>='
64
+ - - ">="
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
69
  version_requirements: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - '>='
71
+ - - ">="
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  - !ruby/object:Gem::Dependency
75
75
  name: jquery-rails
76
76
  requirement: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - '>='
78
+ - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  type: :development
82
82
  prerelease: false
83
83
  version_requirements: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - '>='
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  - !ruby/object:Gem::Dependency
89
89
  name: minitest-reporters
90
90
  requirement: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - '>='
92
+ - - ">="
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  type: :development
96
96
  prerelease: false
97
97
  version_requirements: !ruby/object:Gem::Requirement
98
98
  requirements:
99
- - - '>='
99
+ - - ">="
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: capybara
104
104
  requirement: !ruby/object:Gem::Requirement
105
105
  requirements:
106
- - - '>='
106
+ - - ">="
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  type: :development
110
110
  prerelease: false
111
111
  version_requirements: !ruby/object:Gem::Requirement
112
112
  requirements:
113
- - - '>='
113
+ - - ">="
114
114
  - !ruby/object:Gem::Version
115
115
  version: '0'
116
116
  - !ruby/object:Gem::Dependency
117
117
  name: selenium-webdriver
118
118
  requirement: !ruby/object:Gem::Requirement
119
119
  requirements:
120
- - - '>='
120
+ - - ">="
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0'
123
123
  type: :development
124
124
  prerelease: false
125
125
  version_requirements: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - '>='
127
+ - - ">="
128
128
  - !ruby/object:Gem::Version
129
129
  version: '0'
130
130
  - !ruby/object:Gem::Dependency
131
131
  name: teaspoon
132
132
  requirement: !ruby/object:Gem::Requirement
133
133
  requirements:
134
- - - '>='
134
+ - - ">="
135
135
  - !ruby/object:Gem::Version
136
136
  version: '0'
137
137
  type: :development
138
138
  prerelease: false
139
139
  version_requirements: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - '>='
141
+ - - ">="
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  - !ruby/object:Gem::Dependency
145
145
  name: sqlite3
146
146
  requirement: !ruby/object:Gem::Requirement
147
147
  requirements:
148
- - - '>='
148
+ - - ">="
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0'
151
151
  type: :development
152
152
  prerelease: false
153
153
  version_requirements: !ruby/object:Gem::Requirement
154
154
  requirements:
155
- - - '>='
155
+ - - ">="
156
156
  - !ruby/object:Gem::Version
157
157
  version: '0'
158
158
  - !ruby/object:Gem::Dependency
159
159
  name: thin
160
160
  requirement: !ruby/object:Gem::Requirement
161
161
  requirements:
162
- - - '>='
162
+ - - ">="
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  type: :development
166
166
  prerelease: false
167
167
  version_requirements: !ruby/object:Gem::Requirement
168
168
  requirements:
169
- - - '>='
169
+ - - ">="
170
170
  - !ruby/object:Gem::Version
171
171
  version: '0'
172
172
  - !ruby/object:Gem::Dependency
173
173
  name: byebug
174
174
  requirement: !ruby/object:Gem::Requirement
175
175
  requirements:
176
- - - '>='
176
+ - - ">="
177
177
  - !ruby/object:Gem::Version
178
178
  version: '0'
179
179
  type: :development
180
180
  prerelease: false
181
181
  version_requirements: !ruby/object:Gem::Requirement
182
182
  requirements:
183
- - - '>='
183
+ - - ">="
184
184
  - !ruby/object:Gem::Version
185
185
  version: '0'
186
186
  description: It's like turbolinks, but with partial page replacement and tests
@@ -190,6 +190,9 @@ executables: []
190
190
  extensions: []
191
191
  extra_rdoc_files: []
192
192
  files:
193
+ - MIT-LICENSE
194
+ - README.md
195
+ - lib/assets/javascripts/turbograft.coffee
193
196
  - lib/assets/javascripts/turbograft/click.coffee
194
197
  - lib/assets/javascripts/turbograft/component_url.coffee
195
198
  - lib/assets/javascripts/turbograft/csrf_token.coffee
@@ -198,17 +201,14 @@ files:
198
201
  - lib/assets/javascripts/turbograft/page.coffee
199
202
  - lib/assets/javascripts/turbograft/remote.coffee
200
203
  - lib/assets/javascripts/turbograft/turbolinks.coffee
201
- - lib/assets/javascripts/turbograft.coffee
204
+ - lib/turbograft.js
205
+ - lib/turbograft.rb
202
206
  - lib/turbograft/cookies.rb
203
207
  - lib/turbograft/redirection.rb
204
208
  - lib/turbograft/version.rb
205
209
  - lib/turbograft/x_domain_blocker.rb
206
210
  - lib/turbograft/xhr_headers.rb
207
211
  - lib/turbograft/xhr_url_for.rb
208
- - lib/turbograft.js
209
- - lib/turbograft.rb
210
- - README.md
211
- - MIT-LICENSE
212
212
  homepage: https://github.com/Shopify/turbograft
213
213
  licenses:
214
214
  - MIT
@@ -219,17 +219,17 @@ require_paths:
219
219
  - lib
220
220
  required_ruby_version: !ruby/object:Gem::Requirement
221
221
  requirements:
222
- - - '>='
222
+ - - ">="
223
223
  - !ruby/object:Gem::Version
224
224
  version: '0'
225
225
  required_rubygems_version: !ruby/object:Gem::Requirement
226
226
  requirements:
227
- - - '>='
227
+ - - ">="
228
228
  - !ruby/object:Gem::Version
229
229
  version: '0'
230
230
  requirements: []
231
231
  rubyforge_project:
232
- rubygems_version: 2.0.14
232
+ rubygems_version: 2.2.2
233
233
  signing_key:
234
234
  specification_version: 4
235
235
  summary: turbolinks with partial page replacement