mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.7 KiB
110 lines
2.7 KiB
3 years ago
|
import io from 'socket.io-client'
|
||
|
import Vue from 'vue'
|
||
|
|
||
|
export default function({
|
||
|
app,
|
||
|
$axios,
|
||
|
store,
|
||
|
route
|
||
|
}, inject) {
|
||
|
let socket
|
||
|
|
||
3 years ago
|
const init = async(token) => {
|
||
3 years ago
|
try {
|
||
|
if (socket) {
|
||
|
socket.disconnect()
|
||
|
}
|
||
3 years ago
|
const url = new URL($axios.defaults.baseURL, window.location.href.split(/[?#]/)[0]).href
|
||
3 years ago
|
socket = io(url, {
|
||
|
extraHeaders: { 'xc-auth': token }
|
||
|
})
|
||
3 years ago
|
|
||
3 years ago
|
socket.on('connect_error', () => {
|
||
|
socket.disconnect()
|
||
|
socket = null
|
||
|
})
|
||
3 years ago
|
} catch {
|
||
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
app.router.onReady(() => {
|
||
|
app.router.afterEach(function(to, from) {
|
||
3 years ago
|
if (!socket || (to.path === from.path && (to.query && to.query.type) === (from.query && from.query.type))) {
|
||
3 years ago
|
return
|
||
|
}
|
||
|
socket.emit('page', {
|
||
|
path: to.matched[0].path + (to.query && to.query.type ? `?type=${to.query.type}` : '')
|
||
|
})
|
||
|
})
|
||
3 years ago
|
if (socket) {
|
||
|
socket.emit('page', {
|
||
|
path: route.matched[0].path + (route.query && route.query.type ? `?type=${route.query.type}` : '')
|
||
|
})
|
||
|
}
|
||
3 years ago
|
})
|
||
|
|
||
3 years ago
|
const tele = {
|
||
|
emit(evt, data) {
|
||
3 years ago
|
// debugger
|
||
3 years ago
|
if (socket) {
|
||
|
socket.emit('event', {
|
||
|
event: evt,
|
||
|
...(data || {}),
|
||
3 years ago
|
path: gatPath(app)
|
||
3 years ago
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
inject('tele', tele)
|
||
3 years ago
|
inject('e', tele.emit)
|
||
3 years ago
|
|
||
|
function getListener(binding) {
|
||
|
return function(e) {
|
||
3 years ago
|
if (!socket) {
|
||
|
return
|
||
|
}
|
||
3 years ago
|
const event = binding.value && binding.value[0]
|
||
|
const data = binding.value && binding.value[1]
|
||
|
const extra = binding.value && binding.value.slice(2)
|
||
|
tele.emit(event,
|
||
|
{
|
||
|
data,
|
||
|
extra
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Vue.directive('t', {
|
||
|
bind(el, binding, vnode) {
|
||
|
if (vnode.componentInstance) {
|
||
3 years ago
|
if (vnode.componentInstance.$el) {
|
||
|
vnode.componentInstance.$el.addEventListener('click', getListener(binding))
|
||
|
} else {
|
||
|
vnode.componentInstance.$on('click', getListener(binding))
|
||
|
}
|
||
3 years ago
|
} else {
|
||
|
el.addEventListener('click', getListener(binding))
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
3 years ago
|
store.watch(state => state.project.appInfo && state.project.appInfo.teleEnabled && state.users.token, (token) => {
|
||
3 years ago
|
if (token) {
|
||
3 years ago
|
init(token).then(() => {
|
||
|
})
|
||
3 years ago
|
} else if (socket) {
|
||
3 years ago
|
socket.disconnect()
|
||
|
socket = null
|
||
|
}
|
||
3 years ago
|
})
|
||
3 years ago
|
if (store.state.project.appInfo && store.state.project.appInfo.teleEnabled && store.state.users.token) {
|
||
3 years ago
|
init(store.state.users.token).then(() => {
|
||
|
})
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
|
||
|
function gatPath(app) {
|
||
|
return app && app.router && app.router.app && app.router.app.$route && app.router.app.$route.matched && app.router.app.$route.matched[0] && app.router.app.$route.matched[0].path
|
||
|
}
|