2016-08-18 11:41:13 +02:00
|
|
|
/**
|
|
|
|
* app.js
|
|
|
|
*
|
|
|
|
* This is the entry file for the application, only setup and boilerplate
|
|
|
|
* code.
|
|
|
|
*/
|
2017-11-11 22:14:05 +01:00
|
|
|
|
|
|
|
/* eslint-disable */
|
|
|
|
// Retrieve remote and backend URLs.
|
|
|
|
const remoteURL = process.env.REMOTE_URL || 'http://localhost:1337/admin';
|
|
|
|
const backendURL = process.env.BACKEND_URL || 'http://localhost:1337';
|
|
|
|
|
|
|
|
// Retrieve development URL to avoid to re-build.
|
|
|
|
const devFrontURL = document.getElementsByTagName('body')[0].getAttribute('front');
|
|
|
|
const devBackendURL = document.getElementsByTagName('body')[0].getAttribute('back');
|
|
|
|
|
|
|
|
import './public-path';
|
2016-08-18 11:41:13 +02:00
|
|
|
import 'babel-polyfill';
|
|
|
|
|
|
|
|
// Import all the third party stuff
|
|
|
|
import { Provider } from 'react-redux';
|
2017-09-26 16:36:28 +02:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2017-08-21 15:12:53 +02:00
|
|
|
import { ConnectedRouter } from 'react-router-redux';
|
|
|
|
import createHistory from 'history/createBrowserHistory';
|
2017-09-14 11:10:05 +02:00
|
|
|
import { merge, isFunction } from 'lodash';
|
2017-08-18 14:17:15 +02:00
|
|
|
import 'sanitize.css/sanitize.css';
|
2017-09-09 15:49:59 +02:00
|
|
|
import 'whatwg-fetch';
|
2017-08-18 14:17:15 +02:00
|
|
|
|
2016-08-18 11:41:13 +02:00
|
|
|
import LanguageProvider from 'containers/LanguageProvider';
|
2017-08-21 15:12:53 +02:00
|
|
|
|
2017-08-18 14:17:15 +02:00
|
|
|
import App from 'containers/App';
|
|
|
|
import { showNotification } from 'containers/NotificationProvider/actions';
|
|
|
|
import { pluginLoaded, updatePlugin } from 'containers/App/actions';
|
2016-08-18 11:41:13 +02:00
|
|
|
|
2017-08-18 14:17:15 +02:00
|
|
|
import configureStore from './store';
|
2017-08-14 15:22:42 +02:00
|
|
|
import { translationMessages, languages } from './i18n';
|
2017-11-11 22:14:05 +01:00
|
|
|
/* eslint-enable */
|
2017-11-08 17:40:47 +01:00
|
|
|
|
2016-08-18 11:41:13 +02:00
|
|
|
// Create redux store with history
|
|
|
|
const initialState = {};
|
2017-08-22 10:51:53 +02:00
|
|
|
const history = createHistory({
|
2017-11-11 22:14:05 +01:00
|
|
|
basename: (devFrontURL || remoteURL).replace(window.location.origin, ''),
|
2017-08-22 10:51:53 +02:00
|
|
|
});
|
2017-08-21 15:12:53 +02:00
|
|
|
const store = configureStore(initialState, history);
|
2016-08-18 11:41:13 +02:00
|
|
|
|
|
|
|
const render = (translatedMessages) => {
|
|
|
|
ReactDOM.render(
|
|
|
|
<Provider store={store}>
|
|
|
|
<LanguageProvider messages={translatedMessages}>
|
2017-08-21 15:12:53 +02:00
|
|
|
<ConnectedRouter history={history}>
|
|
|
|
<App />
|
|
|
|
</ConnectedRouter>
|
2016-08-18 11:41:13 +02:00
|
|
|
</LanguageProvider>
|
|
|
|
</Provider>,
|
|
|
|
document.getElementById('app')
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hot reloadable translation json files
|
|
|
|
if (module.hot) {
|
|
|
|
// modules.hot.accept does not accept dynamic dependencies,
|
|
|
|
// have to be constants at compile-time
|
|
|
|
module.hot.accept('./i18n', () => {
|
|
|
|
render(translationMessages);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chunked polyfill for browsers without Intl support
|
2016-09-09 11:21:54 +02:00
|
|
|
window.onload = function onLoad() {
|
|
|
|
if (!window.Intl) {
|
|
|
|
Promise.all([
|
|
|
|
System.import('intl'),
|
|
|
|
System.import('intl/locale-data/jsonp/en.js'),
|
2016-10-13 19:31:29 +02:00
|
|
|
System.import('intl/locale-data/jsonp/fr.js'),
|
2016-09-09 11:21:54 +02:00
|
|
|
]).then(() => render(translationMessages));
|
|
|
|
} else {
|
|
|
|
render(translationMessages);
|
|
|
|
}
|
|
|
|
};
|
2016-08-18 11:41:13 +02:00
|
|
|
|
2017-11-02 19:36:09 +01:00
|
|
|
// Don't inject plugins in development mode.
|
|
|
|
if (window.location.port !== '4000') {
|
2017-11-11 22:14:05 +01:00
|
|
|
fetch(`${devFrontURL || remoteURL}/config/plugins.json`)
|
2017-11-02 19:36:09 +01:00
|
|
|
.then(response => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(plugins => {
|
|
|
|
const body = document.getElementsByTagName('body')[0];
|
|
|
|
|
|
|
|
(plugins || []).forEach(plugin => {
|
|
|
|
const script = document.createElement('script');
|
2017-11-03 15:59:03 +01:00
|
|
|
script.src = plugin.source[process.env.NODE_ENV];
|
2017-11-02 19:36:09 +01:00
|
|
|
|
|
|
|
body.appendChild(script);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:38 +02:00
|
|
|
/**
|
|
|
|
* Public Strapi object exposed to the `window` object
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a plugin
|
|
|
|
*
|
|
|
|
* @param params
|
|
|
|
*/
|
2016-09-06 16:59:56 +02:00
|
|
|
const registerPlugin = (plugin) => {
|
2016-10-13 19:31:29 +02:00
|
|
|
// Merge admin translation messages
|
2017-09-14 11:10:05 +02:00
|
|
|
merge(translationMessages, plugin.translationMessages);
|
2017-08-14 17:09:34 +02:00
|
|
|
|
2017-09-14 11:10:05 +02:00
|
|
|
plugin.leftMenuSections = plugin.leftMenuSections || [];
|
2016-09-30 18:25:04 +02:00
|
|
|
|
2017-09-27 17:54:18 +02:00
|
|
|
switch (true) {
|
|
|
|
// Execute bootstrap function and check if plugin can be rendered
|
|
|
|
case isFunction(plugin.bootstrap) && isFunction(plugin.pluginRequirements):
|
2017-09-27 19:15:03 +02:00
|
|
|
plugin.pluginRequirements(plugin)
|
2017-09-27 17:54:18 +02:00
|
|
|
.then(plugin => {
|
2017-09-28 14:00:22 +02:00
|
|
|
return plugin.bootstrap(plugin);
|
|
|
|
})
|
|
|
|
.then(plugin => {
|
|
|
|
store.dispatch(pluginLoaded(plugin));
|
2017-09-27 17:54:18 +02:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
// Check if plugin can be rendered
|
|
|
|
case isFunction(plugin.pluginRequirements):
|
|
|
|
plugin.pluginRequirements(plugin).then(plugin => {
|
|
|
|
store.dispatch(pluginLoaded(plugin));
|
2017-10-13 16:46:18 +02:00
|
|
|
});
|
2017-09-27 17:54:18 +02:00
|
|
|
break;
|
|
|
|
// Execute bootstrap function
|
|
|
|
case isFunction(plugin.bootstrap):
|
|
|
|
plugin.bootstrap(plugin).then(plugin => {
|
|
|
|
store.dispatch(pluginLoaded(plugin));
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
2017-09-14 11:10:05 +02:00
|
|
|
store.dispatch(pluginLoaded(plugin));
|
|
|
|
}
|
2016-08-26 11:05:38 +02:00
|
|
|
};
|
|
|
|
|
2016-09-30 18:25:04 +02:00
|
|
|
const displayNotification = (message, status) => {
|
|
|
|
store.dispatch(showNotification(message, status));
|
|
|
|
};
|
|
|
|
|
2017-11-11 22:14:05 +01:00
|
|
|
window.strapi = Object.assign(window.strapi || {}, {
|
|
|
|
remoteURL: devFrontURL || remoteURL,
|
|
|
|
backendURL: devBackendURL || backendURL,
|
2016-08-26 11:05:38 +02:00
|
|
|
registerPlugin,
|
2016-09-30 18:25:04 +02:00
|
|
|
notification: {
|
|
|
|
success: (message) => {
|
|
|
|
displayNotification(message, 'success');
|
|
|
|
},
|
|
|
|
warning: (message) => {
|
|
|
|
displayNotification(message, 'warning');
|
|
|
|
},
|
|
|
|
error: (message) => {
|
|
|
|
displayNotification(message, 'error');
|
|
|
|
},
|
|
|
|
info: (message) => {
|
|
|
|
displayNotification(message, 'info');
|
|
|
|
},
|
2016-10-05 11:32:31 +02:00
|
|
|
},
|
2017-03-18 17:34:00 +01:00
|
|
|
refresh: (pluginId) => ({
|
2016-10-13 19:31:29 +02:00
|
|
|
translationMessages: (translationMessagesUpdated) => {
|
2017-09-14 11:10:05 +02:00
|
|
|
render(merge({}, translationMessages, translationMessagesUpdated));
|
2016-10-13 19:31:29 +02:00
|
|
|
},
|
2017-08-14 17:09:34 +02:00
|
|
|
leftMenuSections: (leftMenuSectionsUpdated) => {
|
|
|
|
store.dispatch(updatePlugin(pluginId, 'leftMenuSections', leftMenuSectionsUpdated));
|
2017-03-18 17:34:00 +01:00
|
|
|
},
|
2016-10-13 19:31:29 +02:00
|
|
|
}),
|
2017-08-22 10:51:53 +02:00
|
|
|
router: history,
|
2017-11-09 12:10:08 +01:00
|
|
|
languages,
|
2017-11-11 22:14:05 +01:00
|
|
|
});
|
2016-09-30 18:25:04 +02:00
|
|
|
|
|
|
|
const dispatch = store.dispatch;
|
|
|
|
export {
|
|
|
|
dispatch,
|
2016-10-05 11:32:31 +02:00
|
|
|
};
|