Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Add image deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslkingsley committed Sep 14, 2018
1 parent fae5a23 commit b31102f
Show file tree
Hide file tree
Showing 7 changed files with 22,726 additions and 32 deletions.
22,642 changes: 22,641 additions & 1 deletion dist/js/app.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions resources/js/components/DetailField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
computed: {
url() {
return this.field.value
return this.field.value.preview_url
},
width() {
Expand All @@ -22,7 +22,7 @@
height() {
return this.field.height || '12rem'
}
}
},
},
}
</script>
7 changes: 2 additions & 5 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@
const { resourceName, resourceId, relatedResourceName, relatedResourceId, viaRelationship } = this
const attribute = this.field.attribute
const uri = this.viaRelationship
? `/nova-api/${resourceName}/${resourceId}/${relatedResourceName}/${relatedResourceId}/field/${attribute}?viaRelationship=${viaRelationship}`
: `/nova-api/${resourceName}/${resourceId}/field/${attribute}`
const uri = `/nova-vendor/jameslkingsley/nova-media-library/${this.field.value.id}`
try {
await Nova.request().delete(uri)
Expand All @@ -178,7 +175,7 @@
computed: {
imageUrl() {
return this.previewUrl ? this.previewUrl : this.field.value
return this.previewUrl ? this.previewUrl : this.field.value.preview_url
},
hasError() {
Expand Down
31 changes: 16 additions & 15 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
</template>

<script>
export default {
props: ['resourceName', 'field'],
export default {
props: ['resourceName', 'field'],
computed: {
url() {
return this.field.value
computed: {
url() {
return this.field.value.preview_url
},
style() {
return {
backgroundImage: `url(${this.url})`,
backgroundRepeat: 'no-repeat',
backgroundPosition: '50%',
backgroundSize: 'cover',
}
},
},
style() {
return {
backgroundImage: `url(${this.url})`,
backgroundRepeat: 'no-repeat',
backgroundPosition: '50%',
backgroundSize: 'cover'
}
}
},
}
}
</script>
31 changes: 23 additions & 8 deletions src/Fields/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kingsley\NovaMediaLibrary\Fields;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\Deletable;
Expand All @@ -19,6 +20,20 @@ class Image extends Field implements DeletableContract
*/
public $component = 'nova-media-library-image-field';

/**
* Create a new field.
*
* @return void
*/
public function __construct(...$arguments)
{
parent::__construct(...$arguments);

$this->delete(function () {
//
});
}

/**
* Hydrate the given attribute on the model based on the incoming request.
*
Expand Down Expand Up @@ -66,17 +81,17 @@ protected function resolveAttribute($resource, $attribute)
$conversion = $this->meta()['usingConversion'] ?? [];
$media = $resource->getFirstMedia($collection);

if (empty($media)) {
return null;
}
if ($media) {
if ($media->hasGeneratedConversion($conversion)) {
$media->preview_url = url($media->getUrl($conversion));
} else {
$media->preview_url = url($media->getUrl());
}

if ($media->hasGeneratedConversion($conversion)) {
$media = $media->getUrl($conversion);
} else {
$media = $media->getUrl();
return $media;
}

return url($media);
return null;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Kingsley\NovaMediaLibrary\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Spatie\MediaLibrary\Models\Media;

class MediaController extends Controller
{
/**
* Deletes the given media resource.
*
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, Media $media)
{
$media->delete();
}
}
21 changes: 21 additions & 0 deletions src/NovaMediaLibraryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Spatie\MediaLibrary\Models\Media;
use Illuminate\Support\ServiceProvider;
use Kingsley\NovaMediaLibrary\Policies\MediaPolicy;
use Kingsley\NovaMediaLibrary\Http\Controllers\MediaController;

class NovaMediaLibraryServiceProvider extends ServiceProvider
{
Expand All @@ -23,6 +24,26 @@ public function boot()
Nova::script('nova-media-library', __DIR__.'/../dist/js/app.js');
Nova::style('nova-media-library', __DIR__.'/../dist/css/app.css');
});

$this->app->booted(function () {
$this->routes();
});
}

/**
* Register the field's routes.
*
* @return void
*/
protected function routes()
{
if ($this->app->routesAreCached()) {
return;
}

Route::middleware(['nova'])
->prefix('nova-vendor/jameslkingsley/nova-media-library')
->delete('{media}', MediaController::class . '@destroy');
}

/**
Expand Down

0 comments on commit b31102f

Please sign in to comment.