-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget.js
More file actions
226 lines (209 loc) · 8.6 KB
/
target.js
File metadata and controls
226 lines (209 loc) · 8.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
;(function(context) {
var target = {
global: function() {
this.validate = {};
this.results = {};
this.groups = {};
},
validate_none: function(key, value, options) {
return true;
},
validate_present: function(key, value, options) {
return (typeof value !== "undefined");
},
validate_present_if: function(key, value, options, properties, validate_keys) {
return ($.inArray(options["if_key"], Object.keys(properties)) == -1) || ($.inArray(properties[options["if_key"]], options["if_value"]) == -1) || this.validate_present(key,value,options);
},
validate_check: function(key, value, options) {
return $.inArray(value, ["yes","no",undefined]) != -1;
},
validate_capitalization: function(key, value, options) {
var test = true;
if (value.indexOf("_") != -1) {
test = false;
}
var words = value.split(" ");
var lowers = ["and","of"];
$.each( words , function( index, word ) {
if (lowers.indexOf(word) == -1 && word.charAt(0) != word.charAt(0).toUpperCase()) {
test = false;
}
if (word.indexOf(".") != -1 && word.indexOf(".") != (word.length - 1)) {
test = false;
}
});
return test;
},
validate_numeric: function(key, value, options) {
if (typeof value !== "undefined") {
return $.isNumeric(value);
}
return false;
},
validate_numeric_if: function(key, value, options, properties, validate_keys) {
return ($.inArray(options["if_key"], Object.keys(properties)) == -1) || ($.inArray(properties[options["if_key"]], options["if_value"]) == -1) || this.validate_numeric(key,value,options);
},
validate_allowed_list: function(key, value, options) {
return $.inArray(value,options["allowed_list"]) != -1;
},
validate_allowed_list_or_null: function(key,value, options) {
return (typeof value == "undefined") || (this.validate_allowed_list(key,value,options));
},
validate_allowed_list_if: function(key, value, options, properties, validate_keys) {
return ($.inArray(options["if_key"], Object.keys(properties)) == -1) || ($.inArray(properties[options["if_key"]], options["if_value"]) == -1) || this.validate_allowed_list(key,value,options);
},
validate_yes_no: function(key, value, options) {
return $.inArray(value,["yes","no"]) != -1;
},
validate_yes_no_null: function(key, value, options) {
return (typeof value == "undefined") || (this.validate_yes_no(key, value, options));
},
validate_multi_allowed_list: function(key, value, options) {
var test = true;
if (typeof value == "undefined") {
return false;
}
var values = value.split(',');
$.each( values, function( index, v) {
if ($.inArray(v,options["allowed_list"]) == -1) {
test = false;
}
});
return test;
},
validate_date_after: function(key, value, options) {
return value > options['date_after'];
},
validate_address: function(key, value, options) {
return false;
},
validate_url: function(key, value, options) {
var pattern = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/
return pattern.test(value);
},
validate_unallowed: function(key, value, options, properties, validate_keys) {
var unallowed = [];
Object.keys(properties).forEach(function(prop) {
if (! validate_keys.includes(prop)) {
unallowed.push(prop);
}
});
if (unallowed.length > 0) {
return false;
} else {
return true;
}
},
filter_unallowed: function(key, value, properties, validate_keys) {
var unallowed = [];
Object.keys(properties).forEach(function(prop) {
if (! validate_keys.includes(prop)) {
unallowed.push(prop);
}
});
return unallowed.join(", ");
},
filter_date_year: function(key, value) {
return value.substr(0,10);
},
filter_id: function(key, value) {
return "<a href='http://www.openstreetmap.org/node/" + value + "'>" + value + "</a>";
},
// fix toggle group to work based off of state of all buttons, and if not present in any group, or present in any activated group, then show.
toggle_group: function(e) {
$(e.target).toggleClass('pure-button-active');
var active = $('.pure-button-active');
var not_active = $('.pure-button:not(".pure-button-active")');
$.each( $($('#target-head').children()[0]).children() , function(index, th) {
var tag = $(th).children()[0].innerHTML;
var visible = true;
not_active.each(function (group) {
if ($.inArray(tag, target.groups[ not_active[group].innerHTML ]) != -1) {
visible = false;
}
});
active.each(function (group) {
if ($.inArray(tag, target.groups[ active[group].innerHTML ]) != -1) {
visible = true;
}
});
if (visible) {
$('td:nth-child(' + (index+1) + '),th:nth-child(' + (index+1) + ')').show();
} else {
$('td:nth-child(' + (index+1) + '),th:nth-child(' + (index+1) + ')').hide();
}
});
},
set_validation: function(setting) {
this.validate = setting;
var header = "<tr>";
$.each( target.validate , function (key, params) {
header += "<th><span>" + key + "</span></th>";
target.results[ key ] = { "valid" : 0, "invalid" : 0 };
});
header += "</tr>";
$( header ).appendTo("#target-head");
var group_control = "<div>Filters: ";
$.each( target.validate, function (key, params) {
if (params['group'] == undefined) return;
var groups;
if (! Array.isArray(params['group'])) {
groups = [params['group']];
} else {
groups = params['group']
}
for (var group in groups) {
if (target.groups[ groups[group] ] === undefined) {
target.groups[ groups[group] ] = [];
group_control += "<span class='groupBtn pure-button pure-button-active'>" + groups[group] + "</span> ";
}
target.groups[ groups[group] ].push( key );
}
});
group_control += "</div>";
$( group_control ).appendTo('#controls');
$(".groupBtn").click(target.toggle_group);
},
add_results: function() {
var result_row = "<tr>";
$.each( target.validate, function (key, params) {
var total = target.results[ key ][ 'valid' ] + target.results[ key ][ 'invalid' ];
result_row += "<th>" + (100 * target.results[ key ][ 'valid' ] / total).toPrecision(4) + "%</th>";
});
result_row += "</tr>";
$( result_row ).appendTo("#target-head");
},
load_geojson: function(url) {
$.getJSON( url, function( data ) {
var items = [];
$.each( data['features'], function( i, feature ) {
var item = "<tr>";
$.each( target.validate , function (key, params) {
//Validations
var result = true;
$.each( params['validation'], function(j, validation_callback) {
//console.log(validation_callback);
result = result && (target[ validation_callback ])( key, feature['properties'][key], params['options'], feature['properties'], Object.keys(target.validate) );
});
var result_class = (result ? "valid" : "invalid");
//Filters
var filtered_item = (feature['properties'][key] !== undefined ? feature['properties'][key] : "");
if (params['filter'] !== undefined) {
filtered_item = target[ params['filter'][0] ]( key, feature['properties'][key], feature['properties'], Object.keys(target.validate) );
}
item += "<td class='" + result_class + "'>" + filtered_item + "</td>";
target.results[ key ][result_class]++;
});
item += "</tr>";
items.push(item);
});
$( items.join( "" ) ).appendTo( "#table-listing" );
target.add_results();
$('#target').tablesorter( { debug: true } );
$("#target").tableHeadFixer({'left' : 1, 'head': true});
$(".groupBtn").trigger('click');
});
}
};
window.target = target;
})(window);