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

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

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

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