This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
jquery.nested-fields.js
242 lines (202 loc) · 6.36 KB
/
jquery.nested-fields.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(function($) {
/**
*
* Awesome Nested Fields
*
* Requires jquery-ujs adapter.
* https://github.com/lailsonbm/awesome_nested_fields
*
*/
var defaultSettings = {
beforeInsert: function(item, callback) { callback() },
afterInsert: function(item) {},
beforeRemove: function(item, callback) { callback() },
afterRemove: function(item) {},
itemTemplateSelector: '.item.template',
emptyTemplateSelector: '.empty.template',
containerSelector: '.items, .container',
itemSelector: '.item',
emptySelector: '.empty',
addSelector: '.add',
removeSelector: '.remove',
newItemIndex: 'new_nested_item',
unescapeTemplate: true
};
// PUBLIC API
var methods = {
init: function(options) {
return this.each(function() {
var $this = $(this);
if($this.data('nested-fields.options')) {
log('Nested fields already defined for this element. If you want to redefine options, destroy it and init again.');
return $this;
}
options = $.extend({}, defaultSettings, options);
options.itemTemplate = $(options.itemTemplateSelector, $this);
options.emptyTemplate = $(options.emptyTemplateSelector, $this);
options.container = $(options.containerSelector, $this);
options.add = $(options.addSelector, $this);
$this.data('nested-fields.options', options);
bindInsertToAdd(options);
bindRemoveToItems(options, $this);
});
},
insert: function(callback, options) {
options = $.extend({}, getOptions(this), options);
return insertItemWithCallbacks(callback, options);
},
remove: function(element, options) {
options = $.extend({}, getOptions(this), options);
return removeItemWithCallbacks(element, options);
},
removeAll: function(options) {
options = $.extend({}, getOptions(this), options);
$(methods.items.apply(this)).each(function(i, el) {
methods.remove(el, options);
});
},
items: function() {
return findItems(getOptions(this));
},
destroy: function() {
$(this).removeData('nested-fields.options');
$('*', this).unbind('.nested-fields');
}
};
$.fn.nestedFields = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.nestedFields');
}
};
// Initialization functions
function getOptions(element) {
var $element = $(element);
while($element.length > 0) {
var data = $element.data('nested-fields.options');
if(data) {
return data;
} else {
$element = $element.parent();
}
}
return null;
}
function bindInsertToAdd(options) {
options.add.bind('click.nested-fields', function(e) {
e.preventDefault();
insertItemWithCallbacks(null, options);
});
}
function bindRemoveToItems(options, $this) {
$(options.itemSelector, $this).each(function(i, item) {
bindRemoveToItem(item, options);
});
}
// Insertion functions
function prepareTemplate(options) {
var regexp = new RegExp(options.newItemIndex, 'g');
var newId = new Date().getTime();
var contents = options.itemTemplate.html();
if(options['unescapeTemplate']) {
contents = unescape_html_tags(contents);
}
var newItem = $(contents.replace(regexp, newId));
newItem.attr('data-new-record', true);
newItem.attr('data-record-id', newId);
bindRemoveToItem(newItem, options);
return newItem;
}
function insertItemWithCallbacks(onInsertCallback, options) {
var newItem = prepareTemplate(options);
function insert() {
if(onInsertCallback) {
onInsertCallback(newItem);
}
removeEmpty(options);
options.container.append(newItem);
}
if(!options.skipBefore) {
options.beforeInsert(newItem, insert);
if(options.beforeInsert.length <= 1) {
insert();
}
} else {
insert();
}
if(!options.skipAfter) {
options.afterInsert(newItem);
}
return newItem;
}
function removeEmpty(options) {
findEmpty(options).remove();
}
// Removal functions
function removeItemWithCallbacks(element, options) {
function remove() {
if($element.attr('data-new-record')) { // record is new
$element.remove();
} else { // record should be marked and sent to server
$element.find("INPUT[name$='[_destroy]']").val('true');
$element.hide();
}
insertEmpty(options);
}
var $element = $(element);
if(!options.skipBefore) {
options.beforeRemove($element, remove);
if(options.beforeRemove.length <= 1) {
remove();
}
} else {
remove();
}
if(!options.skipAfter) {
options.afterRemove($element);
}
return $element;
}
function insertEmpty(options) {
if(findItems(options).length === 0) {
var contents = options.emptyTemplate.html();
if(options['unescapeTemplate']) {
contents = unescape_html_tags(contents);
}
options.container.append(contents);
}
}
function bindRemoveToItem(item, options) {
var removeHandler = $(item).find(options.removeSelector);
var needsConfirmation = removeHandler.attr('data-confirm');
var event = needsConfirmation ? 'confirm:complete' : 'click';
removeHandler.bind(event + '.nested-fields', function(e, confirmed) {
e.preventDefault();
if(confirmed === undefined || confirmed === true) {
removeItemWithCallbacks(item, options);
}
return false;
});
}
// Find functions
function findItems(options) {
return options.container.find(options.itemSelector + ':visible');
}
function findEmpty(options) {
return options.container.find(options.emptySelector);
}
// Utility functions
function unescape_html_tags(html) {
var e = document.createElement('div');
e.innerHTML = html;
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}
function log(msg) {
if(console && console.log) {
console.log(msg);
}
}
})(jQuery);