forked from defunkt/jquery-pjax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn_pjax.js
236 lines (165 loc) · 5.41 KB
/
fn_pjax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
if ($.support.pjax) {
module("$.fn.pjax", {
setup: function() {
var self = this
stop()
window.iframeLoad = function(frame) {
self.frame = frame
window.iframeLoad = $.noop
start()
}
$("#qunit-fixture").append("<iframe src='home.html'>")
},
teardown: function() {
delete window.iframeLoad
}
})
asyncTest("pushes new url", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
frame.$("#main").on("pjax:end", function() {
equal(frame.location.pathname, "/dinosaurs.html")
start()
})
frame.$("a[href='/dinosaurs.html']").click()
})
asyncTest("replaces container html from response data", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
frame.$("#main").on("pjax:end", function() {
equal(frame.$("iframe").attr('title'), "YouTube video player")
start()
})
frame.$("a[href='/dinosaurs.html']").click()
})
asyncTest("sets title to response title tag", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
frame.$("#main").on("pjax:end", function() {
equal(frame.document.title, "dinosaurs")
start()
})
frame.$("a[href='/dinosaurs.html']").trigger('click')
})
asyncTest("uses first argument as container", function() {
var frame = this.frame
frame.$("a").pjax("#main")
frame.$("#main").on("pjax:end", function() {
equal(frame.location.pathname, "/dinosaurs.html")
start()
})
frame.$("a[href='/dinosaurs.html']").click()
})
asyncTest("uses first argument as options", function() {
var frame = this.frame
frame.$("a").pjax({container: "#main", push: true})
frame.$("#main").on("pjax:end", function() {
equal(frame.location.pathname, "/dinosaurs.html")
start()
})
frame.$("a[href='/dinosaurs.html']").click()
})
asyncTest("uses first argument as container and second as options", function() {
var frame = this.frame
frame.$("a").pjax("#main", {push: true})
frame.$("#main").on("pjax:end", function() {
equal(frame.location.pathname, "/dinosaurs.html")
start()
})
frame.$("a[href='/dinosaurs.html']").click()
})
asyncTest("defaults to data-pjax container", function() {
var frame = this.frame
frame.$("a").attr('data-pjax', "#main")
frame.$("a").pjax()
frame.$("#main").on("pjax:end", function() {
equal(frame.location.pathname, "/dinosaurs.html")
start()
})
frame.$("a[href='/dinosaurs.html']").click()
})
asyncTest("sets clickedElement to target jquery object", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var link = frame.$("a[href='/dinosaurs.html']")[0]
frame.$("#main").on("pjax:end", function(event, xhr, options) {
equal(link, options.clickedElement[0])
start()
})
frame.$(link).click()
})
asyncTest("sets relatedTarget to clicked element", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var link = frame.$("a[href='/dinosaurs.html']")[0]
frame.$("#main").on("pjax:end", function(event, xhr, options) {
equal(link, event.relatedTarget)
start()
})
frame.$(link).click()
})
asyncTest("doesn't ignore left click", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var event = frame.$.Event('click')
event.which = 0
frame.$("a[href='/dinosaurs.html']").trigger(event)
ok(event.isDefaultPrevented())
start()
})
asyncTest("ignores middle clicks", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var event = frame.$.Event('click')
event.which = 3
frame.$("a[href='/dinosaurs.html']").trigger(event)
ok(!event.isDefaultPrevented())
start()
})
asyncTest("ignores command clicks", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var event = frame.$.Event('click')
event.metaKey = true
frame.$("a[href='/dinosaurs.html']").trigger(event)
ok(!event.isDefaultPrevented())
start()
})
asyncTest("ignores ctrl clicks", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var event = frame.$.Event('click')
event.ctrlKey = true
frame.$("a[href='/dinosaurs.html']").trigger(event)
ok(!event.isDefaultPrevented())
start()
})
asyncTest("ignores cross origin links", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var event = frame.$.Event('click')
frame.$("a[href='https://www.google.com/']").trigger(event)
notEqual(event.result, false)
start()
})
asyncTest("ignores same page anchors", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
var event = frame.$.Event('click')
frame.$("a[href='#main']").trigger(event)
notEqual(event.result, false)
start()
})
asyncTest("scrolls to anchor after load", function() {
var frame = this.frame
frame.$("a").pjax({ container: "#main" })
frame.$("#main").on("pjax:end", function() {
equal(frame.location.pathname, "/dinosaurs.html")
equal(frame.location.hash, "#main")
start()
})
var link = frame.$("a[href='/dinosaurs.html']")
link.attr('href', "/dinosaurs.html#main")
link.click()
})
}