| 1 |
const CharitableSettingsSquare=window.CharitableSettingsSquare||function(e,t,o){let a={};const n={init(){o(n.ready)},ready(){o(e).ready(function(){a={sandboxModeCheckbox:o("#charitable-setting-square-sandbox-mode"),sandboxConnectionStatusBlock:o("#charitable-setting-row-square-connection-status-sandbox"),productionConnectionStatusBlock:o("#charitable-setting-row-square-connection-status-production"),sandboxLocationBlock:o("#charitable-setting-row-square-location-id-sandbox"),sandboxLocationStatusBlock:o("#charitable-setting-row-square-location-status-sandbox"),productionLocationBlock:o("#charitable-setting-row-square-location-id-production"),productionLocationStatusBlock:o("#charitable-setting-row-square-location-status-production"),refreshBtn:o(".charitable-square-refresh-btn"),copyButton:o("#charitable-setting-row-square-webhooks-endpoint-set .charitable-copy-to-clipboard"),webhookEndpointUrl:o("input#charitable-square-webhook-endpoint-url"),webhookMethod:o('input[name="square-webhooks-communication"]'),webhookCommunicationStatusNotice:o("#charitable-setting-row-square-webhooks-communication-status"),webhookConnectBtn:o("#charitable-setting-square-webhooks-connect, .charitable-btn-connect-webhooks")},n.events()})},events(){function i(t,o){const a=e.createElement("textarea");a.value=t,a.style.top="0",a.style.left="0",a.style.position="fixed",a.style.opacity="0",e.body.appendChild(a),a.focus(),a.select();try{e.execCommand("copy")&&(o.addClass("copied"),setTimeout(function(){o.removeClass("copied")},2e3))}catch(e){console.error("Fallback: Oops, unable to copy",e)}e.body.removeChild(a)}a.sandboxModeCheckbox.on("change",n.credentialsFieldsDisplay),a.refreshBtn.on("click",n.refreshTokensCallback),a.webhookMethod.on("change",n.updateWebhookEndpointUrl),o(e).on("click",".charitable-copy-icon",function(e){e.preventDefault();const a=o(this).data("clipboard-target"),n=o(a),c=o(this);if(n.length){const e=n.val();navigator.clipboard&&t.isSecureContext?navigator.clipboard.writeText(e).then(function(){c.addClass("copied"),setTimeout(function(){c.removeClass("copied")},2e3)}).catch(function(t){console.error("Failed to copy: ",t),i(e,c)}):i(e,c)}})},updateWebhookEndpointUrl(){const e=a.webhookMethod.filter(":checked").val(),t=charitable_admin.square.webhook_urls[e];a.webhookEndpointUrl.val(t),a.webhookCommunicationStatusNotice.removeClass("charitable-hide")},refreshTokensCallback(){const e=o(this),a=e.outerWidth(),i=e.text(),c={url:charitable_admin.ajax_url,type:"post",dataType:"json",data:{action:"charitable_square_refresh_connection",nonce:charitable_admin.nonce,mode:e.data("mode")},beforeSend(){e.css("width",a).html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>').prop("disabled",!0)}};let r=charitable_admin.square.refresh_error;o.ajax(c).done(function(o){if(o.success)return e.css("pointerEvents","none").removeClass("charitable-btn-light-grey").addClass("charitable-btn-grey").html("Refreshed!"),e.closest("form").css("cursor","wait"),void(t.location=e.data("url"));Object.prototype.hasOwnProperty.call(o,"data")&&""!==o.data&&(r=o.data),e.css("width","auto").html(i).prop("disabled",!1),n.modals.refreshTokensError(r)}).fail(function(){e.css("width","auto").html(i).prop("disabled",!1),n.modals.refreshTokensError(r)})},credentialsFieldsDisplay(){const e=a.sandboxModeCheckbox.is(":checked");e?(a.sandboxConnectionStatusBlock.show(),a.sandboxLocationBlock.show(),a.sandboxLocationStatusBlock.show(),a.productionConnectionStatusBlock.hide(),a.productionLocationBlock.hide(),a.productionLocationStatusBlock.hide()):(a.sandboxConnectionStatusBlock.hide(),a.sandboxLocationBlock.hide(),a.sandboxLocationStatusBlock.hide(),a.productionConnectionStatusBlock.show(),a.productionLocationBlock.show(),a.productionLocationStatusBlock.show()),e&&a.sandboxConnectionStatusBlock.find(".charitable-square-connected").length||!e&&a.productionConnectionStatusBlock.find(".charitable-square-connected").length||n.modals.modeChangedWarning()},modals:{modeChangedWarning(){o.alert({title:charitable_admin.heads_up,content:charitable_admin.square.mode_update,icon:"fa fa-exclamation-circle",type:"orange",buttons:{confirm:{text:charitable_admin.ok,btnClass:"btn-confirm",keys:["enter"]}}})},refreshTokensError(e){o.alert({title:!1,content:e,icon:"fa fa-exclamation-circle",type:"orange",buttons:{confirm:{text:charitable_admin.ok,btnClass:"btn-confirm",keys:["enter"]}}})},displayWebhookConfigPopup(){o.confirm({title:charitable_admin.square.webhook_create_title,content:charitable_admin.square.webhook_create_description+'<input type="text" id="charitable-square-personal-access-token" placeholder="'+charitable_admin.square.webhook_token_placeholder+'" value=""><p class="charitable-square-webhooks-connect-error error" style="display:none;">'+charitable_admin.square.token_is_required+"</p>",icon:"fa fa-info-circle",type:"blue",buttons:{confirm:{text:charitable_admin.ok,btnClass:"btn-confirm",keys:["enter"],action(){const e=this,t=e.$content.find("#charitable-square-personal-access-token"),o=e.$content.find(".error"),n=t.val().trim();e.$title;return a.webhookConnectBtn.addClass("inactive"),o.hide().text(""),""===n?(o.text(charitable_admin.square.token_is_required).show(),!1):(e.buttons.confirm.setText(charitable_admin.loading),e.buttons.confirm.disable(),!1)}},cancel:{text:charitable_admin.cancel,action(){a.webhookConnectBtn.removeClass("inactive"),this.close()}}}})}}};return n}(document,window,jQuery);CharitableSettingsSquare.init(); |