silverstripe-userforms/client/dist/js/userforms.js

1 line
8.8 KiB
JavaScript

!function(){"use strict";var t={17:function(){jQuery(document).ready((t=>{const e={},r={show(){this.$el.attr("aria-hidden",!1).show()},hide(){this.$el.attr("aria-hidden",!0).hide()}};function s(e){return this.$el=e instanceof t?e:t(e),this.$el.find("h4").text(i18n._t("UserForms.ERROR_CONTAINER_HEADER","Please correct the following errors and try again:")),this}function i(r){const i=this;this.$el=r instanceof t?r:t(r);const n=this.$el.closest(".userform").data("inst");return this.$elButton=t(`.step-button-wrapper[data-for='${this.$el.prop("id")}']`),this.viewed=!1,this.valid=!1,this.id=null,this.hide(),e.DISPLAY_ERROR_MESSAGES_AT_TOP&&(this.errorContainer=new s(this.$el.find(".error-container")),n.$el.on("userform.form.error",((e,r)=>{i.$el.is(":visible")&&t.each(r.errorList,((e,r)=>{i.errorContainer.updateErrorMessage(t(r.element),r.message)}))})),n.$el.on("userform.form.valid",((t,e)=>{i.errorContainer.removeErrorMessage(e)}))),this.$elButton.on("userform.field.hide userform.field.show",(()=>{n.$el.trigger("userform.form.conditionalstep")})),this}function n(e){const r=this;this.$el=e instanceof t?e:t(e),this.$buttons=this.$el.find(".step-button-jump"),this.$jsAlign=this.$el.find(".js-align");const s=this.$el.closest(".userform").data("inst");return this.$buttons.each(((e,s)=>{t(s).on("click",(e=>{e.preventDefault();const s=parseInt(t(e.target).data("step"),10);r.$el.trigger("userform.progress.changestep",s)}))})),s.$el.on("userform.form.changestep",((t,e)=>{r.update(e)})),s.$el.on("userform.form.conditionalstep",(()=>{const e=r.$buttons.filter(":visible");e.each(((e,r)=>{t(r).text(e+1)})),r.$el.find(".progress-bar").attr("aria-valuemax",e.length),r.$el.find(".total-step-number").text(e.length)})),this.$jsAlign.each(((e,s)=>{const i=t(s),n=`${100/(r.$jsAlign.length-1)*e}%`,o=i.innerWidth()/2*-1;i.css({left:n,marginLeft:o}),e===r.$jsAlign.length-1?i.css({marginLeft:2*o}):0===e&&i.css({marginLeft:0})})),this}function o(e){const r=this;this.$el=e instanceof t?e:t(e);const s=this.$el.closest(".userform");this.userformInstance=s.data("inst"),this.$prevButton=this.$el.find(".step-button-prev"),this.$nextButton=this.$el.find(".step-button-next"),this.$prevButton.parent().attr("aria-hidden",!1).show(),this.$nextButton.parent().attr("aria-hidden",!1).show();const i=function(){const e=s.offset();t("html, body").animate({scrollTop:e.top},"slow")};return this.$prevButton.on("click",(t=>{t.preventDefault(),i(),r.$el.trigger("userform.action.prev")})),this.$nextButton.on("click",(t=>{t.preventDefault(),i(),r.$el.trigger("userform.action.next")})),this.userformInstance.$el.on("userform.form.changestep userform.form.conditionalstep",(()=>{r.update()})),this}function a(e){const r=this;return this.$el=e instanceof t?e:t(e),this.steps=[],this.errorContainer=new s(this.$el.children(".error-container")),this.$el.on("userform.action.prev",(()=>{r.prevStep()})),this.$el.on("userform.action.next",(()=>{r.nextStep()})),this.$el.find(".userform-progress").on("userform.progress.changestep",((t,e)=>{r.jumpToStep(e-1)})),this.$el.on("userform.form.valid",((t,e)=>{r.errorContainer.removeStepLink(e)})),this.$el.validate(this.validationOptions),this.$el.find(".optionset.requiredField input").each(((e,r)=>{t(r).rules("add",{required:!0})})),this}s.prototype.hasErrors=function(){return this.$el.find(".error-list").children().length>0},s.prototype.removeErrorMessage=function(t){this.$el.find(`#${t}-top-error`).remove(),this.hasErrors()||this.hide()},s.prototype.addStepLink=function(e){const r=this.$el.closest(".userform").data("inst"),s=`${e.$el.attr("id")}-error-link`;let i=this.$el.find(`#${s}`);const n=e.$el.attr("id"),o=e.$el.data("title");i.length||(i=t(`<li id="${s}"><a href="#${n}">${o}</a></li>`),i.on("click",(t=>{t.preventDefault(),r.jumpToStep(e.id)})),this.$el.find(".error-list").append(i))},s.prototype.removeStepLink=function(e){const r=t(`#${e}`).closest(".form-step").attr("id");this.$el.find(`#${r}-error-link`).remove(),this.$el.find(".error-list").is(":empty")&&this.hide()},s.prototype.updateErrorMessage=function(e,r){const s=e.attr("id");let i=`#${s}`;const n=`${s}-top-error`;let o=t(`#${n}`),a=e.attr("aria-describedby");r?(o.removeClass("fixed"),this.show(),1===o.length?o.show().find("a").html(r):(e.closest(".field[id]").each((()=>{const e=t(this).attr("id");e&&(i=`#${e}`)})),o=t("<li><a></a></li>"),o.attr("id",n).find("a").attr("href",location.pathname+location.search+i).html(r),this.$el.find("ul").append(o),a?a.match(new RegExp(`\\b${n}\\b`))||(a+=` ${n}`):a=n,e.attr("aria-describedby",a))):o.addClass("fixed")},i.prototype.conditionallyHidden=function(){return!this.$elButton.find("button").is(":visible")},n.prototype.update=function(e){const r=t(this.$el.parent(".userform").find(".form-step")[e]);let s=0,i=e/(this.$buttons.length-1)*100;this.$buttons.each(((r,i)=>!(r>e)&&(t(i).is(":visible")&&(s+=1),!0))),this.$el.find(".current-step-number").each(((e,r)=>{t(r).text(s)})),this.$el.find("[aria-valuenow]").each(((e,r)=>{t(r).attr("aria-valuenow",s)})),this.$buttons.each(((e,r)=>{const i=t(r),n=i.parent();if(parseInt(i.data("step"),10)===s&&i.is(":visible"))return n.addClass("current viewed"),void i.removeAttr("disabled");n.removeClass("current")})),this.$el.siblings(".progress-title").text(r.data("title")),i=i?`${i}%`:"",this.$el.find(".progress-bar").width(i)},o.prototype.update=function(){const t=this.userformInstance.steps.length,e=this.userformInstance.currentStep?this.userformInstance.currentStep.id:0;let r=null,s=null;for(this.$el.find(".step-button-prev")[0===e?"hide":"show"](),r=t-1;r>=0;r--)if(s=this.userformInstance.steps[r],!s.conditionallyHidden()){this.$el.find(".step-button-next")[e>=r?"hide":"show"](),this.$el.find(".btn-toolbar")[e>=r?"show":"hide"]();break}},a.prototype.validationOptions={ignore:":hidden,ul",errorClass:"error",errorElement:"span",errorPlacement:(t,e)=>{t.addClass("message"),e.is(":radio")||e.parents(".checkboxset").length>0?t.appendTo(e.closest(".middleColumn, .field")):e.parents(".checkbox").length>0?t.appendTo(e.closest(".field")):t.insertAfter(e)},invalidHandler:(t,e)=>{setTimeout((()=>{e.currentElements.filter(".error").first().focus()}),0)},submitHandler:e=>{let r=!0;const s=t(e).closest(".userform").data("inst");if(s.currentStep&&(s.currentStep.valid=t(e).valid()),t.each(s.steps,((t,e)=>{e.valid||e.conditionallyHidden()||(r=!1,s.errorContainer.addStepLink(e))})),r){const r=t(e).find(".field.requiredField.hide input");r.length>0&&r.removeAttr("required aria-required data-rule-required").valid(),t(e).removeClass("dirty"),e.submit(),s.$el.trigger("userform.form.submit")}else s.errorContainer.show()},success:e=>{const r=t(e).closest(".userform").data("inst"),s=t(e).attr("id"),i=s.substr(0,s.indexOf("-error")).replace(/[\\[\\]]/,"");e.remove(),r.$el.trigger("userform.form.valid",[i])}},a.prototype.addStep=function(t){t instanceof i&&(t.id=this.steps.length,this.steps.push(t))},a.prototype.setCurrentStep=function(t){t instanceof i&&(this.currentStep=t,this.currentStep.show(),this.currentStep.viewed=!0,this.currentStep.$el.addClass("viewed"))},a.prototype.jumpToStep=function(t,e){const r=this.steps[t];let s=!1;const i=void 0===e||e;void 0!==r&&(r.conditionallyHidden()?i?this.jumpToStep(t+1,e):this.jumpToStep(t-1,e):(s=this.$el.valid(),this.currentStep.valid=s,!1===s&&!1===r.viewed||(this.currentStep.hide(),this.setCurrentStep(r),this.$el.trigger("userform.form.changestep",[r.id]))))},a.prototype.nextStep=function(){this.jumpToStep(this.steps.indexOf(this.currentStep)+1,!0)},a.prototype.prevStep=function(){this.jumpToStep(this.steps.indexOf(this.currentStep)-1,!1)},t(".userform").each((function(l,d){const h=t(d);if(0===h.length)return;e.ENABLE_LIVE_VALIDATION=void 0!==h.data("livevalidation"),e.DISPLAY_ERROR_MESSAGES_AT_TOP=void 0!==h.data("toperrors"),!1===e.ENABLE_LIVE_VALIDATION&&t.extend(a.prototype.validationOptions,{onfocusout:!1}),e.DISPLAY_ERROR_MESSAGES_AT_TOP&&t.extend(a.prototype.validationOptions,{invalidHandler:(t,e)=>{h.trigger("userform.form.error",[e])},onfocusout:!1}),h.find(".userform-progress, .step-navigation").attr("aria-hidden",!1).show(),t.extend(i.prototype,r),t.extend(s.prototype,r);const p=new a(h);h.data("inst",p),e.HIDE_FIELD_LABELS&&h.find("label.left").each((()=>{const e=t(this);t(`[name="${e.attr("for")}"]`).attr("placeholder",e.text()),e.remove()})),p.$el.find(".form-step").each(((t,e)=>{const r=new i(e);p.addStep(r)})),p.setCurrentStep(p.steps[0]);const u=h.find(".userform-progress");if(u.length){new n(u).update(0)}const c=h.find(".step-navigation");if(c.length){new o(c).update()}t(document).on("click","input.text[data-showcalendar]",(()=>{const e=t(this);e.ssDatepicker(),e.data("datepicker")&&e.datepicker("show")})),setInterval((()=>{t.ajax({url:"UserDefinedFormController/ping"})}),18e4),void 0!==h.areYouSure&&h.areYouSure({message:i18n._t("UserForms.LEAVE_CONFIRMATION","You have unsaved changes!")})}))}))}},e={};function r(s){var i=e[s];if(void 0!==i)return i.exports;var n=e[s]={exports:{}};return t[s](n,n.exports,r),n.exports}r(17)}();