1 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
2 |
|
3 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import Penpal from 'penpal';
|
17 | import Logger from './utils/logger';
|
18 | import addStylesToPage from './utils/addStylesToPage';
|
19 | const STYLES = "\n html, body {\n background-color: transparent !important;\n }\n";
|
20 | addStylesToPage(STYLES);
|
21 | const logger = new Logger('ExtensionBridge:Child');
|
22 | let extensionViewMethods = {};
|
23 | let connectionPromise;
|
24 |
|
25 | const getExtensionViewMethod = methodName => {
|
26 | const method = extensionViewMethods[methodName];
|
27 |
|
28 | if (method) {
|
29 | return method.bind(extensionViewMethods);
|
30 | } else {
|
31 | throw new Error("Unable to call ".concat(methodName, " on the extension. The extension must register a ").concat(methodName, " function using extensionBridge.register()."));
|
32 | }
|
33 | };
|
34 |
|
35 | const init = function init() {
|
36 | getExtensionViewMethod('init')(...arguments);
|
37 | };
|
38 |
|
39 | const validate = function validate() {
|
40 | return Promise.resolve(getExtensionViewMethod('validate')(...arguments)).then(result => {
|
41 | if (typeof result !== 'boolean') {
|
42 | throw new Error("The extension attempted to return a non-boolean value from validate: ".concat(result));
|
43 | }
|
44 |
|
45 | return result;
|
46 | });
|
47 | };
|
48 |
|
49 | const getSettings = function getSettings() {
|
50 | return Promise.resolve(getExtensionViewMethod('getSettings')(...arguments)).then(result => {
|
51 | if (typeof result !== 'object') {
|
52 | throw new Error('The extension attempted to return a non-object value from getSettings: ' + result);
|
53 | }
|
54 |
|
55 | return result;
|
56 | });
|
57 | };
|
58 |
|
59 | const wrapOpenSharedViewMethod = (methodName, sharedViewName) => function () {
|
60 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
61 | args[_key] = arguments[_key];
|
62 | }
|
63 |
|
64 | let callback;
|
65 |
|
66 | if (typeof args[0] === 'function') {
|
67 | callback = args.shift();
|
68 |
|
69 | console.warn('Passing a callback to extensionBridge.' + methodName + '() has been deprecated. ' + 'The method now returns a promise that should be used instead.');
|
70 | }
|
71 |
|
72 | return connectionPromise.then(parent => {
|
73 | if (parent[methodName]) {
|
74 | return parent[methodName](...args);
|
75 | } else {
|
76 | throw new Error("An error occurred while opening ".concat(sharedViewName, ". The shared view is unavailable."));
|
77 | }
|
78 | }).then(result => {
|
79 | if (callback) {
|
80 | callback(result);
|
81 | }
|
82 |
|
83 | return result;
|
84 | });
|
85 | };
|
86 |
|
87 | connectionPromise = Penpal.connectToParent({
|
88 | methods: {
|
89 | init,
|
90 | validate,
|
91 | getSettings
|
92 | }
|
93 | }).promise;
|
94 | const extensionBridge = {
|
95 | openCodeEditor: wrapOpenSharedViewMethod('openCodeEditor', 'code editor'),
|
96 | openDataElementSelector: wrapOpenSharedViewMethod('openDataElementSelector', 'data element selector'),
|
97 | openRegexTester: wrapOpenSharedViewMethod('openRegexTester', 'regex tester'),
|
98 |
|
99 | register(methods) {
|
100 | extensionViewMethods = _objectSpread({}, methods);
|
101 | connectionPromise.then(parent => parent.extensionRegistered());
|
102 | logger.log('Methods registered by extension.');
|
103 | },
|
104 |
|
105 | setDebug(value) {
|
106 | Penpal.debug = value;
|
107 | Logger.enabled = value;
|
108 | }
|
109 |
|
110 | };
|
111 | window.addEventListener('focus', () => {
|
112 | connectionPromise.then(parent => parent.markAsDirty());
|
113 | });
|
114 |
|
115 | const executeQueuedCall = call => {
|
116 |
|
117 |
|
118 |
|
119 | Promise.resolve(extensionBridge[call.methodName](...call.args)).then(call.resolve, call.reject);
|
120 | };
|
121 |
|
122 | const callQueue = window.extensionBridge._callQueue;
|
123 |
|
124 | while (callQueue.length) {
|
125 | executeQueuedCall(callQueue.shift());
|
126 | }
|
127 |
|
128 | callQueue.push = executeQueuedCall; |
\ | No newline at end of file |