-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
86 lines (67 loc) · 2.12 KB
/
main.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
(function () {
var addRow = function ( ){
var id = randId();
$("#row_container").append( $("#row_template").html().replace(/%id%/g , id) );
return id;
} , randId = function () {
var str = '' , clist = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" , l = 5;
for(var i=0;i<5;i++)
str += clist[ Math.floor( Math.random() * (clist.length-1) ) ];
return str;
},
coords = function (a) {
if(!a)
return {};
a = a.split(",");
if(!a || !a[0] || !a[1])
return {};
return { x : parseFloat(a[0].trim()) , y : parseFloat(a[1].trim() ) };
},token = $("#token").val() , pathToStr = function (p) {
var str = '';
p.forEach(function (o) {
str += "(" + o.x + "," + o.y + "),";
});
return str.substr( 0 , str.length-1 );
};
(function () {
var i = 0;
try {
var paths = <?php echo json_encode($_SESSION['paths']); ?>;
paths.forEach(function (p) {
$("#" + addRow()).val( pathToStr(p) );
i++;
});
}
catch (e) {
console.log(e);
for(;i<4;i++)
addRow(); //Add 4 Paths at first
}
})();
$("#addrow").click(addRow);
$("#mainform").submit(function (e) {
e.preventDefault();
var data = { action : "getPath" , paths : [] , token : token };
$("#row_container").find("[name='row[]']").filter(function () {
var v = this.value.replace(/\s/g,'').match(/\((\d+(\.\d+)?),(\d+(\.\d+)?)\)/g); //matches all (x,y)
if(v) {
v.forEach(function (value,index) {
v[index] = coords( value.replace(/^\(|\)$/,'') ); //remove parenthesis
});
data.paths.push(v);
}
});
$("#response").html("<div class='alert alert-info' >Loading....</div>");
$.post( window.location.href , data , function (r) {
token = r.match(/^[A-Za-z0-9]+/i);
console.log(r,token);
if(!token)
return $("#response").html(r);
r = r.replace(token,'').replace("|",'');
token = token[0];
$("#response").html( r );
}).error(function (e) {
$("#response").html("<div class='alert alert-danger' >Please check your internet connection..</div>");
});
});
})();