1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
!function(t,e,o){window.history.replaceState&&window.history.replaceState(null,null,wu_checkout.baseurl),e.addAction("wu_on_create_order","nextpress/wp-ultimo",(function(t,e){void 0!==e.order.extra.template_id&&(t.template_id=e.order.extra.template_id)})),e.addAction("wu_checkout_loaded","nextpress/wp-ultimo",(function(t){if(void 0!==window.wu_auto_submittable_field&&window.wu_auto_submittable_field){const e={deep:!0};t.$watch(window.wu_auto_submittable_field,(function(){jQuery(this.$el).submit()}),e)}})),e.addAction("wu_checkout_loaded","nextpress/wp-ultimo",(function(t){wu_create_cookie("wu_template",""),wu_create_cookie("wu_selected_products",""),wu_listen_to_cookie_change("wu_template",(function(e){t.template_id=e}))})),t(document).on("click",'[href|="#wu-checkout-add"]',(function(e){e.preventDefault();const o=t(this),r=o.attr("href").split("#").pop().replace("wu-checkout-add-","");"undefined"!=typeof wu_checkout_form&&-1===wu_checkout_form.products.indexOf(r)&&(wu_checkout_form.add_product(r),o.html(wu_checkout.i18n.added_to_order))})),window.addEventListener("pageshow",(function(t){t.persisted&&this.window.wu_checkout_form&&this.window.wu_checkout_form.unblock()})),t(document).ready((function(){if(void 0===window.Vue)return;Object.defineProperty(Vue.prototype,"$moment",{value:moment});const r=function(t){return isNaN(t)?t:parseInt(t,10)},s={plan:r(wu_checkout.plan),errors:[],order:wu_checkout.order,products:o.map(wu_checkout.products,r),template_id:wu_checkout.template_id,template_category:"",gateway:wu_checkout.gateway,request_billing_address:wu_checkout.request_billing_address,country:wu_checkout.country,state:"",city:"",site_url:wu_checkout.site_url,site_domain:wu_checkout.site_domain,is_subdomain:wu_checkout.is_subdomain,discount_code:"",toggle_discount_code:0,payment_method:"",username:"",payment_id:wu_checkout.payment_id,membership_id:wu_checkout.membership_id,cart_type:"new",auto_renew:1,duration:wu_checkout.duration,duration_unit:wu_checkout.duration_unit,prevent_submission:!1,valid_password:!0,stored_templates:{},state_list:[],city_list:[],labels:{}};if(e.applyFilters("wu_before_form_init",s),!jQuery("#wu_form").length)return;Vue.component("colorPicker",{props:["value"],template:'<input type="text">',mounted(){const e=this;t(this.$el).val(this.value).wpColorPicker({width:200,defaultColor:this.value,change(t,o){e.$emit("input",o.color.toString())}})},watch:{value(e){t(this.$el).wpColorPicker("color",e)}},destroyed(){t(this.$el).off().wpColorPicker("destroy")}});const i={functional:!0,template:"#dynamic",props:["template"],render(t,e){const o=e.props.template;return t(o?{template:o}:"<div>nbsp;</div>")}};window.wu_checkout_form=new Vue({el:"#wu_form",data:s,directives:{init:{bind(t,e,o){o.context[e.arg]=e.value}}},components:{dynamic:i},computed:{hooks:()=>wp.hooks,unique_products(){return o.uniq(this.products,!1,(t=>parseInt(t,10)))}},methods:{debounce:t=>o.debounce(t,200,!0),open_url(t,e="_blank"){window.open(t,e)},get_template(t,e){void 0===e.id&&(e.id="default");const o=t+"/"+e.id;if(void 0!==this.stored_templates[o])return this.stored_templates[o];const r=this.hooks.applyFilters("wu_before_template_fetch",{duration:this.duration,duration_unit:this.duration_unit,products:this.products,...e},this);return this.fetch_template(t,r),'<div class="wu-p-4 wu-bg-gray-100 wu-text-center wu-my-2 wu-rounded">'+wu_checkout.i18n.loading+"</div>"},reset_templates(t){if(void 0===t)return void(this.stored_templates={});const e={};o.forEach(this.stored_templates,(function(r,s){const i=s.toString().substr(0,s.toString().indexOf("/"));!1===o.contains(t,i)&&(e[s]=r)})),this.stored_templates=e},fetch_template(t,e){const o=this;void 0===e.id&&(e.id="default"),this.request("wu_render_field_template",{template:t,attributes:e},(function(r){const s=t+"/"+e.id;r.success?Vue.set(o.stored_templates,s,r.data.html):Vue.set(o.stored_templates,s,"<div>"+r.data[0].message+"</div>")}))},go_back(){console.log("Going back..."),this.block(),window.history.back()},set_prevent_submission(t){this.$nextTick((function(){this.prevent_submission=t}))},remove_product(t,e){this.products=o.filter(this.products,(function(o){return o!=t&&o!=e}))},add_plan(t){this.plan&&this.remove_product(this.plan),this.plan=t,this.add_product(t)},add_product(t){this.products.push(t)},has_product(t){return this.products.indexOf(t)>-1||this.products.indexOf(parseInt(t,10))>-1},wu_format_money:t=>window.wu_format_money(t),filter_for_request(t,e=""){const r=this.hooks.doAction("wu_filter_for_request",["stored_templates"],t,e);return o.omit(t,r)},create_order(){if(!jQuery("#wu-order-summary-content").length)return;this.block(),this.order=!1;const t=this,e=this.debounce(this.request),o={...this.$data};delete o.stored_templates,delete o.state_list,delete o.city_list,delete o.labels,e("wu_create_order",this.filter_for_request(o,"wu_create_order"),(function(e){t.order=e.data.order,t.state_list=e.data.states,t.city_list=e.data.cities,t.labels=e.data.labels,t.cart_type=e.data.order.type,t.errors=e.data.order.errors,t.hooks.doAction("wu_on_create_order",t,e.data),e.data.order.url,t.unblock()}),this.handle_errors)},get_errors(){const t=this.errors.map((function(t){return t.message}));return t.length>0&&t},get_error(t){const e=this.errors.filter((function(e){return e.code===t}));return e.length>0&&e[0]},form_success(t){if(!o.isEmpty(t.data)){this.hooks.doAction("wu_on_form_success",this,t.data);const e=t.data.gateway.data;e.payment_id=t.data.payment_id,e.membership_id=t.data.membership_id,e.cart_type=t.data.cart_type,jQuery.each(Object.assign({},e),(function(t,e){const o=document.createElement("input");o.type="hidden",o.name=t,o.value=e,jQuery("#wu_form").append(o)}))}},validate_form(){this.errors=[];const t=jQuery("#wu_form").serializeArray().reduce((function(t,{name:e,value:o}){return"products[]"!==e&&(t[e]=o),t}),{}),e=jQuery.param({...t,products:this.products,membership_id:this.membership_id,payment_id:this.payment_id,auto_renew:this.auto_renew,cart_type:this.type,valid_password:this.valid_password,duration:this.duration,duration_unit:this.duration_unit}),o=this;this.request("wu_validate_form",e,(function(t){if(o.valid_password||o.errors.push({code:"password",message:wu_checkout.i18n.weak_password}),!1===t.success)return o.errors=[].concat(o.errors,t.data),void o.unblock();o.errors.length?o.unblock():(o.form_success(t),!1===o.prevent_submission&&o.resubmit())}),this.handle_errors)},resubmit(){jQuery("#wu_form").get(0).submit()},handle_errors(t){this.unblock(),console.error(t)},on_submit(t){t.preventDefault()},on_change_product(t,o){window.wu_create_cookie("wu_selected_products",t.join(","),.5),this.reset_templates(["template-selection"]),e.doAction("wu_on_change_product",t,o,this),this.create_order()},on_change_gateway(t,o){e.doAction("wu_on_change_gateway",t,o,this)},on_change_country(t,o){e.doAction("wu_on_change_country",t,o,this),this.create_order()},on_change_state(t,o){e.doAction("wu_on_change_state",t,o,this),this.create_order()},on_change_city(t,o){e.doAction("wu_on_change_city",t,o,this),this.create_order()},on_change_duration(t,o){this.reset_templates(),e.doAction("wu_on_change_duration",t,o,this),this.create_order()},on_change_duration_unit(t,o){this.reset_templates(),e.doAction("wu_on_change_duration_unit",t,o,this),this.create_order()},on_change_discount_code(t,o){e.doAction("wu_on_change_discount_code",t,o,this),this.create_order()},block(){const e=jQuery(this.$el).parents().filter((function(){return"rgba(0, 0, 0, 0)"!==t(this).css("backgroundColor")})).first().css("backgroundColor");jQuery(this.$el).wu_block({message:'<div class="spinner is-active wu-float-none" style="float: none !important;"></div>',overlayCSS:{backgroundColor:e||"#ffffff",opacity:.6},css:{padding:0,margin:0,width:"50%",fontSize:"14px !important",top:"40%",left:"35%",textAlign:"center",color:"#000",border:"none",backgroundColor:"none",cursor:"wait"}})},unblock(){jQuery(this.$el).wu_unblock()},request(t,e,o,r){const s="wu_validate_form"===t?wu_checkout.late_ajaxurl:wu_checkout.ajaxurl;jQuery.ajax({method:"POST",url:s+"&action="+t,data:e,success:o,error:r})},check_pass_strength(){if(!jQuery("#pass-strength-result").length)return;jQuery("#pass-strength-result").attr("class","wu-py-2 wu-px-4 wu-bg-gray-100 wu-block wu-text-sm wu-border-solid wu-border wu-border-gray-200");const t=jQuery("#field-password").val();if(!t)return void jQuery("#pass-strength-result").addClass("empty").html("Enter Password");this.valid_password=!1;const e=void 0===wp.passwordStrength.userInputDisallowedList?wp.passwordStrength.userInputBlacklist():wp.passwordStrength.userInputDisallowedList();switch(wp.passwordStrength.meter(t,e,t)){case-1:jQuery("#pass-strength-result").addClass("wu-bg-red-200 wu-border-red-300").html(pwsL10n.unknown);break;case 2:jQuery("#pass-strength-result").addClass("wu-bg-red-200 wu-border-red-300").html(pwsL10n.bad);break;case 3:jQuery("#pass-strength-result").addClass("wu-bg-green-200 wu-border-green-300").html(pwsL10n.good),this.valid_password=!0;break;case 4:jQuery("#pass-strength-result").addClass("wu-bg-green-200 wu-border-green-300").html(pwsL10n.strong),this.valid_password=!0;break;case 5:jQuery("#pass-strength-result").addClass("wu-bg-yellow-200 wu-border-yellow-300").html(pwsL10n.mismatch);break;default:jQuery("#pass-strength-result").addClass("wu-bg-yellow-200 wu-border-yellow-300").html(pwsL10n.short)}}},updated(){this.$nextTick((function(){e.doAction("wu_on_form_updated",this),wu_initialize_tooltip()}))},mounted(){const o=this;jQuery(this.$el).on("click",(function(e){t(this).data("submited_via",t(e.target))})),jQuery(this.$el).on("submit",(async function(t){t.preventDefault();const r=jQuery(this).data("submited_via");if(r){const t=jQuery("<input>");t.attr("type","hidden"),t.attr("name",r.attr("name")),t.attr("value",r.val()),jQuery(this).append(t)}o.block();try{const t=[];await Promise.all(e.applyFilters("wu_before_form_submitted",t,o,o.gateway))}catch(t){return o.errors=[],o.errors.push({code:"before-submit-error",message:t.message}),o.unblock(),void o.handle_errors(t)}o.validate_form(),e.doAction("wu_on_form_submitted",o,o.gateway)})),this.create_order(),e.doAction("wu_checkout_loaded",this),e.doAction("wu_on_change_gateway",this.gateway,this.gateway),jQuery("#field-password").on("input pwupdate",(function(){o.check_pass_strength()})),wu_initialize_tooltip()},watch:{products(t,e){this.on_change_product(t,e)},toggle_discount_code(t){t||(this.discount_code="")},discount_code(t,e){this.on_change_discount_code(t,e)},gateway(t,e){this.on_change_gateway(t,e)},country(t,e){this.state="",this.on_change_country(t,e)},state(t,e){this.city="",this.on_change_state(t,e)},city(t,e){this.on_change_city(t,e)},duration(t,e){this.on_change_duration(t,e)},duration_unit(t,e){this.on_change_duration_unit(t,e)}}})}))}(jQuery,wp.hooks,_); |