diff --git a/app-src/index.html b/app-src/index.html index 1874137f6c..3758ef47df 100644 --- a/app-src/index.html +++ b/app-src/index.html @@ -131,6 +131,7 @@ TODO configure more restrictive Content-Security-Policy + diff --git a/app-src/scripts/main/global-services/extension-interface-s.js b/app-src/scripts/main/global-services/extension-interface-s.js new file mode 100644 index 0000000000..6ec167d31a --- /dev/null +++ b/app-src/scripts/main/global-services/extension-interface-s.js @@ -0,0 +1,51 @@ +/** + * @ngdoc service + * @name superProductivity.ExtensionInterface + * @description + * # ExtensionInterface + * Service in the superProductivity. + */ + +(() => { + 'use strict'; + + const interfaceEl = window; + + class ExtensionInterface { + /* @ngInject */ + constructor() { + this.isInterfaceReady = false; + + interfaceEl.addEventListener('SP_EXTENSION_READY', () => { + this.isInterfaceReady = true; + }); + } + + addEventListener(ev, cb) { + interfaceEl.addEventListener(ev, (ev) => { + cb(ev, ev.detail); + }) + } + + dispatchEvent(evName, data) { + const ev = new CustomEvent(evName, { + detail: data, + }); + + if (this.isInterfaceReady) { + interfaceEl.dispatchEvent(ev); + } else { + setTimeout(() => { + interfaceEl.dispatchEvent(ev); + }, 2000); + } + } + } + + angular + .module('superProductivity') + .service('ExtensionInterface', ExtensionInterface); + + // hacky fix for ff + ExtensionInterface.$$ngIsClass = true; +})(); diff --git a/app-src/scripts/main/global-services/extension-interface-s.spec.js b/app-src/scripts/main/global-services/extension-interface-s.spec.js new file mode 100644 index 0000000000..f8a78904b4 --- /dev/null +++ b/app-src/scripts/main/global-services/extension-interface-s.spec.js @@ -0,0 +1,17 @@ +'use strict'; + +describe('Service: ExtensionInterface', () => { + // load the service's module + beforeEach(module('superProductivity')); + + // instantiate service + let ExtensionInterface; + beforeEach(inject((_ExtensionInterface_) => { + ExtensionInterface = _ExtensionInterface_; + })); + + it('should be defined', () => { + expect(true).toBe(true); + }); + +}); \ No newline at end of file diff --git a/app-src/scripts/main/global-services/jira-s.js b/app-src/scripts/main/global-services/jira-s.js index 04cbaec0cf..274ea74250 100644 --- a/app-src/scripts/main/global-services/jira-s.js +++ b/app-src/scripts/main/global-services/jira-s.js @@ -31,7 +31,7 @@ /* @ngInject */ class Jira { - constructor(IS_ELECTRON, IS_EXTENSION, SimpleToast, Uid, $q, $rootScope, Dialogs, Notifier, $injector, $timeout, REQUEST_TIMEOUT, $log, $window) { + constructor(IS_ELECTRON, IS_EXTENSION, SimpleToast, Uid, $q, $rootScope, Dialogs, Notifier, $injector, $timeout, REQUEST_TIMEOUT, $log, $window, ExtensionInterface) { this.requestsLog = {}; this.IS_ELECTRON = IS_ELECTRON; @@ -47,6 +47,7 @@ this.$log = $log; this.SimpleToast = SimpleToast; this.$window = $window; + this.ExtensionInterface = ExtensionInterface; this.isExtensionReady = false; const that = this; @@ -80,12 +81,10 @@ handleResponse(res); }); } else if (IS_EXTENSION) { - window.addEventListener('SP_JIRA_RESPONSE', (ev) => { - handleResponse(ev.detail); - }); - window.addEventListener('SP_EXTENSION_READY', () => { - this.isExtensionReady = true; + this.ExtensionInterface.addEventListener('SP_JIRA_RESPONSE', (ev, data) => { + handleResponse(data); }); + } } @@ -113,17 +112,7 @@ if (this.IS_ELECTRON) { window.ipcRenderer.send(IPC_JIRA_MAKE_REQUEST_EVENT, request); } else if (this.IS_EXTENSION) { - const ev = new CustomEvent('SP_JIRA_REQUEST', { - detail: request, - }); - - if (this.isExtensionReady) { - window.dispatchEvent(ev); - } else { - setTimeout(() => { - window.dispatchEvent(ev); - }, 2000); - } + this.ExtensionInterface.dispatchEvent('SP_JIRA_REQUEST', request); } return defer.promise;