UNPKG

11.7 kBJavaScriptView Raw
1/***************************************************************************************
2 * (c) 2017 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 ****************************************************************************************/
12
13!function(){"use strict";var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function n(e,n){return e(n={exports:{}},n.exports),n.exports}var t,o=n(function(e,n){Object.defineProperty(n,"__esModule",{value:!0});var t,o=n.ERR_CONNECTION_DESTROYED="ConnectionDestroyed",r=n.ERR_CONNECTION_TIMEOUT="ConnectionTimeout",i=n.ERR_NOT_IN_IFRAME="NotInIframe",a={"http:":"80","https:":"443"},c=/^(https?:)?\/\/([^\/:]+)(:(\d+))?/,u={ERR_CONNECTION_DESTROYED:o,ERR_CONNECTION_TIMEOUT:r,ERR_NOT_IN_IFRAME:i,Promise:function(){try{return window?window.Promise:null}catch(e){return null}}(),debug:!1},d=(t=0,function(){return++t}),s=function(){for(var e=arguments.length,n=Array(e),t=0;t<e;t++)n[t]=arguments[t];var o;u.debug&&(o=console).log.apply(o,["[Penpal]"].concat(n))},l=function(e){var n=[];return e(function(){n.forEach(function(e){e()})}),{then:function(e){n.push(e)}}},f=function(e){return{name:e.name,message:e.message,stack:e.stack}},p=function(e,n,t,r){var i=n.localName,a=n.local,c=n.remote,l=n.remoteOrigin,f=!1;s(i+": Connecting call sender");var p=function(e){return function(){for(var n=arguments.length,t=Array(n),r=0;r<n;r++)t[r]=arguments[r];if(s(i+": Sending "+e+"() call"),f){var p=new Error("Unable to send "+e+"() call due to destroyed connection");throw p.code=o,p}return new u.Promise(function(n,o){var r=d();a.addEventListener("message",function t(u){if(u.source===c&&u.origin===l&&"reply"===u.data.penpal&&u.data.id===r){s(i+": Received "+e+"() reply"),a.removeEventListener("message",t);var d=u.data.returnValue;u.data.returnValueIsError&&(f=d,p=new Error,Object.keys(f).forEach(function(e){return p[e]=f[e]}),d=p),("fulfilled"===u.data.resolution?n:o)(d)}var f,p}),c.postMessage({penpal:"call",id:r,methodName:e,args:t},l)})}};r.then(function(){f=!0}),t.reduce(function(e,n){return e[n]=p(n),e},e)},h=function(e,n,t){var o=e.localName,r=e.local,i=e.remote,a=e.remoteOrigin,c=!1;s(o+": Connecting call receiver");var d=function(e){if(e.source===i&&e.origin===a&&"call"===e.data.penpal){var t=e.data,r=t.methodName,d=t.args,l=t.id;if(s(o+": Received "+r+"() call"),r in n){var p=function(e){return function(n){if(s(o+": Sending "+r+"() reply"),c)s(o+": Unable to send "+r+"() reply due to destroyed connection");else{var t={penpal:"reply",id:l,resolution:e,returnValue:n};"rejected"===e&&n instanceof Error&&(t.returnValue=f(n),t.returnValueIsError=!0);try{i.postMessage(t,a)}catch(e){throw"DataCloneError"===e.name&&i.postMessage({penpal:"reply",id:l,resolution:"rejected",returnValue:f(e),returnValueIsError:!0},a),e}}}};new u.Promise(function(e){return e(n[r].apply(n,d))}).then(p("fulfilled"),p("rejected"))}}};r.addEventListener("message",d),t.then(function(){c=!0,r.removeEventListener("message",d)})};u.connectToChild=function(e){var n=e.url,t=e.appendTo,i=e.methods,d=void 0===i?{}:i,f=e.timeout,m=void 0,v=new l(function(e){m=e}),y=window,g=document.createElement("iframe");(t||document.body).appendChild(g),v.then(function(){g.parentNode&&g.parentNode.removeChild(g)});var w=g.contentWindow||g.contentDocument.parentWindow,E=function(e){var n=document.location,t=c.exec(e),o=void 0,r=void 0,i=void 0;return t?(o=t[1]?t[1]:n.protocol,r=t[2],i=t[4]):(o=n.protocol,r=n.hostname,i=n.port),o+"//"+r+(i&&i!==a[o]?":"+i:"")}(n);return{promise:new u.Promise(function(e,t){var i=void 0;void 0!==f&&(i=setTimeout(function(){var e=new Error("Connection to child timed out after "+f+"ms");e.code=r,t(e),m()},f));var a={},c=void 0,u=void 0,_=function(n){if(n.source===w&&n.origin===E&&"handshake"===n.data.penpal){s("Parent: Received handshake, sending reply"),n.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(d)},n.origin);var t={localName:"Parent",local:y,remote:w,remoteOrigin:n.origin};u&&u();var o=new l(function(e){v.then(e),u=e});h(t,d,o),c&&c.forEach(function(e){delete a[e]}),c=n.data.methodNames,p(a,t,c,v),clearTimeout(i),e(a)}};y.addEventListener("message",_),v.then(function(){y.removeEventListener("message",_);var e=new Error("Connection destroyed");e.code=o,t(e)}),s("Parent: Loading iframe"),g.src=n}),iframe:g,destroy:m}},u.connectToParent=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=e.parentOrigin,t=void 0===n?"*":n,a=e.methods,c=void 0===a?{}:a,d=e.timeout;if(window===window.top){var f=new Error("connectToParent() must be called within an iframe");throw f.code=i,f}var m=void 0,v=new l(function(e){m=e}),y=window,g=y.parent;return{promise:new u.Promise(function(e,n){var i=void 0;void 0!==d&&(i=setTimeout(function(){var e=new Error("Connection to parent timed out after "+d+"ms");e.code=r,n(e),m()},d));var a=function n(o){if(("*"===t||t===o.origin)&&o.source===g&&"handshake-reply"===o.data.penpal){s("Child: Received handshake reply"),y.removeEventListener("message",n);var r={localName:"Child",local:y,remote:g,remoteOrigin:o.origin},a={};h(r,c,v),p(a,r,o.data.methodNames,v),clearTimeout(i),e(a)}};y.addEventListener("message",a),v.then(function(){y.removeEventListener("message",a);var e=new Error("Connection destroyed");e.code=o,n(e)}),s("Child: Sending handshake"),g.postMessage({penpal:"handshake",methodNames:Object.keys(c)},t)}),destroy:m}},n.default=u}),r=(t=o)&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t,i=(o.ERR_CONNECTION_DESTROYED,o.ERR_CONNECTION_TIMEOUT,o.ERR_NOT_IN_IFRAME,"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e}),a=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},c=n(function(n){!function(e){var t=setTimeout;function o(){}function r(e){if(!(this instanceof r))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],s(e,this)}function a(e,n){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,r._immediateFn(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null!==t){var o;try{o=t(e._value)}catch(e){return void u(n.promise,e)}c(n.promise,o)}else(1===e._state?c:u)(n.promise,e._value)})):e._deferreds.push(n)}function c(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"===(void 0===n?"undefined":i(n))||"function"==typeof n)){var t=n.then;if(n instanceof r)return e._state=3,e._value=n,void d(e);if("function"==typeof t)return void s((o=t,a=n,function(){o.apply(a,arguments)}),e)}e._state=1,e._value=n,d(e)}catch(n){u(e,n)}var o,a}function u(e,n){e._state=2,e._value=n,d(e)}function d(e){2===e._state&&0===e._deferreds.length&&r._immediateFn(function(){e._handled||r._unhandledRejectionFn(e._value)});for(var n=0,t=e._deferreds.length;n<t;n++)a(e,e._deferreds[n]);e._deferreds=null}function s(e,n){var t=!1;try{e(function(e){t||(t=!0,c(n,e))},function(e){t||(t=!0,u(n,e))})}catch(e){if(t)return;t=!0,u(n,e)}}r.prototype.catch=function(e){return this.then(null,e)},r.prototype.then=function(e,n){var t=new this.constructor(o);return a(this,new function(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}(e,n,t)),t},r.all=function(e){return new r(function(n,t){if(!e||void 0===e.length)throw new TypeError("Promise.all accepts an array");var o=Array.prototype.slice.call(e);if(0===o.length)return n([]);var r=o.length;function a(e,c){try{if(c&&("object"===(void 0===c?"undefined":i(c))||"function"==typeof c)){var u=c.then;if("function"==typeof u)return void u.call(c,function(n){a(e,n)},t)}o[e]=c,0==--r&&n(o)}catch(e){t(e)}}for(var c=0;c<o.length;c++)a(c,o[c])})},r.resolve=function(e){return e&&"object"===(void 0===e?"undefined":i(e))&&e.constructor===r?e:new r(function(n){n(e)})},r.reject=function(e){return new r(function(n,t){t(e)})},r.race=function(e){return new r(function(n,t){for(var o=0,r=e.length;o<r;o++)e[o].then(n,t)})},r._immediateFn="function"==typeof setImmediate&&function(e){setImmediate(e)}||function(e){t(e,0)},r._unhandledRejectionFn=function(e){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)},r._setImmediateFn=function(e){r._immediateFn=e},r._setUnhandledRejectionFn=function(e){r._unhandledRejectionFn=e},n.exports?n.exports=r:e.Promise||(e.Promise=r)}(e)}),u=function(e){this.moduleName=e};["log","info","warn","error"].forEach(function(e){u.prototype[e]=function(){if(u.enabled){for(var n,t=arguments.length,o=Array(t),r=0;r<t;r++)o[r]=arguments[r];(n=console)[e].apply(n,["["+this.moduleName+"]"].concat(o))}}}),u.enabled=!1;var d,s;d="\n html, body {\n background-color: transparent !important;\n }\n",(s=document.createElement("style")).appendChild(document.createTextNode(d)),document.head.appendChild(s),r.Promise=c;var l=new u("ExtensionBridge:Child"),f={},p=void 0,h=function(e){var n=f[e];if(n)return n.bind(f);throw new Error("Unable to call "+e+" on the extension. The extension must register a "+e+" function using extensionBridge.register().")},m=function(e,n){return function(){for(var t=arguments.length,o=Array(t),r=0;r<t;r++)o[r]=arguments[r];var i=void 0;return"function"==typeof o[0]&&(i=o.shift(),console.warn("Passing a callback to extensionBridge."+e+"() has been deprecated. The method now returns a promise that should be used instead.")),p.then(function(t){if(t[e])return t[e].apply(t,o);throw new Error("An error occurred while opening "+n+". The shared view is unavailable.")}).then(function(e){return i&&i(e),e})}};p=r.connectToParent({methods:{init:function(){h("init").apply(void 0,arguments)},validate:function(){var e=h("validate").apply(void 0,arguments);if("boolean"!=typeof e)throw new Error("The extension attempted to return a non-boolean value from validate: "+e);return e},getSettings:function(){var e=h("getSettings").apply(void 0,arguments);if("object"!==(void 0===e?"undefined":i(e)))throw new Error("The extension attempted to return a non-object value from getSettings: "+e);return e}}}).promise;var v={openCodeEditor:m("openCodeEditor","code editor"),openDataElementSelector:m("openDataElementSelector","data element selector"),openRegexTester:m("openRegexTester","regex tester"),register:function(e){f=a({},e),p.then(function(e){return e.extensionRegistered()}),l.log("Methods registered by extension.")},setDebug:function(e){r.debug=e,u.enabled=e}};window.addEventListener("focus",function(){p.then(function(e){return e.markAsDirty()})});for(var y=function(e){c.resolve(v[e.methodName].apply(v,function(e){if(Array.isArray(e)){for(var n=0,t=Array(e.length);n<e.length;n++)t[n]=e[n];return t}return Array.from(e)}(e.args))).then(e.resolve,e.reject)},g=window.extensionBridge._callQueue;g.length;)y(g.shift());g.push=y}();