pjax_rails 0.2.1 → 0.2.2

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.
@@ -66,8 +66,12 @@ function handleClick(event, container, options) {
66
66
  return
67
67
 
68
68
  // Ignore anchors on the same page
69
- if ( link.hash && link.href.replace(link.hash, '') ===
70
- location.href.replace(location.hash, '') )
69
+ if (link.hash && link.href.replace(link.hash, '') ===
70
+ location.href.replace(location.hash, ''))
71
+ return
72
+
73
+ // Ignore empty anchor "foo.html#"
74
+ if (link.href === location.href + '#')
71
75
  return
72
76
 
73
77
  var defaults = {
@@ -165,6 +169,8 @@ var pjax = $.pjax = function( options ) {
165
169
 
166
170
  if (!fire('pjax:beforeSend', [xhr, settings]))
167
171
  return false
172
+
173
+ options.requestUrl = parseURL(settings.url).href
168
174
  }
169
175
 
170
176
  options.complete = function(xhr, textStatus) {
@@ -227,7 +233,19 @@ var pjax = $.pjax = function( options ) {
227
233
  // If the URL has a hash in it, make sure the browser
228
234
  // knows to navigate to the hash.
229
235
  if ( hash !== '' ) {
230
- window.location.href = hash
236
+ // Avoid using simple hash set here. Will add another history
237
+ // entry. Replace the url with replaceState and scroll to target
238
+ // by hand.
239
+ //
240
+ // window.location.hash = hash
241
+ var url = parseURL(container.url)
242
+ url.hash = hash
243
+
244
+ pjax.state.url = url.href
245
+ window.history.replaceState(pjax.state, container.title, url.href)
246
+
247
+ var target = $(url.hash)
248
+ if (target.length) $(window).scrollTop(target.offset().top)
231
249
  }
232
250
 
233
251
  // DEPRECATED: Invoke original `success` handler
@@ -269,7 +287,7 @@ var pjax = $.pjax = function( options ) {
269
287
 
270
288
  if (options.push && !options.replace) {
271
289
  // Cache current container element before replacing it
272
- containerCache.push(pjax.state.id, context.clone(true, true).contents())
290
+ cachePush(pjax.state.id, context.clone().contents())
273
291
 
274
292
  window.history.pushState(null, "", options.url)
275
293
  }
@@ -412,7 +430,7 @@ function extractContainer(data, xhr, options) {
412
430
 
413
431
  // Prefer X-PJAX-URL header if it was set, otherwise fallback to
414
432
  // using the original requested url.
415
- obj.url = stripPjaxParam(xhr.getResponseHeader('X-PJAX-URL') || options.url)
433
+ obj.url = stripPjaxParam(xhr.getResponseHeader('X-PJAX-URL') || options.requestUrl)
416
434
 
417
435
  // Attempt to parse response html into elements
418
436
  var $data = $(data)
@@ -484,11 +502,9 @@ pjax.defaults = {
484
502
  }
485
503
 
486
504
  // Internal: History DOM caching class.
487
- function Cache() {
488
- this.mapping = {}
489
- this.forwardStack = []
490
- this.backStack = []
491
- }
505
+ var cacheMapping = {}
506
+ var cacheForwardStack = []
507
+ var cacheBackStack = []
492
508
  // Push previous state id and container contents into the history
493
509
  // cache. Should be called in conjunction with `pushState` to save the
494
510
  // previous container contents.
@@ -497,86 +513,61 @@ function Cache() {
497
513
  // value - DOM Element to cache
498
514
  //
499
515
  // Returns nothing.
500
- Cache.prototype.push = function(id, value) {
501
- this.mapping[id] = value
502
- this.backStack.push(id)
516
+ function cachePush(id, value) {
517
+ cacheMapping[id] = value
518
+ cacheBackStack.push(id)
503
519
 
504
520
  // Remove all entires in forward history stack after pushing
505
521
  // a new page.
506
- while (this.forwardStack.length)
507
- delete this.mapping[this.forwardStack.shift()]
522
+ while (cacheForwardStack.length)
523
+ delete cacheMapping[cacheForwardStack.shift()]
508
524
 
509
525
  // Trim back history stack to max cache length.
510
- while (this.backStack.length > pjax.defaults.maxCacheLength)
511
- delete this.mapping[this.backStack.shift()]
512
- }
513
- // Retrieve cached DOM Element for state id.
514
- //
515
- // id - State ID Number
516
- //
517
- // Returns DOM Element(s) or undefined if cache miss.
518
- Cache.prototype.get = function(id) {
519
- return this.mapping[id]
526
+ while (cacheBackStack.length > pjax.defaults.maxCacheLength)
527
+ delete cacheMapping[cacheBackStack.shift()]
520
528
  }
521
- // Shifts cache from forward history cache to back stack. Should be
529
+ // Shifts cache from directional history cache. Should be
522
530
  // called on `popstate` with the previous state id and container
523
531
  // contents.
524
532
  //
525
- // id - State ID Number
526
- // value - DOM Element to cache
533
+ // direction - "forward" or "back" String
534
+ // id - State ID Number
535
+ // value - DOM Element to cache
527
536
  //
528
537
  // Returns nothing.
529
- Cache.prototype.forward = function(id, value) {
530
- this.mapping[id] = value
531
- this.backStack.push(id)
538
+ function cachePop(direction, id, value) {
539
+ var pushStack, popStack
540
+ cacheMapping[id] = value
532
541
 
533
- if (id = this.forwardStack.pop())
534
- delete this.mapping[id]
535
- }
536
- // Shifts cache from back history cache to forward stack. Should be
537
- // called on `popstate` with the previous state id and container
538
- // contents.
539
- //
540
- // id - State ID Number
541
- // value - DOM Element to cache
542
- //
543
- // Returns nothing.
544
- Cache.prototype.back = function(id, value) {
545
- this.mapping[id] = value
546
- this.forwardStack.push(id)
542
+ if (direction === 'forward') {
543
+ pushStack = cacheBackStack
544
+ popStack = cacheForwardStack
545
+ } else {
546
+ pushStack = cacheForwardStack
547
+ popStack = cacheBackStack
548
+ }
547
549
 
548
- if (id = this.backStack.pop())
549
- delete this.mapping[id]
550
+ pushStack.push(id)
551
+ if (id = popStack.pop())
552
+ delete cacheMapping[id]
550
553
  }
551
554
 
552
- var containerCache = new Cache
553
-
554
555
 
555
556
  // Export $.pjax.click
556
557
  pjax.click = handleClick
557
558
 
558
559
 
559
- // Used to detect initial (useless) popstate.
560
- // If history.state exists, assume browser isn't going to fire initial popstate.
561
- var popped = ('state' in window.history), initialURL = location.href
562
-
563
-
564
560
  // popstate handler takes care of the back and forward buttons
565
561
  //
566
562
  // You probably shouldn't use pjax on pages with other pushState
567
563
  // stuff yet.
568
564
  $(window).bind('popstate', function(event){
569
- // Ignore inital popstate that some browsers fire on page load
570
- var initialPop = !popped && location.href == initialURL
571
- popped = true
572
- if ( initialPop ) return
573
-
574
565
  var state = event.state
575
566
 
576
567
  if (state && state.container) {
577
568
  var container = $(state.container)
578
569
  if (container.length) {
579
- var contents = containerCache.get(state.id)
570
+ var contents = cacheMapping[state.id]
580
571
 
581
572
  if (pjax.state) {
582
573
  // Since state ids always increase, we can deduce the history
@@ -585,7 +576,7 @@ $(window).bind('popstate', function(event){
585
576
 
586
577
  // Cache current container before replacement and inform the
587
578
  // cache which direction the history shifted.
588
- containerCache[direction](pjax.state.id, container.clone(true, true).contents())
579
+ cachePop(direction, pjax.state.id, container.clone().contents())
589
580
  }
590
581
 
591
582
  var popstateEvent = $.Event('pjax:popstate', {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pjax_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
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-05-09 00:00:00.000000000 Z
12
+ date: 2012-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jquery-rails
@@ -33,14 +33,12 @@ executables: []
33
33
  extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
- - ./lib/assets/javascripts/pjax/enable_pjax.js
37
- - ./lib/assets/javascripts/pjax/page_triggers.js
38
- - ./lib/assets/javascripts/pjax.js
39
- - ./lib/pjax.rb
40
- - ./lib/pjax_rails.rb
41
- - ./pjax_rails.gemspec
42
- - ./README.md
43
- - ./vendor/assets/javascripts/jquery.pjax.js
36
+ - lib/pjax.rb
37
+ - lib/pjax_rails.rb
38
+ - lib/assets/javascripts/pjax/enable_pjax.js
39
+ - lib/assets/javascripts/pjax/page_triggers.js
40
+ - lib/assets/javascripts/pjax.js
41
+ - vendor/assets/javascripts/jquery.pjax.js
44
42
  homepage:
45
43
  licenses: []
46
44
  post_install_message:
data/README.md DELETED
@@ -1,66 +0,0 @@
1
- PJAX for Rails 3.1+
2
- ===================
3
-
4
- Integrate Chris Wanstrath's [PJAX](https://github.com/defunkt/jquery-pjax) into Rails 3.1+ via the asset pipeline.
5
-
6
- To activate, add this to your app/assets/javascripts/application.js (or whatever bundle you use):
7
-
8
- //=require jquery.pjax
9
-
10
- All links that match `$('a:not([data-remote]):not([data-behavior]):not([data-skip-pjax])')` will then use PJAX.
11
-
12
- The PJAX container has to be marked with data-pjax-container attribute, so for example:
13
-
14
- <body>
15
- <div>
16
- <!-- This will not be touched on PJAX updates -->
17
- <%= Time.now %>
18
- </div>
19
-
20
- <div data-pjax-container>
21
- <!-- PJAX updates will go here -->
22
- <%= content_tag :h3, 'My site' %>
23
- <%= link_to 'About me', about_me_path %>
24
- <!-- The following link will not be pjax'd -->
25
- <%= link_to 'Google', 'http://google.com', 'data-skip-pjax' => true %>
26
- </div>
27
- </body>
28
-
29
-
30
- FIXME: Currently the layout is hardcoded to "application". Need to delegate that to the specific layout of the controller.
31
-
32
- Examples for redirect_to
33
- -----------------------------
34
-
35
- class ProjectsController < ApplicationController
36
- before_filter :set_project, except: [ :index, :create ]
37
-
38
- def index
39
- @projects = current_user.projects
40
- end
41
-
42
- def show
43
- end
44
-
45
- def create
46
- @project = Project.create params[:project]
47
- redirect_to :show, @project
48
- end
49
-
50
- def update
51
- @project.update_attributes params[:project]
52
- redirect_to :show, @project
53
- end
54
-
55
- def destroy
56
- @project.destroy
57
-
58
- index # set the objects needed for rendering index
59
- redirect_to :index
60
- end
61
-
62
- private
63
- def set_project
64
- @project = current_user.projects.find params[:id].to_i
65
- end
66
- end
data/pjax_rails.gemspec DELETED
@@ -1,11 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = 'pjax_rails'
3
- s.version = '0.2.1'
4
- s.author = 'David Heinemeier Hansson (PJAX by Chris Wanstrath)'
5
- s.email = 'david@loudthinking.com'
6
- s.summary = 'PJAX integration for Rails 3.1+'
7
-
8
- s.add_dependency 'jquery-rails'
9
-
10
- s.files = Dir["#{File.dirname(__FILE__)}/**/*"]
11
- end