UNPKG

@digital-blueprint/formalize-app

Version:

[GitHub Repository](https://github.com/digital-blueprint/formalize-app) | [npmjs package](https://www.npmjs.com/package/@digital-blueprint/formalize-app) | [Unpkg CDN](https://unpkg.com/browse/@digital-blueprint/formalize-app/) | [Formalize Bundle](https:

94 lines (90 loc) 11.4 kB
let t,s,i,e,r,o,n,a,l,u,m,h,d,f,c=t=>t;function g(t,s){var i=Object.keys(t);if(Object.getOwnPropertySymbols){var e=Object.getOwnPropertySymbols(t);s&&(e=e.filter((function(s){return Object.getOwnPropertyDescriptor(t,s).enumerable}))),i.push.apply(i,e)}return i}function b(t){for(var s=1;s<arguments.length;s++){var i=null!=arguments[s]?arguments[s]:{};s%2?g(Object(i),!0).forEach((function(s){p(t,s,i[s])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):g(Object(i)).forEach((function(s){Object.defineProperty(t,s,Object.getOwnPropertyDescriptor(i,s))}))}return t}function p(t,s,i){return(s=function(t){var s=function(t,s){if("object"!=typeof t||!t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var e=i.call(t,s||"default");if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===s?String:Number)(t)} /*! * License: LGPL-2.1-or-later * Dependencies: * * @webcomponents/scoped-custom-element-registry: BSD-3-Clause * @dbp-toolkit/app-shell: LGPL-2.1-or-later * @dbp-toolkit/language-select: LGPL-2.1-or-later * @dbp-toolkit/auth: LGPL-2.1-or-later * @dbp-toolkit/theme-switcher: LGPL-2.1-or-later * universal-router: MIT * @dbp-toolkit/matomo: LGPL-2.1-or-later * @dbp-toolkit/tabulator-table: LGPL-2.1-or-later * xlsx: Apache-2.0 * jspdf-autotable: MIT * xss: MIT * cssfilter: MIT * @dbp-toolkit/common: LGPL-2.1-or-later * lit-html: BSD-3-Clause */(t,"string");return"symbol"==typeof s?s:s+""}(s))in t?Object.defineProperty(t,s,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[s]=i,t}import{x as y,S,s as w,t as $,i as v,j as P}from"./shared/notification.DekX2Yn_.es.js";import{D}from"./shared/dbp-formalize-lit-element.Ct2psBFR.es.js";import{e as j,n as A}from"./shared/ref.Ds46LGIc.es.js";import{p as U,a as E,g as F}from"./shared/utils.BRAAprrM.es.js";import"./shared/i18n.sFs-myza.es.js";import"./shared/error.CxyP7_gO.es.js"; /** * @license * Copyright 2020 Google LLC * SPDX-License-Identifier: BSD-3-Clause */const O=Symbol.for(""),k=t=>{if((null==t?void 0:t.r)===O)return null==t?void 0:t._$litStatic$},C=t=>({_$litStatic$:t,r:O}),R=new Map,I=(T=y,(t,...s)=>{const i=s.length;let e,r;const o=[],n=[];let a,l=0,u=!1;for(;l<i;){for(a=t[l];l<i&&void 0!==(r=s[l],e=k(r));)a+=e+t[++l],u=!0;l!==i&&n.push(r),o.push(a),l++}if(l===i&&o.push(t[i]),u){const i=o.join("$$lit$$");void 0===(t=R.get(i))&&(o.raw=o,R.set(i,t=o)),s=n}return T(t,...s)});var T;class x extends(S(D)){constructor(){super(),this.formComponents={},this.formIdentifiers={},this.formRef=j(),this.formUrlSlug="",this.submissionId="",this.loadedSubmission={},this.userAllSubmissions=[],this.userAllDraftSubmissions=[],this.usersSubmissionCount=0,this.formProperties={},this.authTokenExists=!1,this.submissionAllowed=!1,this.formDisplayDenied=!1}static get scopedElements(){return{}}static get properties(){return b(b({},super.properties),{},{submissionAllowed:{type:Boolean,attribute:!1},formDisplayDenied:{type:Boolean,attribute:!1},loadedSubmission:{type:Object,attribute:!1},userAllSubmissions:{type:Object,attribute:!1},formProperties:{type:Array,attribute:!1}})}connectedCallback(){super.connectedCallback(),this.updateComplete.then((()=>{console.log("-- updateComplete --"),this.loadModules()}))}updateFormUrlSlug(){console.log("updateFormUrlSlug this.routingUrl",this.routingUrl),console.log("updateFormUrlSlug this.getRoutingData()",this.getRoutingData());const t=this.getRoutingData().pathSegments[0]||"",s=this.getRoutingData().pathSegments[1]||"";/^[a-z,0-9,-]{36,36}$/.test(s)&&(this.submissionId=s),this.submissionId&&"readonly"===this.getRoutingData().pathSegments[2]&&(this.readOnly=!0),this.formUrlSlug!==t&&(this.formUrlSlug=t,console.log("updateFormUrlSlug this.formUrlSlug",this.formUrlSlug),this.handlePermissionsForCurrentForm())}async handlePermissionsForCurrentForm(){const t=this.formIdentifiers[this.formUrlSlug];this.submissionAllowed=!!t&&await this.checkPermissionsToForm(t)}async checkPermissionsToForm(t){if(""===this.auth.token)return!1;let s;this.authTokenExists=!0;let i=[];const e={method:"GET",headers:{"Content-Type":"application/ld+json",Authorization:"Bearer "+this.auth.token}};try{if(s=await this.httpGetAsync(this.entryPointUrl+"/formalize/forms/"+t,e),!s.ok)return!1;i=await s.json()}catch(t){return this.sendErrorAnalyticsEvent("checkPermissionsToForm","WrongResponse",t),console.error(t),!1}return console.log("checkPermissionsToForm data",i),i.error?(console.error("checkPermissionsToForm data.error",i.error),!1):"hydra:Error"===i["@type"]?(console.error("checkPermissionsToForm hydra:Error",i.detail),!1):(this.formProperties=i,Array.isArray(i.grantedActions)&&(i.grantedActions.includes("manage")||i.grantedActions.includes("create_submissions")))}async loadModules(){try{const t=await fetch(this.basePath+"modules.json"),s=await t.json();let i={},e={};for(const[t,r]of Object.entries(s.forms)){const s=await import(r);console.log("formKey",t);const o=new s.default;o.getFormComponent&&(i[o.getUrlSlug()]=o.getFormComponent()),o.getFormIdentifier&&(e[o.getUrlSlug()]=o.getFormIdentifier())}this.formComponents=i,this.formIdentifiers=e,await this.handlePermissionsForCurrentForm(),await this.getUserAllSubmissionsData(this.formIdentifiers[this.formUrlSlug])}catch(t){console.error("Error loading modules:",t)}}async getUserAllSubmissionsData(t){if(!t||""===this.auth.token)return;const s={method:"GET",headers:{"Content-Type":"application/ld+json",Authorization:"Bearer "+this.auth.token}};try{const i=await this.httpGetAsync(this.entryPointUrl+`/formalize/submissions?formIdentifier=${t}`,s);if(!i.ok)return;const e=await i.json();void 0!==e&&e["hydra:member"]&&e["hydra:member"].length>0&&(this.userAllSubmissions=e["hydra:member"],this.userAllSubmittedSubmissions=e["hydra:member"].filter((t=>4==t.submissionState)),this.userAllDraftSubmissions=e["hydra:member"].filter((t=>1==t.submissionState)),this.usersSubmissionCount=this.userAllSubmittedSubmissions.length,this.requestUpdate())}catch(t){console.error(t)}console.log("Users submissions data:",this.userAllSubmissions)}async getSubmissionData(){if(!this.submissionId)return!1;let t={};const s={method:"GET",headers:{"Content-Type":"application/ld+json",Authorization:"Bearer "+this.auth.token}};try{const i=await this.httpGetAsync(this.entryPointUrl+"/formalize/submissions/"+this.submissionId,s);i.ok||(this.handleErrorResponse(i),this.formDisplayDenied=!0),t=await i.json()}catch(t){console.error(t),this.formDisplayDenied=!0}this.loadedSubmission=t,console.log("this.loadedSubmission",this.loadedSubmission)}getFormHtml(){const m=this.formUrlSlug,h=this.formComponents;if(""===m)return y(t||(t=c` No form identifier provided! `));if(!h)return y(s||(s=c` Loading... `));if(!h[m])return console.log("formUrlSlug not found",m),y(i||(i=c` Form <strong>${0}</strong> not found! `),m);const d="dbp-formalize-form-"+U(m),f=this.formComponents[m];this.registry.get(d)||this.registry.define(d,f);const g=this.formIdentifiers[this.formUrlSlug],b=this.formProperties.allowedSubmissionStates,p=this.formProperties.maxNumSubmissionsPerCreator,S=this.formProperties.allowedActionsWhenSubmitted;if(this.usersSubmissionCount>0&&1==p){if(S.includes("read")||S.includes("manage")){const t=this.userAllSubmittedSubmissions.pop().identifier,s=`${E(this.formUrlSlug)}/${t}/readonly`;return y(e||(e=c` <div class="notification is-warning"> ${0} <a href="${0}"> ${0} </a> </div> `),this._i18n.t("render-form.form-already-submitted-warning"),s,this._i18n.t("render-form.check-previous-submissions-warning"))}return y(r||(r=c` <div class="notification is-warning"> ${0} </div> `),this._i18n.t("render-form.form-already-submitted-warning"))}let $={};if(Object.keys(this.loadedSubmission).length>0&&(this.loadedSubmission.form===`/formalize/forms/${this.formIdentifiers[m]}`?$=this.loadedSubmission:(w({summary:"Error",body:"Invalid submission data",type:"danger",timeout:5}),this.formDisplayDenied=!0)),this.formDisplayDenied)return y(o||(o=c` <div class="notification is-warning"> ${0} </div> `),this._i18n.t("render-form.form-not-accessible"));if(this.usersSubmissionCount>=p)return y(n||(n=c` <div class="notification is-warning"> ${0} <a href="${0}"> ${0} </a> </div> `),this._i18n.t("render-form.form-already-submitted-n-times-warning",{n:this.usersSubmissionCount}),F(this.formIdentifiers[this.formUrlSlug]),this._i18n.t("render-form.check-previous-submissions-warning"));let v=y(a||(a=c``));return this.usersSubmissionCount>0&&(S.includes("read")||S.includes("manage"))&&(v=y(l||(l=c` <div class="notification is-warning"> ${0} <a href="${0}"> ${0} </a> </div> `),this._i18n.t("render-form.form-already-submitted-warning"),F(this.formIdentifiers[this.formUrlSlug]),this._i18n.t("render-form.check-previous-submissions-warning"))),console.log("RENDER-FORM-RENDER: formProperties",this.formProperties),console.log("RENDER-FORM-RENDER: usersSubmissions",this.userAllSubmissions),console.log("RENDER-FORM-RENDER: data",$),I(u||(u=c` ${0} <${0} ${0} id="edit-form" subscribe="auth,lang,entry-point-url,nextcloud-web-app-password-url,nextcloud-web-dav-url,nextcloud-auth-url,nextcloud-name,nextcloud-file-url" form-identifier=${0} form-url-slug=${0} max-number-of-submissions=${0} allowed-submission-states=${0} ?read-only=${0} .formProperties=${0} .userAllSubmissions=${0} .data=${0}></${0}> `),v,C(d),A(this.formRef),g,m,p,b,this.readOnly,this.formProperties,this.userAllSubmissions,$,C(d))}static get styles(){return v(m||(m=c` ${0} .notification { margin-bottom: 2em; } `),$())}render(){const t=this._i18n;return this.isLoggedIn()||this.isAuthPending()?this.submissionAllowed?y(f||(f=c` ${0} `),this.getFormHtml()):y(d||(d=c` <div class="notification is-warning"> ${0} </div> `),t.t("render-form.form-not-accessible")):y(h||(h=c` <div class="notification is-warning">${0}</div> `),t.t("error-login-message"))}async update(t){t.has("auth")&&(this.authTokenExists||""===this.auth.token||(this.authTokenExists=!0,this.handlePermissionsForCurrentForm(),await this.getUserAllSubmissionsData(this.formIdentifiers[this.formUrlSlug]),await this.getSubmissionData())),t.has("routingUrl")&&this.updateFormUrlSlug(),super.update(t)}}P("dbp-formalize-render-form",x); //# sourceMappingURL=dbp-formalize-render-form.js.map