forked from errbit/errbit
/
form.js
98 lines (83 loc) · 3.4 KB
/
form.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
$(function(){
activateNestedForms();
activateCheckboxHooks();
if($('div.watcher.nested').length)
activateTypeSelector('watcher');
if($('div.issue_tracker.nested').length)
activateTypeSelector('issue_tracker', 'tracker_params');
});
function activateNestedForms() {
$('.nested-wrapper').each(function(){
var wrapper = $(this);
makeNestedItemsDestroyable(wrapper);
var addLink = $('<a/>').text('add another').addClass('add-nested');
addLink.click(appendNestedItem);
wrapper.append(addLink);
});
$('.nested a.remove-nested').live('click',removeNestedItem);
}
function makeNestedItemsDestroyable(wrapper) {
wrapper.find('.nested').each(function(){
var nestedItem = $(this);
var destroyLink = $('<a/>').text('remove').addClass('remove-nested');
destroyLink.css('float','right');
nestedItem.find('label').first().before(destroyLink);
})
}
function appendNestedItem() {
var addLink = $(this);
var nestedItem = addLink.parent().find('.nested').first().clone().show();
var timestamp = new Date();
timestamp = timestamp.valueOf();
nestedItem.find('input, select').each(function(){
var input = $(this);
input.attr('id', input.attr('id').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
input.attr('name', input.attr('name').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
if(input.attr('type') != 'radio')
input.val('');
});
nestedItem.find('label').each(function(){
var label = $(this);
label.attr('for', label.attr('for').replace(/([_\[])\d+([\]_])/,'$1'+timestamp+'$2'));
});
addLink.before(nestedItem);
}
function removeNestedItem() {
var destroyLink = $(this);
var nestedItem = destroyLink.closest('.nested');
var inputNameExample = nestedItem.find('input').first().attr('name');
var idFieldName = inputNameExample.replace(/\[[^\]]*\]$/,'[id]');
if($("input[name='"+idFieldName+"']").length) {
var destroyFlagName = inputNameExample.replace(/\[[^\]]*\]$/,'[_destroy]')
var destroyFlag = $('<input/>').attr('name',destroyFlagName).attr('type','hidden').val('true');
$("input[name='"+idFieldName+"']").after(destroyFlag);
}
nestedItem.hide();
}
function activateTypeSelector(field_class, section_class) {
var section_class = section_class || field_class+"_params"; // section_class can be deduced if not given
// disable all inactive tabs to avoid sending its values on server
$('div.'+field_class+' > div.'+section_class).not('.chosen').find('input')
.attr('disabled','disabled').val('');
$('div.'+field_class+' input[name*='+field_class+'_type]').live('click', function(){
var chosen = $(this).val();
var wrapper = $(this).closest('.nested');
wrapper.find('div.chosen.'+section_class).removeClass('chosen').find('input').attr('disabled','disabled');
wrapper.find('div.'+section_class+'.'+chosen).addClass('chosen').find('input').removeAttr('disabled');
});
}
function activateCheckboxHooks() {
// Hooks to hide/show content when a checkbox is clicked
$('input[type="checkbox"][data-hide-when-checked]').each(function(){
$(this).change(function(){
el = $($(this).data('hide-when-checked'));
$(this).attr('checked') ? el.hide() : el.show();
});
});
$('input[type="checkbox"][data-show-when-checked]').each(function(){
$(this).change(function(){
el = $($(this).data('show-when-checked'));
$(this).attr('checked') ? el.show() : el.hide();
});
});
}