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 limit to a custom layout #200

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

28 changes: 27 additions & 1 deletion resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
@@ -61,11 +61,19 @@ export default {
},

data() {

let layoutCounter = [];

this.field.layouts.map(function(value, key) {
layoutCounter[value.name] = value.limit;
});

return {
order: [],
groups: {},
files: {},
limitCounter: this.field.limit
limitCounter: this.field.limit,
layoutCounter: layoutCounter
};
},

@@ -169,6 +177,14 @@ export default {
addGroup(layout, attributes, key, collapsed) {
if(!layout) return;

if(this.layoutCounter[layout.name] === 0) {
Nova.error(this.__('The layout :customLayout is limited to :layoutLimit', {
customLayout: layout.name,
layoutLimit: layout.limit,
}));
return;
}

collapsed = collapsed || false;

let fields = attributes || JSON.parse(JSON.stringify(layout.fields)),
@@ -177,6 +193,10 @@ export default {
this.groups[group.key] = group;
this.order.push(group.key);

if(this.layoutCounter[layout.name] !== null) {
this.layoutCounter[layout.name]--;
}

if (this.limitCounter > 0) {
this.limitCounter--;
}
@@ -208,13 +228,19 @@ export default {
* Remove a group
*/
remove(key) {

let layout = this.groups[key].name;
let index = this.order.indexOf(key);

if(index < 0) return;

this.order.splice(index, 1);
delete this.groups[key];

if(this.layoutCounter[layout.name] !== null) {
this.layoutCounter[layout.name]++;
}

if (this.limitCounter >= 0) {
this.limitCounter++;
}
8 changes: 8 additions & 0 deletions src/Layouts/Layout.php
Original file line number Diff line number Diff line change
@@ -22,6 +22,13 @@ class Layout implements LayoutInterface, JsonSerializable, ArrayAccess, Arrayabl
use HidesAttributes;
use HasFlexible;

/**
* The layout's limit
*
* @var int
*/
protected $limit;

/**
* The layout's name
*
@@ -581,6 +588,7 @@ public function jsonSerialize()
$this->resolve(true);

return [
'limit' => $this->limit,
'name' => $this->name,
'title' => $this->title,
'fields' => $this->fields->jsonSerialize()