forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (117 loc) · 4.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
// EXPERIMENTAL - SUBJECT TO CHANGE
import {Client4} from 'mattermost-redux/client';
import store from 'stores/redux_store.jsx';
import {ActionTypes} from 'utils/constants.jsx';
import {getSiteURL} from 'utils/url.jsx';
window.plugins = {};
// Common libraries exposed on window for plugins to access
window.react = require('react');
window['react-dom'] = require('react-dom');
window.redux = require('redux');
window['react-redux'] = require('react-redux');
window['react-bootstrap'] = require('react-bootstrap');
export function registerComponents(id, components = {}, postTypes = {}) {
const wrappedComponents = {};
Object.keys(components).forEach((name) => {
wrappedComponents[name] = {component: components[name], id};
});
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENTS,
data: wrappedComponents
});
const wrappedPostTypes = {};
Object.keys(postTypes).forEach((type) => {
wrappedPostTypes[type] = {component: postTypes[type], id};
});
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_POST_TYPES,
data: wrappedPostTypes
});
}
export async function initializePlugins() {
if (store.getState().entities.general.config.PluginsEnabled !== 'true') {
return;
}
const {data, error} = await getPlugins()(store.dispatch);
if (error) {
console.error(error); //eslint-disable-line no-console
return;
}
if (data == null || data.length === 0) {
return;
}
data.forEach((m) => {
loadPlugin(m);
});
}
export function getPlugins() {
return async (dispatch) => {
let plugins;
try {
plugins = await Client4.getWebappPlugins();
} catch (error) {
return {error};
}
dispatch({type: ActionTypes.RECEIVED_WEBAPP_PLUGINS, data: plugins});
return {data: plugins};
};
}
export function loadPlugin(manifest) {
function onLoad() {
// Add the plugin's js to the page
const script = document.createElement('script');
script.id = 'plugin_' + manifest.id;
script.type = 'text/javascript';
script.text = this.responseText;
document.getElementsByTagName('head')[0].appendChild(script);
// Initialize the plugin
console.log('Registering ' + manifest.id + ' plugin...'); //eslint-disable-line no-console
const plugin = window.plugins[manifest.id];
plugin.initialize(registerComponents.bind(null, manifest.id), store);
console.log('...done'); //eslint-disable-line no-console
}
// Fetch the plugin's bundled js
const xhrObj = new XMLHttpRequest();
// Backwards compatibility for old plugins
let bundlePath = manifest.webapp.bundle_path;
if (bundlePath.includes('/static/') && !bundlePath.includes('/static/plugins/')) {
bundlePath = bundlePath.replace('/static/', '/static/plugins/');
}
xhrObj.open('GET', getSiteURL() + bundlePath, true);
xhrObj.addEventListener('load', onLoad);
xhrObj.send('');
}
export function removePlugin(manifest) {
const script = document.getElementById('plugin_' + manifest.id);
script.parentNode.removeChild(script);
console.log('Removed ' + manifest.id + ' plugin'); //eslint-disable-line no-console
}
export async function loadPluginsIfNecessary() {
if (store.getState().entities.general.config.PluginsEnabled !== 'true') {
return;
}
const oldManifests = store.getState().plugins.plugins;
const {error} = await getPlugins()(store.dispatch);
if (error) {
console.error(error); //eslint-disable-line no-console
return;
}
const newManifests = store.getState().plugins.plugins;
// Get new plugins and update existing plugins if version changed
Object.values(newManifests).forEach((newManifest) => {
const oldManifest = oldManifests[newManifest.id];
if (!oldManifest || oldManifest.version !== newManifest.version) {
loadPlugin(newManifest);
}
});
// Remove old plugins
Object.keys(oldManifests).forEach((id) => {
if (!newManifests.hasOwnProperty(id)) {
const oldManifest = oldManifests[id];
store.dispatch({type: ActionTypes.REMOVED_WEBAPP_PLUGIN, data: oldManifest});
removePlugin(oldManifest);
}
});
}