azure-devops-extension-sdk
Version:
Azure DevOps web extension JavaScript library.
2 lines (1 loc) • 11.1 kB
JavaScript
define(["exports"],(function(e){"use strict";var t=function(){return t=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},t.apply(this,arguments)};function n(e,t,n,r){return new(n||(n=Promise))((function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function c(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,c)}a((r=r.apply(e,t||[])).next())}))}function r(e,t){var n,r,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(s=0)),s;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return s.label++,{value:c[1],done:!1};case 5:s.label++,r=c[1],c=[0];continue;case 7:c=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){s=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){s.label=c[1];break}if(6===c[0]&&s.label<o[1]){s.label=o[1],o=c;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(c);break}o[2]&&s.ops.pop(),s.trys.pop();continue}c=t.call(e,s)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}"function"==typeof SuppressedError&&SuppressedError;var o=parseInt("10000000000",36),i=Number.MAX_SAFE_INTEGER||9007199254740991;var s,c=function(){function e(){this.objects={}}return e.prototype.register=function(e,t){this.objects[e]=t},e.prototype.unregister=function(e){delete this.objects[e]},e.prototype.getInstance=function(e,t){var n=this.objects[e];if(n)return"function"==typeof n?n(t):n},e}(),a=1,u=function(){function e(e,t){this.promises={},this.nextMessageId=1,this.nextProxyId=1,this.proxyFunctions={},this.postToWindow=e,this.targetOrigin=t,this.registry=new c,this.channelId=a++,this.targetOrigin||(this.handshakeToken=Math.floor(Math.random()*(i-o)+o).toString(36)+Math.floor(Math.random()*(i-o)+o).toString(36))}return e.prototype.getObjectRegistry=function(){return this.registry},e.prototype.invokeRemoteMethod=function(e,t,o,i,s){return n(this,void 0,void 0,(function(){var n,c,a=this;return r(this,(function(r){return n={id:this.nextMessageId++,methodName:e,instanceId:t,instanceContext:i,params:this._customSerializeObject(o,s),serializationSettings:s},this.targetOrigin||(n.handshakeToken=this.handshakeToken),c=new Promise((function(e,t){a.promises[n.id]={resolve:e,reject:t}})),this._sendRpcMessage(n),[2,c]}))}))},e.prototype.getRemoteObjectProxy=function(e,t){return this.invokeRemoteMethod("",e,void 0,t)},e.prototype.invokeMethod=function(e,t){var n=this;if(t.methodName){var r=e[t.methodName];if("function"==typeof r)try{var o=[];t.params&&(o=this._customDeserializeObject(t.params,{}));var i=r.apply(e,o);i&&i.then&&"function"==typeof i.then?i.then((function(e){n._success(t,e,t.handshakeToken)}),(function(e){n.error(t,e)})):this._success(t,i,t.handshakeToken)}catch(e){this.error(t,e)}else this.error(t,new Error("RPC method not found: "+t.methodName))}else this._success(t,e,t.handshakeToken)},e.prototype.getRegisteredObject=function(e,t){if("__proxyFunctions"===e)return this.proxyFunctions;var n=this.registry.getInstance(e,t);return n||(n=h.getInstance(e,t)),n},e.prototype.onMessage=function(e){var t=this;if(e.instanceId){var n=this.getRegisteredObject(e.instanceId,e.instanceContext);if(!n)return!1;"function"==typeof n.then?n.then((function(n){t.invokeMethod(n,e)}),(function(n){t.error(e,n)})):this.invokeMethod(n,e)}else{var r=this.promises[e.id];if(!r)return!1;e.error?r.reject(this._customDeserializeObject([e.error],{})[0]):r.resolve(this._customDeserializeObject([e.result],{})[0]),delete this.promises[e.id]}return!0},e.prototype.owns=function(e,t,n){if(this.postToWindow===e){if(this.targetOrigin)return!!t&&("null"===t.toLowerCase()||0===this.targetOrigin.toLowerCase().indexOf(t.toLowerCase()));if(n.handshakeToken&&n.handshakeToken===this.handshakeToken)return this.targetOrigin=t,!0}return!1},e.prototype.error=function(e,t){this._sendRpcMessage({id:e.id,error:this._customSerializeObject([t],e.serializationSettings)[0],handshakeToken:e.handshakeToken})},e.prototype._success=function(e,t,n){this._sendRpcMessage({id:e.id,result:this._customSerializeObject([t],e.serializationSettings)[0],handshakeToken:n})},e.prototype._sendRpcMessage=function(e){this.postToWindow.postMessage(JSON.stringify(e),"*")},e.prototype._customSerializeObject=function(e,t,n,r,o){var i=this;if(void 0===r&&(r=1),void 0===o&&(o=1),e&&!(o>100)&&!(e instanceof Node||e instanceof Window||e instanceof Event)){var s,c;(c=n||{newObjects:[],originalObjects:[]}).originalObjects.push(e);var a=function(n,s,a){var u;try{u=n[a]}catch(e){}var f=typeof u;if("undefined"!==f){var h=-1;if("object"===f&&(h=c.originalObjects.indexOf(u)),h>=0){var d=c.newObjects[h];d.__circularReferenceId||(d.__circularReferenceId=r++),s[a]={__circularReference:d.__circularReferenceId}}else"function"===f?(i.nextProxyId++,s[a]={__proxyFunctionId:i._registerProxyFunction(u,e),_channelId:i.channelId}):"object"===f?u&&u instanceof Date?s[a]={__proxyDate:u.getTime()}:s[a]=i._customSerializeObject(u,t,c,r,o+1):"__proxyFunctionId"!==a&&(s[a]=u)}};if(e instanceof Array){s=[],c.newObjects.push(s);for(var u=0,f=e.length;u<f;u++)a(e,s,u)}else{s={},c.newObjects.push(s);var h={};try{h=function(e){for(var t={};e&&e!==Object.prototype;){for(var n=0,r=Object.getOwnPropertyNames(e);n<r.length;n++){var o=r[n];"constructor"!==o&&(t[o]=!0)}e=Object.getPrototypeOf(e)}return t}(e)}catch(e){}for(var d in h)(d&&"_"!==d[0]||t&&t.includeUnderscoreProperties)&&a(e,s,d)}return c.originalObjects.pop(),c.newObjects.pop(),s}},e.prototype._registerProxyFunction=function(e,t){var n=this.nextProxyId++;return this.proxyFunctions["proxy"+n]=function(){return e.apply(t,Array.prototype.slice.call(arguments,0))},n},e.prototype._customDeserializeObject=function(e,t){var n=this,r=this;if(!e)return null;var o=function(e,o){var i=e[o],s=typeof i;"__circularReferenceId"===o&&"number"===s?(t[i]=e,delete e[o]):"object"===s&&i&&(i.__proxyFunctionId?e[o]=function(){return r.invokeRemoteMethod("proxy"+i.__proxyFunctionId,"__proxyFunctions",Array.prototype.slice.call(arguments,0),{},{includeUnderscoreProperties:!0})}:i.__proxyDate?e[o]=new Date(i.__proxyDate):i.__circularReference?e[o]=t[i.__circularReference]:n._customDeserializeObject(i,t))};if(e instanceof Array)for(var i=0,s=e.length;i<s;i++)o(e,i);else if("object"==typeof e)for(var c in e)o(e,c);return e},e}(),f=function(){function e(){var e=this;this._channels=[],this._handleMessageReceived=function(t){var n;if("string"==typeof t.data)try{n=JSON.parse(t.data)}catch(e){}if(n){for(var r=!1,o=void 0,i=0,s=e._channels;i<s.length;i++){var c=s[i];c.owns(t.source,t.origin,n)&&(o=c,r=c.onMessage(n)||r)}o&&!r&&(window.console&&console.error("No handler found on any channel for message: ".concat(JSON.stringify(n))),n.instanceId&&o.error(n,new Error("The registered object ".concat(n.instanceId," could not be found."))))}},window.addEventListener("message",this._handleMessageReceived)}return e.prototype.addChannel=function(e,t){var n=new u(e,t);return this._channels.push(n),n},e.prototype.removeChannel=function(e){this._channels=this._channels.filter((function(t){return t!==e}))},e}(),h=new c,d=new f,p=window;p._AzureDevOpsSDKVersion&&console.error("The AzureDevOps SDK is already loaded. Only one version of this module can be loaded in a given document."),p._AzureDevOpsSDKVersion=4,e.HostType=void 0,(s=e.HostType||(e.HostType={}))[s.Unknown=0]="Unknown",s[s.Deployment=1]="Deployment",s[s.Enterprise=2]="Enterprise",s[s.Organization=4]="Organization";var l,v,g,y,m,b,w,_,k,O,x="DevOps.HostControl",j=d.addChannel(window.parent),C=new Promise((function(e){O=e}));function R(e,t){var n,r=window;"function"==typeof r.CustomEvent?n=new r.CustomEvent(e,t):(t=t||{bubbles:!1,cancelable:!1},(n=document.createEvent("CustomEvent")).initCustomEvent(e,t.bubbles,t.cancelable,t.detail)),window.dispatchEvent(n)}function E(){return n(this,void 0,void 0,(function(){return r(this,(function(e){return[2,C]}))}))}function T(e){return"Attempted to call ".concat(e,"() before init() was complete. Wait for init to complete or place within a ready() callback.")}function M(e){k||((k=document.createElement("style")).type="text/css",document.head.appendChild(k));var t=[];if(e)for(var n in e)t.push("--"+n+": "+e[n]);k.innerText=":root { "+t.join("; ")+" } body { color: var(--text-primary-color) }",R("themeApplied",{detail:e})}j.getObjectRegistry().register("DevOps.SdkClient",{dispatchEvent:R}),e.applyTheme=M,e.getAccessToken=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){return[2,j.invokeRemoteMethod("getAccessToken",x).then((function(e){return e.token}))]}))}))},e.getAppToken=function(){return n(this,void 0,void 0,(function(){return r(this,(function(e){return[2,j.invokeRemoteMethod("getAppToken",x).then((function(e){return e.token}))]}))}))},e.getConfiguration=function(){if(!m)throw new Error(T("getConfiguration"));return m},e.getContributionId=function(){if(!b)throw new Error(T("getContributionId"));return b},e.getExtensionContext=function(){if(!y)throw new Error(T("getExtensionContext"));return y},e.getHost=function(){if(!_)throw new Error(T("getHost"));return _},e.getPageContext=function(){if(!g)throw new Error(T("getPageContext"));return g},e.getService=function(e){return n(this,void 0,void 0,(function(){return r(this,(function(t){return[2,E().then((function(){return j.invokeRemoteMethod("getService","DevOps.ServiceManager",[e])}))]}))}))},e.getTeamContext=function(){if(!l)throw new Error(T("getTeamContext"));return l},e.getUser=function(){if(!w)throw new Error(T("getUser"));return w},e.getWebContext=function(){if(!v)throw new Error(T("getWebContext"));return v},e.init=function(e){return new Promise((function(n){var r=t(t({},e),{sdkVersion:4});j.invokeRemoteMethod("initialHandshake",x,[r]).then((function(e){var t=e.context;g=t.pageContext,v=g?g.webContext:void 0,l=v?v.team:void 0,m=e.initialConfig||{},b=e.contributionId,y=t.extension,w=t.user,_=t.host,e.themeData&&(M(e.themeData),window.addEventListener("themeChanged",(function(e){M(e.detail.data)}))),O(),n()}))}))},e.notifyLoadFailed=function(e){return j.invokeRemoteMethod("notifyLoadFailed",x,[e])},e.notifyLoadSucceeded=function(){return j.invokeRemoteMethod("notifyLoadSucceeded",x)},e.ready=E,e.register=function(e,t){j.getObjectRegistry().register(e,t)},e.resize=function(e,t){var n=document.body;if(n){var r="number"==typeof e?e:n?n.scrollWidth:void 0,o="number"==typeof t?t:n?n.scrollHeight:void 0;j.invokeRemoteMethod("resize",x,[r,o])}},e.sdkVersion=4,e.unregister=function(e){j.getObjectRegistry().unregister(e)}}));