From cc3df53059e92bac0ed491546e9c30f6adf0a4c6 Mon Sep 17 00:00:00 2001 From: Kaio Valdemar Date: Thu, 17 Oct 2019 11:23:01 -0300 Subject: [PATCH] SetPattern --- README.md | 1 + dist/patternlock.js | 78 +++++++++++++++++++++++++-------------- dist/patternlock.min.js | 2 +- docs/patternlock.js | 78 +++++++++++++++++++++++++-------------- patternlock.js | 81 ++++++++++++++++++++++++++--------------- 5 files changed, 154 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index d6de609..cb71c09 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ The returned object from `new PatternLock(...)` has the following utility functi * **`success()`** Validates the pattern as correct * **`error()`** Validates the pattern as incorrect * **`getPattern()`** Returnes the currently active pattern or `NaN` +* **`setPattern(array)`** Set a pattern based on a array The pattern lock constructor accepts a second argument - an object literal with the following properties: diff --git a/dist/patternlock.js b/dist/patternlock.js index a42e238..58d75ba 100644 --- a/dist/patternlock.js +++ b/dist/patternlock.js @@ -21,12 +21,10 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope global.PatternLock = factory(global.jQuery, global); } })(function ($, window) { - var _scrollKeys; - - var svgns = 'http://www.w3.org/2000/svg'; - var moveEvent = 'touchmove mousemove'; - - var scrollKeys = (_scrollKeys = { + var _scrollKeys, + svgns = 'http://www.w3.org/2000/svg', + moveEvent = 'touchmove mousemove', + scrollKeys = (_scrollKeys = { 37: true, // left 38: true, // up 39: true, // right @@ -41,16 +39,22 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope } function PatternLock(element, options) { - var svg = $(element); - var self = this; - var root = svg[0]; - var dots = svg.find('.lock-dots circle'); - var lines = svg.find('.lock-lines'); - var actives = svg.find('.lock-actives'); - var pt = root.createSVGPoint(); - var code = []; - var currentline = void 0; - var currenthandler = void 0; + var svg = $(element), + self = this, + root = svg[0], + dots = svg.find('.lock-dots circle'), + lines = svg.find('.lock-lines'), + actives = svg.find('.lock-actives'), + pt = root.createSVGPoint(), + code = [], + dotsMap = [], + currentline = void 0, + currenthandler = void 0, + i = 0; + + for(i=0; i dots.index(i)+1).join('')) } + function setPattern(arr){ + clear() + if (arr === undefined) return + let marker,line, dotActual, dotBefore = null + for(i = 0; i < arr.length ; i++ ){ + if(dotsMap[arr[i]] !== undefined){ + dotActual = dotsMap[arr[i]] + marker = createNewMarker(dotActual.x,dotActual.y) + actives.append(marker) + if(dotBefore != null){ + line = createNewLine(dotBefore.x, dotBefore.y, dotActual.x, dotActual.y) + lines.append(line) + } + if (options.vibrate) vibrate() + dotBefore = dotActual + } + } + } + function end() { enableScroll() stopTrack(currentline) @@ -139,7 +164,7 @@ } function isUsed(target) { - for (let i = 0; i < code.length; i++) { + for (i = 0; i < code.length; i++) { if (code[i] === target) { return true } @@ -148,7 +173,7 @@ } function isAvailable(target) { - for (let i = 0; i < dots.length; i++) { + for (i = 0; i < dots.length; i++) { if (dots[i] === target) { return true } @@ -172,8 +197,8 @@ let {x, y} = getMousePos(e) target = document.elementFromPoint(x, y); } - let cx = target.getAttribute('cx') - let cy = target.getAttribute('cy') + let cx = target.getAttribute('cx'), + cy = target.getAttribute('cy') if (isAvailable(target) && !isUsed(target)) { stopTrack(currentline, target) currentline = beginTrack(target) @@ -186,18 +211,18 @@ svg.off('touchmove mousemove', currenthandler) } if (target === undefined) return - let x = target.getAttribute('cx') - let y = target.getAttribute('cy') + let x = target.getAttribute('cx'), + y = target.getAttribute('cy') line.setAttribute('x2', x) line.setAttribute('y2', y) } function beginTrack(target) { code.push(target) - let x = target.getAttribute('cx') - let y = target.getAttribute('cy') - var line = createNewLine(x, y) - var marker = createNewMarker(x, y) + let x = target.getAttribute('cx'), + y = target.getAttribute('cy'), + line = createNewLine(x, y), + marker = createNewMarker(x, y) actives.append(marker) currenthandler = updateLine(line) svg.on('touchmove mousemove', currenthandler) @@ -207,7 +232,7 @@ } function createNewMarker(x, y) { - var marker = document.createElementNS(svgns, "circle") + let marker = document.createElementNS(svgns, "circle") marker.setAttribute('cx', x) marker.setAttribute('cy', y) marker.setAttribute('r', 6) @@ -215,7 +240,7 @@ } function createNewLine(x1, y1, x2, y2) { - var line = document.createElementNS(svgns, "line") + let line = document.createElementNS(svgns, "line") line.setAttribute('x1', x1) line.setAttribute('y1', y1) if (x2 === undefined || y2 == undefined) { @@ -242,12 +267,10 @@ } } - PatternLock.defaults = { onPattern: () => {}, vibrate: true, } - return PatternLock })); \ No newline at end of file