Merge pull request #191 from cachethq/component-reordering

Components can now be re-ordered, closes #165
This commit is contained in:
James Brooks
2015-01-01 13:43:40 +00:00
17 changed files with 35880 additions and 44 deletions
+26
View File
@@ -69,6 +69,32 @@ $(function() {
$(this).parents('div.alert').addClass('hide');
});
// Sortable components.
var componentList = document.getElementById("component-list");
new Sortable(componentList, {
group: "omega",
handle: ".drag-handle",
onUpdate: function() {
// Loop each component, setting the order input to the new order.
var $components = $('#component-list .striped-list-item');
$.each($components, function(id) {
// Order should start from 1 now.
$(this).find('input[rel=order]').val(id + 1);
});
// Now POST the form to the internal API.
$.ajax({
async: true,
url: '/dashboard/api/components/order',
type: 'POST',
data: $('form[name=componentList]').serializeObject(),
success: function() {
(new CachetHQ.Notifier).notify('Components updated.', 'success');
}
});
}
});
// Toggle inline component statuses.
$('form.component-inline').on('click', 'input[type=radio]', function() {
var $form = $(this).parents('form');
+20
View File
@@ -19,4 +19,24 @@ class DashAPIController extends Controller
App::abort(500);
}
}
/**
* Updates a components ordering.
*
* @return array
*/
public function postUpdateComponentOrder()
{
$componentData = Input::all();
unset($componentData['component'][0]); // Remove random 0 index.
foreach ($componentData['component'] as $componentId => $order) {
$component = Component::find($componentId);
$component->update([
'order' => $order,
]);
}
return $componentData;
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ class DashComponentController extends Controller
*/
public function showComponents()
{
$components = Component::all();
$components = Component::orderBy('order')->orderBy('created_at')->get();
return View::make('dashboard.components.index')->with([
'pageTitle' => 'Components - Dashboard',
+3 -1
View File
@@ -28,8 +28,10 @@ class HomeController extends Controller
*/
public function showIndex()
{
$components = Component::orderBy('order')->orderBy('created_at')->get();
return View::make('index', [
'components' => $this->component->all(),
'components' => $components,
'pageTitle' => Setting::get('app_name')
]);
}
@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class AlterTableComponentsAddOrderColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('components', function (Blueprint $table) {
$table->tinyInteger('order')->default(0)->after('status');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('components', function (Blueprint $table) {
$table->dropColumn('order');
});
}
}
+1
View File
@@ -21,6 +21,7 @@ class Component extends Eloquent implements \Dingo\Api\Transformer\Transformable
'user_id',
'tags',
'link',
'order',
];
/**
+1
View File
@@ -40,6 +40,7 @@ Route::group(['before' => 'auth', 'prefix' => 'dashboard'], function () {
// Internal API.
// This should only be used for making requests within the dashboard.
Route::group(['prefix' => 'api'], function () {
Route::post('components/order', 'DashAPIController@postUpdateComponentOrder');
Route::post('components/{component}', 'DashAPIController@postUpdateComponent');
});
});
+19 -16
View File
@@ -12,24 +12,27 @@
</div>
<div class="content-wrapper">
<div class="row">
<div class="col-sm-12 striped-list">
@forelse($components as $component)
<div class='row striped-list-item'>
<div class='col-md-8'>
<strong>{{ $component->name }}</strong> <small>{{ $component->humanStatus }}</small>
@if($component->description)
<p><small>{{ $component->description }}</small></p>
@endif
</div>
<div class='col-md-4 text-right'>
<a href='/dashboard/components/{{ $component->id }}/edit' class='btn btn-default'>Edit</a>
<a href='/dashboard/components/{{ $component->id }}/delete' class='btn btn-danger'>Delete</a>
<form name='componentList'>
<div class="col-sm-12 striped-list" id='component-list'>
@forelse($components as $component)
<div class='row striped-list-item'>
<div class='col-md-8'>
<h4><span class='drag-handle'><i class='ion-drag'></i></span> {{ $component->name }} <small>{{ $component->humanStatus }}</small></h4>
@if($component->description)
<p><small>{{ $component->description }}</small></p>
@endif
</div>
<div class='col-md-4 text-right'>
<a href='/dashboard/components/{{ $component->id }}/edit' class='btn btn-default'>Edit</a>
<a href='/dashboard/components/{{ $component->id }}/delete' class='btn btn-danger'>Delete</a>
</div>
<input type='hidden' rel='order' name='component[{{ $component->id }}]' value='{{ $component->order }}' />
</div>
@empty
<div class='list-group-item text-danger'>You should add a component.</div>
@endforelse
</div>
@empty
<div class='list-group-item text-danger'>You should add a component.</div>
@endforelse
</div>
</form>
</div>
</div>
@stop