-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapter.js
131 lines (108 loc) · 3.7 KB
/
Adapter.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
import axios from 'axios';
import { PjaxEventBus } from './PjaxEventBus';
class Plugin {
static install(Vue, options) {
Vue.$pjaxAdapter = new Plugin(Vue, options);
Vue.mixin({
created() {
if (window.app && window.app.__vue__ && window.app.__vue__.$store) {
this.$store = window.app.__vue__.$store;
}
},
mounted() {
if (!Vue.$pjaxAdapter.hasInitialised) {
Vue.$pjaxAdapter.init();
}
},
});
}
constructor(Vue, options) {
this.Vue = Vue;
this.config = Object.assign(this.defaultConfig, options);
}
init() {
this.setHeaders();
this.configureBackButton();
this.configureClickHandler();
this.hasInitialised = true;
}
setHeaders() {
axios.defaults.headers.common['X-PJAX'] = true;
axios.defaults.headers.common['X-PJAX-Container'] = this.config.targetSelector;
}
configureBackButton() {
window.onpopstate = () => {
window.location = window.location.href;
};
}
configureClickHandler() {
document.addEventListener('click', this.clickListener.bind(this));
}
clickListener(e) {
let element = e.target;
if (element.nodeName == 'A') {
if (this.isDisabledByDataAttribute(element)) return true;
if (this.isDisabledByClassAttribute(element)) return true;
e.preventDefault();
this.clickHandler(element);
}
}
isDisabledByDataAttribute(element) {
let testedNode = element;
while (testedNode !== document) {
if (testedNode.dataset != null && testedNode.dataset.noPjax !== undefined) return true;
testedNode = testedNode.parentNode;
}
return false;
}
isDisabledByClassAttribute(element) {
let testedNode = element;
while (testedNode !== document) {
if (testedNode.classList.contains('no-pjax')) return true;
testedNode = testedNode.parentNode;
}
return false;
}
clickHandler(link) {
PjaxEventBus.$emit('pjax:send');
return axios.get(link.href)
.then(
response => {
document.querySelector('head > title').innerHTML = this.extractTitle(response.data);
document.querySelector(this.config.targetSelector).innerHTML = this.withoutTitle(response.data);
window.history.pushState({}, '', response.headers['x-pjax-url']);
new this.Vue({
el: this.config.targetSelector,
});
PjaxEventBus.$emit('pjax:success');
PjaxEventBus.$emit('pjax:complete');
},
error => {
if (error != undefined
&& error.hasOwnProperty('response')
&& error.response != undefined
&& error.response.hasOwnProperty('status')
&& error.response.status === 503) {
window.location.reload();
}
PjaxEventBus.$emit('pjax:error');
PjaxEventBus.$emit('pjax:complete');
}
);
}
extractTitle(html) {
return this.titlePattern.exec(html)[1];
}
withoutTitle(html) {
return html.replace(this.titlePattern.exec(html)[0], '');
}
get defaultConfig() {
return {
targetSelector: '#pjax-container',
};
}
get titlePattern() {
return new RegExp(/\s*<title>(.*)<\/title>\s*/);
}
}
export default Plugin;