Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min and max to list field #1113

Merged
merged 3 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Added ability to choose how you want the preview button to open [#1096](https://github.com/getgrav/grav-plugin-admin/pull/1096)
* `base.html.twig` now extends a `base-root.html.twig` file
* Add month+date indication to the stats graph to avoid confusion when there are days without visits
* Added `min` and `max` options for `list` form field [#1113](https://github.com/getgrav/grav-plugin-admin/pull/1113)
1. [](#bugfix)
* Fixed issue with tab widths on Pages overlapping non-english toggle switch [#1089](https://github.com/getgrav/grav-plugin-admin/issues/1089)
* Added `vendor` to ignores for direct install of Grav
Expand Down
38 changes: 36 additions & 2 deletions themes/grav/app/forms/fields/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default class CollectionsField {
onUpdate: () => this.reindex(container)
}));
});

this._updateActionsStateBasedOnMinMax(list);
}

addItem(event) {
Expand All @@ -44,10 +46,17 @@ export default class CollectionsField {
let list = $(button.closest('[data-type="collection"]'));
let template = $(list.find('> [data-collection-template="new"]').data('collection-template-html'));

this._updateActionsStateBasedOnMinMax(list);
let items = list.closest('[data-type="collection"]').find('> ul > [data-collection-item]');
let maxItems = list.data('max');
if (typeof maxItems !== 'undefined' && items.length >= maxItems) {
return;
}

list.find('> [data-collection-holder]')[position === 'top' ? 'prepend' : 'append'](template);
this.reindex(list);

let items = list.closest('[data-type="collection"]').find('> ul > [data-collection-item]');
items = list.closest('[data-type="collection"]').find('> ul > [data-collection-item]');
let topAction = list.closest('[data-type="collection"]').find('[data-action-add="top"]');
let sortAction = list.closest('[data-type="collection"]').find('[data-action="sort"]');

Expand All @@ -65,10 +74,18 @@ export default class CollectionsField {
let item = button.closest('[data-collection-item]');
let list = $(button.closest('[data-type="collection"]'));

this._updateActionsStateBasedOnMinMax(list);
let items = list.closest('[data-type="collection"]').find('> ul > [data-collection-item]');
let minItems = list.data('min');

if (typeof minItems !== 'undefined' && items.length <= minItems) {
return;
}

item.remove();
this.reindex(list);

let items = list.closest('[data-type="collection"]').find('> ul > [data-collection-item]');
items = list.closest('[data-type="collection"]').find('> ul > [data-collection-item]');
let topAction = list.closest('[data-type="collection"]').find('[data-action-add="top"]');
let sortAction = list.closest('[data-type="collection"]').find('[data-action="sort"]');

Expand Down Expand Up @@ -212,6 +229,23 @@ export default class CollectionsField {
}
});
}

_updateActionsStateBasedOnMinMax(list) {
let items = list.closest('[data-type="collection"]').find('> ul > [data-collection-item]');
let minItems = list.data('min');
let maxItems = list.data('max');

list.find('> .collection-actions [data-action="add"]').attr('disabled', false);
list.find('> ul > li > .item-actions [data-action="delete"]').attr('disabled', false);

if (typeof minItems !== 'undefined' && items.length <= minItems) {
list.find('> ul > li > .item-actions [data-action="delete"]').attr('disabled', true);
}

if (typeof maxItems !== 'undefined' && items.length >= maxItems) {
list.find('> .collection-actions [data-action="add"]').attr('disabled', true);
}
}
}

export let Instance = new CollectionsField();
4 changes: 2 additions & 2 deletions themes/grav/js/admin.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion themes/grav/templates/forms/fields/list/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
{% endblock %}
>

<div class="form-list-wrapper {{ field.size }}" data-type="collection">
<div class="form-list-wrapper {{ field.size }}" data-type="collection"
{% if field.min is defined %}data-min="{{ field.min }}"{% endif %}
{% if field.max is defined %}data-max="{{ field.max }}"{% endif %}
>
{% if fieldControls in ['top', 'both'] %}
<div class="collection-actions{{ not value|length ? ' hidden' : '' }}">
<button class="button" type="button" data-action="expand_all"
Expand Down