awesome_nested_fields 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -192,10 +192,10 @@ The code above inserts a new item and does not execute the `beforeInsert` callba
|
|
192
192
|
These methods can be called from the element where nested fields are applied (e.g. a form) or from any element inside it (e.g. an input or the container itself).
|
193
193
|
|
194
194
|
|
195
|
-
|
195
|
+
Multiple Nested Fields
|
196
196
|
-----------------------
|
197
197
|
|
198
|
-
It is easy to have multiple nested fields on the same page. Instead of applying `nestedFields()`
|
198
|
+
It is easy to have multiple nested fields on the same page. Instead of applying `nestedFields()` to the form, put the elements (items, container, add, remove) inside a wrapper and apply nested fields to it.
|
199
199
|
|
200
200
|
<!-- ERB Code -->
|
201
201
|
<h2>Phones</h2>
|
@@ -6,6 +6,16 @@ module AwesomeNestedFields
|
|
6
6
|
end
|
7
7
|
|
8
8
|
require 'awesome_nested_fields/version'
|
9
|
+
|
10
|
+
def self.escape_html_tags(html)
|
11
|
+
html.gsub(/[&><]/) do |char|
|
12
|
+
case char
|
13
|
+
when '<' then '<'
|
14
|
+
when '>' then '>'
|
15
|
+
when '&' then '&'
|
16
|
+
end
|
17
|
+
end.html_safe
|
18
|
+
end
|
9
19
|
end
|
10
20
|
|
11
21
|
require 'rails/form_helper'
|
data/lib/rails/form_helper.rb
CHANGED
@@ -9,6 +9,7 @@ ActionView::Helpers::FormBuilder.class_eval do
|
|
9
9
|
options[:empty_template_class] ||= ['template', 'empty', association.to_s.singularize].join(' ')
|
10
10
|
options[:show_empty] ||= false
|
11
11
|
options[:render_template] = options.key?(:render_template) ? options[:render_template] : true
|
12
|
+
options[:escape_template] = options.key?(:escape_template) ? options[:escape_template] : true
|
12
13
|
|
13
14
|
output = @template.capture { fields_for(association, &block) }
|
14
15
|
|
@@ -30,11 +31,15 @@ protected
|
|
30
31
|
|
31
32
|
def render_nested_fields_template(association, options, &block)
|
32
33
|
templates = @template.content_tag(:script, type: 'text/html', class: options[:item_template_class]) do
|
33
|
-
fields_for(association, options[:new_object], child_index: options[:new_item_index], &block)
|
34
|
+
template = fields_for(association, options[:new_object], child_index: options[:new_item_index], &block)
|
35
|
+
template = AwesomeNestedFields.escape_html_tags(template) if options[:escape_template]
|
36
|
+
template
|
34
37
|
end
|
35
38
|
|
36
39
|
if options[:show_empty]
|
37
|
-
|
40
|
+
template = @template.content_tag(:script, type: 'text/html', class: options[:empty_template_class], &block)
|
41
|
+
template = AwesomeNestedFields.escape_html_tags(template) if options[:escape_template]
|
42
|
+
templates << template
|
38
43
|
end
|
39
44
|
|
40
45
|
templates
|
@@ -20,7 +20,8 @@
|
|
20
20
|
emptySelector: '.empty',
|
21
21
|
addSelector: '.add',
|
22
22
|
removeSelector: '.remove',
|
23
|
-
newItemIndex: 'new_nested_item'
|
23
|
+
newItemIndex: 'new_nested_item',
|
24
|
+
unescapeTemplate: true
|
24
25
|
};
|
25
26
|
|
26
27
|
// PUBLIC API
|
@@ -29,10 +30,7 @@
|
|
29
30
|
return this.each(function() {
|
30
31
|
var $this = $(this);
|
31
32
|
if($(this).data('nested-fields.options')) {
|
32
|
-
|
33
|
-
return $this;
|
34
|
-
} else if(getOptions($this)) {
|
35
|
-
console.log('You cannot nest nested fields. Who would say that, uh?');
|
33
|
+
log('Nested fields already defined for this element. If you want to redefine options, destroy it and init again.');
|
36
34
|
return $this;
|
37
35
|
}
|
38
36
|
|
@@ -81,20 +79,20 @@
|
|
81
79
|
} else if (typeof method === 'object' || !method) {
|
82
80
|
return methods.init.apply(this, arguments);
|
83
81
|
} else {
|
84
|
-
$.error(
|
82
|
+
$.error('Method ' + method + ' does not exist on jQuery.nestedFields');
|
85
83
|
}
|
86
84
|
};
|
87
85
|
|
88
86
|
// Initialization functions
|
89
87
|
|
90
88
|
function getOptions(element) {
|
91
|
-
element = $(element);
|
92
|
-
while(element.length > 0) {
|
93
|
-
var data = element.data('nested-fields.options');
|
89
|
+
var $element = $(element);
|
90
|
+
while($element.length > 0) {
|
91
|
+
var data = $element.data('nested-fields.options');
|
94
92
|
if(data) {
|
95
93
|
return data;
|
96
94
|
} else {
|
97
|
-
element = element.parent();
|
95
|
+
$element = $element.parent();
|
98
96
|
}
|
99
97
|
}
|
100
98
|
return null;
|
@@ -120,6 +118,9 @@
|
|
120
118
|
var newId = new Date().getTime();
|
121
119
|
|
122
120
|
var contents = options.itemTemplate.html();
|
121
|
+
if(options['unescapeTemplate']) {
|
122
|
+
contents = unescape_html_tags(contents);
|
123
|
+
}
|
123
124
|
var newItem = $(contents.replace(regexp, newId));
|
124
125
|
newItem.attr('data-new-record', true);
|
125
126
|
newItem.attr('data-record-id', newId);
|
@@ -219,4 +220,18 @@
|
|
219
220
|
return options.container.find(options.emptySelector);
|
220
221
|
}
|
221
222
|
|
223
|
+
// Utility functions
|
224
|
+
|
225
|
+
function unescape_html_tags(html) {
|
226
|
+
var e = document.createElement('div');
|
227
|
+
e.innerHTML = html;
|
228
|
+
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
|
229
|
+
}
|
230
|
+
|
231
|
+
function log(msg) {
|
232
|
+
if(console && console.log) {
|
233
|
+
console.log(msg);
|
234
|
+
}
|
235
|
+
}
|
236
|
+
|
222
237
|
})(jQuery);
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_nested_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-07-
|
12
|
+
date: 2011-07-26 00:00:00.000000000 -03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
17
|
-
requirement: &
|
17
|
+
requirement: &2156094920 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 1.0.0
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2156094920
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rails
|
28
|
-
requirement: &
|
28
|
+
requirement: &2156094260 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: 3.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2156094260
|
37
37
|
description: Awesome dynamic nested fields for Rails and jQuery
|
38
38
|
email: lailson@guava.com.br
|
39
39
|
executables: []
|