web-apps/apps/common/mobile/lib/controller/Plugins.jsx

281 lines
9.4 KiB
React
Raw Normal View History

2021-04-26 21:02:07 +00:00
import React, { useEffect, useState } from 'react';
2021-04-27 13:39:39 +00:00
import { inject, observer } from 'mobx-react';
2021-04-26 21:02:07 +00:00
import { f7 } from 'framework7-react';
2021-04-27 13:39:39 +00:00
import { Device } from '../../utils/device';
2021-04-26 21:02:07 +00:00
2021-04-27 13:39:39 +00:00
const PluginsController = inject('storeAppOptions')(observer(props => {
const { storeAppOptions } = props;
2021-04-26 21:02:07 +00:00
let configPlugins = {autostart:[]},
2021-04-27 13:39:39 +00:00
serverPlugins = {autostart:[]},
modal,
iframe;
2021-04-26 21:02:07 +00:00
useEffect(() => {
const onDocumentReady = () => {
2021-04-27 13:39:39 +00:00
Common.Notifications.on('engineCreated', api => {
2021-04-26 21:02:07 +00:00
api.asc_registerCallback("asc_onPluginShow", showPluginModal);
api.asc_registerCallback("asc_onPluginClose", pluginClose);
api.asc_registerCallback("asc_onPluginResize", pluginResize);
2021-05-04 19:05:26 +00:00
api.asc_registerCallback('asc_onPluginsInit', onPluginsInit);
2021-04-27 13:39:39 +00:00
if(!storeAppOptions.customization || storeAppOptions.plugins !== false) {
loadPlugins();
}
2021-04-26 21:02:07 +00:00
});
Common.Gateway.on('init', loadConfig);
};
onDocumentReady();
return () => {
const api = Common.EditorApi.get();
2021-09-23 09:19:24 +00:00
if ( api ) {
api.asc_unregisterCallback("asc_onPluginShow", showPluginModal);
api.asc_unregisterCallback("asc_onPluginClose", pluginClose);
api.asc_unregisterCallback("asc_onPluginResize", pluginResize);
api.asc_unregisterCallback('asc_onPluginsInit', onPluginsInit);
}
2021-04-26 21:02:07 +00:00
};
});
2021-04-27 13:39:39 +00:00
const onDlgBtnClick = e => {
const api = Common.EditorApi.get();
let index = $$(e.currentTarget).index();
api.asc_pluginButtonClick(index);
};
2021-04-26 21:02:07 +00:00
const showPluginModal = (plugin, variationIndex, frameId, urlAddition) => {
let isAndroid = Device.android;
let variation = plugin.get_Variations()[variationIndex];
if (variation.get_Visual()) {
let url = variation.get_Url();
url = ((plugin.get_BaseUrl().length == 0) ? url : plugin.get_BaseUrl()) + url;
if (urlAddition)
url += urlAddition;
let isCustomWindow = variation.get_CustomWindow(),
arrBtns = variation.get_Buttons(),
newBtns = [],
2021-04-27 13:39:39 +00:00
size = variation.get_Size();
2021-04-26 21:02:07 +00:00
if (arrBtns.length) {
arrBtns.forEach((b, index) => {
2021-04-27 13:39:39 +00:00
if ((storeAppOptions.isEdit || b.isViewer !== false)) {
2021-04-26 21:02:07 +00:00
newBtns[index] = {
text: b.text,
2021-06-17 18:38:09 +00:00
attributes: {result: index},
close: false
2021-04-26 21:02:07 +00:00
};
}
});
}
2021-04-27 13:39:39 +00:00
f7.popover.close('.document-menu.modal-in', false);
2021-04-26 21:02:07 +00:00
2021-04-27 13:39:39 +00:00
modal = f7.dialog.create({
2021-04-26 21:02:07 +00:00
title: '',
text: '',
2021-04-27 13:39:39 +00:00
content: '<div id="plugin-frame" class="">'+'</div>',
2021-04-26 21:02:07 +00:00
buttons : isCustomWindow ? undefined : newBtns
}).open();
2021-04-27 13:39:39 +00:00
iframe = document.createElement("iframe");
2021-04-26 21:02:07 +00:00
iframe.id = frameId;
iframe.name = 'pluginFrameEditor';
iframe.width = '100%';
iframe.height = '100%';
2021-04-27 13:39:39 +00:00
iframe.align = "top";
iframe.frameBorder = 0;
iframe.scrolling = "no";
2021-04-26 21:02:07 +00:00
iframe.src = url;
2021-04-27 13:39:39 +00:00
$$('#plugin-frame').append(iframe);
modal.$el.find('.dialog-button').on('click', onDlgBtnClick);
2021-04-26 21:02:07 +00:00
2021-04-27 13:39:39 +00:00
modal.$el.css({
2021-04-26 21:02:07 +00:00
margin: '0',
width: '90%',
left: '5%',
2021-04-27 13:39:39 +00:00
height: 'auto'
2021-04-26 21:02:07 +00:00
});
2021-04-27 13:39:39 +00:00
modal.$el.find('.dialog-inner').css({padding: '0'});
2021-04-26 21:02:07 +00:00
2021-04-27 13:39:39 +00:00
if (Device.phone) {
2021-04-26 21:02:07 +00:00
let height = Math.min(size[1], 240);
2021-04-27 13:39:39 +00:00
modal.$el.find('#plugin-frame').css({height: height + 'px'});
2021-04-26 21:02:07 +00:00
} else {
let height = Math.min(size[1], 500);
2021-04-27 13:39:39 +00:00
modal.$el.find('#plugin-frame').css({height: height + 'px'});
2021-04-26 21:02:07 +00:00
}
if (isAndroid) {
$$('.view.collaboration-root-view.navbar-through').removeClass('navbar-through').addClass('navbar-fixed');
$$('.view.collaboration-root-view .navbar').prependTo('.view.collaboration-root-view > .pages > .page');
}
}
};
const pluginClose = plugin => {
2022-01-14 16:54:07 +00:00
if (plugin && modal) {
2021-06-17 18:38:09 +00:00
modal.close();
2021-04-26 21:02:07 +00:00
}
};
const pluginResize = size => {
2021-04-27 13:39:39 +00:00
if (Device.phone) {
2021-04-26 21:02:07 +00:00
let height = Math.min(size[1], 240);
2021-04-27 13:39:39 +00:00
modal.$el.find('#plugin-frame').css({height: height + 'px'});
2021-04-26 21:02:07 +00:00
} else {
let height = Math.min(size[1], 500);
2021-04-27 13:39:39 +00:00
modal.$el.find('#plugin-frame').css({height: height + 'px'});
2021-04-26 21:02:07 +00:00
}
};
const getPlugins = (pluginsData, fetchFunction) => {
if (!pluginsData || pluginsData.length < 1)
return Promise.resolve([]);
fetchFunction = fetchFunction || function (url) {
return fetch(url)
.then(function(response) {
if (response.ok) return response.json();
else return Promise.reject(url);
}).then(function(json) {
json.baseUrl = url.substring(0, url.lastIndexOf("config.json"));
return json;
});
};
let loaded = [];
return pluginsData.map(fetchFunction).reduce(function (previousPromise, currentPromise) {
return previousPromise
.then(function()
{
return currentPromise;
})
.then(function(item)
{
loaded.push(item);
return Promise.resolve(item);
})
.catch(function(item)
{
return Promise.resolve(item);
});
}, Promise.resolve())
.then(function ()
{
return Promise.resolve(loaded);
});
};
const loadConfig = data => {
configPlugins.config = data.config.plugins;
};
2021-05-04 19:05:26 +00:00
const onPluginsInit = pluginsdata => {
!(pluginsdata instanceof Array) && (pluginsdata = pluginsdata["pluginsData"]);
2021-07-12 15:41:43 +00:00
parsePlugins(pluginsdata)
};
const parsePlugins = pluginsdata => {
let isEdit = storeAppOptions.isEdit;
if ( pluginsdata instanceof Array ) {
let lang = storeAppOptions.lang.split(/[\-_]/)[0];
pluginsdata.forEach((item) => {
item.variations.forEach( (itemVar) => {
let description = itemVar.description;
if (typeof itemVar.descriptionLocale == 'object')
description = itemVar.descriptionLocale[lang] || itemVar.descriptionLocale['en'] || description || '';
if(itemVar.buttons !== undefined) {
itemVar.buttons.forEach( (button) => {
if (typeof button.textLocale == 'object')
button.text = button.textLocale[lang] || button.textLocale['en'] || button.text || '';
button.visible = (isEdit || button.isViewer !== false);
})
}
})
});
}
registerPlugins(pluginsdata);
2021-05-04 19:05:26 +00:00
};
2021-04-26 21:02:07 +00:00
const registerPlugins = plugins => {
let arr = [];
plugins.forEach(item => {
let plugin = new Asc.CPlugin();
2021-08-30 20:44:46 +00:00
plugin.deserialize(item);
2021-04-26 21:02:07 +00:00
arr.push(plugin);
});
2021-04-27 13:39:39 +00:00
const api = Common.EditorApi.get();
2021-04-26 21:02:07 +00:00
api.asc_pluginsRegister('', arr);
};
const mergePlugins = () => {
if (serverPlugins.plugins !== undefined && configPlugins.plugins !== undefined) {
let arr = [],
plugins = configPlugins;
if (plugins.plugins && plugins.plugins.length > 0) {
arr = plugins.plugins;
}
plugins = serverPlugins;
if (plugins.plugins && plugins.plugins.length > 0) {
arr = arr.concat(plugins.plugins);
}
2021-07-12 15:41:43 +00:00
parsePlugins(arr);
2021-04-26 21:02:07 +00:00
}
};
const loadPlugins = () => {
if (configPlugins.config) {
getPlugins(configPlugins.config.pluginsData)
.then(function(loaded)
{
configPlugins.plugins = loaded;
mergePlugins();
});
} else {
configPlugins.plugins = false;
}
let server_plugins_url = '../../../../plugins.json';
Common.Utils.loadConfig(server_plugins_url, function (obj) {
if (obj != 'error') {
serverPlugins.config = obj;
getPlugins(serverPlugins.config.pluginsData)
.then(function(loaded)
{
serverPlugins.plugins = loaded;
mergePlugins();
});
} else
serverPlugins.plugins = false;
});
};
return <></>
2021-04-27 13:39:39 +00:00
}));
2021-04-26 21:02:07 +00:00
export default PluginsController;