-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatternLock.js
207 lines (198 loc) · 8.22 KB
/
patternLock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*jslint browser: true*/
/*global $, jQuery*/
(function($) {
'use strict';
$.fn.patternLock = function(options) {
var started = false,
nums = [],
arrCoordinates = [],
patternClearTimeout = null,
isCanvas = (function() {
//function taken from http://stackoverflow.com/questions/2745432/best-way-to-detect-that-html5-canvas-is-not-supported
var elem = document.createElement('canvas');
return !!(elem.getContext && elem.getContext('2d'));
}()),
that = this;
var canvas, canvasContext, context;
var i, j, idCounter;
var defaults = function() {
return {
rows: 3,
columns: 3,
width: 250,
height: 250,
randomizeIds: false, // this should be used to randomizeId of td
isCircle: true, // this will be required to identify if holes are of shape circle or square
showPatternLine: true,
patternLineColor: '#000000',
fieldName: '',
valueSeparator:',',
valueArray:[],
centerCircle:false,
lineWidth: 4,
centerCircleSize: 10
};
};
//this is to keep from overriding our "defaults" object.
var opts = $.extend({}, defaults(), options);
//Initializing value array
if(opts.valueArray.length===0 || opts.valueArray.length !== opts.rows*opts.columns ){
for (i = 0; i < (opts.rows*opts.columns) ; i++) {
opts.valueArray[i] = i+1;
}
}
var content = '<div class="patternlock" style="width:' + opts.width + 'px;height:' + opts.height + 'px"><div class="insideWrapper">';
if(opts.fieldName != undefined && opts.fieldName !=='' && opts.fieldName != null){
content += '<input type="hidden" name="'+opts.fieldName+'">';
}
if(isCanvas===true && opts.showPatternLine===true) {
content += '<canvas class="patternLockCanvas" width="100%" height="100%;"></canvas>';
}
content += '<table class="tbl tbl1" cellspacing="25px">';
idCounter = 0;
for (i = 1; i <= opts.rows; i++) {
content = content + "<tr>";
for (j = 1; j <= opts.columns; j++) {
content = content + '<td data-value="' + opts.valueArray[idCounter++] + '">';
if(opts.centerCircle) {
content = content +'<div class="centerCircle" style="width:'+opts.centerCircleSize+'px;height:'+opts.centerCircleSize+'px"> </div>';
}
content = content +'</td>';
}
content = content + "</tr>";
}
content = content + '</table></div></div>';
this.append(content);
if(isCanvas===true && opts.showPatternLine===true) {
canvas = $('.patternLockCanvas', that)[0];
canvas.width = opts.width;
canvas.height = opts.width;
canvasContext = canvas.getContext('2d');
}
function isInsideCircle(x, y, r, left, top) {
return Math.sqrt(Math.pow(left - x, 2) + Math.pow(top - y, 2)) <= r;
}
function SimpleCircle(x, y, r) {
this.centerX = x;
this.centerY = y;
this.radius = r;
}
SimpleCircle.prototype = {
distanceTo: function(pageX, pageY) {
return Math.sqrt(Math.pow(pageX - this.centerX, 2) + Math.pow(pageY - this.centerY, 2));
},
includesXY: function(x, y) {
return this.distanceTo(x, y) <= this.radius;
}
};
function isMouseOverLockHoles(element, left, top) {
var offset = element.offset();
if (opts.isCircle === true) {
var radius = element.width() / 2;
// circle = new SimpleCircle(offset.left + radius, offset.top + radius, radius);
//TO-DO: are we going to support only circle of square as well
return isInsideCircle(offset.left + radius, offset.top + radius, radius, left, top);
// return circle.includesXY(left, top);
}
return top >= offset.top && left >= offset.left && left <= (offset.left + element[0].offsetWidth) && top <= (offset.top + element[0].offsetHeight);
}
function getCenter(ele) {
if(isCanvas===false || opts.showPatternLine===false) {
return;
}
var offset = $(ele).offset(),
width = $(ele).outerWidth(),
height = $(ele).outerHeight(),
centerX = offset.left + width / 2,
centerY = offset.top + height / 2,
rect = canvas.getBoundingClientRect();
centerX = centerX - rect.left;
centerY = centerY - rect.top;
return {
'x': centerX,
'y': centerY
};
}
function clearCanvas() {
if(isCanvas===false || opts.showPatternLine===false) {
return;
}
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
var w = canvas.width;
canvas.width = 1;
canvas.width = w;
}
function drawLine() {
if(isCanvas===false || opts.showPatternLine===false || arrCoordinates.length < 2) {
return;
}
var c = arrCoordinates;
i = c.length - 1;
canvasContext.lineWidth = opts.lineWidth;
canvasContext.beginPath();
canvasContext.moveTo(c[i - 1].x, c[i - 1].y);
canvasContext.lineTo(c[i].x, c[i].y);
canvasContext.strokeStyle = opts.patternLineColor;
canvasContext.stroke();
canvasContext.closePath();
}
function pattenDrawEnd() {
if (started === true) {
$('#pattern').text(nums.join(','));
if(opts.fieldName != undefined && opts.fieldName !=='' && opts.fieldName != null){
$('input[type=hidden][name='+opts.fieldName+']').val(nums.join(opts.valueSeparator));
}
started = false;
if (patternClearTimeout) {
clearTimeout(patternClearTimeout);
}
patternClearTimeout = setTimeout(function() {
$('.tbl td').removeClass('selected');
clearCanvas();
}, 1000);
}
}
$('.tbl').on('vmousemove', function(evt) {
evt.preventDefault();
context = $(this);
$('td', context).each(function() {
if (isMouseOverLockHoles($(this), evt.pageX, evt.pageY)) {
var num = $(this).attr('data-value'),
lastNum = nums[nums.length - 1];
if (started === true && lastNum !== num) {
arrCoordinates.push(getCenter(this));
drawLine();
$(this).addClass('selected');
nums.push($(this).attr('data-value'));
}
}
});
});
$('.tbl').on('vmouseup', function(evt) {
evt.preventDefault();
pattenDrawEnd();
});
$(document).on('vmouseup', function() {
pattenDrawEnd();
});
$('.tbl').on('vmousedown', function(evt) {
if (patternClearTimeout) {
clearTimeout(patternClearTimeout);
}
evt.preventDefault();
context = $(this);
$('td', context).each(function() {
if (isMouseOverLockHoles($(this), evt.pageX, evt.pageY)) {
started = true;
nums = [];
arrCoordinates = [];
$('td', context).removeClass('selected');
clearCanvas();
$(this).addClass('selected');
nums.push($(this).attr('data-value'));
arrCoordinates.push(getCenter(this));
}
});
});
};
}(jQuery));