|
68 | 68 | $.subformRepeatable.prototype.prepareTemplate = function(){ |
69 | 69 | // create from template |
70 | 70 | if (this.options.rowTemplateSelector) { |
71 | | - var tmplElement = this.$container.find(this.options.rowTemplateSelector).last()[0] || {}; |
72 | | - // do a decodeURIComponent() here, because the text value is url encoded |
73 | | - // to make sure we dont destroy our markup |
74 | | - this.template = decodeURIComponent($.trim(tmplElement.text || tmplElement.textContent)); //(text || textContent) is IE8 fix |
| 71 | + // Find the template element and get its HTML content, this is our template. |
| 72 | + this.template = $.trim(this.$container.find(this.options.rowTemplateSelector).html()) || ''; |
75 | 73 | } |
76 | 74 | // create from existing rows |
77 | 75 | else { |
|
144 | 142 | $row.remove(); |
145 | 143 | }; |
146 | 144 |
|
147 | | - // fix names ind id`s for field that in $row |
148 | | - $.subformRepeatable.prototype.fixUniqueAttributes = function($row, count){ |
149 | | - var group = $row.attr('data-group'),// current group name |
150 | | - basename = $row.attr('data-base-name'), // group base name, without count |
151 | | - count = count || 0, |
| 145 | + // fix names and id`s for fields in $row |
| 146 | + $.subformRepeatable.prototype.fixUniqueAttributes = function( |
| 147 | + $row, // the jQuery object to do fixes in |
| 148 | + count, // existing count of rows |
| 149 | + group, // current group name, e.g. 'optionsX' |
| 150 | + basename // group base name, without count, e.g. 'options' |
| 151 | + ) { |
| 152 | + var group = (typeof group === 'undefined' ? $row.attr('data-group') : group), |
| 153 | + basename = (typeof basename === 'undefined' ? $row.attr('data-base-name') : basename), |
| 154 | + count = (typeof count === 'undefined' ? 0 : count), |
152 | 155 | countnew = Math.max(this.lastRowNum, count), |
153 | | - groupnew = basename + countnew; // new group name |
| 156 | + groupnew = basename + countnew; |
154 | 157 |
|
155 | 158 | this.lastRowNum = countnew; |
156 | 159 | $row.attr('data-group', groupnew); |
|
203 | 206 | $row.find('label[for="' + forOldAttr + '"]').attr('for', idNew); |
204 | 207 | } |
205 | 208 |
|
206 | | - // Create 2 strings: basename + old group, and basename + new group |
207 | | - var search = '[' + basename + '][' + group + ']', |
208 | | - replace = '[' + basename + '][' + groupnew + ']'; |
209 | | - // Does our row still contain the basename + old group? This should not happen! |
210 | | - if ($row.html().indexOf(search) !== -1) { |
211 | | - console.log('Old basename+group still existant in $row html'); |
212 | | - } |
213 | | - // Recursively replace our basename + old group with basename + new group |
214 | | - // inside of nested subform template elements. |
215 | | - this.recursiveReplaceNested($row, search, replace); |
216 | | - }; |
217 | | - |
218 | | - $.subformRepeatable.prototype.recursiveReplaceNested = function($row, search, replace) { |
219 | | - // Try to find the row remplate selector in $row |
| 209 | + /** |
| 210 | + * Recursively replace our basename + old group with basename + new group |
| 211 | + * inside of nested subform template elements. First we try to find such |
| 212 | + * template elements, then we iterate through them and do the same replacements |
| 213 | + * that we have made here inside of them. |
| 214 | + */ |
220 | 215 | var nestedTemplates = $row.find(this.options.rowTemplateSelector); |
221 | | - if (nestedTemplates.length < 1) { |
222 | | - return; |
223 | | - } |
224 | 216 | // If we found it, iterate over the found ones (might be more than one!) |
225 | 217 | for (var i = 0; i < nestedTemplates.length; i++) { |
226 | | - // Get the element |
227 | | - var nestedTemplate = $(nestedTemplates[i]); |
228 | | - // Do our replacement; the HTML content is urlencoded, so we urlencode |
229 | | - // our search/replace parameters, also because we then don't need to |
230 | | - // regexp escape the search parameter (we are using regexp to globally replace). |
231 | | - nestedTemplate.html(nestedTemplate.html().replace( |
232 | | - new RegExp(encodeURIComponent(search), 'g'), |
233 | | - encodeURIComponent(replace) |
234 | | - )); |
235 | | - // URI decode its HTML content to could have access to deeper nested ones |
236 | | - var nestedTemplateContent = decodeURIComponent(nestedTemplate.html()); |
237 | | - // Create a new element out of it |
238 | | - var nestedElement = $(nestedTemplateContent); |
239 | | - // And recursively do the replacements more times |
240 | | - this.recursiveReplaceNested(nestedElement, search, replace); |
241 | | - // Now re-insert the replaced html content into our element |
242 | | - nestedTemplate.html(encodeURIComponent(nestedElement.prop('outerHTML'))); |
| 218 | + // Get the nested templates content (as DocumentFragment) and cast it |
| 219 | + // to a jQuery object |
| 220 | + var nestedTemplate = $($(nestedTemplates[i]).prop('content')); |
| 221 | + // Fix the attributes for this nested template. |
| 222 | + this.fixUniqueAttributes(nestedTemplate, count, group, basename); |
243 | 223 | } |
244 | 224 | }; |
245 | 225 |
|
|
312 | 292 | repeatableElement: ".subform-repeatable-group", |
313 | 293 | // selector for the row template element with URL-encoded template inside it, |
314 | 294 | // must *NOT* be unique per nested subform! |
315 | | - rowTemplateSelector: 'script.subform-repeatable-template-section', |
| 295 | + rowTemplateSelector: 'template.subform-repeatable-template-section', |
316 | 296 | // container for rows, same as main container by default |
317 | 297 | rowsContainer: null |
318 | 298 | }; |
|
0 commit comments