Skip to content

Commit

Permalink
Properly handle Collections that specify a custom key, rather than fa…
Browse files Browse the repository at this point in the history
…lling back to indexed list (#632)
  • Loading branch information
w00fz committed Jun 1, 2016
1 parent ae08766 commit d3b33fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
16 changes: 14 additions & 2 deletions themes/grav/app/forms/fields/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,32 @@ export default class CollectionsField {
items.each((index, item) => {
item = $(item);
let observed = item.find('[data-key-observe]');
let observedValue = observed.val();
let hasCustomKey = observed.length;
let currentKey = item.data('collection-key-backup');

item.attr('data-collection-key', hasCustomKey ? observed.val() : index);
item.attr('data-collection-key', hasCustomKey ? observedValue : index);

['name', 'data-grav-field-name', 'for', 'id'].forEach((prop) => {
item.find('[' + prop + ']').each(function() {
item.find('[' + prop + '], [_' + prop + ']').each(function() {
let element = $(this);
let indexes = [];
let regexps = [
new RegExp('\\[(\\d+|\\*|' + currentKey + ')\\]', 'g'),
new RegExp('\\.(\\d+|\\*|' + currentKey + ')\\.', 'g')
];

if (!observedValue) {
element.attr(`_${prop}`, element.attr(prop));
element.attr(prop, null);
return;
}

if (element.attr(`_${prop}`)) {
element.attr(prop, element.attr(`_${prop}`));
element.attr(`_${prop}`, null);
}

element.parents('[data-collection-key]').map((idx, parent) => indexes.push($(parent).attr('data-collection-key')));
indexes.reverse();

Expand Down
Loading

0 comments on commit d3b33fb

Please sign in to comment.