(function(e){typeof define==`function`&&define.amd?define([],e):e()})(function(){var e=((e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports))(((e,t)=>{t.exports={}}));function t(n,r,i,a){if(!(n instanceof Error))throw n;if(!(typeof window>`u`&&r||a))throw n.message+=` on line `+i,n;var o,s,c,l;try{a||=e().readFileSync(r,{encoding:`utf8`}),o=3,s=a.split(`
`),c=Math.max(i-o,0),l=Math.min(s.length,i+o)}catch(e){n.message+=` - could not read from `+r+` (`+e.message+`)`,t(n,null,i);return}o=s.slice(c,l).map(function(e,t){var n=t+c+1;return(n==i?`  > `:`    `)+n+`| `+e}).join(`
`),n.path=r;try{n.message=(r||`Pug`)+`:`+i+`
`+o+`

`+n.message}catch{}throw n}function n(e){var n=``,r,i;try{i=1,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`<div class="g-terms-editor-group form-group">`,i=2,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`<label class="control-label">`,i=2,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`Terms of Use (optional)</label>`,i=3,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`<div class="g-terms-editor-container"></div>`,i=4,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`<div class="alert alert-info">`,i=5,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`<i class="icon-info-circled"></i>`,i=6,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`<span>`,i=7,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`If the Terms of Use is non-empty, users will be required to agree to it before accessing`,i=8,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`
`,i=8,r=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/editCollectionTermsWidget.pug`,n+=`anything in this collection.</span></div></div>`}catch(e){t(e,r,i)}return n}var r=girder.$,{AccessType:i}=girder.constants,a=girder.views.widgets.EditCollectionWidget,o=girder.views.widgets.MarkdownWidget,{wrap:s}=girder.utilities.PluginUtils;s(a,`initialize`,function(e,...t){e.apply(this,t),(this.create||this.model.getAccessLevel()>=i.ADMIN)&&(this.termsEditor=new o({text:this.model?this.model.get(`terms`):``,prefix:`collection-terms`,placeholder:`Enter collection Terms of Use`,enableUploads:!1,parentView:this}))}),s(a,`render`,function(e){if(e.call(this),this.termsEditor){let e=r(n({enabled:!0}));this.termsEditor.setElement(e.find(`.g-terms-editor-container`)).render(),this.$(`.modal-body>.g-validation-failed-message`).before(e)}return this}),s(a,`_saveCollection`,function(e,t){return this.termsEditor?(t.terms=this.termsEditor.val(),this.model.set(t),this.model.save().then(()=>{if(this.model.hasTerms())return this.model.currentUserSetAcceptTerms()}).done(()=>{this.trigger(`g:saved`,this.model)})):e.call(this,t)});function c(t,n,r,i){if(!(t instanceof Error))throw t;if(!(typeof window>`u`&&n||i))throw t.message+=` on line `+r,t;var a,o,s,l;try{i||=e().readFileSync(n,{encoding:`utf8`}),a=3,o=i.split(`
`),s=Math.max(r-a,0),l=Math.min(o.length,r+a)}catch(e){t.message+=` - could not read from `+n+` (`+e.message+`)`,c(t,null,r);return}a=o.slice(s,l).map(function(e,t){var n=t+s+1;return(n==r?`  > `:`    `)+n+`| `+e}).join(`
`),t.path=n;try{t.message=(n||`Pug`)+`:`+r+`
`+a+`

`+t.message}catch{}throw t}function l(e){var t=``,n,r;try{var i=e||{};(function(e,i){r=1,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/collectionInfoWidget.pug`,t+=`<div class="g-info-dialog-description">`,r=2,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/collectionInfoWidget.pug`,t+=`<label>`,r=2,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/collectionInfoWidget.pug`,t+=`Terms of Use</label>`,r=3,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/collectionInfoWidget.pug`,t+=`<div class="g-terms-info">`,r=3,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/collectionInfoWidget.pug`,t=t+(i(e.get(`terms`))??``)+`</div></div>`}).call(this,`collection`in i?i.collection:typeof collection<`u`?collection:void 0,`renderMarkdown`in i?i.renderMarkdown:typeof renderMarkdown<`u`?renderMarkdown:void 0)}catch(e){c(e,n,r)}return t}var u=girder.$,{renderMarkdown:d}=girder.misc,{wrap:f}=girder.utilities.PluginUtils,p=girder.views.widgets.CollectionInfoWidget;f(p,`render`,function(e){if(e.call(this),this.model.get(`terms`)){let e=u(l({collection:this.model,renderMarkdown:d}));this.$(`.modal-body>.g-info-dialog-description`).after(e)}return this});var{getCurrentUser:m}=girder.auth,{restRequest:h}=girder.rest,g=girder.models.CollectionModel,_={};g.prototype.hasTerms=function(){return!!this.get(`terms`)},g.prototype.currentUserHasAcceptedTerms=async function(){let e=await this._hashTerms(),t=m();if(t)return t.get(`terms`)?.collection?.[this.id]?.hash===e;{let t=`terms.collection.${this.id}`;try{return window.localStorage.getItem(t)===e}catch{return _[this.id]===e}}},g.prototype.currentUserSetAcceptTerms=async function(){let e=await this._hashTerms(),t=m();if(t)return h({url:`collection/${this.id}/acceptTerms`,method:`POST`,data:{termsHash:e}}).done(()=>{let n=t.get(`terms`)||{};n.collection=n.collection||{},n.collection[this.id]={hash:e,accepted:null},t.set(`terms`,n)});{let t=`terms.collection.${this.id}`;try{window.localStorage.setItem(t,e)}catch{_[this.id]=e}return girder.$.Deferred().resolve().promise()}},g.prototype._hashTerms=async function(){let e=new window.TextEncoder().encode(this.get(`terms`)),t=await window.crypto.subtle.digest(`SHA-256`,e);return Array.from(new Uint8Array(t)).map(e=>e.toString(16).padStart(2,`0`)).join(``)};function v(e){var t=``+e,n=y.exec(t);if(!n)return e;var r,i,a,o=``;for(r=n.index,i=0;r<t.length;r++){switch(t.charCodeAt(r)){case 34:a=`&quot;`;break;case 38:a=`&amp;`;break;case 60:a=`&lt;`;break;case 62:a=`&gt;`;break;default:continue}i!==r&&(o+=t.substring(i,r)),i=r+1,o+=a}return i===r?o:o+t.substring(i,r)}var y=/["&<>]/;function b(t,n,r,i){if(!(t instanceof Error))throw t;if(!(typeof window>`u`&&n||i))throw t.message+=` on line `+r,t;var a,o,s,c;try{i||=e().readFileSync(n,{encoding:`utf8`}),a=3,o=i.split(`
`),s=Math.max(r-a,0),c=Math.min(o.length,r+a)}catch(e){t.message+=` - could not read from `+n+` (`+e.message+`)`,b(t,null,r);return}a=o.slice(s,c).map(function(e,t){var n=t+s+1;return(n==r?`  > `:`    `)+n+`| `+e}).join(`
`),t.path=n;try{t.message=(n||`Pug`)+`:`+r+`
`+a+`

`+t.message}catch{}throw t}function x(e){var t=``,n,r;try{var i=e||{};(function(e,i){r=1,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`<div class="g-terms-container">`,r=2,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`<div class="g-body-title">`,r=2,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t=t+v(e.name()??``)+`</div>`,r=3,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`<label>`,r=3,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`Terms of Use</label>`,r=4,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`<div class="g-terms-info">`,r=4,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t=t+(i(e.get(`terms`))??``)+`</div>`,r=6,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`<div class="btn-toolbar">`,r=7,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`<button class="btn btn-primary btn-md" id="g-terms-accept">`,r=7,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`I Accept</button>`,r=8,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`<button class="btn btn-primary btn-md" id="g-terms-reject">`,r=8,n=`/home/circleci/project/girder/plugins/terms/girder_terms/web_client/templates/termsAcceptance.pug`,t+=`I Decline</button></div></div>`}).call(this,`collection`in i?i.collection:typeof collection<`u`?collection:void 0,`renderMarkdown`in i?i.renderMarkdown:typeof renderMarkdown<`u`?renderMarkdown:void 0)}catch(e){b(e,n,r)}return t}var S=girder.Backbone,{renderMarkdown:C}=girder.misc,w=girder.views.View,T=girder.router,E=w.extend({events:{"click #g-terms-accept":async function(e){let t=this.$(`button`);t.girderEnable(!1),await this.model.currentUserSetAcceptTerms(),t.girderEnable(!0),S.history.loadUrl(S.history.getHash())},"click #g-terms-reject":function(e){T.navigate(``,{trigger:!0})}},initialize:function(e){this.model=e.collection,this.render()},render:function(){return this.$el.html(x({collection:this.model,renderMarkdown:C})),this}}),D=girder._,O=girder.events,k=girder.views.body.CollectionView,A=girder.views.body.FolderView,j=girder.views.body.ItemView,M=girder.models.CollectionModel,N=girder.models.FolderModel,P=girder.models.ItemModel;k.fetchAndInit=function(e,t){let n=new M({_id:e});n.fetch().done(async()=>{n.hasTerms()&&!await n.currentUserHasAcceptedTerms()?O.trigger(`g:navigateTo`,E,{collection:n}):O.trigger(`g:navigateTo`,k,D.extend({collection:n},t||{}))})},A.fetchAndInit=function(e,t){let n,r=new N({_id:e});r.fetch().then(()=>{if(r.get(`baseParentType`)===`collection`)return n=new M({_id:r.get(`baseParentId`)}),n.fetch()}).done(async()=>{n&&n.hasTerms()&&!await n.currentUserHasAcceptedTerms()?O.trigger(`g:navigateTo`,E,{collection:n}):O.trigger(`g:navigateTo`,A,D.extend({folder:r},t||{}))})},j.fetchAndInit=function(e,t){let n,r=new P({_id:e});r.fetch().then(()=>{if(r.get(`baseParentType`)===`collection`)return n=new M({_id:r.get(`baseParentId`)}),n.fetch()}).done(async()=>{n&&n.hasTerms()&&!await n.currentUserHasAcceptedTerms()?O.trigger(`g:navigateTo`,E,{collection:n}):O.trigger(`g:navigateTo`,j,D.extend({item:r},t||{}))})}});