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 (fixes #632)
  • Loading branch information
w00fz committed Jun 1, 2016
1 parent 7b0e691 commit ae08766
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Always submit checkboxes that are not checked and force a 0 value [#616](https://github.com/getgrav/grav-plugin-admin/issues/616)
* Fix encoding in tooltips again [#622](https://github.com/getgrav/grav-plugin-admin/issues/622)
* Do not show `move` cursor for Collections that aren't sortable [#624](https://github.com/getgrav/grav-plugin-admin/issues/624)
* Properly handle Collections that specify a custom key, rather than falling back to indexed list [#632](https://github.com/getgrav/grav-plugin-admin/issues/632)

# v1.1.0-beta.5
## 05/23/2016
Expand Down
30 changes: 23 additions & 7 deletions themes/grav/app/forms/fields/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class CollectionsField {

list.on('click', '> .collection-actions [data-action="add"]', (event) => this.addItem(event));
list.on('click', '> ul > li > .item-actions [data-action="delete"]', (event) => this.removeItem(event));
list.on('input', '[data-key-observe]', (event) => this.observeKey(event));

list.find('[data-collection-holder]').each((index, container) => {
container = $(container);
Expand Down Expand Up @@ -52,28 +53,43 @@ export default class CollectionsField {
this.reindex(list);
}

reindex(list) {
list = $(list).closest('[data-type="collection"]');
observeKey(event) {
let input = $(event.target);
let value = input.val();
let item = input.closest('[data-collection-key]');

let items = list.find('> ul > [data-collection-item]');
item.data('collection-key-backup', item.data('collection-key')).data('collection-key', value);
this.reindex(null, item);
}

reindex(list, items) {
items = items || $(list).closest('[data-type="collection"]').find('> ul > [data-collection-item]');

items.each((index, item) => {
item = $(item);
item.attr('data-collection-key', index);
let observed = item.find('[data-key-observe]');
let hasCustomKey = observed.length;
let currentKey = item.data('collection-key-backup');

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

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

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

let replaced = element.attr(prop).replace(/\[(\d+|\*)\]/g, (/* str, p1, offset */) => {
return `[${indexes.shift()}]`;
let replaced = element.attr(prop).replace(regexps[0], (/* str, p1, offset */) => {
return `[${indexes.shift() || currentKey}]`;
});

replaced = replaced.replace(/\.(\d+|\*)\./g, (/* str, p1, offset */) => {
replaced = replaced.replace(regexps[1], (/* str, p1, offset */) => {
return `.${indexes.shift()}.`;
});

Expand Down
28 changes: 14 additions & 14 deletions themes/grav/js/admin.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion themes/grav/templates/forms/fields/key/key.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<input
type="text"
value="{{ value|e('html_attr')|join(', ') }}"
data-list-key="{{ scope|fieldName }}"
data-key-observe="{{ (scope ~ field.name)|fieldName }}"
{% block input_attributes %}
{% if field.classes is defined %}class="{{ field.classes }}" {% endif %}
{% if field.id is defined %}id="{{ field.id|e }}" {% endif %}
Expand Down

0 comments on commit ae08766

Please sign in to comment.