-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP implement displaying and editing of the export area
References #4
- Loading branch information
Showing
13 changed files
with
599 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/resources/assets/js/annotations/controllers/ExportAreaSettingsController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/resources/assets/js/annotations/factories/ExportArea.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
Oops, something went wrong.