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

Ht 38 select task #51

Merged
merged 17 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from 16 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
116 changes: 100 additions & 16 deletions client/app/project/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,92 @@
*/
angular
.module('taskingManager')
.controller('projectController', ['$location', 'mapService', 'projectService', 'styleService', projectController]);
.controller('projectController', ['$scope', '$location', 'mapService', 'projectService', 'styleService', 'taskService', projectController]);

function projectController($location, mapService, projectService, styleService) {
function projectController($scope, $location, mapService, projectService, styleService, taskService) {
var vm = this;
vm.project = null;
vm.projectData = null;
vm.taskVectorLayer = null;
vm.map = null;

// tab and view control
vm.currentTab = '';
vm.mappingStep = '';

//selected task
vm.selectedTask = null;
vm.isSelectTaskMappable = false;

//interaction
var select = new ol.interaction.Select({
style: styleService.getSelectedStyleFunction
});

activate();

function activate() {
//TODO: Set up sidebar tabs
vm.currentTab = 'description';
vm.mappingStep = 'select';
mapService.createOSMMap('map');
vm.map = mapService.getOSMMap();

vm.map.addInteraction(select);
select.on('select', function (event) {
$scope.$apply(function () {
var feature = event.selected[0];
onTaskSelection(feature);
});
});

var id = $location.search().project;
initialiseProject(id);
//TODO: put the project metadata (description instructions on disebar tabs
//TODO: put the project metadata (description and instructions on siedbar tabs)
}

/**
* Get a project with using it's id
* Sets up a randomly selected task as the currently selected task
*/
function initialiseProject(id){
vm.selectRandomTask = function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add function comment

var feature = taskService.getRandomMappableTaskFeature(vm.taskVectorLayer.getSource().getFeatures());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a lot neater now 👍

if (feature) {
select.getFeatures().clear();
select.getFeatures().push(feature);
onTaskSelection(feature);
// padding to makes sure there is plenty of clear space around feature on map to keep visual
// context of feature location
var vPadding = vm.map.getSize()[1] * 0.3;
vm.map.getView().fit(feature.getGeometry().getExtent(), {padding: [vPadding, vPadding, vPadding, vPadding]});
}
else {
vm.selectedTask = null;
vm.isSelectTaskMappable = false;
vm.mappingStep = 'none-available';
}
};

/**
* clears the currently selected task. Clears down/resets the vm properties and clears the feature param in the select interaction object.
*/
vm.clearCurrentSelection = function () {
vm.selectedTask = null;
vm.isSelectTaskMappable = false;
vm.currentTab = 'mapping';
vm.mappingStep = 'select';
select.getFeatures().clear();
};

/**
* Initilaise a project using it's id
* @param id - id of the project to initialise
*/
function initialiseProject(id) {
var resultsPromise = projectService.getProject(id);
resultsPromise.then(function (data) {
//project returned successfully
vm.project = data;
addAoiToMap(vm.project.areaOfInterest);
addProjectTasksToMap(vm.project.tasks);
}, function(){
vm.projectData = data;
addAoiToMap(vm.projectData.areaOfInterest);
addProjectTasksToMap(vm.projectData.tasks);
}, function () {
// project not returned successfully
// TODO - may want to handle error
});
Expand All @@ -47,16 +102,18 @@
* Adds project tasks to map as features from geojson
* @param tasks
*/
function addProjectTasksToMap(tasks){
function addProjectTasksToMap(tasks) {
//TODO: may want to refactor this into a service at some point so that it can be reused
var source = new ol.source.Vector();
var vector = new ol.layer.Vector({
vm.taskVectorLayer = new ol.layer.Vector({
source: source,
name: 'tasks',
style: styleService.getTaskStyleFunction
});
vm.map.addLayer(vector);
vm.map.addLayer(vm.taskVectorLayer);

// read tasks JSON into features
console.log(tasks);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove console.log

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

var format = new ol.format.GeoJSON();
var taskFeatures = format.readFeatures(tasks, {
dataProjection: 'EPSG:4326',
Expand All @@ -70,11 +127,12 @@
* Adds the aoi feature to the map
* @param aoi
*/
function addAoiToMap(aoi){
function addAoiToMap(aoi) {
//TODO: may want to refactor this into a service at some point so that it can be resused
var source = new ol.source.Vector();
var vector = new ol.layer.Vector({
source: source
source: source,
name: 'aoi'
});
vm.map.addLayer(vector);

Expand All @@ -86,5 +144,31 @@
});
source.addFeature(aoiFeatures);
}

/**
* Gets a task from the server and uses sets up the task returned as the currently selected task
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in description

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @param feature
*/
function onTaskSelection(feature) {
//get id from feature
var taskId = feature.get('taskId');
var projectId = vm.projectData.projectId;
// get full task from task service call
var taskPromise = taskService.getTask(projectId, taskId);
taskPromise.then(function (data) {
//task returned successfully
vm.selectedTask = data;
vm.isSelectTaskMappable = !data.taskLocked && (data.taskStatus === 'READY' || data.taskStatus === 'INVALIDATED');
vm.currentTab = 'mapping';
vm.mappingStep = 'view';
}, function () {
// task not returned successfully
// TODO - may want to handle error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is handled so we can remove the TODO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

vm.selectedTask = null;
vm.isSelectTaskMappable = false;
vm.currentTab = 'mapping';
vm.mappingStep = 'task-get-error';
});
}
}
})();
Loading