]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/js/profiledetail.js
Extended profile - show and edit experience
[quix0rs-gnu-social.git] / plugins / ExtendedProfile / js / profiledetail.js
1 var reorder = function(class) {
2     console.log("QQQ Enter reorder");
3
4     var divs = $.find('div[class=' + class + ']');
5     console.log('divs length = ' + divs.length);
6
7     $(divs).find('a').hide();
8
9     $(divs).each(function(i, div) {
10         console.log("ROW " + i);
11         $(div).find('a.remove_row').show();
12         replaceIndex(rowIndex(div), i);
13     });
14
15     $(divs).last().find('a.add_row').show();
16
17     if (divs.length == 1) {
18         $(divs).find('a.remove_row').hide();
19     }
20
21 };
22
23 var rowIndex = function(div) {
24     var idstr = $(div).attr('id');
25     var id = idstr.match(/\d+/);
26     console.log("id = " + id);
27     return id;
28 };
29
30 var rowCount = function(class) {
31     var divs = $.find('div[class=' + class + ']');
32     return divs.length;
33 };
34
35 var replaceIndex = function(elem, oldIndex, newIndex) {
36     $(elem).find('*').each(function() {
37         $.each(this.attributes, function(i, attrib) {
38             var regexp = /extprofile-.*-\d.*/;
39             var value = attrib.value;
40             var match = value.match(regexp);
41             if (match !== null) {
42                 attrib.value = value.replace("-" + oldIndex, "-" + newIndex);
43                 console.log('match: oldIndex = ' + oldIndex + ' newIndex = ' + newIndex + ' name = ' + attrib.name + ' value = ' + attrib.value);
44             }
45         });
46     });
47 }
48
49 var resetRow = function(elem) {
50     $(elem).find('input').attr('value', '');
51     $(elem).find("select option[value='office']").attr("selected", true);
52 }
53
54 var addRow = function() {
55     var div = $(this).closest('div');
56     var id = $(div).attr('id');
57     var class = $(div).attr('class');
58     var index = id.match(/\d+/);
59     console.log("Current row = " + index + ', class = ' + class);
60     var tr = $(this).closest('tr');
61     var newtr = $(tr).clone();
62     var newIndex = parseInt(index) + 1;
63     replaceIndex(newtr, index, newIndex);
64     resetRow(newtr);
65     $(tr).after(newtr);
66     reorder(class);
67 };
68
69 var removeRow = function() {
70     var div = $(this).closest('div');
71     var id = $(div).attr('id');
72     var class = $(div).attr('class');
73
74     cnt = rowCount(class);
75     console.debug("removeRow - cnt = " + cnt);
76     if (cnt > 1) {
77         var target = $(this).closest('tr');
78         target.remove();
79         reorder(class);
80     }
81 };
82
83 var init = function() {
84     reorder('phone-edit');
85     reorder('experience-edit');
86 }
87
88 $(document).ready(
89
90 function() {
91     init();
92     $('.add_row').live('click', addRow);
93     $('.remove_row').live('click', removeRow);
94 }
95
96 );