ajaxify_rails 0.0.6 → 0.0.7
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/README.md
CHANGED
@@ -13,13 +13,17 @@ Features:
|
|
13
13
|
- Falls back to a hash based approach for browsers without the history interface (like Internet Explorer version <10)
|
14
14
|
- Hash based and non-hash URLs are interchangeable.
|
15
15
|
- Transparently handles redirects and supports page titles and flash messages.
|
16
|
-
- Requires Ruby 1.9 and the asset pipeline.
|
17
16
|
- Tested with Chrome, Firefox, Safari and Internet Explorer 8+
|
18
17
|
|
19
18
|
Demo: http://ajaxify-demo.herokuapp.com/
|
20
19
|
|
21
20
|
Inspired by the pjax_rails gem (https://github.com/rails/pjax_rails)
|
22
21
|
|
22
|
+
## Requirements
|
23
|
+
|
24
|
+
- Ruby 1.9 and the asset pipeline.
|
25
|
+
- Your app doesn't use named anchors (#). Named anchors can't be correctly represented in the fallback hash based url scheme.
|
26
|
+
|
23
27
|
## Installation
|
24
28
|
|
25
29
|
Add this line to your application's Gemfile:
|
@@ -75,7 +79,7 @@ update the page's title tag after the main content has changed.
|
|
75
79
|
|
76
80
|
It's a common use case to have a navigation that needs to change its appearence and possibly functioning when the user navigates
|
77
81
|
to a different section of the page. Ajaxify provides a success callback that is triggered after successful
|
78
|
-
updates of
|
82
|
+
updates of the page's main content. Just hook into it in your javascript and make your layout changes:
|
79
83
|
|
80
84
|
Ajaxify.success ->
|
81
85
|
# update navigation and/or other layout elements
|
@@ -98,7 +102,7 @@ Also make sure that you supply invisible wrapper tags in your layout with the fl
|
|
98
102
|
We all know them. Those big requests changing the layout of the page so significantly that
|
99
103
|
loading ajax into a content area and doing some minor layout tweaks here and there simply doesn't cut it. Sigh.
|
100
104
|
|
101
|
-
There might also be links and forms
|
105
|
+
There might also be links and forms which already have their own ajax functionality.
|
102
106
|
|
103
107
|
To turn Ajaxify off for certain links and forms, simply add the class `no_ajaxify` directly to the link or form:
|
104
108
|
|
@@ -22,7 +22,6 @@
|
|
22
22
|
|
23
23
|
# internal use only
|
24
24
|
#
|
25
|
-
hash_changed: null
|
26
25
|
ignore_hash_change: null
|
27
26
|
load_page_from_hash: null
|
28
27
|
|
@@ -74,27 +73,34 @@
|
|
74
73
|
false
|
75
74
|
|
76
75
|
|
76
|
+
# (history interface browsers only)
|
77
77
|
window.onpopstate = (e) ->
|
78
78
|
if e.state
|
79
79
|
e.state.cache = false
|
80
80
|
self.load e.state, true
|
81
81
|
|
82
82
|
|
83
|
+
# (non history interface browsers only)
|
83
84
|
window.onhashchange = ->
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
self.ignore_hash_change = false
|
85
|
+
unless self.ignore_hash_change
|
86
|
+
self.on_hash_change()
|
87
|
+
else
|
88
|
+
self.ignore_hash_change = false
|
89
89
|
|
90
90
|
|
91
|
+
# load content from url hash (non history interface browsers)
|
91
92
|
on_hash_change: ->
|
92
93
|
url = window.location.hash.replace(/#/, "")
|
93
|
-
|
94
|
-
|
94
|
+
|
95
|
+
base_path_regexp = this.base_path_regexp()
|
96
|
+
if match = window.location.pathname.match(base_path_regexp)
|
97
|
+
url = match[0] + url
|
98
|
+
|
99
|
+
url = '/' if url == ''
|
95
100
|
this.hash_changed = true
|
96
101
|
this.load
|
97
102
|
url: url
|
103
|
+
, true
|
98
104
|
|
99
105
|
|
100
106
|
load: (options, pop_state = false) ->
|
@@ -164,11 +170,7 @@
|
|
164
170
|
options.url = current_url.replace(/(&|\?)ajaxify_redirect=true/,'')
|
165
171
|
options.type = 'GET'
|
166
172
|
|
167
|
-
|
168
|
-
this.update_url options, pop_state
|
169
|
-
else
|
170
|
-
this.hash_changed = false
|
171
|
-
|
173
|
+
this.update_url options, pop_state
|
172
174
|
|
173
175
|
if this.handle_extra_content
|
174
176
|
this.handle_extra_content()
|
@@ -208,20 +210,22 @@
|
|
208
210
|
,'', options.url
|
209
211
|
|
210
212
|
else
|
211
|
-
this.ignore_hash_change = true # avoids loading the page for hash changes caused by link clicks
|
213
|
+
this.ignore_hash_change = true # for non histroy interface browsers: avoids loading the page for hash changes caused by link clicks
|
212
214
|
hash = "#{options.url.replace(new RegExp(this.protocol_with_host()), '')}"
|
215
|
+
|
213
216
|
base_path_regexp = this.base_path_regexp()
|
214
217
|
if base_path_regexp
|
215
218
|
hash = hash.replace(base_path_regexp, '')
|
216
219
|
hash = "/#{hash}" unless hash == '' or hash.indexOf('/') == 0
|
220
|
+
|
217
221
|
window.location.hash = hash
|
218
|
-
# this.ignore_hash_change = false
|
219
222
|
|
220
223
|
|
221
224
|
base_path_regexp: ->
|
222
225
|
return null unless this.base_paths
|
223
226
|
# match starting and ending with base path, e.g. "^\/en$" (i.e. we are at the base path root) or
|
224
|
-
# starting with base path and continuing with '/', e.g. "^\/en\/" (i.e. we are NOT at the base path root)
|
227
|
+
# starting with base path and continuing with '/', e.g. "^\/en\/" (i.e. we are NOT at the base path root) or
|
228
|
+
# starting with base path and continuing with '?', e.g. "^\/en\?" (i.e. we are at the base path root and have query params)
|
225
229
|
self = this
|
226
230
|
new RegExp("^\/(#{ $.map(this.base_paths, (el) ->
|
227
231
|
el = self.regexp_escape el
|
@@ -229,50 +233,59 @@
|
|
229
233
|
).join('|')})", 'i')
|
230
234
|
|
231
235
|
|
232
|
-
regexp_escape: (str) ->
|
233
|
-
str.replace new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g'), '\\$&'
|
234
|
-
|
235
|
-
|
236
|
-
protocol_with_host: ->
|
237
|
-
loc = window.location
|
238
|
-
"#{loc.protocol}//#{loc.host}"
|
239
|
-
|
240
|
-
|
241
236
|
correct_url: ->
|
242
237
|
if this.active
|
243
|
-
|
238
|
+
|
239
|
+
if window.location.hash.indexOf('#') == 0 # if url has a '#' in it treat it as a non history interface hash based scheme url
|
244
240
|
if !window.history.pushState
|
245
241
|
Ajaxify.load_page_from_hash = true # notify Ajaxify that a hash will be loaded and ignore all other calls to load until hash url is loaded
|
246
242
|
else
|
247
|
-
|
243
|
+
# load proper url in case browser supports history api
|
244
|
+
path = window.location.pathname
|
245
|
+
path = '' if path == '/'
|
246
|
+
path = path + window.location.hash.replace(/#/, "")
|
248
247
|
window.location.href = "#{this.protocol_with_host()}#{path}"
|
249
248
|
|
250
|
-
else if !window.history.pushState
|
251
|
-
|
252
|
-
if window.location.pathname
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
249
|
+
else if !window.history.pushState # move path behind '#' for browsers without history api
|
250
|
+
|
251
|
+
if window.location.pathname == '/'
|
252
|
+
if window.location.search != ''
|
253
|
+
window.location.href = "#{this.protocol_with_host()}/#/#{window.location.search}" # move search behind #
|
254
|
+
return
|
255
|
+
|
256
|
+
base_path_regexp = this.base_path_regexp()
|
257
|
+
if base_path_regexp and (match = window.location.pathname.match(base_path_regexp))
|
258
|
+
if match[0] == window.location.pathname
|
259
|
+
if window.location.search == ''
|
260
|
+
return # we are on a base path here already, so don't do anything
|
260
261
|
else
|
261
|
-
path =
|
262
|
+
path = match[0].replace(/\?/,'') + '#'
|
262
263
|
else
|
263
|
-
path = "
|
264
|
-
|
265
|
-
window.location.href = "#{this.protocol_with_host()}#{path}#{window.location.search}"
|
264
|
+
path = "#{match[0].replace(/\/$/,'')}#/#{window.location.pathname.replace(match[0],'')}"
|
266
265
|
else
|
267
|
-
|
268
|
-
|
266
|
+
path = "/##{window.location.pathname}"
|
267
|
+
|
268
|
+
window.location.href = "#{this.protocol_with_host()}#{path}#{window.location.search}"
|
269
|
+
|
269
270
|
|
270
271
|
init: ->
|
271
272
|
this.correct_url()
|
272
273
|
|
274
|
+
|
275
|
+
#
|
276
|
+
# utility functions
|
277
|
+
#
|
278
|
+
|
273
279
|
is_string: (variable) ->
|
274
280
|
Object.prototype.toString.call(variable) == '[object String]'
|
275
281
|
|
282
|
+
regexp_escape: (str) ->
|
283
|
+
str.replace new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g'), '\\$&'
|
284
|
+
|
285
|
+
protocol_with_host: ->
|
286
|
+
loc = window.location
|
287
|
+
"#{loc.protocol}//#{loc.host}"
|
288
|
+
|
276
289
|
|
277
290
|
jQuery ->
|
278
291
|
Ajaxify.ajaxify()
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ajaxify_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
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-
|
12
|
+
date: 2012-09-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -67,7 +67,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
67
|
version: '0'
|
68
68
|
segments:
|
69
69
|
- 0
|
70
|
-
hash:
|
70
|
+
hash: 3241729094271332625
|
71
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
segments:
|
78
78
|
- 0
|
79
|
-
hash:
|
79
|
+
hash: 3241729094271332625
|
80
80
|
requirements: []
|
81
81
|
rubyforge_project:
|
82
82
|
rubygems_version: 1.8.24
|