2017-04-12 08:27:57 -04:00
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
2016-02-08 07:26:10 -05:00
// See License.txt for license information.
2016-03-14 08:50:46 -04:00
import $ from 'jquery' ;
2016-03-23 04:06:08 +05:00
require ( 'perfect-scrollbar/jquery' ) ( $ ) ;
2016-03-14 08:50:46 -04:00
import React from 'react' ;
import ReactDOM from 'react-dom' ;
2017-04-25 11:46:02 -04:00
import { Provider } from 'react-redux' ;
2016-06-22 10:30:01 -04:00
import { Router , browserHistory } from 'react-router/es6' ;
2016-08-30 18:56:02 -04:00
import PDFJS from 'pdfjs-dist' ;
2017-04-25 11:46:02 -04:00
2016-06-22 10:30:01 -04:00
import * as Websockets from 'actions/websocket_actions.jsx' ;
2017-04-25 11:46:02 -04:00
import { loadMeAndConfig } from 'actions/user_actions.jsx' ;
2017-02-13 09:27:28 -05:00
import ChannelStore from 'stores/channel_store.jsx' ;
2016-03-24 12:05:50 -04:00
import * as I18n from 'i18n/i18n.jsx' ;
2017-04-25 11:46:02 -04:00
2016-06-22 10:30:01 -04:00
// Import our styles
import 'bootstrap-colorpicker/dist/css/bootstrap-colorpicker.css' ;
import 'google-fonts/google-fonts.css' ;
import 'sass/styles.scss' ;
2016-08-29 09:50:00 -04:00
import 'katex/dist/katex.min.css' ;
2016-06-22 10:30:01 -04:00
2017-04-28 13:16:03 -04:00
// Redux actions
2017-04-25 11:46:02 -04:00
import store from 'stores/redux_store.jsx' ;
2017-04-28 13:16:03 -04:00
const dispatch = store . dispatch ;
const getState = store . getState ;
import { viewChannel } from 'mattermost-redux/actions/channels' ;
import { getClientConfig , getLicenseConfig , setUrl } from 'mattermost-redux/actions/general' ;
2017-04-25 11:46:02 -04:00
2016-06-22 10:30:01 -04:00
// Import the root of our routing tree
import rRoot from 'routes/route_root.jsx' ;
2016-04-01 11:48:19 -04:00
2016-08-30 18:56:02 -04:00
PDFJS . disableWorker = true ;
2016-02-08 07:26:10 -05:00
// This is for anything that needs to be done for ALL react components.
// This runs before we start to render anything.
function preRenderSetup ( callwhendone ) {
2016-04-21 22:37:01 -07:00
window . onerror = ( msg , url , line , column , stack ) => {
var l = { } ;
l . level = 'ERROR' ;
l . message = 'msg: ' + msg + ' row: ' + line + ' col: ' + column + ' stack: ' + stack + ' url: ' + url ;
$ . ajax ( {
2016-05-24 14:31:30 -07:00
url : '/api/v3/general/log_client' ,
2016-04-21 22:37:01 -07:00
dataType : 'json' ,
contentType : 'application/json' ,
type : 'POST' ,
data : JSON . stringify ( l )
} ) ;
if ( window . mm _config && window . mm _config . EnableDeveloper === 'true' ) {
2016-12-21 23:29:38 +01:00
window . ErrorStore . storeLastError ( { type : 'developer' , message : 'DEVELOPER MODE: A JavaScript error has occurred. Please use the JavaScript console to capture and report the error (row: ' + line + ' col: ' + column + ').' } ) ;
2016-04-21 22:37:01 -07:00
window . ErrorStore . emitChange ( ) ;
2016-02-08 07:26:10 -05:00
}
2016-04-21 22:37:01 -07:00
} ;
2016-02-08 07:26:10 -05:00
2016-04-21 22:37:01 -07:00
var d1 = $ . Deferred ( ) ; //eslint-disable-line new-cap
2016-02-08 07:26:10 -05:00
2017-04-25 11:46:02 -04:00
setUrl ( window . location . origin ) ;
2017-05-04 16:36:31 -04:00
if ( document . cookie . indexOf ( 'MMUSERID=' ) > - 1 ) {
2017-04-25 11:46:02 -04:00
loadMeAndConfig ( ( ) => d1 . resolve ( ) ) ;
} else {
getClientConfig ( ) ( store . dispatch , store . getState ) . then (
( config ) => {
global . window . mm _config = config ;
getLicenseConfig ( ) ( store . dispatch , store . getState ) . then (
( license ) => {
global . window . mm _license = license ;
d1 . resolve ( ) ;
}
) ;
}
) ;
}
2016-02-08 07:26:10 -05:00
2016-05-16 19:09:37 -04:00
// Make sure the websockets close and reset version
2016-03-14 08:50:46 -04:00
$ ( window ) . on ( 'beforeunload' ,
( ) => {
2017-04-07 14:47:58 -04:00
// Turn off to prevent getting stuck in a loop
$ ( window ) . off ( 'beforeunload' ) ;
2017-05-04 16:36:31 -04:00
if ( document . cookie . indexOf ( 'MMUSERID=' ) > - 1 ) {
2017-04-28 13:16:03 -04:00
viewChannel ( '' , ChannelStore . getCurrentId ( ) || '' ) ( dispatch , getState ) ;
2017-02-15 18:54:24 -05:00
}
2016-03-23 10:20:52 -04:00
Websockets . close ( ) ;
2016-03-14 08:50:46 -04:00
}
) ;
2016-03-21 12:33:44 -04:00
function afterIntl ( ) {
2016-04-21 22:37:01 -07:00
$ . when ( d1 ) . done ( ( ) => {
2016-06-02 16:47:26 -03:00
I18n . doAddLocaleData ( ) ;
2016-04-21 22:37:01 -07:00
callwhendone ( ) ;
} ) ;
2016-03-21 12:33:44 -04:00
}
if ( global . Intl ) {
afterIntl ( ) ;
} else {
2016-03-25 22:36:55 -04:00
I18n . safariFix ( afterIntl ) ;
2016-03-21 12:33:44 -04:00
}
2017-05-09 06:56:39 -05:00
// Prevent drag and drop files from navigating away from the app
document . addEventListener ( 'drop' , ( e ) => {
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
} ) ;
document . addEventListener ( 'dragover' , ( e ) => {
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
} ) ;
2016-02-08 07:26:10 -05:00
}
function renderRootComponent ( ) {
ReactDOM . render ( (
2017-04-25 11:46:02 -04:00
< Provider store = { store } >
< Router
history = { browserHistory }
routes = { rRoot }
/ >
< / Provider >
2016-02-08 07:26:10 -05:00
) ,
document . getElementById ( 'root' ) ) ;
}
2017-05-04 15:45:19 -04:00
let serverVersion = '' ;
store . subscribe ( ( ) => {
const newServerVersion = getState ( ) . entities . general . serverVersion ;
if ( serverVersion && serverVersion !== newServerVersion ) {
console . log ( 'Detected version update refreshing the page' ) ; //eslint-disable-line no-console
window . location . reload ( true ) ;
}
serverVersion = newServerVersion ;
} ) ;
2016-02-08 07:26:10 -05:00
global . window . setup _root = ( ) => {
// Do the pre-render setup and call renderRootComponent when done
preRenderSetup ( renderRootComponent ) ;
} ;