|
1 | 1 | (function($) { |
| 2 | + var activeFormHelpers = { |
| 3 | + generateResourceId: function() { |
| 4 | + 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 | + }, |
| 11 | + }; |
2 | 12 |
|
3 | | - var createNewResourceID = function() { |
4 | | - return new Date().getTime(); |
5 | | - } |
| 13 | + $.fn.removeFormField = function(nodeToDelete) { |
| 14 | + // maybe nodeToDelete could be field index |
| 15 | + |
| 16 | + this.trigger('before-remove', [nodeToDelete]); |
| 17 | + |
| 18 | + var timeout = this.data('remove-timeout') || 0; |
| 19 | + |
| 20 | + console.log(nodeToDelete) |
| 21 | + var input = nodeToDelete.find("input").filter(function(i, t) { |
| 22 | + console.log(t, $(t).attr("name")) |
| 23 | + return false |
| 24 | + }) |
| 25 | + |
| 26 | + var context = this; |
| 27 | + setTimeout(function() { |
| 28 | + if (isDynamic) { |
| 29 | + nodeToDelete.remove(); |
| 30 | + } else { |
| 31 | + nodeToDelete.find("input[type=hidden]").val("1"); |
| 32 | + nodeToDelete.hide(); |
| 33 | + } |
| 34 | + context.trigger('after-remove', [nodeToDelete]); |
| 35 | + }, timeout); |
| 36 | + |
| 37 | + return this |
| 38 | + }; |
| 39 | + |
| 40 | + $.fn.addFormField = function(assoc, options) { |
| 41 | + if(options == undefined) { |
| 42 | + options = {} |
| 43 | + } |
| 44 | + var selector = "." + assoc + "_template"; |
| 45 | + var contentTemplate = this.find(selector) |
| 46 | + |
| 47 | + if(contentTemplate.length == 0) { |
| 48 | + contentTemplate = this.siblings().filter(selector) |
| 49 | + } |
| 50 | + |
| 51 | + var newId = activeFormHelpers.generateResourceId(); |
| 52 | + var regex = new RegExp("new_" + assoc, "g"); |
| 53 | + var content = contentTemplate.text().replace(regex, newId); |
| 54 | + var contentNode = $(activeFormHelpers.decodeHtml(content)); |
| 55 | + |
| 56 | + if(!options.insertionMethod) { |
| 57 | + options.insertionMethod = 'before'; |
| 58 | + } |
| 59 | + |
| 60 | + this.trigger('before-insert', [contentNode]); |
| 61 | + |
| 62 | + var addedContent = this[options.insertionMethod](contentNode); |
| 63 | + |
| 64 | + this.trigger('after-insert', [contentNode]); |
| 65 | + |
| 66 | + return this; |
| 67 | + }; |
6 | 68 |
|
7 | 69 | $(document).on('click', '.add_fields', function(e) { |
8 | 70 | e.preventDefault(); |
9 | 71 |
|
10 | 72 | var $link = $(this); |
| 73 | + var form = $link.parents("form").eq(0); |
11 | 74 | var assoc = $link.data('association'); |
12 | | - var content = $link.data('association-insertion-template'); |
13 | | - var insertionMethod = $link.data('association-insertion-method') || $link.data('association-insertion-position') || 'before'; |
| 75 | + var insertionMethod = $link.data('association-insertion-method') || $link.data('association-insertion-position'); |
14 | 76 | var insertionNode = $link.data('association-insertion-node'); |
15 | 77 | var insertionTraversal = $link.data('association-insertion-traversal'); |
16 | | - var newId = createNewResourceID(); |
17 | | - var regex = new RegExp("new_" + assoc, "g"); |
18 | | - var newContent = content.replace(regex, newId); |
19 | 78 |
|
20 | 79 | if (insertionNode){ |
21 | 80 | if (insertionTraversal){ |
|
27 | 86 | insertionNode = $link.parent(); |
28 | 87 | } |
29 | 88 |
|
30 | | - var contentNode = $(newContent); |
31 | | - insertionNode.trigger('before-insert', [contentNode]); |
32 | | - |
33 | | - var addedContent = insertionNode[insertionMethod](contentNode); |
34 | | - |
35 | | - insertionNode.trigger('after-insert', [contentNode]); |
| 89 | + insertionNode.addFormField(assoc, { |
| 90 | + insertionMethod: insertionMethod, |
| 91 | + insertionTraversal: insertionTraversal, |
| 92 | + }) |
36 | 93 | }); |
37 | 94 |
|
38 | 95 | $(document).on('click', '.remove_fields.dynamic, .remove_fields.existing', function(e) { |
|
41 | 98 | var $link = $(this); |
42 | 99 | var wrapperClass = $link.data('wrapper-class') || 'nested-fields'; |
43 | 100 | var nodeToDelete = $link.closest('.' + wrapperClass); |
44 | | - var triggerNode = nodeToDelete.parent(); |
45 | 101 |
|
46 | | - triggerNode.trigger('before-remove', [nodeToDelete]); |
47 | | - |
48 | | - var timeout = triggerNode.data('remove-timeout') || 0; |
| 102 | + var triggerNode = nodeToDelete.parent(); |
49 | 103 |
|
50 | | - setTimeout(function() { |
51 | | - if ($link.hasClass('dynamic')) { |
52 | | - nodeToDelete.remove(); |
53 | | - } else { |
54 | | - $link.prev("input[type=hidden]").val("1"); |
55 | | - nodeToDelete.hide(); |
56 | | - } |
57 | | - triggerNode.trigger('after-remove', [nodeToDelete]); |
58 | | - }, timeout); |
| 104 | + triggerNode.removeFormField(nodeToDelete, $link.hasClass('dynamic')) |
59 | 105 | }); |
60 | 106 |
|
61 | 107 | })(jQuery); |
0 commit comments