Skip to content

Commit 88b7074

Browse files
committed
first draft
0 parents  commit 88b7074

12 files changed

+320
-0
lines changed

Gemfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source :rubygems
2+
3+
gem "rake"
4+
gem "sinatra", "1.0"
5+
6+
group :development do
7+
gem "shotgun", "0.8"
8+
end

Gemfile.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
rack (1.2.2)
5+
rake (0.8.7)
6+
shotgun (0.8)
7+
rack (>= 1.0)
8+
sinatra (1.0)
9+
rack (>= 1.0)
10+
11+
PLATFORMS
12+
ruby
13+
14+
DEPENDENCIES
15+
rake
16+
shotgun (= 0.8)
17+
sinatra (= 1.0)

app/pjax.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'sinatra'
2+
3+
module Pjax
4+
class App < Sinatra::Base
5+
dir = File.dirname(File.expand_path(__FILE__))
6+
set :root, "#{dir}/.."
7+
set :public, "#{dir}/.."
8+
set :app_file, __FILE__
9+
set :views, "app/views"
10+
enable :static
11+
12+
get '/' do
13+
erb :index
14+
end
15+
16+
get '/:page.html' do
17+
erb :"#{params[:page]}"
18+
end
19+
20+
helpers do
21+
def title(str)
22+
if pjax?
23+
"<title>#{str}</title>"
24+
else
25+
@title = str
26+
nil
27+
end
28+
end
29+
30+
def pjax?
31+
env['X-PJAX']
32+
end
33+
end
34+
end
35+
end

app/views/aliens.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%= title 'aliens' %>
2+
3+
<ul>
4+
<li><a href="/">kingly</a></li>
5+
<li><a href="/dinosaurs.html">dinosaurs</a></li>
6+
<li>aliens</li>
7+
</ul>
8+
9+
<img src="/img/aliens.jpg" title="aliens">

app/views/dinosaurs.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%= title 'dinosaurs' %>
2+
3+
<ul>
4+
<li><a href="/">kingly</a></li>
5+
<li>dinosaurs</li>
6+
<li><a href="/aliens.html">aliens</a></li>
7+
</ul>
8+
9+
<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/GcjxwXCxBBU" frameborder="0" allowfullscreen></iframe>

app/views/index.erb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<%= title 'kingly' %>
2+
3+
<ul>
4+
<li>kingly</li>
5+
<li><a href="/dinosaurs.html">dinosaurs</a></li>
6+
<li><a href="/aliens.html">aliens</a></li>
7+
</ul>
8+
9+
<pre> .
10+
/ \
11+
_\ /_
12+
. . (,'v`.) . .
13+
\) ( ) ,' `. ( ) (/
14+
\`. / `-' `-' \ ,'/
15+
: ' _______ ' :
16+
| _,-' ,-. `-._ |
17+
|,' ( )__`-'__( ) `.|
18+
(|,-,'-._ _.-`.-.|)
19+
/ /<( o)> <( o)>\ \
20+
: : | | : :
21+
| | ; : | |
22+
| | (.-.) | |
23+
| | ,' ___ `. | |
24+
; |)/ ,'---'. \(| :
25+
_,-/ |/\( )/\| \-._
26+
_..--'.-( | `-'''-' | )-.`--.._
27+
`. ;`._________,': ,'
28+
,' `/ \'`.
29+
`------.------' SSt
30+
'</pre>

app/views/layout.erb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title><%= @title ? "#{@title} - " : nil %> pjax</title>
6+
<script src="jquery.js"></script>
7+
<script src="jquery.pjax.js"></script>
8+
9+
<script src="pages.js"></script>
10+
</head>
11+
12+
<h1>pjax</h1>
13+
14+
<h3>It's <%= Time.now.strftime '%I:%M:%S %p' %></h3>
15+
16+
<%= yield %>
17+
18+
</html>

config.ru

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/app')
2+
3+
require 'pjax'
4+
run Pjax::App.new

img/aliens.jpg

47.6 KB
Loading

jquery.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jquery.pjax.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// jquery.pjax.js
2+
// copyright chris wanstrath
3+
// https://github.com/defunkt/pjax
4+
5+
// When called on a link, fetches the href with ajax into the
6+
// container specified as the first parameter or with the data-pjax
7+
// attribute on the link itself.
8+
//
9+
// Tries to make sure the back button and ctrl+click work the way
10+
// you'd expect.
11+
//
12+
// Accepts a jQuery ajax options object that may include these
13+
// pjax specific options:
14+
//
15+
// container - Where to stick the response body. Usually a String selector.
16+
// $(container).html(xhr.responseBody)
17+
// push - Whether to pushState the URL. Defaults to true (of course).
18+
// replace - Want to use replaceState instead? That's cool.
19+
//
20+
// For convenience the first parameter can be either the container or
21+
// the options object.
22+
//
23+
// Returns the jQuery object
24+
jQuery.fn.pjax = function( container, options ) {
25+
var $ = jQuery
26+
27+
if ( options )
28+
options.container = container
29+
else
30+
options = $.isPlainObject(container) ? container : {container:container}
31+
32+
return this.live('click', function(event){
33+
// Middle click, cmd click, and ctrl click should open
34+
// links in a new tab as normal.
35+
if ( event.which == 2 || event.metaKey )
36+
return true
37+
38+
var defaults = {
39+
url: this.href,
40+
container: $(this).attr('data-pjax')
41+
}
42+
43+
$.pjax($.extend({}, defaults, options))
44+
45+
event.preventDefault()
46+
})
47+
}
48+
49+
50+
// Loads a URL with ajax, puts the response body inside a container,
51+
// then pushState()'s the loaded URL. Also ensures the back button
52+
// works the way you'd expect.
53+
//
54+
// Works just like $.ajax in that it accepts a jQuery ajax
55+
// settings object (with keys like url, type, data, etc).
56+
//
57+
// Accepts these extra keys:
58+
//
59+
// container - Where to stick the response body.
60+
// $(container).html(xhr.responseBody)
61+
// push - Whether to pushState the URL. Defaults to true (of course).
62+
// replace - Want to use replaceState instead? That's cool.
63+
//
64+
// Use it just like $.ajax:
65+
//
66+
// var xhr = $.pjax({ url: this.href, container: '#main' })
67+
// console.log( xhr.readyState )
68+
//
69+
// Returns whatever $.ajax returns.
70+
jQuery.pjax = function( options ) {
71+
// Helper
72+
var $ = jQuery, $container = $(options.container)
73+
74+
var defaults = {
75+
timeout: 650,
76+
push: true,
77+
replace: false,
78+
type: 'GET',
79+
dataType: 'html',
80+
beforeSend: function(xhr){
81+
xhr.setRequestHeader('X-PJAX', 'true')
82+
},
83+
error: function(){ window.location = options.url },
84+
success: function( data ) {
85+
// If we got no data or an entire web page, go directly
86+
// to the page and let normal error handling happen.
87+
if ( !$.trim(data) || /<html/i.test(data) )
88+
return window.location = options.url
89+
90+
// Make it happen.
91+
$container.html(data)
92+
93+
// If there's a <title> tag in the response, use it as
94+
// the page's title.
95+
var title = $.trim( $container.find('title').remove().text() )
96+
if ( title ) document.title = title
97+
98+
var state = { pjax: options.container }
99+
100+
if ( options.data )
101+
state.url = options.url + '?' + $.param(options.data)
102+
103+
if ( options.replace ) {
104+
window.history.replaceState(state, document.title, options.url)
105+
} else if ( options.push ) {
106+
window.history.pushState(state, document.title, options.url)
107+
}
108+
109+
if ( (options.replace || options.push) && window._gaq )
110+
_gaq.push(['_trackPageview'])
111+
112+
// Invoke their success handler if they gave us one.
113+
success.apply(this, arguments)
114+
}
115+
}
116+
117+
// We don't want to let anyone override our success handler.
118+
var success = options.success || $.noop
119+
delete options.success
120+
121+
options = $.extend(true, {}, defaults, options)
122+
var xhr = $.ajax(options)
123+
124+
$(document).trigger('pjax', xhr, options)
125+
return xhr
126+
}
127+
128+
129+
// onpopstate fires at some point after the first page load, by design.
130+
// pjax only cares about the back button, so we ignore the first onpopstate.
131+
//
132+
// Of course, older webkit doesn't fire the onopopstate event on load.
133+
// So we have to special case. The joys.
134+
jQuery.pjax.firstLoad = true
135+
136+
if ( jQuery.browser.webkit && parseInt(jQuery.browser.version) < 534 )
137+
jQuery.pjax.firstLoad = false
138+
139+
140+
// Bind our popstate handler which takes care of the back and
141+
// forward buttons, but only once we've called pjax().
142+
//
143+
// You probably shouldn't use pjax on pages with other pushState
144+
// stuff yet.
145+
jQuery(window).bind('popstate', function(event){
146+
if ( jQuery.pjax.firstLoad )
147+
return jQuery.pjax.firstLoad = false
148+
149+
var state = event.state
150+
151+
if ( state && $(state.pjax).length )
152+
jQuery.pjax({
153+
url: state.url || location.href,
154+
container: state.pjax,
155+
push: false
156+
})
157+
else
158+
window.location = location.href
159+
})
160+
161+
162+
// Add the state property to jQuery's event object so we can use it in
163+
// $(window).bind('popstate')
164+
jQuery.event.props.push('state')
165+
166+
167+
// Fall back to normalcy for older browsers.
168+
if ( !window.history || !window.history.pushState ) {
169+
jQuery.pjax = jQuery.fn.pjax = $.noop
170+
};

pages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$(function(){
2+
console.log('hey')
3+
4+
});

0 commit comments

Comments
 (0)