forked from MoOx/pjax
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpjax.js
1192 lines (1006 loc) · 32.8 KB
/
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Pjax = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
// var executeScripts = require("./lib/execute-scripts"); // unused-var
var forEachEls = require("./lib/foreach-els");
var parseOptions = require("./lib/parse-options");
var switches = require("./lib/switches");
var newUid = require("./lib/uniqueid");
var on = require("./lib/events/on");
var trigger = require("./lib/events/trigger");
var clone = require("./lib/util/clone");
var contains = require("./lib/util/contains");
var extend = require("./lib/util/extend");
var noop = require("./lib/util/noop");
var Pjax = function(options) {
this.state = {
numPendingSwitches: 0,
href: null,
options: null
};
this.options = parseOptions(options);
this.log("Pjax options", this.options);
if (this.options.scrollRestoration && "scrollRestoration" in history) {
history.scrollRestoration = "manual";
}
this.maxUid = this.lastUid = newUid();
this.parseDOM(document);
on(
window,
"popstate",
function(st) {
if (st.state) {
var opt = clone(this.options);
opt.url = st.state.url;
opt.title = st.state.title;
// Since state already exists, prevent it from being pushed again
opt.history = false;
opt.scrollPos = st.state.scrollPos;
if (st.state.uid < this.lastUid) {
opt.backward = true;
} else {
opt.forward = true;
}
this.lastUid = st.state.uid;
// @todo implement history cache here, based on uid
this.loadUrl(st.state.url, opt);
}
}.bind(this)
);
};
Pjax.switches = switches;
Pjax.prototype = {
log: require("./lib/proto/log"),
getElements: function(el) {
return el.querySelectorAll(this.options.elements);
},
parseDOM: function(el) {
var parseElement = require("./lib/proto/parse-element");
forEachEls(this.getElements(el), parseElement, this);
},
refresh: function(el) {
this.parseDOM(el || document);
},
reload: function() {
window.location.reload();
},
attachLink: require("./lib/proto/attach-link"),
attachForm: require("./lib/proto/attach-form"),
forEachSelectors: function(cb, context, DOMcontext) {
return require("./lib/foreach-selectors").bind(this)(
this.options.selectors,
cb,
context,
DOMcontext
);
},
switchSelectors: function(selectors, fromEl, toEl, options) {
return require("./lib/switches-selectors").bind(this)(
this.options.switches,
this.options.switchesOptions,
selectors,
fromEl,
toEl,
options
);
},
latestChance: function(href) {
window.location = href;
},
onSwitch: function() {
trigger(window, "resize scroll");
this.state.numPendingSwitches--;
// debounce calls, so we only run this once after all switches are finished.
if (this.state.numPendingSwitches === 0) {
this.afterAllSwitches();
}
},
loadContent: function(html, options) {
if (typeof html !== "string") {
trigger(document, "pjax:complete pjax:error", options);
return;
}
var tmpEl = document.implementation.createHTMLDocument("pjax");
// parse HTML attributes to copy them
// since we are forced to use documentElement.innerHTML (outerHTML can't be used for <html>)
var htmlRegex = /<html[^>]+>/gi;
var htmlAttribsRegex = /\s?[a-z:]+(?:=['"][^'">]+['"])*/gi;
var matches = html.match(htmlRegex);
if (matches && matches.length) {
matches = matches[0].match(htmlAttribsRegex);
if (matches.length) {
matches.shift();
matches.forEach(function(htmlAttrib) {
var attr = htmlAttrib.trim().split("=");
if (attr.length === 1) {
tmpEl.documentElement.setAttribute(attr[0], true);
} else {
tmpEl.documentElement.setAttribute(attr[0], attr[1].slice(1, -1));
}
});
}
}
tmpEl.documentElement.innerHTML = html;
this.log(
"load content",
tmpEl.documentElement.attributes,
tmpEl.documentElement.innerHTML.length
);
// Clear out any focused controls before inserting new page contents.
if (
document.activeElement &&
contains(document, this.options.selectors, document.activeElement)
) {
try {
document.activeElement.blur();
} catch (e) {} // eslint-disable-line no-empty
}
this.switchSelectors(this.options.selectors, tmpEl, document, options);
},
abortRequest: require("./lib/abort-request"),
doRequest: require("./lib/send-request"),
handleResponse: require("./lib/proto/handle-response"),
loadUrl: function(href, options) {
options =
typeof options === "object"
? extend({}, this.options, options)
: clone(this.options);
this.log("load href", href, options);
// Abort any previous request
this.abortRequest(this.request);
trigger(document, "pjax:send", options);
// Do the request
this.request = this.doRequest(
href,
options,
this.handleResponse.bind(this)
);
},
afterAllSwitches: function() {
// FF bug: Won’t autofocus fields that are inserted via JS.
// This behavior is incorrect. So if theres no current focus, autofocus
// the last field.
//
// http://www.w3.org/html/wg/drafts/html/master/forms.html
var autofocusEl = Array.prototype.slice
.call(document.querySelectorAll("[autofocus]"))
.pop();
if (autofocusEl && document.activeElement !== autofocusEl) {
autofocusEl.focus();
}
// execute scripts when DOM have been completely updated
this.options.selectors.forEach(function(selector) {
forEachEls(document.querySelectorAll(selector), function(el) {
// executeScripts(el);
if (el === 0) ; // intentially left blank!
});
});
var state = this.state;
if (state.options.history) {
if (!window.history.state) {
this.lastUid = this.maxUid = newUid();
window.history.replaceState(
{
url: window.location.href,
title: document.title,
uid: this.maxUid,
scrollPos: [0, 0]
},
document.title
);
}
// Update browser history
this.lastUid = this.maxUid = newUid();
window.history.pushState(
{
url: state.href,
title: state.options.title,
uid: this.maxUid,
scrollPos: [0, 0]
},
state.options.title,
state.href
);
}
this.forEachSelectors(function(el) {
this.parseDOM(el);
}, this);
// Fire Events
trigger(document, "pjax:complete pjax:success", state.options);
if (typeof state.options.analytics === "function") {
state.options.analytics();
}
if (state.options.history) {
// First parse url and check for hash to override scroll
var a = document.createElement("a");
a.href = this.state.href;
if (a.hash) {
var name = a.hash.slice(1);
name = decodeURIComponent(name);
var curtop = 0;
var target =
document.getElementById(name) || document.getElementsByName(name)[0];
if (target) {
// http://stackoverflow.com/questions/8111094/cross-browser-javascript-function-to-find-actual-position-of-an-element-in-page
if (target.offsetParent) {
do {
curtop += target.offsetTop;
target = target.offsetParent;
} while (target);
}
}
window.scrollTo(0, curtop);
} else if (state.options.scrollTo !== false) {
// Scroll page to top on new page load
if (state.options.scrollTo.length > 1) {
window.scrollTo(state.options.scrollTo[0], state.options.scrollTo[1]);
} else {
window.scrollTo(0, state.options.scrollTo);
}
}
} else if (state.options.scrollRestoration && state.options.scrollPos) {
window.scrollTo(state.options.scrollPos[0], state.options.scrollPos[1]);
}
this.state = {
numPendingSwitches: 0,
href: null,
options: null
};
}
};
Pjax.isSupported = require("./lib/is-supported");
// arguably could do `if( require("./lib/is-supported")()) {` but that might be a little to simple
if (Pjax.isSupported()) {
module.exports = Pjax;
}
// if there isn’t required browser functions, returning stupid api
else {
var stupidPjax = noop;
for (var key in Pjax.prototype) {
if (
Pjax.prototype.hasOwnProperty(key) &&
typeof Pjax.prototype[key] === "function"
) {
stupidPjax[key] = noop;
}
}
module.exports = stupidPjax;
}
},{"./lib/abort-request":2,"./lib/events/on":3,"./lib/events/trigger":4,"./lib/foreach-els":5,"./lib/foreach-selectors":6,"./lib/is-supported":7,"./lib/parse-options":8,"./lib/proto/attach-form":9,"./lib/proto/attach-link":10,"./lib/proto/handle-response":11,"./lib/proto/log":12,"./lib/proto/parse-element":13,"./lib/send-request":14,"./lib/switches":16,"./lib/switches-selectors":15,"./lib/uniqueid":17,"./lib/util/clone":18,"./lib/util/contains":19,"./lib/util/extend":20,"./lib/util/noop":21}],2:[function(require,module,exports){
var noop = require("./util/noop");
module.exports = function(request) {
if (request && request.readyState < 4) {
request.onreadystatechange = noop;
request.abort();
}
};
},{"./util/noop":21}],3:[function(require,module,exports){
var forEachEls = require("../foreach-els");
module.exports = function(els, events, listener, useCapture) {
events = typeof events === "string" ? events.split(" ") : events;
events.forEach(function(e) {
forEachEls(els, function(el) {
el.addEventListener(e, listener, useCapture);
});
});
};
},{"../foreach-els":5}],4:[function(require,module,exports){
var forEachEls = require("../foreach-els");
module.exports = function(els, events, opts) {
events = typeof events === "string" ? events.split(" ") : events;
events.forEach(function(e) {
var event;
event = document.createEvent("HTMLEvents");
event.initEvent(e, true, true);
event.eventName = e;
if (opts) {
Object.keys(opts).forEach(function(key) {
event[key] = opts[key];
});
}
forEachEls(els, function(el) {
var domFix = false;
if (!el.parentNode && el !== document && el !== window) {
// THANK YOU IE (9/10/11)
// dispatchEvent doesn't work if the element is not in the DOM
domFix = true;
document.body.appendChild(el);
}
el.dispatchEvent(event);
if (domFix) {
el.parentNode.removeChild(el);
}
});
});
};
},{"../foreach-els":5}],5:[function(require,module,exports){
/* global HTMLCollection: true */
module.exports = function(els, fn, context) {
if (
els instanceof HTMLCollection ||
els instanceof NodeList ||
els instanceof Array
) {
return Array.prototype.forEach.call(els, fn, context);
}
// assume simple DOM element
return fn.call(context, els);
};
},{}],6:[function(require,module,exports){
var forEachEls = require("./foreach-els");
module.exports = function(selectors, cb, context, DOMcontext) {
DOMcontext = DOMcontext || document;
selectors.forEach(function(selector) {
forEachEls(DOMcontext.querySelectorAll(selector), cb, context);
});
};
},{"./foreach-els":5}],7:[function(require,module,exports){
module.exports = function() {
// Borrowed wholesale from https://github.com/defunkt/jquery-pjax
return (
window.history &&
window.history.pushState &&
window.history.replaceState &&
// pushState isn’t reliable on iOS until 5.
!navigator.userAgent.match(
/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/
)
);
};
},{}],8:[function(require,module,exports){
/* global _gaq: true, ga: true */
var defaultSwitches = require("./switches");
module.exports = function(options) {
options = options || {};
options.elements = options.elements || "a[href], form[action]";
options.selectors = options.selectors || ["title", ".js-Pjax"];
options.switches = options.switches || {};
options.switchesOptions = options.switchesOptions || {};
options.history =
typeof options.history === "undefined" ? true : options.history;
options.analytics =
typeof options.analytics === "function" || options.analytics === false
? options.analytics
: defaultAnalytics;
options.scrollTo =
typeof options.scrollTo === "undefined" ? 0 : options.scrollTo;
options.scrollRestoration =
typeof options.scrollRestoration !== "undefined"
? options.scrollRestoration
: true;
options.cacheBust =
typeof options.cacheBust === "undefined" ? true : options.cacheBust;
options.debug = options.debug || false;
options.timeout = options.timeout || 0;
options.currentUrlFullReload =
typeof options.currentUrlFullReload === "undefined"
? false
: options.currentUrlFullReload;
// We can’t replace body.outerHTML or head.outerHTML.
// It creates a bug where a new body or head are created in the DOM.
// If you set head.outerHTML, a new body tag is appended, so the DOM has 2 body nodes, and vice versa
if (!options.switches.head) {
options.switches.head = defaultSwitches.switchElementsAlt;
}
if (!options.switches.body) {
options.switches.body = defaultSwitches.switchElementsAlt;
}
return options;
};
/* istanbul ignore next */
function defaultAnalytics() {
if (window._gaq) {
_gaq.push(["_trackPageview"]);
}
if (window.ga) {
ga("send", "pageview", { page: location.pathname, title: document.title });
}
}
},{"./switches":16}],9:[function(require,module,exports){
var on = require("../events/on");
var clone = require("../util/clone");
var attrState = "data-pjax-state";
var formAction = function(el, event) {
if (isDefaultPrevented(event)) {
return;
}
// Since loadUrl modifies options and we may add our own modifications below,
// clone it so the changes don't persist
var options = clone(this.options);
// Initialize requestOptions
options.requestOptions = {
requestUrl: el.getAttribute("action") || window.location.href,
requestMethod: el.getAttribute("method") || "GET"
};
// create a testable virtual link of the form action
var virtLinkElement = document.createElement("a");
virtLinkElement.setAttribute("href", options.requestOptions.requestUrl);
var attrValue = checkIfShouldAbort(virtLinkElement, options);
if (attrValue) {
el.setAttribute(attrState, attrValue);
return;
}
event.preventDefault();
if (el.enctype === "multipart/form-data") {
options.requestOptions.formData = new FormData(el);
} else {
options.requestOptions.requestParams = parseFormElements(el);
}
el.setAttribute(attrState, "submit");
options.triggerElement = el;
this.loadUrl(virtLinkElement.href, options);
};
function parseFormElements(el) {
var requestParams = [];
var formElements = el.elements;
for (var i = 0; i < formElements.length; i++) {
var element = formElements[i];
var tagName = element.tagName.toLowerCase();
// jscs:disable disallowImplicitTypeConversion
if (
!!element.name &&
element.attributes !== undefined &&
tagName !== "button"
) {
// jscs:enable disallowImplicitTypeConversion
var type = element.attributes.type;
if (
!type ||
(type.value !== "checkbox" && type.value !== "radio") ||
element.checked
) {
// Build array of values to submit
var values = [];
if (tagName === "select") {
var opt;
for (var j = 0; j < element.options.length; j++) {
opt = element.options[j];
if (opt.selected && !opt.disabled) {
values.push(opt.hasAttribute("value") ? opt.value : opt.text);
}
}
} else {
values.push(element.value);
}
for (var k = 0; k < values.length; k++) {
requestParams.push({
name: encodeURIComponent(element.name),
value: encodeURIComponent(values[k])
});
}
}
}
}
return requestParams;
}
function checkIfShouldAbort(virtLinkElement, options) {
// Ignore external links.
if (
virtLinkElement.protocol !== window.location.protocol ||
virtLinkElement.host !== window.location.host
) {
return "external";
}
// Ignore click if we are on an anchor on the same page
if (
virtLinkElement.hash &&
virtLinkElement.href.replace(virtLinkElement.hash, "") ===
window.location.href.replace(location.hash, "")
) {
return "anchor";
}
// Ignore empty anchor "foo.html#"
if (virtLinkElement.href === window.location.href.split("#")[0] + "#") {
return "anchor-empty";
}
// if declared as a full reload, just normally submit the form
if (
options.currentUrlFullReload &&
virtLinkElement.href === window.location.href.split("#")[0]
) {
return "reload";
}
}
var isDefaultPrevented = function(event) {
return event.defaultPrevented || event.returnValue === false;
};
module.exports = function(el) {
var that = this;
el.setAttribute(attrState, "");
on(el, "submit", function(event) {
formAction.call(that, el, event);
});
};
},{"../events/on":3,"../util/clone":18}],10:[function(require,module,exports){
var on = require("../events/on");
var clone = require("../util/clone");
var attrState = "data-pjax-state";
var linkAction = function(el, event) {
if (isDefaultPrevented(event)) {
return;
}
// Since loadUrl modifies options and we may add our own modifications below,
// clone it so the changes don't persist
var options = clone(this.options);
var attrValue = checkIfShouldAbort(el, event);
if (attrValue) {
el.setAttribute(attrState, attrValue);
return;
}
event.preventDefault();
// don’t do "nothing" if user try to reload the page by clicking the same link twice
if (
this.options.currentUrlFullReload &&
el.href === window.location.href.split("#")[0]
) {
el.setAttribute(attrState, "reload");
this.reload();
return;
}
el.setAttribute(attrState, "load");
options.triggerElement = el;
this.loadUrl(el.href, options);
};
function checkIfShouldAbort(el, event) {
// Don’t break browser special behavior on links (like page in new window)
if (
event.which > 1 ||
event.metaKey ||
event.ctrlKey ||
event.shiftKey ||
event.altKey
) {
return "modifier";
}
// we do test on href now to prevent unexpected behavior if for some reason
// user have href that can be dynamically updated
// Ignore external links.
if (
el.protocol !== window.location.protocol ||
el.host !== window.location.host
) {
return "external";
}
// Ignore anchors on the same page (keep native behavior)
if (
el.hash &&
el.href.replace(el.hash, "") ===
window.location.href.replace(location.hash, "")
) {
return "anchor";
}
// Ignore empty anchor "foo.html#"
if (el.href === window.location.href.split("#")[0] + "#") {
return "anchor-empty";
}
}
var isDefaultPrevented = function(event) {
return event.defaultPrevented || event.returnValue === false;
};
module.exports = function(el) {
var that = this;
el.setAttribute(attrState, "");
on(el, "click", function(event) {
linkAction.call(that, el, event);
});
on(
el,
"keyup",
function(event) {
if (event.keyCode === 13) {
linkAction.call(that, el, event);
}
}.bind(this)
);
};
},{"../events/on":3,"../util/clone":18}],11:[function(require,module,exports){
var clone = require("../util/clone");
var newUid = require("../uniqueid");
var trigger = require("../events/trigger");
module.exports = function(responseText, request, href, options) {
options = clone(options || this.options);
options.request = request;
// Fail if unable to load HTML via AJAX
if (responseText === false) {
trigger(document, "pjax:complete pjax:error", options);
return;
}
// push scroll position to history
var currentState = window.history.state || {};
window.history.replaceState(
{
url: currentState.url || window.location.href,
title: currentState.title || document.title,
uid: currentState.uid || newUid(),
scrollPos: [
document.documentElement.scrollLeft || document.body.scrollLeft,
document.documentElement.scrollTop || document.body.scrollTop
]
},
document.title,
window.location.href
);
// Check for redirects
var oldHref = href;
if (request.responseURL) {
if (href !== request.responseURL) {
href = request.responseURL;
}
} else if (request.getResponseHeader("X-PJAX-URL")) {
href = request.getResponseHeader("X-PJAX-URL");
} else if (request.getResponseHeader("X-XHR-Redirected-To")) {
href = request.getResponseHeader("X-XHR-Redirected-To");
}
// Add back the hash if it was removed
var a = document.createElement("a");
a.href = oldHref;
var oldHash = a.hash;
a.href = href;
if (oldHash && !a.hash) {
a.hash = oldHash;
href = a.href;
}
this.state.href = href;
this.state.options = options;
try {
this.loadContent(responseText, options);
} catch (e) {
trigger(document, "pjax:error", options);
if (!this.options.debug) {
if (console && console.error) {
console.error("Pjax switch fail: ", e);
}
return this.latestChance(href);
} else {
throw e;
}
}
};
},{"../events/trigger":4,"../uniqueid":17,"../util/clone":18}],12:[function(require,module,exports){
module.exports = function() {
if (this.options.debug && console) {
if (typeof console.log === "function") {
console.log.apply(console, arguments);
}
// IE is weird
else if (console.log) {
console.log(arguments);
}
}
};
},{}],13:[function(require,module,exports){
var attrState = "data-pjax-state";
module.exports = function(el) {
switch (el.tagName.toLowerCase()) {
case "a":
// only attach link if el does not already have link attached
if (!el.hasAttribute(attrState)) {
this.attachLink(el);
}
break;
case "form":
// only attach link if el does not already have link attached
if (!el.hasAttribute(attrState)) {
this.attachForm(el);
}
break;
default:
throw "Pjax can only be applied on <a> or <form> submit";
}
};
},{}],14:[function(require,module,exports){
var updateQueryString = require("./util/update-query-string");
module.exports = function(location, options, callback) {
options = options || {};
var queryString;
var requestOptions = options.requestOptions || {};
var requestMethod = (requestOptions.requestMethod || "GET").toUpperCase();
var requestParams = requestOptions.requestParams || null;
var formData = requestOptions.formData || null;
var requestPayload = null;
var request = new XMLHttpRequest();
var timeout = options.timeout || 0;
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
callback(request.responseText, request, location, options);
} else if (request.status !== 0) {
callback(null, request, location, options);
}
}
};
request.onerror = function(e) {
console.log(e);
callback(null, request, location, options);
};
request.ontimeout = function() {
callback(null, request, location, options);
};
// Prepare the request payload for forms, if available
if (requestParams && requestParams.length) {
// Build query string
queryString = requestParams
.map(function(param) {
return param.name + "=" + param.value;
})
.join("&");
switch (requestMethod) {
case "GET":
// Reset query string to avoid an issue with repeat submissions where checkboxes that were
// previously checked are incorrectly preserved
location = location.split("?")[0];
// Append new query string
location += "?" + queryString;
break;
case "POST":
// Send query string as request payload
requestPayload = queryString;
break;
}
} else if (formData) {
requestPayload = formData;
}
// Add a timestamp as part of the query string if cache busting is enabled
if (options.cacheBust) {
location = updateQueryString(location, "t", Date.now());
}
request.open(requestMethod, location, true);
request.timeout = timeout;
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.setRequestHeader("X-PJAX", "true");
request.setRequestHeader(
"X-PJAX-Selectors",
JSON.stringify(options.selectors)
);
// Send the proper header information for POST forms
if (requestPayload && requestMethod === "POST" && !formData) {
request.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
}
request.send(requestPayload);
return request;
};
},{"./util/update-query-string":22}],15:[function(require,module,exports){
var forEachEls = require("./foreach-els");
var defaultSwitches = require("./switches");
module.exports = function(
switches,
switchesOptions,
selectors,
fromEl,
toEl,
options
) {
var switchesQueue = [];
selectors.forEach(function(selector) {
var newEls = fromEl.querySelectorAll(selector);
var oldEls = toEl.querySelectorAll(selector);
if (this.log) {
this.log("Pjax switch", selector, newEls, oldEls);
}
if (newEls.length !== oldEls.length) {
throw "DOM doesn’t look the same on new loaded page: ’" +
selector +
"’ - new " +
newEls.length +
", old " +
oldEls.length;
}
forEachEls(
newEls,
function(newEl, i) {
var oldEl = oldEls[i];
if (this.log) {
this.log("newEl", newEl, "oldEl", oldEl);
}
var callback = switches[selector]
? switches[selector].bind(
this,
oldEl,
newEl,
options,
switchesOptions[selector]
)
: defaultSwitches.outerHTML.bind(this, oldEl, newEl, options);
switchesQueue.push(callback);
},
this
);
}, this);
this.state.numPendingSwitches = switchesQueue.length;
switchesQueue.forEach(function(queuedSwitch) {
queuedSwitch();
});
};
},{"./foreach-els":5,"./switches":16}],16:[function(require,module,exports){
var on = require("./events/on");
module.exports = {
outerHTML: function(oldEl, newEl) {
oldEl.outerHTML = newEl.outerHTML;
this.onSwitch();
},
innerHTML: function(oldEl, newEl) {
oldEl.innerHTML = newEl.innerHTML;
if (newEl.className === "") {
oldEl.removeAttribute("class");
} else {
//oldEl.className = newEl.className;
}
this.onSwitch();
},
switchElementsAlt: function(oldEl, newEl) {
oldEl.innerHTML = newEl.innerHTML;
// Copy attributes from the new element to the old one
if (newEl.hasAttributes()) {
var attrs = newEl.attributes;
for (var i = 0; i < attrs.length; i++) {