Skip to content

Commit

Permalink
WIP implement displaying and editing of the export area
Browse files Browse the repository at this point in the history
References #4
  • Loading branch information
mzur committed Jul 26, 2016
1 parent 4c32348 commit 790672a
Show file tree
Hide file tree
Showing 13 changed files with 599 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ elixir(function (mix) {
process.chdir('src');
// mix.sass('main.scss', 'public/assets/styles/main.css');
mix.angular('resources/assets/js/projects/', 'public/assets/scripts', 'projects.js');
mix.angular('resources/assets/js/annotations/', 'public/assets/scripts', 'annotations.js');
mix.task('publish', 'public/assets/**/*');
});

gulp.task('publish', function () {
gulp.src('').pipe(shell('php ../../../../artisan vendor:publish --provider="Dias\\Modules\\Export\\ExportServiceProvider" --force'));
gulp.src('').pipe(shell('php ../../../../artisan vendor:publish --provider="Dias\\Modules\\Export\\ExportServiceProvider" --tag public --force'));
});
2 changes: 2 additions & 0 deletions src/ExportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function boot(Modules $modules,Router $router)

$modules->addMixin('export', 'projectsShow');
$modules->addMixin('export', 'projectsShowScripts');
$modules->addMixin('export', 'annotationsSettings');
$modules->addMixin('export', 'annotationsScripts');
}

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

namespace Dias\Modules\Export\Http\Controllers\Api;

use Exception;
use Dias\Modules\Export\Transect;
use Dias\Transect as BaseTransect;
use Dias\Http\Controllers\Api\Controller;

class TransectExportAreaController extends Controller
{
/**
* Show the export area of the transect
*
* @api {get} transects/:id/export-area Show the export area
* @apiGroup Transects
* @apiName IndexTransectsExportArea
* @apiPermission member
* @apiDescription The export area is a rectangle defined by two points. This endpoint returns an array containing the coordinates as follows: `[x1, y1, x2, y2]`.
* The first point may be any of the 4 points of the rectangle. The second point is the point not directly adjacent to the first.
*
* @apiSuccessExample {json} Success response:
* [100, 100, 1200, 600]
*
* @param int $id Transect ID
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$transect = BaseTransect::findOrFail($id);
$this->authorize('access', $transect);

return Transect::convert($transect)->exportArea;
}

/**
* Set the export area
*
* @api {post} transects/:id/export-area Set the export area
* @apiGroup Transects
* @apiName StoreTransectsExportArea
* @apiPermission admin
*
* @apiParam (Required attributes) {Number[]} coordinates Coordinates of the export area formatted as `[x1, y1, x2, y2]` array of integers
*
* @param int $id Transect ID
* @return \Illuminate\Http\Response
*/
public function store($id)
{
$transect = BaseTransect::findOrFail($id);
$this->authorize('update', $transect);
$this->validate($this->request, Transect::$storeRules);

$transect = Transect::convert($transect);

try {
$transect->exportArea = $this->request->input('coordinates');
$transect->save();
} catch (Exception $e) {
return $this->buildFailedValidationResponse($this->request, [
'coordinates' => $e->getMessage(),
]);
}
}

/**
* Remove the export area
*
* @api {delete} transects/:id/export-area Remove the export area
* @apiGroup Transects
* @apiName DestroyTransectsExportArea
* @apiPermission admin
*
* @param int $id Transect ID
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$transect = BaseTransect::findOrFail($id);
$this->authorize('update', $transect);
$transect = Transect::convert($transect);
$transect->exportArea = null;
$transect->save();
}
}
16 changes: 16 additions & 0 deletions src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@
'prefix' => 'api/v1',
'middleware' => 'auth.api',
], function ($router) {

$router->get('projects/{id}/reports/basic', [
'uses' => 'ReportsController@basic',
]);

$router->get('projects/{id}/reports/extended', [
'uses' => 'ReportsController@extended',
]);

$router->get('projects/{id}/reports/full', [
'uses' => 'ReportsController@full',
]);

$router->get('transects/{id}/export-area', [
'uses' => 'TransectExportAreaController@show',
]);

$router->post('transects/{id}/export-area', [
'uses' => 'TransectExportAreaController@store',
]);

$router->delete('transects/{id}/export-area', [
'uses' => 'TransectExportAreaController@destroy',
]);
});

// this route should be public (is protected by random uids)
$router->get('api/v1/reports/{uid}/{filename}', [
'uses' => 'Api\ReportsController@show',
]);

85 changes: 85 additions & 0 deletions src/Transect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Dias\Modules\Export;

use Dias\Transect as BaseTransect;
use Exception;

/**
* Extends the base Dias transect
*/
class Transect extends BaseTransect {

/**
* Name of the attribute that stores export area rectangle for the transect
*
* @var string
*/
const EXPORT_AREA_ATTRIBUTE = 'export_area';

/**
* Validation rules for setting the export area
*
* @var array
*/
public static $storeRules = [
'coordinates' => 'required|array',
];

/**
* Converts a regular Dias transect to an export transect
*
* @param BaseTransect $transect Regular Dias transect instance
*
* @return Transect
*/
public static function convert(BaseTransect $transect)
{
$instance = new static;
$instance->setRawAttributes($transect->attributes);
$instance->exists = $transect->exists;
return $instance->setRelations($transect->relations);
}

/**
* Return the dynamic attribute for the export area
*
* @return array
*/
public function getExportAreaAttribute()
{
return array_get($this->attrs, self::EXPORT_AREA_ATTRIBUTE);
}

/**
* Set or update the dynamic attribute for the export area
*
* @param array $value The value to set
*/
public function setExportAreaAttribute($value)
{
if (!is_array($value) && !is_null($value)) {
throw new Exception("Export area coordinates must be an array!");
}

$attrs = $this->attrs;

if ($value === null) {
unset($attrs[self::EXPORT_AREA_ATTRIBUTE]);
} else {
if (sizeof($value) !== 4) {
throw new Exception("Malformed export area coordinates!");
}

foreach ($value as $coordinate) {
if (!is_int($coordinate)) {
throw new Exception("Malformed export area coordinates!");
}
}

$attrs[self::EXPORT_AREA_ATTRIBUTE] = $value;
}

$this->attrs = $attrs;
}
}
1 change: 1 addition & 0 deletions src/public/assets/scripts/annotations.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @namespace dias.annotations
* @ngdoc controller
* @name ExportAreaSettingsController
* @memberOf dias.annotations
* @description Controller for ATE example patches settings
*/
angular.module('dias.annotations').controller('ExportAreaSettingsController', function ($scope, exportArea) {
"use strict";

$scope.setDefaultSettings('export_area_opacity', '1');

$scope.edit = function () {
if (!$scope.isShown()) {
$scope.settings.export_area_opacity = '1';
}

exportArea.toggleEdit();
};

$scope.isEditing = exportArea.isEditing;

$scope.isShown = function () {
return $scope.settings.export_area_opacity !== '0';
};

$scope.delete = function () {
if (exportArea.hasArea() && confirm('Do you really want to delete the export area?')) {
exportArea.deleteArea();
}
};

$scope.$on('image.shown', exportArea.updateHeight);

$scope.$watch('settings.export_area_opacity', exportArea.setOpacity);
}
);
25 changes: 25 additions & 0 deletions src/resources/assets/js/annotations/factories/ExportArea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @ngdoc factory
* @name ExportArea
* @memberOf dias.annotations
* @description Provides the resource for the export area of a transect
* @requires $resource
* @returns {Object} A new [ngResource](https://docs.angularjs.org/api/ngResource/service/$resource) object
* @example
// get the export area
var area = ExportArea.query({transect_id: 1}, function () {
console.log(area); // [10, 20, 30, 40]
});
// set the area
ExportArea.save({transect_id: 1}, {coordinates: [10, 20, 30, 40]});
// delete the area
ExportArea.delete({transect_id: 1});
*
*/
angular.module('dias.annotations').factory('ExportArea', function ($resource, URL) {
"use strict";

return $resource(URL + '/api/v1/transects/:transect_id/export-area');
});
Loading

0 comments on commit 790672a

Please sign in to comment.