turbograft 0.0.7 → 0.0.8

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: 9363a348161d11d97e367b31a33aa3bbef88b085
4
- data.tar.gz: f97e93ef362b214d9878967583f1e4611496331a
3
+ metadata.gz: de6aed257f2451693856924987ff6238f3b86f6f
4
+ data.tar.gz: 020bf3757da79669cca06b78dc237f7f0f423bd2
5
5
  SHA512:
6
- metadata.gz: af7f17cbeaea58085a590b55d5c71c80aa085d9344df499d06be82467a661cca1d3b63db9dff8879402a551671b5eadf9f58a9f756607ed5e8f80624b218f4c0
7
- data.tar.gz: b8d5e300a0b964f6f2ef55e896e5ba2b34119dbe9e46f45820c851312a614a0e6397d7d73c507ae7cdc76f3a4d9e84929060de33f039562b1dc97fd73c842c90
6
+ metadata.gz: 950bfd3d8f0f12ccc9089a66383c6197f9288e4ea8f3fdd5122168102fdcfa9a4e3627077f4bbc31fb7a48293378195f91afc4135a192c72e3c53f3ed21bdf7f
7
+ data.tar.gz: 19a95050d2a3d94fdb4ba20a804b94e1edba12f1afa83a0ba1b1d36df0ef37797fd8769899aeefac92d81afc2cf0c6b727da776cc8eba6b80d2a4bc4966881fc
@@ -0,0 +1,82 @@
1
+ hasClass = (node, search) ->
2
+ node.classList.contains(search)
3
+
4
+ nodeIsDisabled = (node) ->
5
+ node.getAttribute('disabled') || hasClass(node, 'disabled')
6
+
7
+ TurboGraft.handlers.partialGraftClickHandler = (ev) ->
8
+ target = ev.target
9
+ partialGraft = target.getAttribute("partial-graft")
10
+ return unless partialGraft?
11
+ ev.preventDefault()
12
+ href = target.getAttribute("href")
13
+ refresh = target.getAttribute("refresh")
14
+ throw new Error("TurboGraft developer error: href is not defined on node #{target}") unless href?
15
+ throw new Error("TurboGraft developer error: refresh is not defined on node #{target}") unless refresh?
16
+
17
+ keys = refresh.trim().split(" ")
18
+
19
+ Page.refresh
20
+ url: href,
21
+ onlyKeys: keys
22
+
23
+ TurboGraft.handlers.remoteMethodHandler = (ev) ->
24
+ target = ev.target
25
+ httpRequestType = target.getAttribute('remote-method')
26
+ return unless httpRequestType
27
+ ev.preventDefault()
28
+ httpUrl = target.getAttribute('href')
29
+ throw new Error("Turbograft developer error: You did not provide a URL for remote-method") unless httpUrl
30
+
31
+ if target.getAttribute("remote-once")
32
+ target.removeAttribute("remote-once")
33
+ target.removeAttribute("remote-method")
34
+
35
+ options =
36
+ httpRequestType: httpRequestType
37
+ httpUrl: httpUrl
38
+ fullRefresh: target.getAttribute('full-refresh')?
39
+ refreshOnSuccess: target.getAttribute('refresh-on-success')
40
+ refreshOnError: target.getAttribute('refresh-on-error')
41
+
42
+ if !options.refreshOnSuccess && !options.refreshOnError
43
+ options.fullRefresh = true
44
+
45
+ remote = new TurboGraft.Remote(options, null, target)
46
+ return
47
+
48
+ TurboGraft.handlers.remoteFormHandler = (ev) ->
49
+ target = ev.target
50
+ return unless target.getAttribute('remote-form')?
51
+ ev.preventDefault()
52
+ httpUrl = target.getAttribute('action')
53
+ httpRequestType = target.getAttribute('method')
54
+ throw new Error("Turbograft developer error: You did not provide a request type for remote-method") unless httpRequestType
55
+ throw new Error("Turbograft developer error: You did not provide a URL for remote-method") unless httpUrl
56
+
57
+ options =
58
+ httpRequestType: httpRequestType
59
+ httpUrl: httpUrl
60
+ fullRefresh: target.getAttribute('full-refresh')?
61
+ refreshOnSuccess: target.getAttribute('refresh-on-success')
62
+ refreshOnError: target.getAttribute('refresh-on-error')
63
+
64
+ if !options.refreshOnSuccess && !options.refreshOnError
65
+ options.fullRefresh = true
66
+
67
+ remote = new TurboGraft.Remote(options, target, target)
68
+ return
69
+
70
+ documentListenerForButtons = (eventType, handler, useCapture = false) ->
71
+ document.addEventListener eventType, (ev) ->
72
+ target = ev.target
73
+ isNodeDisabled = nodeIsDisabled(target)
74
+ ev.preventDefault() if isNodeDisabled
75
+ return if !(target.nodeName == "A" || target.nodeName == "BUTTON") || isNodeDisabled
76
+ handler(ev)
77
+
78
+ documentListenerForButtons('click', TurboGraft.handlers.partialGraftClickHandler, true)
79
+ documentListenerForButtons('click', TurboGraft.handlers.remoteMethodHandler, true)
80
+
81
+ document.addEventListener "submit", (ev) ->
82
+ TurboGraft.handlers.remoteFormHandler(ev)
@@ -0,0 +1,70 @@
1
+ class TurboGraft.Remote
2
+ constructor: (@opts, form, target) ->
3
+ formData = if form then new FormData(form) else new FormData()
4
+
5
+ @initiator = target
6
+
7
+ actualRequestType = if @opts.httpRequestType.toLowerCase() == 'get' then 'GET' else 'POST'
8
+
9
+ formData.append("_method", @opts.httpRequestType)
10
+
11
+ @refreshOnSuccess = @opts.refreshOnSuccess.split(" ") if @opts.refreshOnSuccess
12
+ @refreshOnError = @opts.refreshOnError.split(" ") if @opts.refreshOnError
13
+
14
+ xhr = new XMLHttpRequest
15
+ xhr.open(actualRequestType, @opts.httpUrl, true)
16
+ xhr.setRequestHeader('Accept', 'text/html, application/xhtml+xml, application/xml')
17
+
18
+ xhr.addEventListener 'loadstart', =>
19
+ triggerEvent 'turbograft:remote:start',
20
+ xhr: xhr,
21
+ initiator: @initiator
22
+
23
+ xhr.addEventListener 'error', @onError
24
+ xhr.addEventListener 'load', (event) =>
25
+ if xhr.status < 400
26
+ @onSuccess(event)
27
+ else
28
+ @onError(event)
29
+
30
+ xhr.addEventListener 'loadend', =>
31
+ triggerEvent 'turbograft:remote:always',
32
+ xhr: xhr,
33
+ initiator: @initiator
34
+
35
+ xhr.send(formData)
36
+ xhr
37
+
38
+ onSuccess: (ev) ->
39
+ xhr = ev.target
40
+ triggerEvent 'turbograft:remote:success',
41
+ xhr: xhr,
42
+ initiator: @initiator
43
+
44
+ if redirect = xhr.getResponseHeader('X-Next-Redirect')
45
+ Page.visit(redirect, reload: true)
46
+ return
47
+
48
+ if @opts.fullRefresh && @refreshOnSuccess
49
+ Page.refresh(onlyKeys: @refreshOnSuccess)
50
+ else if @opts.fullRefresh
51
+ Page.refresh()
52
+ else if @refreshOnSuccess
53
+ Page.refresh
54
+ response: xhr
55
+ onlyKeys: @refreshOnSuccess
56
+
57
+ onError: (ev) ->
58
+ xhr = ev.target
59
+ triggerEvent 'turbograft:remote:fail',
60
+ xhr: xhr,
61
+ initiator: @initiator
62
+
63
+ if @refreshOnError
64
+ Page.refresh
65
+ response: xhr
66
+ onlyKeys: @refreshOnError
67
+ else
68
+ triggerEvent 'turbograft:remote:fail:unhandled',
69
+ xhr: xhr,
70
+ initiator: @initiator
@@ -1,8 +1,4 @@
1
- #= require click
2
- #= require component_url
3
- #= require csrf_token
4
- #= require link
5
- #= require page
6
- #= require page_cache
7
- #= require turbolinks
8
- #= require initializers
1
+ #= require_self
2
+ #= require_tree ./turbograft
3
+
4
+ window.TurboGraft ?= { handlers: {} }
@@ -1,3 +1,3 @@
1
1
  module TurboGraft
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
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.7
4
+ version: 0.0.8
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-10-21 00:00:00.000000000 Z
16
+ date: 2014-11-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,25 +190,25 @@ executables: []
190
190
  extensions: []
191
191
  extra_rdoc_files: []
192
192
  files:
193
- - lib/assets/javascripts/click.coffee
194
- - lib/assets/javascripts/component_url.coffee
195
- - lib/assets/javascripts/csrf_token.coffee
196
- - lib/assets/javascripts/initializers.coffee
197
- - lib/assets/javascripts/link.coffee
198
- - lib/assets/javascripts/page.coffee
199
- - lib/assets/javascripts/page_cache.coffee
193
+ - MIT-LICENSE
194
+ - README.md
200
195
  - lib/assets/javascripts/turbograft.coffee
201
- - lib/assets/javascripts/turbolinks.coffee
196
+ - lib/assets/javascripts/turbograft/click.coffee
197
+ - lib/assets/javascripts/turbograft/component_url.coffee
198
+ - lib/assets/javascripts/turbograft/csrf_token.coffee
199
+ - lib/assets/javascripts/turbograft/initializers.coffee
200
+ - lib/assets/javascripts/turbograft/link.coffee
201
+ - lib/assets/javascripts/turbograft/page.coffee
202
+ - lib/assets/javascripts/turbograft/page_cache.coffee
203
+ - lib/assets/javascripts/turbograft/remote.coffee
204
+ - lib/assets/javascripts/turbograft/turbolinks.coffee
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
@@ -1,16 +0,0 @@
1
- partialGraftClickHandler = (ev) ->
2
- target = ev.target
3
- partialGraft = target.getAttribute("partial-graft")
4
- return unless partialGraft?
5
- href = target.getAttribute("href")
6
- refresh = target.getAttribute("refresh")
7
- throw "TurboGraft developer error: href is not defined on node #{target}" if !href?
8
- throw "TurboGraft developer error: refresh is not defined on node #{target}" if !refresh?
9
-
10
- keys = refresh.trim().split(" ")
11
-
12
- Page.refresh
13
- url: href,
14
- onlyKeys: keys
15
-
16
- document.addEventListener 'click', partialGraftClickHandler, true