-
Notifications
You must be signed in to change notification settings - Fork 3
/
frontendforms-slidercaptcha.min.js
1 lines (1 loc) · 7.85 KB
/
frontendforms-slidercaptcha.min.js
1
function showSliderCaptcha(t,e,n){slidertexts.barText;var i=sliderCaptcha({id:t+"-captcha",loadingText:slidertexts.loadingText,failedText:slidertexts.failedText,barText:slidertexts.barText,offset:slidertexts.offset,onSuccess:function(){setTimeout((function(){i.reset(),document.getElementById(t+"-xPos").value=e,document.getElementById(t+"-yPos").value=n,document.getElementById(t+"-slider-captcha").checked=!0,document.getElementById(t+"-captcha").setAttribute("data-validated",!0),document.getElementById(t+"-captcha").innerHTML=""}),500)},setSrc:function(){}})}function listenToSliderCaptchaCheckboxes(){let t=document.getElementsByClassName("ff-slidercaptcha-checkbox");for(let e=0;e<t.length;e++){let n=t[e].dataset.x,i=t[e].dataset.y,o=t[e].dataset.formid;t[e].checked=!1,t[e].addEventListener("click",(function(t){t.preventDefault();let e=document.getElementById(o+"-captcha");e.innerHTML||"false"!=e.getAttribute("data-validated")||showSliderCaptcha(o,n,i)}),!1)}}!function(){"use strict";var t,e=function(){var t,e,n,i=arguments.length,o=arguments[0]||{};for("object"!=typeof o&&"function"!=typeof o&&(o={}),1==i&&(o=this,t--),t=1;t<i;t++)for(n in e=arguments[t])Object.prototype.hasOwnProperty.call(e,n)&&(o[n]=e[n]);return o},n=function(t){return"function"==typeof t&&"number"!=typeof t.nodeType},i=function(t,n){this.$element=t,this.options=e({},i.DEFAULTS,n),this.$element.style.position="relative",this.$element.style.width=this.options.width+"px",this.$element.style.margin="10px 0",this.init()};i.VERSION="1.0",i.Author="[email protected]",i.DEFAULTS={width:280,height:155,PI:Math.PI,sliderL:42,sliderR:9,offset:5,loadingText:"æ£åœ¨åŠ è½½ä¸...",failedText:"å†è¯•ä¸€æ¬¡",barText:"å‘å³æ»‘动填充拼图",repeatIcon:"",maxLoadCount:3,localImages:function(){return"images/Pic"+Math.round(4*Math.random())+".jpg"},verify:function(t,e){var n=!1;return $.ajax({url:e,data:{datas:JSON.stringify(t)},dataType:"json",type:"post",async:!1,success:function(t){n=JSON.stringify(t),console.log("返回结果:"+n)}}),n},remoteUrl:null},window.sliderCaptcha=function(t){var e=document.getElementById(t.id);return new i(e,"object"==typeof t&&t)},window.sliderCaptcha.Constructor=i,(t=i.prototype).init=function(){this.initDOM(),this.initImg(),this.bindEvents()},t.initDOM=function(){var t,i,o=function(t,e){var n=document.createElement(t);return n.className=e,n},s=function(t,e){var n=document.createElement("canvas");return n.width=t,n.height=e,n}(this.options.width-2,this.options.height),r=s.cloneNode(!0),a=o("div","ffm-sliderContainer"),d=o("i","ffm-refreshIcon "+this.options.repeatIcon),l=o("div","ffm-sliderMask"),c=o("div","ffm-sliderbg"),h=o("div","ffm-slider"),u=o("i","ffm-sliderIcon"),p=o("span","ffm-sliderText");r.className="ffm-block",p.innerHTML=this.options.barText,(t=this.$element).appendChild(s),t.appendChild(d),t.appendChild(r),h.appendChild(u),l.appendChild(h),a.appendChild(c),a.appendChild(l),a.appendChild(p),t.appendChild(a),i={canvas:s,block:r,sliderContainer:a,refreshIcon:d,slider:h,sliderMask:l,sliderIcon:u,text:p,canvasCtx:s.getContext("2d"),blockCtx:r.getContext("2d")},n(Object.assign)?Object.assign(this,i):e(this,i)},t.initImg=function(){var t,e=this,i=window.navigator.userAgent.indexOf("Trident")>-1,o=this.options.sliderL+2*this.options.sliderR+3,s=function(t,n){var o=e.options.sliderL,s=e.options.sliderR,r=e.options.PI,a=e.x,d=e.y;t.beginPath(),t.moveTo(a,d),t.arc(a+o/2,d-s+2,s,.72*r,2.26*r),t.lineTo(a+o,d),t.arc(a+o+s-2,d+o/2,s,1.21*r,2.78*r),t.lineTo(a+o,d+o),t.lineTo(a,d+o),t.arc(a+s-2,d+o/2,s+.4,2.76*r,1.24*r,!0),t.lineTo(a,d),t.lineWidth=2,t.fillStyle="rgba(255, 255, 255, 0.7)",t.strokeStyle="rgba(255, 255, 255, 0.7)",t.stroke(),t[n](),t.globalCompositeOperation=i?"xor":"destination-over"},r=function(t,e){return Math.round(Math.random()*(e-t)+t)},a=new Image;a.crossOrigin="Anonymous",t=0,a.onload=function(){e.x=r(o+10,e.options.width-(o+10)),e.y=r(10+2*e.options.sliderR,e.options.height-(o+10)),s(e.canvasCtx,"fill"),s(e.blockCtx,"clip"),e.canvasCtx.drawImage(a,0,0,e.options.width-2,e.options.height),e.blockCtx.drawImage(a,0,0,e.options.width-2,e.options.height);var t=e.y-2*e.options.sliderR-1,n=e.blockCtx.getImageData(e.x-3,t,o,o);e.block.width=o,e.blockCtx.putImageData(n,0,t+1),e.text.textContent=e.text.getAttribute("data-text")},a.onerror=function(){if(t++,"file:"===window.location.protocol&&(t=e.options.maxLoadCount,console.error("can't load pic resource file from File protocal. Please try http or https")),t>=e.options.maxLoadCount)return e.text.textContent="åŠ è½½å¤±è´¥",void e.classList.add("text-danger");a.src=e.options.localImages()},a.setSrc=function(){var o,s="";t=0,e.text.classList.remove("text-danger"),n(e.options.setSrc)&&(s=e.options.setSrc()),s&&""!==s||(s="https://picsum.photos/"+e.options.width+"/"+e.options.height+"/?image="+Math.round(20*Math.random())),i?((o=new XMLHttpRequest).onloadend=function(t){var e=new FileReader;e.readAsDataURL(t.target.response),e.onloadend=function(t){a.src=t.target.result}},o.open("GET",s),o.responseType="blob",o.send()):a.src=s},a.setSrc(),this.text.setAttribute("data-text",this.options.barText),this.text.textContent=this.options.loadingText,this.img=a},t.clean=function(){this.canvasCtx.clearRect(0,0,this.options.width,this.options.height),this.blockCtx.clearRect(0,0,this.options.width,this.options.height),this.block.width=this.options.width},t.bindEvents=function(){var t=this;this.$element.addEventListener("selectstart",(function(){return!1})),this.refreshIcon.addEventListener("click",(function(){t.text.textContent=t.options.barText,t.reset(),n(t.options.onRefresh)&&t.options.onRefresh.call(t.$element)}));var e,i,o=[],s=!1,r=function(n){t.text.classList.contains("text-danger")||(e=n.clientX||n.touches[0].clientX,i=n.clientY||n.touches[0].clientY,s=!0)},a=function(n){var r;if(!s)return!1;var a=n.clientX||n.touches[0].clientX,d=n.clientY||n.touches[0].clientY,l=a-e,c=d-i;if(l<0||l+40>t.options.width)return!1;t.slider.style.left=l-1+"px",r=(t.options.width-60)/(t.options.width-40)*l,t.block.style.left=r+"px",t.sliderContainer.classList.add("ffm-sliderContainer_active"),t.sliderMask.style.width=l+4+"px",o.push(Math.round(c))},d=function(i){var r;if(!s||(s=!1,(i.clientX||i.changedTouches[0].clientX)===e))return!1;t.sliderContainer.classList.remove("ffm-sliderContainer_active"),t.trail=o,(r=t.verify()).spliced&&r.verified?(t.sliderContainer.classList.add("ffm-sliderContainer_success"),n(t.options.onSuccess)&&t.options.onSuccess.call(t.$element)):(t.sliderContainer.classList.add("ffm-sliderContainer_fail"),n(t.options.onFail)&&t.options.onFail.call(t.$element),setTimeout((function(){t.text.innerHTML=t.options.failedText,t.reset()}),1e3))};this.slider.addEventListener("mousedown",r),this.slider.addEventListener("touchstart",r),document.addEventListener("mousemove",a),document.addEventListener("touchmove",a),document.addEventListener("mouseup",d),document.addEventListener("touchend",d),document.addEventListener("mousedown",(function(){return!1})),document.addEventListener("touchstart",(function(){return!1})),document.addEventListener("swipe",(function(){return!1}))},t.verify=function(){var t=this.trail,e=parseInt(this.block.style.left),n=!1;if(null!==this.options.remoteUrl)n=this.options.verify(t,this.options.remoteUrl);else{var i=function(t,e){return t+e},o=t.reduce(i)/t.length,s=t.map((function(t){return t-o}));n=0!==Math.sqrt(s.map((function(t){return t*t})).reduce(i)/t.length)}return{spliced:Math.abs(e-this.x)<this.options.offset,verified:n}},t.reset=function(){this.sliderContainer.classList.remove("ffm-sliderContainer_fail"),this.sliderContainer.classList.remove("ffm-sliderContainer_success"),this.slider.style.left=0,this.block.style.left=0,this.sliderMask.style.width=0,this.clean(),this.text.setAttribute("data-text",this.text.textContent),this.text.textContent=this.options.loadingText,this.img.setSrc()}}(),window.onload=function(){listenToSliderCaptchaCheckboxes()};