UNPKG

pattern-lock-js

Version:

An android inspired patter lock in pure javascript

1 lines 3.74 kB
"use strict";function _defineProperty(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(e){var t=Function("return this")()||(0,eval)("this");"function"==typeof define&&define.amd?define(["jquery"],function(n){return e(n,t)}):"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=e(require("jquery"),t):t.PatternLock=e(t.jQuery,t)}(function(e,t){function n(){navigator.vibrate=navigator.vibrate||navigator.webkitVibrate||navigator.mozVibrate||navigator.msVibrate,navigator.vibrate&&t.navigator.vibrate(25)}function r(o,a){function s(){C.removeClass("error"),C.addClass("success")}function f(){C.removeClass("success"),C.addClass("error")}function v(){return parseInt(q.map(function(e){return L.index(e)+1}).join(""))}function l(){p(),w(F),F&&F.remove(),C.off(u,A);var e=a.onPattern.call(_,v());!0===e?s():!1===e&&f()}function d(){q=[],F=void 0,N=void 0,C.removeClass("success error"),M.empty(),O.empty()}function m(e){(e=e||t.event).preventDefault&&e.preventDefault(),e.returnValue=!1}function y(e){if(c[e.keyCode])return m(e),!1}function b(){t.addEventListener&&t.addEventListener("DOMMouseScroll",m,!1),t.onwheel=m,t.onmousewheel=document.onmousewheel=m,t.ontouchmove=m,document.onkeydown=y}function p(){t.removeEventListener&&t.removeEventListener("DOMMouseScroll",m,!1),t.onmousewheel=document.onmousewheel=null,t.onwheel=null,t.ontouchmove=null,document.onkeydown=null}function g(e){for(var t=0;t<q.length;t++)if(q[t]===e)return!0;return!1}function h(e){for(var t=0;t<L.length;t++)if(L[t]===e)return!0;return!1}function x(e){return function(t){if(t.preventDefault(),F===e){var n=E(t.target,t);return e.setAttribute("x2",n.x),e.setAttribute("y2",n.y),!1}}}function A(e,t){if(!t){var n=k(e),r=n.x,o=n.y;t=document.elementFromPoint(r,o)}t.getAttribute("cx"),t.getAttribute("cy");h(t)&&!g(t)&&(w(F,t),F=P(t))}function w(e,t){if(void 0!==e&&(N&&C.off("touchmove mousemove",N),void 0!==t)){var n=t.getAttribute("cx"),r=t.getAttribute("cy");e.setAttribute("x2",n),e.setAttribute("y2",r)}}function P(e){q.push(e);var t=e.getAttribute("cx"),r=e.getAttribute("cy"),o=j(t,r),i=S(t,r);return O.append(i),N=x(o),C.on("touchmove mousemove",N),M.append(o),a.vibrate&&n(),o}function S(e,t){var n=document.createElementNS(i,"circle");return n.setAttribute("cx",e),n.setAttribute("cy",t),n.setAttribute("r",6),n}function j(e,t,n,r){var o=document.createElementNS(i,"line");return o.setAttribute("x1",e),o.setAttribute("y1",t),void 0===n||void 0==r?(o.setAttribute("x2",e),o.setAttribute("y2",t)):(o.setAttribute("x2",n),o.setAttribute("y2",r)),o}function k(e){return{x:e.clientX||e.originalEvent.touches[0].clientX,y:e.clientY||e.originalEvent.touches[0].clientY}}function E(e,t){var n=k(t),r=n.x,o=n.y;return V.x=r,V.y=o,V.matrixTransform(e.getScreenCTM().inverse())}var C=e(o),_=this,D=C[0],L=C.find(".lock-dots circle"),M=C.find(".lock-lines"),O=C.find(".lock-actives"),V=D.createSVGPoint(),q=[],F=void 0,N=void 0;a=Object.assign(r.defaults,a||{}),C.on("touchstart mousedown",function(t){d(),t.preventDefault(),b(),C.on(u,A);var n="touchstart"==t.type?"touchend":"mouseup";e(document).one(n,function(e){l()})}),Object.assign(this,{clear:d,success:s,error:f,getPattern:v})}var o,i="http://www.w3.org/2000/svg",u="touchmove mousemove",c=(o={37:!0,38:!0,39:!0,40:!0,32:!0},_defineProperty(o,"38",!0),_defineProperty(o,34,!0),_defineProperty(o,35,!0),_defineProperty(o,36,!0),o);return r.defaults={onPattern:function(){},vibrate:!0},r});