|
1 | 1 | (function($) { |
| 2 | + // Form API, draft version: |
| 3 | + // $(".task_list").addFormField("tasks") |
| 4 | + // $(".task_list").removeFormField("tasks", 2) // destroy second field |
| 5 | + |
2 | 6 | var activeFormHelpers = { |
3 | 7 | generateResourceId: function() { |
4 | 8 | return new Date().getTime(); |
5 | | - }, |
6 | | - decodeHtml: function(input){ |
7 | | - var e = document.createElement('div'); |
8 | | - e.innerHTML = input; |
9 | | - return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue; |
10 | | - }, |
| 9 | + } |
11 | 10 | }; |
12 | 11 |
|
13 | | - $.fn.removeFormField = function(nodeToDelete) { |
14 | | - // maybe nodeToDelete could be field index |
| 12 | + $.fn.removeFormField = function(nodeToDeleteOrIndex, options) { |
| 13 | + if(options == undefined) { |
| 14 | + options = {} |
| 15 | + } |
| 16 | + |
| 17 | + var nodeToDelete; |
| 18 | + |
| 19 | + if(nodeToDeleteOrIndex instanceof jQuery) { |
| 20 | + nodeToDelete = nodeToDeleteOrIndex; |
| 21 | + } else { |
| 22 | + var wrapperClass = this.data('wrapper-class') || 'nested-fields'; |
| 23 | + console.log(wrapperClass) |
| 24 | + nodeToDelete = this.find("." + wrapperClass).filter(function(i, b) { |
| 25 | + return $(b).css('display') != 'none'; |
| 26 | + }).eq(nodeToDeleteOrIndex); |
| 27 | + } |
15 | 28 |
|
16 | 29 | this.trigger('before-remove', [nodeToDelete]); |
17 | 30 |
|
18 | 31 | var timeout = this.data('remove-timeout') || 0; |
19 | 32 |
|
20 | | - console.log(nodeToDelete) |
21 | 33 | var input = nodeToDelete.find("input").filter(function(i, t) { |
22 | | - console.log(t, $(t).attr("name")) |
23 | 34 | return false |
24 | 35 | }) |
25 | 36 |
|
| 37 | + var isDynamic = nodeToDelete.hasClass("dynamic"); |
| 38 | + |
26 | 39 | var context = this; |
27 | 40 | setTimeout(function() { |
28 | 41 | if (isDynamic) { |
|
41 | 54 | if(options == undefined) { |
42 | 55 | options = {} |
43 | 56 | } |
44 | | - var selector = "." + assoc + "_template"; |
45 | | - var contentTemplate = this.find(selector) |
46 | | - |
47 | | - if(contentTemplate.length == 0) { |
48 | | - contentTemplate = this.siblings().filter(selector) |
49 | | - } |
| 57 | + var templateSelector = "." + assoc + "_template"; |
| 58 | + |
| 59 | + var templateContainer; |
| 60 | + $(this).parents().each(function(i, el){ |
| 61 | + var found = $(el).find(templateSelector) |
| 62 | + if(found.length > 0) { |
| 63 | + templateContainer = found.eq(0) |
| 64 | + return false; |
| 65 | + } |
| 66 | + }) |
50 | 67 |
|
51 | 68 | var newId = activeFormHelpers.generateResourceId(); |
52 | 69 | var regex = new RegExp("new_" + assoc, "g"); |
53 | | - var content = contentTemplate.text().replace(regex, newId); |
54 | | - var contentNode = $(activeFormHelpers.decodeHtml(content)); |
| 70 | + var content = templateContainer.html().replace(regex, newId); |
| 71 | + var contentNode = $(content); |
| 72 | + |
| 73 | + contentNode.addClass("dynamic") |
55 | 74 |
|
56 | 75 | if(!options.insertionMethod) { |
57 | | - options.insertionMethod = 'before'; |
| 76 | + options.insertionMethod = 'append'; |
58 | 77 | } |
59 | 78 |
|
60 | 79 | this.trigger('before-insert', [contentNode]); |
|
80 | 99 | if (insertionTraversal){ |
81 | 100 | insertionNode = $link[insertionTraversal](insertionNode); |
82 | 101 | } else { |
83 | | - insertionNode = insertionNode == "this" ? $link : $(insertionNode); |
| 102 | + if(insertionNode == "this") { |
| 103 | + insertionNode = $(this) |
| 104 | + } else { |
| 105 | + $(this).parents().each(function(i, el){ |
| 106 | + var found = $(el).find(insertionNode) |
| 107 | + if(found.length > 0) { |
| 108 | + insertionNode = found.eq(0) |
| 109 | + return false; |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
84 | 113 | } |
85 | 114 | } else { |
86 | 115 | insertionNode = $link.parent(); |
|
92 | 121 | }) |
93 | 122 | }); |
94 | 123 |
|
95 | | - $(document).on('click', '.remove_fields.dynamic, .remove_fields.existing', function(e) { |
| 124 | + $(document).on('click', '.remove_fields, .remove_fields', function(e) { |
96 | 125 | e.preventDefault(); |
97 | 126 |
|
98 | 127 | var $link = $(this); |
|
101 | 130 |
|
102 | 131 | var triggerNode = nodeToDelete.parent(); |
103 | 132 |
|
104 | | - triggerNode.removeFormField(nodeToDelete, $link.hasClass('dynamic')) |
| 133 | + triggerNode.removeFormField(nodeToDelete) |
105 | 134 | }); |
106 | 135 |
|
107 | 136 | })(jQuery); |
0 commit comments