-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
43 lines (35 loc) · 1.07 KB
/
index.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
function handleVueDestruction(vue) {
const event = vue.$options.destroyEvent || defaultEvent();
document.addEventListener(event, function teardown() {
vue.$destroy();
document.removeEventListener(event, teardown);
});
}
const Mixin = {
beforeMount: function() {
// If this is the root component, we want to cache the original element contents to replace later
// We don't care about sub-components, just the root
if (this === this.$root && this.$el) {
handleVueDestruction(this);
// cache original element
this.$cachedHTML = this.$el.outerHTML;
// register root hook to restore original element on destroy
this.$once('hook:destroyed', function() {
if( this.$el.parentNode )
this.$el.outerHTML = this.$cachedHTML
});
}
}
};
function plugin(Vue, options) {
// Install a global mixin
Vue.mixin(Mixin)
}
function defaultEvent() {
if (typeof Turbo !== 'undefined') {
return 'turbo:visit';
}
return 'turbolinks:visit';
}
export { Mixin as turbolinksAdapterMixin };
export default plugin;