|
62 | 62 |
|
63 | 63 | // Get the fieldset
|
64 | 64 | var fieldset = target.data('fieldset');
|
65 |
| - // Get the container |
66 |
| - var container = target.parent().parent(); |
| 65 | + |
| 66 | + //collect parents |
| 67 | + var parents = []; |
| 68 | + //get the parents from source to top |
| 69 | + var getParents = function(element) { |
| 70 | + var parent = element.parent(); |
| 71 | + var type = parent[0].tagName; |
| 72 | + var name = parent.data('reindex'); |
| 73 | + |
| 74 | + //fieldset? and has a reindex name? |
| 75 | + if (type === 'FIELDSET' && name && name) { |
| 76 | + var index = element.index() - 1; |
| 77 | + //get the current element index from parent |
| 78 | + parents.push({ |
| 79 | + name: name, |
| 80 | + index: index < 0 ? 0 : index |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + //we should continue |
| 85 | + if (type !== 'FORM') { |
| 86 | + getParents(parent); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + //start gathering the parents |
| 91 | + getParents(target); |
| 92 | + //reverse so we can start replacing from bottom to top |
| 93 | + var parent = parents.reverse().shift(); |
| 94 | + var container = $('fieldset[data-reindex="' + parent.name + '"]'); |
67 | 95 |
|
68 | 96 | // Remove the target
|
69 | 97 | target.parent().remove();
|
70 | 98 |
|
71 | 99 | // Get all the inputs
|
72 | 100 | var inputs = container.find('[name]');
|
73 | 101 | // Re-index
|
| 102 | + reindex(inputs, true); |
| 103 | + }); |
| 104 | + |
| 105 | + $(window).on('table-field-add-click', function(e, target) { |
| 106 | + target = $(target); |
| 107 | + |
| 108 | + //get the name |
| 109 | + var name = target.parent().data('name'); |
| 110 | + //get the columns |
| 111 | + var columns = target.parent().data('columns').split('|'); |
| 112 | + //get the template |
| 113 | + var template = target.parent().find('#table-field-template').html(); |
| 114 | + //determine the index |
| 115 | + var index = target.parent().find('tbody').children('tr').length; |
| 116 | + |
| 117 | + //parse the template |
| 118 | + template = $(template); |
| 119 | + //clone the input |
| 120 | + var input = $('<div />') |
| 121 | + .append( |
| 122 | + template |
| 123 | + .find('.input-template') |
| 124 | + .removeAttr('class') |
| 125 | + .remove() |
| 126 | + .clone() |
| 127 | + ); |
| 128 | + |
| 129 | + //on each columns |
| 130 | + for(var i in columns) { |
| 131 | + //get the html |
| 132 | + var html = input.html(); |
| 133 | + |
| 134 | + //replace name |
| 135 | + var re = new RegExp('{NAME}', 'g'); |
| 136 | + html = html.replace(re, name); |
| 137 | + //replace index |
| 138 | + re = new RegExp('{INDEX}', 'g'); |
| 139 | + html = html.replace(re, index); |
| 140 | + //replace column |
| 141 | + re = new RegExp('{COLUMN}', 'g'); |
| 142 | + html = html.replace(re, columns[i]); |
| 143 | + |
| 144 | + template.append(html); |
| 145 | + } |
| 146 | + |
| 147 | + //active scripts |
| 148 | + template = $(template).doon(); |
| 149 | + //append to body |
| 150 | + target.parent().find('tbody').append(template); |
| 151 | + }); |
| 152 | + |
| 153 | + $(window).on('table-field-remove-click', function(e, target) { |
| 154 | + target = $(target); |
| 155 | + |
| 156 | + //get the container |
| 157 | + var container = target |
| 158 | + .parent() |
| 159 | + .parent() |
| 160 | + .parent(); |
| 161 | + |
| 162 | + //remove |
| 163 | + target |
| 164 | + .parent() |
| 165 | + .parent() |
| 166 | + .remove(); |
| 167 | + |
| 168 | + //get all inputs from container |
| 169 | + var inputs = container.find('[name]'); |
| 170 | + //reindex |
74 | 171 | reindex(inputs);
|
75 | 172 | });
|
76 | 173 |
|
77 |
| - var reindex = function(inputs) { |
| 174 | + var reindex = function(inputs, filter) { |
78 | 175 | // Get the input names
|
79 | 176 | var names = {};
|
80 | 177 | $.each(inputs, function(index, element) {
|
|
94 | 191 |
|
95 | 192 | // Get the re-index filters
|
96 | 193 | var filters = [];
|
97 |
| - $('[data-reindex]').map(function(index, element) { |
98 |
| - filters.push($(element).data('reindex')); |
99 |
| - }); |
| 194 | + |
| 195 | + // Filter? |
| 196 | + if (filter) { |
| 197 | + $('[data-reindex]').map(function(index, element) { |
| 198 | + filters.push($(element).data('reindex')); |
| 199 | + }); |
| 200 | + } |
100 | 201 |
|
101 | 202 | // Re-index names
|
102 | 203 | var reindexed = arrange(names, filters);
|
|
105 | 206 | // Split by pairs
|
106 | 207 | serialized = serialized.split('&');
|
107 | 208 |
|
| 209 | + console.log(names); |
| 210 | + |
108 | 211 | // On each serialized pairs
|
109 | 212 | for(var i in serialized) {
|
110 | 213 | // Get the parts key & value
|
|
144 | 247 | // If it's a string but it's an object
|
145 | 248 | if (isNaN(parseInt(key)) && type === '[object Object]' && key !== '{INDEX}') {
|
146 | 249 | // If it's not fieldset
|
147 |
| - if (filters.indexOf(key) === -1) { |
| 250 | + if (filters.length && filters.indexOf(key) === -1) { |
148 | 251 | continue;
|
149 | 252 | }
|
150 | 253 |
|
|
0 commit comments