-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.min.js
1 lines (1 loc) · 4.41 KB
/
scripts.min.js
1
JFCustomWidgetUtils.domReady(function(){function a(b){var a=JFCustomWidget.getWidgetSettings(),e=document.getElementById("access-code"),f=document.getElementById("autofill"),g=document.getElementById("message"),h="",i=[],j=function(b){for(var c=0,d=b.length,a=0;a<d;a++)c+=(b.charCodeAt(a)-64)*Math.pow(26,d-a-1);return c}(a.codeColumn),k="submitInput"in a&&"Yes"===a.submitInput||JFCustomWidget.isWidgetRequired(),l=null,m=null;Boolean(b.enterprise);var n="autofillByCondition"in a&&"Yes"===a.autofillByCondition;function o(c,b,d,e){for(var a=0;a<c;a++)if(b[a][e]==d)return b[a];return 0}function p(){if(JFCustomWidget.sendData({value:""}),"resetInvalid"in a&&"Yes"===a.resetInvalid&&i.length>0){var b=i.filter(function(a){return!!a.label}).map(function(a){return a.label});JFCustomWidget.clearFields(b),i=[]}}function q(a,b){switch(a){case"valid":image="accept.png";break;case"invalid":case"error":image="delete.png";break;case"loading":image="loader.gif"}g.style.background='url("https://www.jotform.com/images/'+image+'") no-repeat left center',g.style.display="block",g.innerHTML=b,d()}function r(){return"invalidCodeTxt"in a?a.invalidCodeTxt:"Unknown access code."}function c(){return k||(h=""),{valid:!!h,value:h}}function d(){var a=document.getElementsByTagName("body")[0].clientHeight||94;JFCustomWidget.isFromCardform()&&a<94&&(a=94),JFCustomWidget.requestFrameResize({height:a})}function s(a,b,c){a.addEventListener?a.addEventListener(b,c):a.attachEvent("on"+b,function(){c.call(a)})}function t(){var a=navigator.userAgent.match(/(?:MSIE |Trident\/.*; rv:)(\d+)/);return a?parseInt(a[1]):void 0}this.init=function(){"hide"in a&&"Yes"===a.hide&&JFCustomWidget.hideWidgetContainer(),f.innerHTML=a.fillBtnText||"Auto Complete Fields",s(e,"input",function(){0===e.value.length&&(document.getElementById("message").innerHTML="",document.getElementById("message").style.background="")}),s(e,"keypress",function(a){13===a.keyCode&&f.click()}),s(f,"click",function(){h=e.value.trim(),function j(a){var e=!1,b=new XMLHttpRequest;if(!b)throw new Error("Http request is nowhere to be found");if(9>=t()&&(a.url=a.url.replace(/^https?:/,window.location.protocol)),"withCredentials"in b?b.open(a.method||"POST",a.url,!0):"undefined"!=typeof XDomainRequest?((b=new XDomainRequest).open(a.method||"POST",a.url),e=!0):b=null,!b)throw new Error("CORS not supported");if(!e){var d={"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"};if(a.headers)for(var c in a.headers)d[c]=a.headers[c];for(var f in d)b.setRequestHeader(f,d[f])}b.onload=function(){a.hasOwnProperty("success")&&a.success(b)},b.onerror=function(){a.hasOwnProperty("error")&&a.error(b)};var g=a.parameters;if("object"==typeof a.parameters){var h=[];for(var c in a.parameters){var i=[c,encodeURIComponent(a.parameters[c])];h.push(i.join("="))}g=h.join("&")}b.send(g)}({url:"https://shots.jotform.com/Michal_S/dynamicstf/spreadsheet.php",parameters:{action:"getData",url:a.sURL,apiKey:a.apiKey,sheetName:a.sheetName,labelsRow:a.labelsRow||1,codeColumn:j,code:h,formID:b.formID},success:function(e){if(0===e.responseText.length)return console.error("Error occured. Server has no response."),!1;var b=JSON.parse(e.responseText).values;if(!b.success&&b.hasOwnProperty("error"))return console.error(b.error),q("invalid",r()),p(),h="",!1;var f=b[a.labelsRow-1],l=f.length,g=b.length;if(i=[],0===o(g,b,h,j-1))q("invalid",r());else{for(var d=0;d<l;d++)i.push({label:f[d],value:o(g,b,h,j-1)[d]});q("valid","validCodeTxt"in a?a.validCodeTxt:"Fields have been auto filled.")}JFCustomWidget.hideWidgetError(),JFCustomWidget.setFieldsValueByLabel(i),k&&JFCustomWidget.sendData(c())},error:function(a){console.error(a),q("invalid",a.message||r()),p()}})}),b&&b.value&&(e.value=b.value,h=b.value),d()},this.getData=c,this.resizeWidget=d,this.fillAccessCode=function(a){if(n){JFCustomWidget.sendData({value:h});var b=a.value?a.value.trim():"";b&&m!==b&&(e.value=m=b,l&&clearTimeout(l),l=setTimeout(function(){f.click()},1e3))}else e.value=a.value||"",JFCustomWidget.sendData({value:h})},this.resetAccessCode=function(){e.value=h="",g.style.display="none",JFCustomWidget.sendData({value:""}),JFCustomWidget.hideWidgetError(),d()}}JFCustomWidget.subscribe("ready",function(c){var b=new a(c);b.init(),JFCustomWidget.subscribe("submit",function(){JFCustomWidget.sendSubmit(b.getData())}),JFCustomWidget.subscribe("show",function(){b.resizeWidget()}),JFCustomWidget.subscribe("populate",b.fillAccessCode),JFCustomWidget.subscribe("clear",b.resetAccessCode)})})