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 readOnlyPrevious #520

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
}
],
"require": {
"php": "^7.3|^8.0",
"php": "^7.3|^8.0|^8.1|^8.2",
"ext-json": "*",
"laravel/framework": "^8.0|^9.0|^10.0|^11.0",
"laravel/nova": "^4.0",
"laravel/nova": "^4",
"nova-kit/nova-packages-tool": "^1.3.1"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
"sortablejs": "^1.15.0",
"vue-loader": "^16.8.3",
"vuex": "^4.0.2"
},
"dependencies": {
"laravel-nova-ui": "^0.4.12"
}
}
13 changes: 7 additions & 6 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export default {
* Set the initial, internal value for the field.
*/
setInitialValue() {
const firstLoad = true
this.value = this.currentField.value || [];
this.files = {};

this.populateGroups();
this.populateGroups(firstLoad);
this.$nextTick(this.initSortable.bind(this));
},

Expand Down Expand Up @@ -184,7 +184,7 @@ export default {
/**
* Set the displayed layouts from the field's current value
*/
populateGroups() {
populateGroups(firstLoad = false) {
this.order.splice(0, this.order.length);
this.groups = {};

Expand All @@ -193,7 +193,8 @@ export default {
this.getLayout(this.value[i].layout),
this.value[i].attributes,
this.value[i].key,
this.currentField.collapsed
this.currentField.collapsed,
firstLoad
);
}
},
Expand All @@ -209,13 +210,13 @@ export default {
/**
* Append the given layout to flexible content's list
*/
addGroup(layout, attributes, key, collapsed) {
addGroup(layout, attributes, key, collapsed, firstLoad) {
if(!layout) return;

collapsed = collapsed || false;

let fields = attributes || JSON.parse(JSON.stringify(layout.fields)),
group = new Group(layout.name, layout.title, fields, this.currentField, key, collapsed);
group = new Group(layout.name, layout.title, fields, this.currentField, key, collapsed, firstLoad);

this.groups[group.key] = group;
this.order.push(group.key);
Expand Down
3 changes: 2 additions & 1 deletion resources/js/components/FormGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{{ group.title }}
</p>

<div class="flex" v-if="!readonly">
<div class="flex" v-if="!readonly && !hideGroupButtons">
<button
dusk="drag-group"
type="button"
Expand Down Expand Up @@ -113,6 +113,7 @@ export default {
removeMessage: false,
collapsed: this.group.collapsed,
readonly: this.group.readonly,
hideGroupButtons: this.field.hideGroupButtons,
};
},

Expand Down
4 changes: 3 additions & 1 deletion resources/js/group.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export default class Group {

constructor(name, title, fields, field, key, collapsed = true) {
constructor(name, title, fields, field, key, collapsed = true, firstLoad = false) {
this.name = name;
this.title = title;
this.fields = fields;
this.key = key || this.getTemporaryUniqueKey(field.attribute);
this.collapsed = collapsed;
// if readOnlyPrevious is true and if firstLoad is true, all previus fields map to readonly.
this.fields = field.readOnlyPrevious? (firstLoad ? fields.map(attributes => ({ ...attributes, readonly: true })) : fields):fields;
this.readonly = field.readonly;

this.renameFields();
Expand Down
65 changes: 44 additions & 21 deletions src/Flexible.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ public function fullWidth()
return $this->withMeta(['fullWidth' => true]);
}

/**
* HideGroupButtons. Hide drag and delete buttons
*
* @param bool $value
* @return mixed
*/
public function hideGroupButtons(bool $value = true)
{
return $this->withMeta(['hideGroupButtons' => $value]);
}

/**
* readOnlyPrevious Previous layouts are read only.
*
* @param bool $value
* @return mixed
*/
public function readOnlyPrevious(bool $value = true)
{
return $this->withMeta(['readOnlyPrevious' => $value]);
}

/**
* Make the flexible content take up the full width
* of the form. Labels will sit above
Expand Down Expand Up @@ -160,8 +182,8 @@ public function resolver($resolver)
$resolver = new $resolver();
}

if (! ($resolver instanceof ResolverInterface)) {
throw new \Exception('Resolver Class "'.get_class($resolver).'" does not implement ResolverInterface.');
if (!($resolver instanceof ResolverInterface)) {
throw new \Exception('Resolver Class "' . get_class($resolver) . '" does not implement ResolverInterface.');
}

$this->resolver = $resolver;
Expand Down Expand Up @@ -191,8 +213,8 @@ public function addLayout(...$arguments)
$layout = new $layout();
}

if (! ($layout instanceof LayoutInterface)) {
throw new \Exception('Layout Class "'.get_class($layout).'" does not implement LayoutInterface.');
if (!($layout instanceof LayoutInterface)) {
throw new \Exception('Layout Class "' . get_class($layout) . '" does not implement LayoutInterface.');
}

$this->registerLayout($layout);
Expand Down Expand Up @@ -235,7 +257,7 @@ public function collapsed(bool $value = true)
*/
protected function registerLayout(LayoutInterface $layout)
{
if (! $this->layouts) {
if (!$this->layouts) {
$this->layouts = new LayoutsCollection();
$this->withMeta(['layouts' => $this->layouts]);
}
Expand Down Expand Up @@ -306,7 +328,7 @@ public function isShownOnDetail(NovaRequest $request, $resource): bool
*/
protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute)
{
if (! $request->exists($requestAttribute)) {
if (!$request->exists($requestAttribute)) {
return;
}

Expand Down Expand Up @@ -338,7 +360,7 @@ protected function fillAttribute(NovaRequest $request, $requestAttribute, $model
*/
protected function syncAndFillGroups(NovaRequest $request, $requestAttribute): array
{
if (! ($raw = $this->extractValue($request, $requestAttribute))) {
if (!($raw = $this->extractValue($request, $requestAttribute))) {
$this->fireRemoveCallbacks(collect());
$this->groups = collect();

Expand All @@ -354,7 +376,7 @@ protected function syncAndFillGroups(NovaRequest $request, $requestAttribute): a

$group = $this->findGroup($key) ?? $this->newGroup($layout, $key);

if (! $group instanceof Layout) {
if (!$group instanceof Layout) {
return [];
}

Expand Down Expand Up @@ -382,7 +404,7 @@ protected function fireRemoveCallbacks(Collection $new_groups)
return $item->inUseKey();
});
$removed_groups = $this->groups->filter(function ($item) use ($new_group_keys) {
return ! $new_group_keys->contains($item->inUseKey());
return !$new_group_keys->contains($item->inUseKey());
})->each(function ($group) {
if (method_exists($group, 'fireRemoveCallback')) {
$group->fireRemoveCallback($this);
Expand All @@ -401,11 +423,11 @@ protected function extractValue(NovaRequest $request, $attribute)
{
$value = $request[$attribute];

if (! $value) {
if (!$value) {
return;
}

if (! is_array($value)) {
if (!is_array($value)) {
throw new \Exception('Unable to parse incoming Flexible content, data should be an array.');
}

Expand Down Expand Up @@ -449,7 +471,7 @@ protected function resolveGroupsForDisplay($groups)
*/
protected function buildGroups($resource, $attribute)
{
if (! $this->resolver) {
if (!$this->resolver) {
$this->resolver(Resolver::class);
}

Expand Down Expand Up @@ -480,7 +502,7 @@ protected function newGroup($layout, $key)
{
$layout = $this->layouts->find($layout);

if (! $layout instanceof Layout) {
if (!$layout instanceof Layout) {
return null;
}

Expand Down Expand Up @@ -535,13 +557,13 @@ public function getUpdateRules(NovaRequest $request)
*/
protected function getFlexibleRules(NovaRequest $request, $specificty)
{
if (! ($value = $this->extractValue($request, $this->attribute))) {
if (!($value = $this->extractValue($request, $this->attribute))) {
return [];
}

$rules = $this->generateRules($request, $value, $specificty);

if (! is_a($request, ScopedRequest::class)) {
if (!is_a($request, ScopedRequest::class)) {
// We're not in a nested flexible, meaning we're
// assuming the field is located at the root of
// the model's attributes. Therefore, we should now
Expand Down Expand Up @@ -570,16 +592,16 @@ protected function generateRules(NovaRequest $request, $value, $specificty)
return collect($value)->map(function ($item, $key) use ($request, $specificty) {
$group = $this->newGroup($item['layout'], $item['key']);

if (! $group) {
if (!$group) {
return [];
}

$scope = ScopedRequest::scopeFrom($request, $item['attributes'], $item['key']);

return $group->generateRules($scope, $specificty, $this->attribute.'.'.$key);
return $group->generateRules($scope, $specificty, $this->attribute . '.' . $key);
})
->collapse()
->all();
->collapse()
->all();
}

/**
Expand Down Expand Up @@ -609,7 +631,8 @@ protected static function registerValidationKeys(array $rules)
}, $rules);

static::$validatedKeys = array_merge(
static::$validatedKeys, $validatedKeys
static::$validatedKeys,
$validatedKeys
);
}

Expand Down Expand Up @@ -638,7 +661,7 @@ protected function registerOriginModel($model)
$model = $model->getOriginal();
}

if (! is_a($model, \Illuminate\Database\Eloquent\Model::class)) {
if (!is_a($model, \Illuminate\Database\Eloquent\Model::class)) {
return;
}

Expand Down
20 changes: 0 additions & 20 deletions webpack.mix.js.dist

This file was deleted.

Loading