UNPKG

@googleworkspace/drive-picker-element

Version:
2 lines 6.28 kB
"use strict";(()=>{var f="https://apis.google.com/js/api.js",w="https://accounts.google.com/gsi/client";async function E(r="client:picker"){return window.gapi||await b(f),await new Promise(e=>{window.gapi.load(r,e)}),window.google}async function m(r){return window.google?.accounts?.oauth2||await b(w),new Promise((e,i)=>{window.google.accounts.oauth2.initTokenClient({...r,callback:e,error_callback:i}).requestAccessToken()})}async function b(r){return new Promise((e,i)=>{let t=document.createElement("script");t.src=r,t.onload=()=>e(),t.onerror=n=>i(n);let o=document.querySelector(`script[src="${r}"]`);o&&(console.log(`drive-picker: appending a copy of an existing script ${r}`),Array.from(o.attributes).forEach(n=>{["id","src","onload","onerror"].includes(n.name)||t.setAttribute(n.name,n.value)}),o.nonce&&(t.nonce=o.nonce)),document.head.appendChild(t)})}function g(r,e){let i=r.getAttribute(e);return i?Number(i):null}var d=(t=>(t[t.FALSE=0]="FALSE",t[t.TRUE=1]="TRUE",t[t.DEFAULT=2]="DEFAULT",t))(d||{});function u(r,e){return r.hasAttribute(e)}function y(r,e){let i=r.getAttribute(e)?.toUpperCase();if(!i)return 2;let t=d[i];if(t!==void 0)return t;throw new Error(`Invalid value, "${i}", for attribute ${e}. Must be one of ${Object.keys(d).filter(Number.isNaN).join(", ")}`)}function s(r,e,i,t){let o=y(e,r);return o===2?t:i.call(t,o===1)}var l=class extends HTMLElement{static get observedAttributes(){return["enable-drives","file-ids","include-folders","mime-types","mode","owned-by-me","parent","query","select-folder-enabled","starred","view-id"]}get view(){let e=new window.google.picker.DocsView(this.viewId);s("enable-drives",this,e.setEnableDrives,e),s("include-folders",this,e.setIncludeFolders,e),s("owned-by-me",this,e.setOwnedByMe,e),s("select-folder-enabled",this,e.setSelectFolderEnabled,e),s("starred",this,e.setStarred,e);let i=this.getAttribute("mime-types");i!==null&&e.setMimeTypes(i);let t=this.getAttribute("file-ids");t!==null&&e.setFileIds(t);let o=this.getAttribute("mode");o&&e.setMode(google.picker.DocsViewMode[o]);let n=this.getAttribute("parent");n!==null&&e.setParent(n);let c=this.getAttribute("query");return c!==null&&e.setQuery(c),e}get viewId(){let e=this.getAttribute("view-id");return e?window.google.picker.ViewId[e]:void 0}};var a=class extends HTMLElement{static get observedAttributes(){return["app-id","client-id","debounce-delay","developer-key","hide-title-bar","locale","max-items","mine-only","multiselect","nav-hidden","oauth-token","origin","relay-url","scope","title"]}picker;observer;google;loading;debounceTimer;get visible(){return!!this.picker?.isVisible()}set visible(e){this.picker?.setVisible(e)}get tokenClientConfig(){let e=this.getAttribute("client-id"),i=this.getAttribute("scope")??"https://www.googleapis.com/auth/drive.file";if(!e||!i)throw new Error("client-id and scope are required attributes");return{client_id:e,hd:this.getAttribute("hd")??void 0,include_granted_scopes:!!this.getAttribute("include-granted-scope"),login_hint:this.getAttribute("login-hint")??void 0,prompt:this.getAttribute("prompt")??"",scope:i}}attributeChangedCallback(){this.isConnected&&this.scheduleBuild()}scheduleBuild(){clearTimeout(this.debounceTimer),this.debounceTimer=window.setTimeout(()=>{this.build()},g(this,"debounce-delay")??0)}async build(){if(this.picker?.dispose(),await this.loading,!this.google)return;let e=new this.google.picker.PickerBuilder().setCallback(p=>{this.callbackToDispatchEvent(p)}),i=this.getAttribute("app-id");i?e=e.setAppId(i):console.warn("drive-picker: app-id attribute is missing. This may cause issues with backend API integration.");let t=this.getAttribute("developer-key");t!==null&&(e=e.setDeveloperKey(t));let o=this.getAttribute("locale");o!==null&&(e=e.setLocale(o));let n=g(this,"max-items");n!==null&&(e=e.setMaxItems(n));let c=this.getAttribute("origin");c!==null&&(e=e.setOrigin(c));let h=this.getAttribute("relay-url");h!==null&&(e=e.setRelayUrl(h));let k=this.getAttribute("title");k!==null&&(e=e.setTitle(k)),s("hide-title-bar",this,e.hideTitleBar,e);let v=this.getAttribute("oauth-token")??await this.requestAccessToken();if(v){e=e.setOAuthToken(v),u(this,"multiselect")&&(e=e.enableFeature(this.google.picker.Feature.MULTISELECT_ENABLED)),u(this,"mine-only")&&(e=e.enableFeature(this.google.picker.Feature.MINE_ONLY)),u(this,"nav-hidden")&&(e=e.enableFeature(this.google.picker.Feature.NAV_HIDDEN));for(let p of this.views)e=e.addView(p);this.picker=e.build(),this.picker.setVisible(!0)}}get views(){let e=T(this);return e.length?e:["all"]}async connectedCallback(){this.loading=E().then(e=>{this.google=e}),this.observer=new MutationObserver(e=>{e.filter(t=>t.type==="childList"||t.type==="attributes"&&t.target!==this).length&&this.scheduleBuild()}),this.observer?.observe(this,{childList:!0,subtree:!0,attributes:!0}),this.scheduleBuild()}callbackToDispatchEvent(e){let i;switch(e.action){case google.picker.Action.CANCEL:i=["picker:canceled","picker-canceled"];break;case google.picker.Action.PICKED:i=["picker:picked","picker-picked"];break;case google.picker.Action.ERROR:i=["picker:error","picker-error"];break;default:return}for(let t of i)this.dispatchEvent(new CustomEvent(t,{detail:e}))}async requestAccessToken(){return m(this.tokenClientConfig).then(e=>{let{access_token:i}=e;if(!i){this.dispatchEvent(new CustomEvent("picker:oauth:error",{detail:e})),this.dispatchEvent(new CustomEvent("picker-oauth-error",{detail:e}));return}return this.dispatchEvent(new CustomEvent("picker:authenticated",{detail:{token:i}})),this.dispatchEvent(new CustomEvent("picker-oauth-response",{detail:e})),this.dispatchEvent(new CustomEvent("picker:oauth:response",{detail:e})),this.dispatchEvent(new CustomEvent("picker-oauth-response",{detail:e})),i}).catch(e=>{this.dispatchEvent(new CustomEvent("picker:oauth:error",{detail:e})),this.dispatchEvent(new CustomEvent("picker-oauth-error",{detail:e}))})}disconnectedCallback(){this.picker?.dispose()}};function A(r){return"view"in r&&r.view instanceof window.google.picker.View}function P(r){return r.filter(e=>A(e)).map(e=>e.view)}function T(r,e="*"){return P(Array.from(r.querySelectorAll(e)))}customElements.define("drive-picker",a);customElements.define("drive-picker-docs-view",l);})(); //# sourceMappingURL=index.iife.min.js.map