affiliates-field-choice.js
2.87 KB
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
/**
* affiliates-field-choice.js
*
* Copyright (c) 2010 - 2015 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package affiliates
* @since affiliates 2.8.0
*/
jQuery(document).ready(function(){
var aff_reg_field_count = jQuery('#registration-fields > table > tbody tr').length;
jQuery("#registration-fields").on('click','button.field-add',function(event){
event.stopPropagation();
var i = aff_reg_field_count++,
row =
'<tr>' +
'<td>' +
'<input type="checkbox" name="field-enabled['+i+']" checked="checked" />' +
'</td>' +
'<td>' +
'<input type="text" name="field-name['+i+']" value="" />' +
'</td>' +
'<td>' +
'<input type="text" name="field-label['+i+']" value="" />' +
'</td>' +
'<td>' +
'<input type="checkbox" name="field-required['+i+']" />' +
'</td>' +
'<td>' +
'<input type="hidden" name="field-type['+i+']" value="text" />' +
'<button class="field-remove" type="button" value="'+i+'">Remove</button>' + // @todo l8n
'</td>' +
'</tr>';
jQuery('#registration-fields > table > tbody').append(row);
});
jQuery("#registration-fields").on('click','button.field-remove',function(event){
event.stopPropagation();
jQuery(event.target).parent().parent().remove();
});
jQuery("#registration-fields").on('click','button.field-up,button.field-down',function(event){
event.stopPropagation();
var row = jQuery(this).parents("tr:first");
if (jQuery(this).is(".field-up")) {
var prev = row.prev();
row.insertBefore(prev);
row.find('input').each(function(){
var name = jQuery(this).attr('name');
var newName = name.replace(/\d+/, function(match,offset,string){
return parseInt(match)-1;
});
jQuery(this).attr('name',newName);
});
prev.find('input').each(function(){
var name = jQuery(this).attr('name');
var newName = name.replace(/\d+/, function(match,offset,string){
return parseInt(match)+1;
});
jQuery(this).attr('name',newName);
});
} else {
var next = row.next();
row.insertAfter(next);
row.find('input').each(function(){
var name = jQuery(this).attr('name');
var newName = name.replace(/\d+/, function(match,offset,string){
return parseInt(match)+1;
});
jQuery(this).attr('name',newName);
});
next.find('input').each(function(){
var name = jQuery(this).attr('name');
var newName = name.replace(/\d+/, function(match,offset,string){
return parseInt(match)-1;
});
jQuery(this).attr('name',newName);
});
}
});
});