-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
226 lines (154 loc) · 4.46 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
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 () {
const options = $( '.option' );
const ranks = $( '.rank' );
const get_result_button = $( '#get_result_button' );
const arrow = $( '.arrow' );
const checkbox = $( '.checkbox' );
const root = $( '#root' );
const payload_field = $( '#payload_field' );
let user_ip = '';
$('input[name="format"]').on('change',(event)=>{
const sections = $('section');
if(event.target.value === 'wizard')
sections.hide();
else
sections.show();
});
// Sending results
get_result_button.click( function () {
//Get tree
const tree = get_children(root);
//Get ranks
const ranks_values = [];
$.each(ranks,function(key,element){
const el = $(element);
let rank = el.attr('id');
rank = rank.replace('rank_','');
if(el.is(':checked'))
ranks_values.push(rank);
});
//Get options
const options_values = [];
$.each(options,function(key,element){
options_values.push($(element).is( ':checked' ))
});
//Send data
const exportType = $('input[name="format"]:checked')[0].value;
const payload = JSON.stringify([tree,ranks_values,exportType,options_values,user_ip]);
payload_field.attr('value',payload);
} );
function get_children(element){
let tree = {};
const children = element.find('> li');
$.each(children,function(key,el){
const child = $(el);
const child_name = child.attr('data-name');
const ul = child.find('> ul');
//if(child_name.substr(0,4)==='(no ')
if(child_name==='incertae sedis')
tree = {...tree, ...get_children(ul)};
else if(child.hasClass('mixed')){
if(ul.length===1)
tree[child_name] = get_children(ul);
}
else if(child.hasClass('checked'))
tree[child_name] = 'true';
});
return tree;
}
// Folding
arrow.click( function () {
const el = $( this );
const list = el.parent().find( '> ul' );
el.toggleClass( 'rotated' );
list.toggleClass( 'collapsed' );
} );
// Checkboxes
checkbox.click( function(){
const el = $(this);
const li = el.parent();
const children = li.find( 'li' );
const is_mixed = li.hasClass( 'mixed' );
const is_checked = li.hasClass( 'checked' );
if(is_mixed || !is_checked){
li.addClass('checked').removeClass('mixed');
children.addClass('checked').removeClass('mixed');
}
else {
li.removeClass('checked').removeClass('mixed');
children.removeClass('checked').removeClass('mixed');
}
notify_parent(li);
} );
function notify_parent(caller){
const el = caller.parent();
const li = el.parent();
if(!li.is('li'))
return;//parent is already root
const children = el.find('li');
const was_checked = li.hasClass('checked');
const was_mixed = li.hasClass('mixed');
let all_unchecked = true;
let all_checked = true;
$.each(children,function(key,element){
const el = $(element);
if(el.hasClass('mixed')){
all_unchecked = false;
all_checked = false;
return false;
}
if(el.hasClass('checked'))
all_unchecked = false;
else
all_checked = false;
});
let is_checked = false;
let is_mixed = false;
if(all_unchecked && all_checked)//there are 0 children
return;
if(all_unchecked)//make unchecked
li.removeClass('checked mixed');
else if(all_checked){//make checked
li.addClass( 'checked' ).removeClass( 'mixed' );
is_checked = true;
}
else {//make mixed
li.addClass('mixed').removeClass('checked');
is_mixed = true;
}
if(was_checked!==is_checked || was_mixed!==is_mixed)//notify parent if made any changes
notify_parent(li);
}
// Ranks
const defined_ranks = [];//get a list of spans
$.each(ranks,function(key,element){
const el = $(element);
const rank_name = el.attr('id');
defined_ranks[rank_name] = $('.'+rank_name);
});
function shadow_rank(rank){//update span's opacity
const el = $(rank);
const rank_name = el.attr('id');
if(el.is(':checked'))
defined_ranks[rank_name].removeClass('shadow');
else
defined_ranks[rank_name].addClass('shadow');
}
$.each(ranks,function(key,rank){//update opacity of all span's
shadow_rank(rank);
});
ranks.change(function(){//set a listener for a checkbox change
shadow_rank($(this));
});
//Get user IP
const xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4 && xmlHttp.status === 200){
const response_json = xmlHttp.responseText;
const response = JSON.parse(response_json);
user_ip = response.ip;
}
}
xmlHttp.open("GET", "https://api.ipify.org?format=json", true);
xmlHttp.send(null);
} );