-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeater.js
149 lines (126 loc) · 5.2 KB
/
repeater.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var repeater = {
config : {},
init : function(params) {
repeater.config = params;
repeater.on();
},
off : function() {
var containers = $(repeater.config.repeatableSelector);
var adds = containers.find(repeater.config.addSelector);
adds.off("click.Repeater");
var removes = containers.find(repeater.config.removeSelector);
removes.off("click.Repeater");
},
on : function() {
var containers = $(repeater.config.repeatableSelector);
var adds = containers.find(repeater.config.addSelector);
adds.on("click.Repeater", repeater.onAdd);
var removes = containers.find(repeater.config.removeSelector);
removes.each(function() {
var $this = $(this);
var nearest = $this.closest(repeater.config.repeatableSelector);
var sections = repeater._getDirectRepeatableSections(nearest);
if (sections.length === 1) {
$this.hide();
} else {
$this.show();
}
});
removes.on("click.Repeater", repeater.onRemove);
},
onRemove : function(event) {
event.preventDefault();
repeater.off();
var el = $(event.target);
var section = el.closest(repeater.config.repeatableSection);
var container = el.closest(repeater.config.repeatableSelector);
section.remove();
repeater._renumber(container);
repeater.on();
},
onAdd : function(event) {
event.preventDefault();
repeater.off();
var el = $(event.target);
var container = el.closest(repeater.config.repeatableSelector);
var sectionList = repeater._getDirectSectionList(container);
var repeatable = repeater._getDirectRepeatableSections(container);
var template = repeatable.last();
var newId = repeatable.length;
var indexRx = new RegExp(container.attr("data-index-rx"));
var newSection = repeater._newSection({
template: template,
newId : newId,
regex : indexRx
});
sectionList.append(newSection);
newSection.find(repeater.config.formControl).first().focus();
repeater.on();
},
_getDirectRepeatableSections : function(container) {
return container.find(repeater.config.repeatableSection).not(function(index) {
var nearest = $(this).closest(repeater.config.repeatableSelector);
return nearest[0] !== container[0];
});
},
_getDirectSectionList : function(container) {
return container.find(repeater.config.repeatableList).not(function(index) {
var nearest = $(this).closest(repeater.config.repeatableSelector);
return nearest[0] !== container[0];
});
},
_getDirectRepeatable : function(section) {
return section.find(repeater.config.repeatableSelector).not(function(index) {
var nearest = $(this).closest(repeater.config.repeatableSection);
return nearest[0] !== section[0];
});
},
_newSection : function(params) {
var newSection = params.template.clone();
repeater._pruneSubsections({parent: newSection});
repeater._updateSectionIndexes({section: newSection, newId : params.newId, regex: params.regex});
newSection.find(repeater.config.formControl).val("");
return newSection;
},
_pruneSubsections : function(params) {
var parent = params.parent;
var subs = repeater._getDirectRepeatable(parent);
subs.each(function() {
var sections = repeater._getDirectRepeatableSections($(this));
// remove all but the first section
for (var i = 1; i < sections.length; i++) {
$(sections[i]).remove();
}
// recurse into the remaining section
repeater._pruneSubsections({parent: $(sections[0])})
});
},
_updateSectionIndexes : function(params) {
var section = params.section;
section.find('label').each(function () {
var label = $(this);
var currentLabel = label.attr('for');
var newLabel = currentLabel.replace(params.regex, "$1" + params.newId + "$3");
label.attr('for', newLabel);
});
section.find(repeater.config.formControl).each(function () {
var formControl = $(this);
var currentId = formControl.attr('id');
var newId = currentId.replace(params.regex, "$1" + params.newId + "$3");
formControl.attr('id', newId);
formControl.attr("name", newId);
});
},
_renumber : function(container) {
var indexRx = new RegExp(container.attr("data-index-rx"));
var repeatable = repeater._getDirectRepeatableSections(container);
for (var i = 0; i < repeatable.length; i++) {
var section = $(repeatable[i]);
repeater._updateSectionIndexes({section: section, newId : i, regex: indexRx});
var subcontainers = section.find(repeater.config.repeatableSelector);
subcontainers.each(function() {
repeater._renumber($(this));
});
}
}
};