]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/js/profiledetail.js
Extended profile - more work on getting complex fields to save
[quix0rs-gnu-social.git] / plugins / ExtendedProfile / js / profiledetail.js
1 var removeRow = function() {
2     var cnt = rowCount(this);
3     var table = $(this).closest('table');
4     console.log("row count = " + cnt);
5     if (cnt > 1) {
6         var target = $(this).closest('tr');
7         target.remove();
8         reorder(table);
9     }
10 };
11
12 var rowCount = function(row) {
13     var top = $(row).closest('table');
14     var trs = $(top).find('tr');
15     return trs.length - 1; // exclude th section header row
16 };
17
18 var reorder = function(table) {
19     var trs = $(table).find('tr').has('td');
20
21     $(trs).find('a').hide();
22
23     $(trs).each(function(i, tr) {
24         console.log("ROW " + i);
25         $(tr).find('a.remove_row').show();
26         replaceIndex(rowIndex(tr), i);
27     });
28
29     $(trs).last().find('a.add_row').show();
30
31     if (trs.length == 1) {
32         $(trs).find('a.remove_row').hide();
33     }
34
35 };
36
37 var rowIndex = function(elem) {
38     var idStr = $(elem).find('div').attr('id');
39     var id = idStr.match(/\d+/);
40     console.log("id = " + id);
41 };
42
43 var replaceIndex = function(elem, oldIndex, newIndex) {
44     $(elem).find('*').each(function() {
45         $.each(this.attributes, function(i, attrib) {
46             var regexp = /extprofile-.*-\d.*/;
47             var value = attrib.value;
48             var match = value.match(regexp);
49             if (match != null) {
50                 attrib.value = value.replace("-" + oldIndex, "-" + newIndex);
51                 console.log('match: oldIndex = ' + oldIndex + ' newIndex = ' + newIndex + ' name = ' + attrib.name + ' value = ' + attrib.value);
52             }
53         });
54     });
55 }
56
57 var resetRow = function(elem) {
58     $(elem).find('input').attr('value', '');
59     $(elem).find("select option[value='office']").attr("selected", true);
60 }
61
62 var addRow = function() {
63     var divId = $(this).closest('div').attr('id');
64     var index = divId.match(/\d+/);
65     console.log("Current row = " + index);
66     var tr = $(this).closest('tr');
67     var newtr = $(tr).clone();
68     var newIndex = parseInt(index) + 1;
69     replaceIndex(newtr, index, newIndex);
70     resetRow(newtr);
71     $(tr).after(newtr);
72     console.log("number of rows: " + rowCount(tr));
73     reorder($(this).closest('table'));
74 };
75
76 $(document).ready(
77
78 function() {
79     $('.add_row').live('click', addRow);
80     $('.remove_row').live('click', removeRow);
81 }
82
83 );