forked from pawnesh/family_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps-family.js
472 lines (440 loc) · 15.8 KB
/
ps-family.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
(function($) {
var rootDiv = '';
var treeGround = null;
var newMemberForm = '';
var memberName = '';
var memberGender = '';
var memberAge = '';
var memberPic = '';
var memberRelation = '';
var familyTree = new Array();
var memberId = 0;
var selectedMember = null;// refrence to selected member
var self = true;
var memberSpace = 92;
var memberWidth = 115;
var memberHeight = 107;
var memberDetails = null;
var options_menu = null;
var object = new Object();
var rut = null;
var parent = null;
$.fn.pk_family = function(options) {
if (rootDiv == null) {
// error message in console
jQuery.error('wrong id given');
return;
}
rootDiv = this;
init();
}
// function to create tree from json data
$.fn.pk_family_create = function(options) {
if (rootDiv == null) {
// error message in console
jQuery.error('wrong id given');
return;
}
rootDiv = this;
var settings = $.extend({
// These are the defaults.
data: "",
}, options);
var obj = jQuery.parseJSON(settings.data);
addBreadingGround();
parent = $('<ul>');
$(parent).appendTo(treeGround);
traverseObj(obj);
createNewMemberForm();
member_details();
createOptionsMenu();
document.oncontextmenu = function() {
return false;
};
}
function tempTest(obj) {
for (var i in obj) {
document.write(i + " ");
if (i.indexOf('a') > -1 && i.length == 2) {
;
} else {
tempTest(obj[i]);
}
}
return;
}
function traverseObj(obj) {
for (var i in obj) {
if (i.indexOf("li") > -1) {
var li = $('<li>');
$(li).appendTo(parent);
parent = li;
traverseObj(obj[i]);
parent = $(parent).parent();
}
if (i.indexOf("a") > -1 && i.length == 2) {
var link = $('<a>');
link.attr('data-name', obj[i].name);
link.attr('data-age', obj[i].age);
link.attr('data-gender', obj[i].gender);
link.attr('data-relation', obj[i].relation);
if(obj[i].relation == 'Spouse'){
link.attr('class', 'spouse');
}
var center = $('<center>').appendTo(link);
var pic = $('<img>').attr('src', obj[i].pic);
var extraData = "";
if (obj[i].gender == "Male") {
extraData = "(M)";
} else {
extraData = "(F)";
}
$(pic).appendTo(center);
$(center).append($('<br>'));
$('<span>').html(obj[i].name + " " + extraData).appendTo(center);
$(link).mousedown(function(event) {
if (event.button == 2) {
displayPopMenu(this, event);
return false;
}
return true;
});
$(link).appendTo(parent);
}
if (i.indexOf("ul") > -1) {
var ul = $('<ul>');
$(ul).appendTo(parent);
parent = ul;
traverseObj(obj[i]);
return;
}
}
return;
}
// function to send data to server
$.send_Family = $.fn.pk_family_send = function(options) {
if (rootDiv == null) {
// error message in console
jQuery.error('wrong id given');
return;
}
var settings = $.extend({
// These are the defaults.
url: "",
}, options);
var data = createSendURL();
data = data.replace(new RegExp(']', 'g'), "");
data = data.replace(new RegExp('\\[', 'g'), "");
console.log(data);
$.ajax({
url: settings.url + "?tree=" + data,
}).done(function() {
alert('completed');
});
}
function createSendURL() {
rut = $(treeGround).find("ul:first");
parent = object;
object = createJson(rut);
return (JSON.stringify(object));
}
function createJson(root) {
var thisObj = [];
if ($(root).prop('tagName') == "UL") {
var item = {};
var i = 0;
$(root).children('li').each(function() {
item["li" + i] = createJson(this);
i++;
});
thisObj.push(item);
return thisObj;
}
if ($(root).prop('tagName') == "LI") {
var item = {};
var i = 0;
$(root).children('a').each(function() {
var m = "a" + i;
item[m] = {};
item[m]["name"] = $(this).attr("data-name");
item[m]["age"] = $(this).attr("data-age");
item[m]["gender"] = $(this).attr("data-gender");
try {
item[m]["relation"] = $(this).attr("data-relation");
} catch (e) {
item[m]["relation"] = "self";
}
item[m]["pic"] = $(this).find('img:first').attr("src");
i++;
});
if ($(root).find('ul:first').length) {
parent = thisObj;
item["ul"] = createJson($(root).find('ul:first'));
}
thisObj.push(item);
return thisObj;
}
}
function init() {
// addMemberButton();
addBreadingGround();
createNewMemberForm();
member_details();
createOptionsMenu();
displayFirstForm();
document.oncontextmenu = function() {
return false;
};
}
function createOptionsMenu() {
var div = $('<div>').attr('id', 'pk-popmenu');
var ul = $('<ul>');
// add member button
var liAdd = $('<li>').html('Add Member').appendTo(ul);
liAdd.click(function(event) {
displayForm();
$(options_menu).css('display', 'none');
});
// view member button
var liDisplay = $('<li>').html('View Details').appendTo(ul);
liDisplay.click(function(event) {
displayData(selectedMember);
$(options_menu).css('display', 'none');
});
// remove member button
var liRemove = $('<li>').html('Remove Member').appendTo(ul);
liRemove.click(function(event) {
removeMember(selectedMember);
$(options_menu).css('display', 'none');
});
// cancel the pop menu
var liCancel = $('<li>').html('Cancel').appendTo(ul);
liCancel.click(function(event) {
//displayForm(this);
$(options_menu).css('display', 'none');
});
$(div).append(ul);
options_menu = div;
$(options_menu).appendTo(rootDiv);
}
function createNewMemberForm() {
var memberForm = $('<div>').attr('id', 'pk-memberForm');
var cross = $('<div>').attr('class', 'pk-cross');
$(cross).text('X');
$(cross).click(closeForm);
$(cross).appendTo(memberForm);
var table = $('<table>').appendTo(memberForm);
// name
$('<tr>').html('<td><label>Name</label></td><td><input type="text" value="" id="pk-name"/></td>').appendTo(table);
$('<tr>').html(' <td><label>Gender</label></td><td><select id="pk-gender"><option value="Male">Male</option><option value="Female">Female</option></select></td>').appendTo(table);
$('<tr>').html('<td><label>Age</label></td><td><input type="text" value="" id="pk-age"></td>').appendTo(table);
$('<tr>').html(' <td class="relations"><label>Relation</label></td><td class="relations"><select id="pk-relation">\n\\n\
<option value="Mother">Mother</option>\n\
<option value="Father">Father</option>\n\\n\
<option value="Sibling">Sibling</option>\n\\n\
<option value="Child">Child</option>\n\\n\
<option value="Spouse">Spouse</option>\n\\n\
</select></td>').appendTo(table);
$('<tr>').html('<td><label>Upload Photo</label></td><td><input type="file" id="pk-picture"></td>').appendTo(table);
var buttonSave = $('<input>').attr('type', 'button');
$(buttonSave).attr('value', 'Save');
$(buttonSave).click(saveForm);
var td = $('<td>').attr('colspan', '2');
td.css('text-align', 'center');
td.append(buttonSave);
$('<tr>').append(td).appendTo(table);
newMemberForm = memberForm;
$(newMemberForm).appendTo(rootDiv);
}
function member_details() {
memberDetails = $('<div>').attr('id', 'pk-member-details');
$(memberDetails).appendTo(rootDiv);
}
function closeForm() {
$(newMemberForm).css('display', 'none');
}
function saveForm() {
memberName = $('#pk-name').val();
memberGender = $('#pk-gender').val();
memberAge = $('#pk-age').val();
memberPic = $('#pk-picture');
memberRelation = $('#pk-relation').val();
//clear exsiting data from form
$('#pk-name').val('');
$('#pk-age').val('');
$('#pk-relation').val('');
// after saving
addMember();
closeForm();
}
function addBreadingGround() {
var member = $('<div>').attr('id', 'treeGround');
$(member).attr('class', 'tree-ground');
$(member).appendTo(rootDiv);
treeGround = member;
$(treeGround).draggable();
}
function addMemberButton() {
var member = $('<input>').attr('type', 'button');
$(member).attr('value', 'Add Member');
$(member).click(displayForm);
$(member).appendTo(rootDiv);
}
function displayForm(input) {
$('.relations').css('display', '');
$(newMemberForm).css('display', 'block');
}
function displayPopMenu(input, event) {
if ($(options_menu).css('display') == 'none') {
selectedMember = input;
self = false;
$(options_menu).css('display', 'block');
$(options_menu).css('top', event.clientY);
$(options_menu).css('left', event.clientX);
}
}
function displayFirstForm() {
selectedMember = null;
self = true;
$('.relations').css('display', 'none');
$(newMemberForm).css('display', 'block');
$('#pk-relation').val('Main');
}
function addMember() {
var aLink = $('<a>').attr('href', '#');
var center = $('<center>').appendTo(aLink);
var pic = $('<img>').attr('src', 'images/profile.png');
var extraData = "";
if (memberGender == "Male") {
extraData = "(M)";
} else {
extraData = "(F)";
$(pic).attr('src', 'images/profile-f.png');
}
$(pic).appendTo(center);
$(center).append($('<br>'));
$('<span>').html(memberName + " " + extraData).appendTo(center);
readImage(memberPic, pic);
var li = $('<li>').append(aLink);
$(aLink).attr('data-name', memberName);
$(aLink).attr('data-gender', memberGender);
$(aLink).attr('data-age', memberAge);
$(aLink).attr('data-relation', memberRelation);
$(aLink).mousedown(function(event) {
if (event.button == 2) {
displayPopMenu(this, event);
return false;
}
return true;
});
var sParent = $(selectedMember).parent(); // super parent
if (selectedMember != null) {
if (memberRelation == 'Mother') {
var parent = $(sParent).parent();
var parentParent = $(parent).parent();
var fatherElement = $(parentParent).find("a:first");
if(fatherElement.length == 0){
console.log('adding adajecent to father');
console.log(fatherElement);
var tmp = $(fatherElement).parent();
$(aLink).attr('class', 'mother');
$(tmp).append(aLink);
}else{
console.log('adding mother alone');
var ul = $('<ul>').append(li);
$(parent).appendTo(li);
$(parentParent).append(ul);
}
}
if (memberRelation == 'Spouse') {
$(aLink).attr('class', 'spouse');
var toPrepend = $(sParent).find('a:first');
$(sParent).prepend(aLink);
$(sParent).prepend(toPrepend);
}
if (memberRelation == 'Child') {
var toAddUL = $(sParent).find('UL:first');
if ($(toAddUL).prop('tagName') == 'UL') {
$(toAddUL).append(li);
} else {
var ul = $('<ul>').append(li);
$(sParent).append(ul);
}
}
if (memberRelation == 'Sibling') {
$(sParent).parent().append(li);
}
if (memberRelation == 'Father') {
var parent = $(sParent).parent();
var parentParent = $(parent).parent();
var ul = $('<ul>').append(li);
$(parent).appendTo(li);
$(parentParent).append(ul);
}
} else {
var ul = $('<ul>').append(li);
$(treeGround).append(ul);
}
}
// will show existing user info
function displayData(element) {
var innerContent = $('<table>');
var content = '';
var cross = $('<div>').attr('class', 'pk-cross');
$(cross).text('X');
$(cross).click(function() {
$(memberDetails).css('display', 'none')
});
$(memberDetails).empty();
$(cross).appendTo(memberDetails);
content = content + '<tr><td>Name</td><td>' + $(element).attr('data-name') + '</td></tr>';
content = content + '<tr><td>Age</td><td>' + $(element).attr('data-age') + '</td></tr>';
content = content + '<tr><td>Gender</td><td>' + $(element).attr('data-gender') + '</td></tr>';
if ($(element).attr('data-relation')) {
content = content + '<tr><td>Relation</td><td>' + $(element).attr('data-relation') + '</td></tr>';
} else {
content = content + '<tr><td>Relation</td><td>Self</td></tr>';
}
content = content + '<tr><td colspan="2" style="text-align:center;"><img src="' + $(element).find('img').attr('src') + '"/></td></tr>';
$(innerContent).html(content);
$(memberDetails).append(innerContent);
$(memberDetails).css('display', 'block');
}
function readImage(input, pic) {
var files = $(input).prop('files');
if (files && files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$(pic).attr('src', e.target.result);
}
reader.readAsDataURL(files[0]);
}
}
function removeMember(member) {
if ($(member).attr('data-relation') == 'Sibling') {
$(member).parent().remove();
}
if ($(member).attr('data-relation') == 'Child') {
var cLen = $(member).parent().children('li').length;
if (cLen > 1)
$(member).remove();
else {
$(member).parent().remove();
}
}
if ($(member).attr('data-relation') == 'Father') {
var child = $(member).children('ul');
var parent = $(member).parent().parent();
$(child).appendTo(parent);
$(member).remove();
}
if ($(member).attr('data-relation') == 'Spouse') {
$(member).remove();
}
}
}(jQuery));