turbolinks 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/assets/javascripts/turbolinks.js.coffee +30 -14
- metadata +1 -1
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Turbolinks
|
2
2
|
===========
|
3
3
|
|
4
|
-
Turbolinks makes following links in your web application faster. Instead of letting the browser
|
4
|
+
Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, and potentially spend extra HTTP requests checking if the assets are up-to-date, we keep the current instance alive and replace only the body and the title in the head. Think CGI vs persistent process.
|
5
5
|
|
6
6
|
This is similar to pjax, but instead of worrying about what element on the page to replace, and tailoring the server-side response to fit, we replace the entire body. This means that you get the bulk of the speed benefits from pjax (no recompiling of the JavaScript or CSS) without having to tailor the server-side response. It just works.
|
7
7
|
|
@@ -25,12 +25,27 @@ triggerPageChange = ->
|
|
25
25
|
event.initEvent 'page:change', true, true
|
26
26
|
document.dispatchEvent event
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
createDocument = do ->
|
29
|
+
createDocumentUsingParser = (html) ->
|
30
|
+
(new DOMParser).parseFromString html, "text/html"
|
31
|
+
|
32
|
+
createDocumentUsingWrite = (html) ->
|
33
|
+
doc = document.implementation.createHTMLDocument ""
|
34
|
+
doc.open "replace"
|
35
|
+
doc.write html
|
36
|
+
doc.close
|
37
|
+
doc
|
38
|
+
|
39
|
+
if window.DOMParser
|
40
|
+
testDoc = createDocumentUsingParser "<html><body><p>test"
|
41
|
+
|
42
|
+
if testDoc?.body?.childNodes.length is 1
|
43
|
+
createDocumentUsingParser
|
44
|
+
else
|
45
|
+
createDocumentUsingWrite
|
33
46
|
|
47
|
+
replaceHTML = (html) ->
|
48
|
+
doc = createDocument html
|
34
49
|
originalBody = document.body
|
35
50
|
document.documentElement.appendChild doc.body, originalBody
|
36
51
|
document.documentElement.removeChild originalBody
|
@@ -39,25 +54,27 @@ replaceHTML = (html) ->
|
|
39
54
|
|
40
55
|
extractLink = (event) ->
|
41
56
|
link = event.target
|
42
|
-
until link is document or link.nodeName is 'A'
|
43
|
-
link = link.parentNode
|
57
|
+
link = link.parentNode until link is document or link.nodeName is 'A'
|
44
58
|
link
|
45
59
|
|
46
60
|
crossOriginLink = (link) ->
|
47
|
-
location.protocol isnt link.protocol
|
61
|
+
location.protocol isnt link.protocol or location.host isnt link.host
|
48
62
|
|
49
63
|
anchoredLink = (link) ->
|
50
|
-
((link.hash
|
64
|
+
((link.hash and link.href.replace(link.hash, '')) is location.href.replace(location.hash, '')) or
|
51
65
|
(link.href is location.href + '#')
|
52
66
|
|
67
|
+
nonHtmlLink = (link) ->
|
68
|
+
link.href.match(/\.[a-z]+$/g) and not link.href.match(/\.html?$/g)
|
69
|
+
|
53
70
|
noTurbolink = (link) ->
|
54
71
|
link.getAttribute('data-no-turbolink')?
|
55
72
|
|
56
73
|
newTabClick = (event) ->
|
57
|
-
event.which > 1
|
74
|
+
event.which > 1 or event.metaKey or event.ctrlKey
|
58
75
|
|
59
76
|
ignoreClick = (event, link) ->
|
60
|
-
crossOriginLink(link)
|
77
|
+
crossOriginLink(link) or anchoredLink(link) or nonHtmlLink(link) or noTurbolink(link) or newTabClick(event)
|
61
78
|
|
62
79
|
handleClick = (event) ->
|
63
80
|
link = extractLink event
|
@@ -67,8 +84,7 @@ handleClick = (event) ->
|
|
67
84
|
event.preventDefault()
|
68
85
|
|
69
86
|
|
70
|
-
browserSupportsPushState =
|
71
|
-
window.history && window.history.pushState && window.history.replaceState
|
87
|
+
browserSupportsPushState = window.history and window.history.pushState and window.history.replaceState
|
72
88
|
|
73
89
|
|
74
90
|
if browserSupportsPushState
|
@@ -80,4 +96,4 @@ if browserSupportsPushState
|
|
80
96
|
handleClick event
|
81
97
|
|
82
98
|
# Call Turbolinks.visit(url) from client code
|
83
|
-
@Turbolinks = { visit: visit }
|
99
|
+
@Turbolinks = { visit: visit }
|