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

Sortable Additional Images #39

Merged
merged 1 commit into from
Apr 21, 2024
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
7 changes: 4 additions & 3 deletions app/Listeners/ManageProductImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public function __construct()
*/
public function handle($event)
{
$images = [ $event->data['base_image'] => ['img_type' => 'base'] ];
foreach($event->data['additional_images'] ?? [] as $additional_image) {
$order = 0;
$images = [$event->data['base_image'] => ['img_type' => 'base']];
foreach ($event->data['additional_images'] ?? [] as $additional_image) {
$additional_image != $event->data['base_image'] && (
$images[$additional_image] = ['img_type' => 'additional']
$images[$additional_image] = ['img_type' => 'additional', 'order' => ++$order]
);
}

Expand Down
3 changes: 2 additions & 1 deletion app/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public function brand()
public function images()
{
return $this->belongsToMany(Image::class)
->withPivot('img_type')
->withPivot(['img_type', 'order'])
->orderBy('order')
->withTimestamps();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddOrderColumnToImageProductTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('image_product', function (Blueprint $table) {
$table->integer('order')->default(0)->after('img_type');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('image_product', function (Blueprint $table) {
$table->dropColumn('order');
});
}
}
3 changes: 3 additions & 0 deletions resources/views/admin/products/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


@push('styles')
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<style>
.nav-tabs {
border: 2px solid #ddd;
Expand Down Expand Up @@ -251,8 +252,10 @@
@endpush

@push('scripts')
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script>
$(document).ready(function () {
$('.additional_images-previews').sortable();
// $('[name="name"]').keyup(function () {
// $($(this).data('target')).val(slugify($(this).val()));
// });
Expand Down
8 changes: 4 additions & 4 deletions resources/views/admin/products/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,20 @@
<span>Browse</span>
</button>
</label>
<div class="additional_images-previews d-flex flex-wrap" style="margin-left: -5px;">
<ul id="sortable" class="additional_images-previews d-flex flex-wrap" style="margin-left: -5px;">
@php
$ids = old('additional_images', $product->additional_images->pluck('id')->toArray());
$srcs = old('additional_images_srcs', $product->additional_images->pluck('src')->toArray());
@endphp
@foreach($srcs as $src)
<div id="preview-{{$ids[$loop->index]}}" class="additional_images-preview position-relative" style="height: 150px; width: 150px; margin: 5px;">
<li id="preview-{{$ids[$loop->index]}}" class="additional_images-preview position-relative" style="height: 150px; width: 150px; margin: 5px;">
<i class="fa fa-times text-danger position-absolute" style="font-size: large; top: 0; right: 0; background: #ddd; padding: 2px; border-radius: 3px; cursor: pointer;" onclick="this.parentNode.remove()"></i>
<img src="{{ $src }}" alt="Additional Image" data-toggle="modal" data-target="#multi-picker" id="additional_image-preview" class="img-thumbnail img-responsive">
<input type="hidden" name="additional_images[]" value="{{ $ids[$loop->index] }}" style="margin: 5px;">
<input type="hidden" name="additional_images_srcs[]" value="{{ $src }}" style="margin: 5px;">
</div>
</li>
@endforeach
</div>
</ul>
<div class="clearfix"></div>
@error('additional_images')
<small class="text-danger">{{ $message }}</small>
Expand Down
Loading