main/view/main.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

/* (1) Imports
---------------------------------------------------------*/
/* (1) NPM libs */
2017-11-28 12:37:49 +00:00
import Vue from 'vue'
2017-12-04 23:00:22 +00:00
import VueRouter from 'vue-router'
/* (2) Internal libs */
2017-12-04 23:00:22 +00:00
import {API} from './lib/api-es6'
import {InfoBox} from './lib/infobox-es6'
import {WSClient,WSClientBuilder} from './lib/ws-client-es6'
2017-12-04 23:00:22 +00:00
import routes from './routes'
2017-11-28 12:37:49 +00:00
/* (3) Vues */
import wrapper_vue from './vue/wrapper.vue'
/* (2) Initialisation
---------------------------------------------------------*/
/* (1) global store init */
require('./vue-config');
window.gstore.add('server', window._SERVER);
window.infobox = new InfoBox(gstore.data.info);
/* (2) API */
window.api = new API(gstore.data.is_local ? 'http://ndli1718/api/v/1.0/' : 'https://ndli1718.xdrm.io/api/v/1.0/');
/* (3) wsclient */
window.wsc = new WSClientBuilder(gstore.data.is_local ? 'ws://localhost:9999' : 'wss://websocket.xdrm.io');
2017-12-04 23:00:22 +00:00
/* (4) Init vue router */
Vue.use(VueRouter);
const router = new VueRouter({
2017-12-04 23:00:22 +00:00
mode: 'history',
routes: routes[0]
});
/* (5) Render view */
2017-11-28 12:37:49 +00:00
new Vue({
el: '#main-vue',
router,
2017-12-04 23:00:22 +00:00
render: h => h(wrapper_vue)
});
/* (6) Navigate at page load (to update notifications count) */
gstore.data.func.nav(router, null);
/* (3) Set WebSocket channels
---------------------------------------------------------*/
/* (1) Message channel */
window.wsc_chat = wsc.channel('chat').listen(function(msg, err){
// {1} Manage error //
if( msg == null && err != null )
return infobox.show('Erreur de connexion WebSocket@chat ('+err+')', 'error', 3000);
// {2} Manage wsclient error //
if( typeof msg.error != 'boolean' || msg.error !== false )
return infobox.show('Erreur de connexion WebSocket@chat', 'warning', 3000);
// {3} If no message -> exit //
if( msg.msg == null )
return;
// {4} Play sound if 1msg received + not already on page //
// note: 1msg means a new message but not the page load past buffer
if( msg.msg.length == 1 && router.app.$route.path != '/inbox' )
( new Audio('https://notificationsounds.com/message-tones/communication-channel-519/download/mp3') ).play();
// {5} Add messages to stack //
gstore.data.notif.inbox.data = gstore.data.notif.inbox.data.concat( msg.msg );
// {6} Add notification count if not already on page //
if( router.app.$route.path != '/inbox' )
gstore.data.notif.inbox.count += msg.msg.length;
}).send({name: _SERVER.session.name});