]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/js/profiledetail.js
Extended profile - add fancy datepicker widgets
[quix0rs-gnu-social.git] / plugins / ExtendedProfile / js / profiledetail.js
1 var SN_EXTENDED = SN_EXTENDED || {};
2
3 SN_EXTENDED.reorder = function(cls) {
4
5     var divs = $('div[class=' + cls + ']');
6
7     $(divs).each(function(i, div) {
8         $(div).find('a.remove_row').show();
9         SN_EXTENDED.replaceIndex(SN_EXTENDED.rowIndex(div), i);
10     });
11
12     var lastDiv = $(divs).last().closest('tr');
13     lastDiv.addClass('supersizeme');
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 SN_EXTENDED.rowIndex = function(div) {
23     var idstr = $(div).attr('id');
24     var id = idstr.match(/\d+/);
25     return id;
26 };
27
28 SN_EXTENDED.rowCount = function(cls) {
29     var divs = $.find('div[class=' + cls + ']');
30     return divs.length;
31 };
32
33 SN_EXTENDED.replaceIndex = function(elem, oldIndex, newIndex) {
34     $(elem).find('*').each(function() {
35         $.each(this.attributes, function(i, attrib) {
36             var regexp = /extprofile-.*-\d.*/;
37             var value = attrib.value;
38             var match = value.match(regexp);
39             if (match !== null) {
40                 attrib.value = value.replace("-" + oldIndex, "-" + newIndex);
41             }
42         });
43     });
44 }
45
46 SN_EXTENDED.resetRow = function(elem) {
47     $(elem).find('input').attr('value', '');
48     $(elem).find("select option[value='office']").attr("selected", true);
49 };
50
51 SN_EXTENDED.addRow = function() {
52     var div = $(this).closest('div');
53     var id = div.attr('id');
54     var cls = div.attr('class');
55     var index = id.match(/\d+/);
56     var newIndex = parseInt(index) + 1;
57     var newtr = $(div).closest('tr').clone();
58     SN_EXTENDED.replaceIndex(newtr, index, newIndex);
59     $(newtr).removeClass('supersizeme');
60     SN_EXTENDED.resetRow(newtr);
61     $(div).closest('tr').after(newtr);
62     SN_EXTENDED.reorder(cls);
63 };
64
65 SN_EXTENDED.removeRow = function() {
66     var div = $(this).closest('div');
67     var id = $(div).attr('id');
68     var cls = $(div).attr('class');
69
70     var cnt = SN_EXTENDED.rowCount(cls);
71     if (cnt > 1) {
72         var target = $(this).closest('tr');
73         target.remove();
74         SN_EXTENDED.reorder(cls);
75     }
76 };
77
78 $(document).ready(function() {
79
80     $("input#extprofile-manager").autocomplete({
81         source: 'finduser',
82         minLength: 2 });
83
84     $.datepicker.formatDate('yy-mm-dd');
85
86         $("input[name$=-start], input[name$=-end], #extprofile-birthday").datepicker({ dateFormat: 'd M yy' });
87
88     var multifields = ["phone-item", "experience-item", "education-item", "im-item"];
89
90     for (f in multifields) {
91         SN_EXTENDED.reorder(multifields[f]);
92     }
93
94     $("input#extprofile-manager").autocomplete({
95         source: 'finduser',
96         minLength: 2 });
97
98     $('.add_row').live('click', SN_EXTENDED.addRow);
99     $('.remove_row').live('click', SN_EXTENDED.removeRow);
100
101 });