|
| 1 | +(function($) { |
| 2 | + // Form API, draft version: |
| 3 | + // $(".task_list").addFormField("tasks") |
| 4 | + // $(".task_list").removeFormField("tasks", 2) // destroy second field |
| 5 | + |
| 6 | + var activeFormHelpers = { |
| 7 | + generateResourceId: function() { |
| 8 | + return new Date().getTime(); |
| 9 | + } |
| 10 | + }; |
| 11 | + |
| 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 | + } |
| 28 | + |
| 29 | + this.trigger('before-remove', [nodeToDelete]); |
| 30 | + |
| 31 | + var timeout = this.data('remove-timeout') || 0; |
| 32 | + |
| 33 | + var input = nodeToDelete.find("input").filter(function(i, t) { |
| 34 | + return false |
| 35 | + }) |
| 36 | + |
| 37 | + var isDynamic = nodeToDelete.hasClass("dynamic"); |
| 38 | + |
| 39 | + var context = this; |
| 40 | + setTimeout(function() { |
| 41 | + if (isDynamic) { |
| 42 | + nodeToDelete.remove(); |
| 43 | + } else { |
| 44 | + nodeToDelete.find("input[type=hidden]").val("1"); |
| 45 | + nodeToDelete.hide(); |
| 46 | + } |
| 47 | + context.trigger('after-remove', [nodeToDelete]); |
| 48 | + }, timeout); |
| 49 | + |
| 50 | + return this |
| 51 | + }; |
| 52 | + |
| 53 | + $.fn.addFormField = function(assoc, options) { |
| 54 | + if(options == undefined) { |
| 55 | + options = {} |
| 56 | + } |
| 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 | + }) |
| 67 | + |
| 68 | + var newId = activeFormHelpers.generateResourceId(); |
| 69 | + var regex = new RegExp("new_" + assoc, "g"); |
| 70 | + var content = templateContainer.html().replace(regex, newId); |
| 71 | + var contentNode = $(content); |
| 72 | + |
| 73 | + contentNode.addClass("dynamic") |
| 74 | + |
| 75 | + if(!options.insertionMethod) { |
| 76 | + options.insertionMethod = 'append'; |
| 77 | + } |
| 78 | + |
| 79 | + this.trigger('before-insert', [contentNode]); |
| 80 | + |
| 81 | + var addedContent = this[options.insertionMethod](contentNode); |
| 82 | + |
| 83 | + this.trigger('after-insert', [contentNode]); |
| 84 | + |
| 85 | + return this; |
| 86 | + }; |
| 87 | + |
| 88 | + $(document).on('click', '.add_fields', function(e) { |
| 89 | + e.preventDefault(); |
| 90 | + |
| 91 | + var $link = $(this); |
| 92 | + var form = $link.parents("form").eq(0); |
| 93 | + var assoc = $link.data('association'); |
| 94 | + var insertionMethod = $link.data('association-insertion-method') || $link.data('association-insertion-position'); |
| 95 | + var insertionNode = $link.data('association-insertion-node'); |
| 96 | + var insertionTraversal = $link.data('association-insertion-traversal'); |
| 97 | + |
| 98 | + if (insertionNode){ |
| 99 | + if (insertionTraversal){ |
| 100 | + insertionNode = $link[insertionTraversal](insertionNode); |
| 101 | + } else { |
| 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 | + } |
| 113 | + } |
| 114 | + } else { |
| 115 | + insertionNode = $link.parent(); |
| 116 | + } |
| 117 | + |
| 118 | + insertionNode.addFormField(assoc, { |
| 119 | + insertionMethod: insertionMethod, |
| 120 | + insertionTraversal: insertionTraversal, |
| 121 | + }) |
| 122 | + }); |
| 123 | + |
| 124 | + $(document).on('click', '.remove_fields, .remove_fields', function(e) { |
| 125 | + e.preventDefault(); |
| 126 | + |
| 127 | + var $link = $(this); |
| 128 | + var wrapperClass = $link.data('wrapper-class') || 'nested-fields'; |
| 129 | + var nodeToDelete = $link.closest('.' + wrapperClass); |
| 130 | + |
| 131 | + var triggerNode = nodeToDelete.parent(); |
| 132 | + |
| 133 | + triggerNode.removeFormField(nodeToDelete) |
| 134 | + }); |
| 135 | + |
| 136 | +})(jQuery); |
0 commit comments