diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-08-29 09:54:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-29 09:54:02 -0400 |
commit | 257edc9ea3b25328aa44098e963815c3c3d25312 (patch) | |
tree | ed72b2f646ea9287fdccb5076b99b01bc8585a1d /webapp/plugins/index.js | |
parent | 82a8bd99cc5fe59fe4577c9b0d2c06a82c89e628 (diff) | |
download | chat-257edc9ea3b25328aa44098e963815c3c3d25312.tar.gz chat-257edc9ea3b25328aa44098e963815c3c3d25312.tar.bz2 chat-257edc9ea3b25328aa44098e963815c3c3d25312.zip |
Experimental implementation for webapp plugins (#7185)
* Start of experimental implementation for webapp plugins
* Updates to webapp plugin architecture
* Update pluggable test
* Remove debug code
Diffstat (limited to 'webapp/plugins/index.js')
-rw-r--r-- | webapp/plugins/index.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/webapp/plugins/index.js b/webapp/plugins/index.js new file mode 100644 index 000000000..2e8240cec --- /dev/null +++ b/webapp/plugins/index.js @@ -0,0 +1,51 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +// EXPERIMENTAL - SUBJECT TO CHANGE + +import store from 'stores/redux_store.jsx'; +import {ActionTypes} from 'utils/constants.jsx'; +import {getSiteURL} from 'utils/url.jsx'; + +window.plugins = {}; + +export function registerComponents(components) { + store.dispatch({ + type: ActionTypes.RECEIVED_PLUGIN_COMPONENTS, + data: components || {} + }); +} + +export function initializePlugins() { + const pluginJson = window.mm_config.Plugins || '[]'; + + let pluginManifests; + try { + pluginManifests = JSON.parse(pluginJson); + } catch (error) { + console.error('Invalid plugins JSON: ' + error); //eslint-disable-line no-console + return; + } + + pluginManifests.forEach((m) => { + function onLoad() { + // Add the plugin's js to the page + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.text = this.responseText; + document.getElementsByTagName('head')[0].appendChild(script); + + // Initialize the plugin + console.log('Registering ' + m.id + ' plugin...'); //eslint-disable-line no-console + const plugin = window.plugins[m.id]; + plugin.initialize(registerComponents, store); + console.log('...done'); //eslint-disable-line no-console + } + + // Fetch the plugin's bundled js + const xhrObj = new XMLHttpRequest(); + xhrObj.open('GET', getSiteURL() + m.bundle_path, true); + xhrObj.addEventListener('load', onLoad); + xhrObj.send(''); + }); +} |