/
project.controller.js
1570 lines (1431 loc) · 68 KB
/
project.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function () {
'use strict';
/**
* Project controller which manages activating a project the UI for user task selection and contribution
* TODO: refactor this controller. It is getting big!
*/
angular
.module('taskingManager')
.controller('projectController', ['$timeout', '$interval', '$scope', '$location', '$routeParams', '$window', 'moment', 'configService', 'mapService', 'projectService', 'styleService', 'taskService', 'geospatialService', 'editorService', 'authService', 'accountService', 'userService', 'licenseService', 'messageService', 'drawService', 'languageService', 'userPreferencesService', projectController]);
function projectController($timeout, $interval, $scope, $location, $routeParams, $window, moment, configService, mapService, projectService, styleService, taskService, geospatialService, editorService, authService, accountService, userService, licenseService, messageService, drawService, languageService, userPreferencesService) {
var vm = this;
vm.id = 0;
vm.projectData = null;
vm.taskVectorLayer = null;
vm.highlightVectorLayer = null;
vm.lockedByCurrentUserVectorLayer = null;
vm.map = null;
vm.user = null;
vm.maxlengthComment = configService.maxCommentLength;
vm.taskUrl = '';
// tab and view control
vm.currentTab = '';
vm.mappingStep = '';
vm.validatingStep = '';
vm.userCanMap = true;
vm.userCanValidate = true;
//error control
vm.taskErrorMapping = '';
vm.taskErrorValidation = '';
vm.taskLockError = false;
vm.taskLockErrorMessage = '';
vm.taskUnLockError = false;
vm.taskUnLockErrorMessage = '';
vm.taskSplitError = false;
vm.taskSplitCode == null;
vm.wasAutoUnlocked = false;
//authorization
vm.isAuthorized = false;
//status flags
vm.isSelectedMappable = false;
vm.isSelectedValidatable = false;
vm.isSelectedSplittable = false;
//task data
vm.selectedTaskData = null;
vm.lockedTaskData = null;
vm.lockTime = {};
vm.multiSelectedTasksData = [];
vm.multiLockedTasks = [];
//editor
vm.editorStartError = '';
vm.selectedEditor = 'ideditor';
//interaction
vm.selectInteraction = null;
vm.mappedTasksPerUser = [];
vm.lockedTasksForCurrentUser = [];
//bound from the html
vm.comment = '';
vm.usernames = [];
//table sorting control
vm.propertyName = 'username';
vm.reverse = true;
// License
vm.showLicenseModal = false;
vm.lockingReason = '';
//interval timer promise for autorefresh
var autoRefresh = undefined;
// Watch the languageService for change in language and get the project again when needed
$scope.$watch(function () {
return languageService.getLanguageCode();
}, function () {
updateDescriptionAndInstructions(vm.id);
}, true);
activate();
function activate() {
vm.currentTab = 'instructions';
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
vm.selectedEditor = 'ideditor'; // default to iD editor
mapService.createOSMMap('map');
mapService.addOverviewMap();
vm.map = mapService.getOSMMap();
vm.id = $routeParams.id;
updateMappedTaskPerUser(vm.id);
// Check the user's role and initialise project after the async call has finished
var session = authService.getSession();
if (session && session.username && session.username != "") {
var resultsPromise = accountService.getUser(session.username);
resultsPromise.then(function (user) {
vm.user = user;
initialiseProject(vm.id);
}, function () {
initialiseProject(vm.id);
});
}
else {
initialiseProject(vm.id);
}
var tab = $location.search().tab;
if (tab === 'chat') {
vm.currentTab = 'chat';
}
//start up a timer for autorefreshing the project.
autoRefresh = $interval(function () {
refreshProject(vm.id);
updateMappedTaskPerUser(vm.id);
//TODO do a selected task refesh too
}, 10000);
// set up the preferred editor from user preferences
vm.selectedEditor = userPreferencesService.getFavouriteEditor();
}
// listen for navigation away from the page event and stop the autrefresh timer
$scope.$on('$routeChangeStart', function () {
if (angular.isDefined(autoRefresh)) {
$interval.cancel(autoRefresh);
autoRefresh = undefined;
}
})
/**
* calculates padding number to makes sure there is plenty of clear space around feature on map to keep visual
* context of feature location
* @returns {number} - padding number
*/
function getPaddingSize() {
// padding to makes sure there is plenty of clear space around feature on map to keep visual
// context of feature location
return vm.map.getSize()[1] * 0.3;
}
/**
* convenience method to reset task data controller properties
*/
vm.resetTaskData = function () {
vm.selectedTaskData = null;
vm.lockedTaskData = null;
vm.multiSelectedTasksData = [];
vm.multiLockedTasks = [];
vm.lockedTasksForCurrentUser = [];
}
vm.updatePreferedEditor = function () {
userPreferencesService.setFavouriteEditor(vm.selectedEditor);
}
/**
* convenience method to reset task status controller properties
*/
vm.resetStatusFlags = function () {
vm.isSelectedMappable = false;
vm.isSelectedValidatable = false;
vm.isSelectedSplittable = false;
}
/**
* convenience method to reset error controller properties
*/
vm.resetErrors = function () {
vm.taskErrorMapping = '';
vm.taskErrorValidation = '';
vm.taskLockError = false;
vm.taskLockErrorMessage = '';
vm.taskUnLockError = false;
vm.taskUnLockErrorMessage = '';
vm.taskSplitError = false;
vm.taskSplitCode == null;
vm.taskUndoError = false;
vm.wasAutoUnlocked = false;
}
/**
* Make the passed in feature the selected feature and ensure view and map updates for selected feature
* @param feature - ol.Feature the feature to be selected
*/
function selectFeature(feature) {
vm.selectInteraction.getFeatures().clear();
vm.selectInteraction.getFeatures().push(feature);
onTaskSelection(feature);
}
/**
* Sets up a randomly selected task as the currently selected task
*/
vm.selectRandomTaskMap = function () {
var feature = taskService.getRandomMappableTaskFeature(vm.taskVectorLayer.getSource().getFeatures());
if (feature) {
selectFeature(feature);
var padding = getPaddingSize();
vm.map.getView().fit(feature.getGeometry().getExtent(), {padding: [padding, padding, padding, padding]});
}
else {
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a single function call was considered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
vm.taskErrorMapping = 'none-available';
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
}
};
vm.selectRandomTaskValidate = function () {
var feature = taskService.getRandomTaskFeatureForValidation(vm.taskVectorLayer.getSource().getFeatures());
if (feature) {
selectFeature(feature);
var padding = getPaddingSize();
vm.map.getView().fit(feature.getGeometry().getExtent(), {padding: [padding, padding, padding, padding]});
}
else {
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a single function call was considered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
vm.taskErrorValidation = 'none-available';
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
}
};
/**
* Select tasks to validate by letting the user draw a polygon
*/
vm.selectByPolygonValidate = function () {
if (vm.drawPolygonInteraction.getActive()) {
drawService.getSource().clear();
vm.selectInteraction.setActive(true);
vm.drawPolygonInteraction.setActive(false);
}
else {
vm.selectInteraction.setActive(false);
vm.drawPolygonInteraction.setActive(true);
}
};
/**
* Add the interactions for selecting tasks
*/
function addInteractions() {
// Priority areas: initialise the draw service with interactions
drawService.initInteractions(true, false, false, false, false, false);
// Get the interactions in the controller so events can be handled
vm.source = drawService.getSource();
// Draw interaction - adds it to the map in the draw service
vm.drawPolygonInteraction = drawService.getDrawPolygonInteraction();
// Select interaction
vm.selectInteraction = new ol.interaction.Select({
style: styleService.getSelectedTaskStyle,
layers: [vm.taskVectorLayer]
});
vm.map.addInteraction(vm.selectInteraction);
vm.selectInteraction.on('select', function (event) {
$scope.$apply(function () {
var feature = event.selected[0];
onTaskSelection(feature);
});
});
vm.drawPolygonInteraction.on('drawstart', function () {
drawService.getSource().clear();
});
// Check which tasks intersect and add them to the selected features manually
vm.drawPolygonInteraction.on('drawend', function (event) {
var selectedFeatures = vm.selectInteraction.getFeatures();
selectedFeatures.clear();
var polygon = event.feature.getGeometry();
var features = vm.taskVectorLayer.getSource().getFeatures();
vm.selectedTasksForValidation = [];
for (var i = 0; i < features.length; i++) {
if (polygon.intersectsExtent(features[i].getGeometry().getExtent())) {
var taskStatus = features[i].getProperties().taskStatus;
if (taskStatus === 'MAPPED' || taskStatus === 'VALIDATED') {
selectedFeatures.push(features[i]);
vm.selectedTasksForValidation.push(features[i].getProperties().taskId);
}
}
}
// Reactivate select after 300ms (to avoid single click trigger)
$timeout(function () {
vm.selectInteraction.setActive(true);
vm.drawPolygonInteraction.setActive(false);
drawService.getSource().clear();
}, 300);
});
}
/**
* 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.mappingStep = 'selecting';
// vm.validatingStep = 'selecting';
vm.selectInteraction.getFeatures().clear();
};
/**
* Undo task status change
*/
vm.undo = function(){
vm.taskUndoError = false;
var projectId = vm.projectData.projectId;
var taskId = vm.selectedTaskData.taskId;
var resultsPromise = taskService.undo(projectId, taskId);
resultsPromise.then(function (data) {
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
setUpSelectedTask(data);
refreshProject(vm.projectData.projectId);
}, function (error) {
// TODO - show message
vm.taskUndoError = true;
});
};
vm.getLockTime = function() {
var task = null;
if (vm.selectedTaskData) {
task = vm.selectedTaskData;
}
else if (vm.multiSelectedTasksData) {
task = vm.multiSelectedTasksData[0];
}
if (task != null && task.taskId in vm.lockTime) {
var lockTime = moment.utc(vm.lockTime[task.taskId]);
return lockTime.add(2, 'hours').diff(moment.utc(), 'minutes');
}
else {
return null;
}
};
/**
* Initilaise a project using it's id
* @param id - id of the project to initialise
*/
function initialiseProject(id) {
vm.errorGetProject = false;
var resultsPromise = projectService.getProject(id);
resultsPromise.then(function (data) {
//project returned successfully
vm.projectData = data;
vm.userCanMap = vm.user && projectService.userCanMapProject(vm.user.mappingLevel, vm.projectData.mapperLevel, vm.projectData.enforceMapperLevel);
vm.userCanValidate = vm.user && projectService.userCanValidateProject(vm.user.role, vm.projectData.enforceValidatorRole);
addAoiToMap(vm.projectData.areaOfInterest);
addPriorityAreasToMap(vm.projectData.priorityAreas);
addProjectTasksToMap(vm.projectData.tasks, true);
// Add OpenLayers interactions
addInteractions();
//add a layer for users locked tasks
if (!vm.lockedByCurrentUserVectorLayer) {
var source = new ol.source.Vector();
vm.lockedByCurrentUserVectorLayer = new ol.layer.Vector({
source: source,
name: 'lockedByCurrentUser',
style: styleService.getLockedByCurrentUserTaskStyle
});
vm.map.addLayer(vm.lockedByCurrentUserVectorLayer);
} else {
vm.lockedByCurrentUserVectorLayer.getSource().clear();
}
//add a layer for task highlighting
if (!vm.highlightVectorLayer) {
var source = new ol.source.Vector();
vm.highlightVectorLayer = new ol.layer.Vector({
source: source,
name: 'highlight',
style: styleService.getHighlightedTaskStyle
});
vm.map.addLayer(vm.highlightVectorLayer);
} else {
vm.highlightVectorLayer.getSource().clear();
}
if ($location.search().task) {
selectTaskById($location.search().task);
}
}, function () {
// project not returned successfully
vm.errorGetProject = true;
});
}
/**
* Update description and metadata
* @param id
*/
function updateDescriptionAndInstructions(id) {
vm.errorGetProject = false;
var resultsPromise = projectService.getProject(id);
resultsPromise.then(function (data) {
vm.projectData = data;
}, function () {
// project not returned successfully
vm.errorGetProject = true;
});
}
/**
* Gets project data from server and updates the map
* @param id - id of project to be refreshed
*/
function refreshProject(id) {
vm.errorGetProject = false;
var resultsPromise = projectService.getProject(id);
resultsPromise.then(function (data) {
//project returned successfully
vm.projectData = data;
addProjectTasksToMap(vm.projectData.tasks, false);
//TODO: move the selected task refresh to a separate function so it can be called separately
if (vm.selectedTaskData) {
var selectedFeature = taskService.getTaskFeatureById(vm.taskVectorLayer.getSource().getFeatures(), vm.selectedTaskData.taskId);
//this just forces the selected styling to apply
vm.selectInteraction.getFeatures().clear();
vm.selectInteraction.getFeatures().push(selectedFeature);
}
else if (vm.multiSelectedTasksData.length > 0) {
var tasks = vm.multiSelectedTasksData.map(function (task) {
return task.taskId;
});
var selectedFeatures = taskService.getTaskFeaturesByIds(vm.taskVectorLayer.getSource().getFeatures(), tasks);
vm.selectInteraction.getFeatures().clear();
selectedFeatures.forEach(function (feature) {
vm.selectInteraction.getFeatures().push(feature);
}
)
}
}, function () {
// project not returned successfully
vm.errorGetProject = true;
});
}
/**
* Select a task using its ID.
* @param taskId
*/
function selectTaskById(taskId) {
//select task on map if id provided in url
var task = taskService.getTaskFeatureById(vm.taskVectorLayer.getSource().getFeatures(), taskId);
if (task) {
selectFeature(task);
var padding = getPaddingSize();
vm.map.getView().fit(task.getGeometry().getExtent(), {padding: [padding, padding, padding, padding]});
}
}
/**
* Adds project tasks to map as features from geojson
* @param tasks
* @param fitToProject - boolean to control whether to refit map view to project extent
*/
function addProjectTasksToMap(tasks, fitToProject) {
//TODO: may want to refactor this into a service at some point so that it can be reused
var source;
if (!vm.taskVectorLayer) {
source = new ol.source.Vector();
vm.taskVectorLayer = new ol.layer.Vector({
source: source,
name: 'tasks',
style: styleService.getTaskStyle
});
vm.map.addLayer(vm.taskVectorLayer);
// change mouse cursor when over vector feature
vm.map.on('pointermove', function (e) {
var pixel = vm.map.getEventPixel(e.originalEvent);
var hit = vm.map.hasFeatureAtPixel(pixel, {
layerFilter: function (layerCandidate) {
return layerCandidate == vm.taskVectorLayer;
}
});
vm.map.getTargetElement().style.cursor = hit ? 'pointer' : '';
});
} else {
source = vm.taskVectorLayer.getSource();
source.clear();
}
var taskFeatures = geospatialService.getFeaturesFromGeoJSON(tasks);
source.addFeatures(taskFeatures);
//add locked tasks to the locked tasks vector layer
var projectId = vm.projectData.projectId;
if (authService.isUserLoggedIn()) {
updateLockedTasksForCurrentUser(projectId);
}
if (fitToProject) {
vm.map.getView().fit(source.getExtent());
}
}
/**
* Updates the data for mapped tasks by user
* @param projectId
*/
function updateMappedTaskPerUser(projectId) {
var mappedTasksByUserPromise = taskService.getMappedTasksByUser(projectId);
mappedTasksByUserPromise.then(function (data) {
vm.mappedTasksPerUser = data.mappedTasks;
}, function () {
vm.mappedTasksPerUser = [];
});
}
/**
* Updates the map and contoller data for tasks locked by current user
* @param projectId
*/
function updateLockedTasksForCurrentUser(projectId) {
var lockedTasksPromise = taskService.getLockedTasksForCurrentUser(projectId);
lockedTasksPromise.then(function (lockedTasks) {
vm.lockedTasksForCurrentUser = lockedTasks;
vm.lockedByCurrentUserVectorLayer.getSource().clear();
if (vm.lockedTasksForCurrentUser.length > 0) {
var features = taskService.getTaskFeaturesByIds(vm.taskVectorLayer.getSource().getFeatures(), vm.lockedTasksForCurrentUser);
vm.lockedByCurrentUserVectorLayer.getSource().addFeatures(features);
}
}, function () {
if (vm.lockedByCurrentUserVectorLayer) {
vm.lockedByCurrentUserVectorLayer.getSource().clear();
}
vm.lockedTasksForCurrentUser = [];
if (vm.mappingStep === 'locked' || vm.validatingStep === 'locked') {
vm.mappingStep = 'viewing';
vm.validatingStep = 'viewing';
vm.wasAutoUnlocked = true;
}
});
}
function getLastLockedAction(task) {
var mostRecentAction = task.taskHistory[0];
task.taskHistory.forEach(function(action) {
if (action.actionDate > mostRecentAction.actionDate) {
mostRecentAction = action;
}
});
return mostRecentAction;
}
/**
* Has the current user got tasks locked for mapping
* @returns {boolean}
*/
vm.hasTaskLockedForMapping = function () {
if (vm.taskVectorLayer) {
var lockedFeatures = taskService.getTaskFeaturesByIdAndStatus(vm.taskVectorLayer.getSource().getFeatures(), vm.lockedTasksForCurrentUser, 'LOCKED_FOR_MAPPING');
return lockedFeatures.length == 1;
}
};
/**
* Reselect tasks user has locked for mapping
*/
vm.reselectTaskForMapping = function () {
if (vm.taskVectorLayer) {
var lockedFeatures = taskService.getTaskFeaturesByIdAndStatus(vm.taskVectorLayer.getSource().getFeatures(), vm.lockedTasksForCurrentUser, 'LOCKED_FOR_MAPPING');
if (lockedFeatures.length == 1) {
selectFeature(lockedFeatures[0]);
onTaskSelection(lockedFeatures[0]);
var padding = getPaddingSize();
vm.map.getView().fit(lockedFeatures[0].getGeometry().getExtent(), {padding: [padding, padding, padding, padding]});
}
}
};
/**
* Has the current user got tasks locked for validation
* @returns {boolean}
*/
vm.hasTasksLockedForValidation = function () {
if (vm.taskVectorLayer) {
var lockedFeatures = taskService.getTaskFeaturesByIdAndStatus(vm.taskVectorLayer.getSource().getFeatures(), vm.lockedTasksForCurrentUser, 'LOCKED_FOR_VALIDATION');
return lockedFeatures.length > 0;
}
};
/**
* Reselect tasks user has locked for validation
*/
vm.reselectTasksForValidation = function () {
if (vm.taskVectorLayer) {
var lockedFeatures = taskService.getTaskFeaturesByIdAndStatus(vm.taskVectorLayer.getSource().getFeatures(), vm.lockedTasksForCurrentUser, 'LOCKED_FOR_VALIDATION');
if (lockedFeatures.length == 1) {
selectFeature(lockedFeatures[0]);
onTaskSelection(lockedFeatures[0]);
var padding = getPaddingSize();
vm.map.getView().fit(lockedFeatures[0].getGeometry().getExtent(), {padding: [padding, padding, padding, padding]});
}
else if (lockedFeatures.length > 1) {
vm.selectInteraction.getFeatures().clear();
//select each one by one
lockedFeatures.forEach(function (feature) {
vm.selectInteraction.getFeatures().push(feature);
});
var extent = geospatialService.getBoundingExtentFromFeatures(lockedFeatures);
// Zoom to the extent to get the right zoom level for the editorsgit commit -a
vm.map.getView().fit(extent);
//put the UI in to locked for multi validation mode
var lockPromise = taskService.getLockedTaskDetailsForCurrentUser(vm.projectData.projectId);
lockPromise.then(function (tasks) {
// Filter to get the ones locked for validation
var tasksLockedForValidation = tasks.filter(function (task) {
if (task.taskStatus == 'LOCKED_FOR_VALIDATION'){
return task;
}
});
// refresh the project, to ensure we catch up with any status changes that have happened meantime
// on the server
// TODO - The following reset lines are repeated in several places in this file.
// Refactoring to a single function call was considered, however it was decided that the ability to
// call the resets individually was desirable and would help readability.
// The downside is that any change will have to be replicated in several places.
// A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
vm.currentTab = 'validation';
vm.validatingStep = 'multi-locked';
vm.multiSelectedTasksData = tasksLockedForValidation;
vm.multiLockedTasks = tasksLockedForValidation;
vm.isSelectedValidatable = true;
}, function (error) {
// TODO - handle error
});
}
}
};
/**
* Adds the aoi feature to the map
* @param 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,
name: 'aoi'
});
vm.map.addLayer(vector);
// read tasks JSON into features
var aoiFeature = geospatialService.getFeatureFromGeoJSON(aoi)
source.addFeature(aoiFeature);
}
/**
* Adds the priority areas to the map
* @param priorityAreas
*/
function addPriorityAreasToMap(priorityAreas) {
var source = new ol.source.Vector();
var vector = new ol.layer.Vector({
source: source,
name: 'priorityareas'
});
vm.map.addLayer(vector);
source.on('addfeature', function (event) {
// Add style to make it stand out from the AOI
var style = new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255,0,0,0.2)' //red
}),
stroke: new ol.style.Stroke({
color: 'rgba(255,0,0,1)', //red
width: 1
})
});
event.feature.setStyle(style);
});
if (priorityAreas) {
for (var i = 0; i < priorityAreas.length; i++) {
var feature = geospatialService.getFeatureFromGeoJSON(priorityAreas[i]);
source.addFeature(feature);
}
if (priorityAreas.length > 0) {
vm.hasPriorityArea = true;
}
}
}
/**
* Gets a task from the server and sets up the task returned as the currently selected task
* @param feature
*/
function onTaskSelection(feature) {
//if no feature has been clicked on, go to unselected more
if (!feature) {
vm.selectedTaskData = null;
vm.lockedTaskData = null;
vm.multiSelectedTasksData = [];
vm.multiLockedTasks = [];
vm.resetErrors();
vm.resetStatusFlags();
vm.clearCurrentSelection();
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
return;
}
//we don't want to allow selection of multiple features by map click
//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
//reset task errors
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a single function call was considered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.selectedTaskData = null;
vm.lockedTaskData = null;
vm.multiSelectedTasksData = [];
vm.multiLockedTasks = [];
setUpSelectedTask(data);
// TODO: This is a bit icky. Need to find something better. Maybe when roles are in place.
// Need to make a decision on what tab to go to if user has clicked map but is not on mapping or validating
// tab
if (vm.currentTab === 'description' || vm.currentTab === 'instructions') {
//prioritise validation
vm.currentTab = vm.isSelectedValidatable ? 'validation' : 'mapping';
}
}, function () {
// task not returned successfully
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a sinhle function call was cinsidered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
vm.taskErrorMapping = 'task-get-error';
vm.taskErrorValidation = 'task-get-error';
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
if (vm.currentTab === 'description' || vm.currentTab !== 'instructions') {
//prioritise mapping
vm.currentTab = 'mapping';
}
});
}
/**
* Sets up the view model for the task options and actions for passed in task data object.
* @param data - task JSON data object
*/
function setUpSelectedTask(data) {
var isLockedByMeMapping = data.taskStatus === 'LOCKED_FOR_MAPPING' && data.lockHolder === vm.user.username;
var isLockedByMeValidation = data.taskStatus === 'LOCKED_FOR_VALIDATION' && data.lockHolder === vm.user.username;
vm.isSelectedMappable = (isLockedByMeMapping || data.taskStatus === 'READY' || data.taskStatus === 'INVALIDATED');
vm.isSelectedValidatable = (isLockedByMeValidation || data.taskStatus === 'MAPPED' || data.taskStatus === 'VALIDATED' || data.taskStatus === 'BADIMAGERY');
vm.selectedTaskData = data;
// Format the comments by adding links to the usernames
var history = vm.selectedTaskData.taskHistory;
if (history) {
for (var i = 0; i < history.length; i++) {
history[i].actionText = messageService.formatUserNamesToLink(history[i].actionText);
}
}
//jump to locked step if mappable and locked by me
if (isLockedByMeMapping) {
vm.mappingStep = 'locked';
vm.lockedTaskData = data;
vm.currentTab = 'mapping';
vm.lockTime[vm.selectedTaskData.taskId] = getLastLockedAction(vm.lockedTaskData).actionDate;
}
else {
vm.mappingStep = 'viewing';
}
//jump to validatable step if validatable and locked by me
if (isLockedByMeValidation) {
vm.validatingStep = 'locked';
vm.lockedTaskData = data;
vm.currentTab = 'validation';
vm.lockTime[vm.selectedTaskData.taskId] = getLastLockedAction(vm.lockedTaskData).actionDate;
}
else {
vm.validatingStep = 'viewing';
}
//update browser address bar with task id search params
$location.search('task', data.taskId);
}
/**
* Get the full URL for the social media widget
* @returns {*}
*/
vm.getSocialMediaUrl = function () {
return $location.absUrl();
};
/**
* Call api to unlock currently locked task after mapping. Will pass the comment and new status to api. Will update view and map after unlock.
* @param comment
* @param status
*/
vm.unLockTaskMapping = function (comment, status) {
var projectId = vm.projectData.projectId;
var taskId = vm.lockedTaskData.taskId;
var unLockPromise = taskService.unLockTaskMapping(projectId, taskId, comment, status);
vm.comment = '';
unLockPromise.then(function (data) {
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a single function call was considered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
refreshProject(projectId);
updateMappedTaskPerUser(projectId);
vm.clearCurrentSelection();
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
}, function (error) {
onUnLockError(projectId, error);
});
};
/**
* Call api to stop mapping currebtly locked task. Will pass the comment to api. Will update view and map after unlock.
* @param comment
*/
vm.stopMapping = function (comment) {
var projectId = vm.projectData.projectId;
var taskId = vm.lockedTaskData.taskId;
var stopPromise = taskService.stopMapping(projectId, taskId, comment);
vm.comment = '';
stopPromise.then(function (data) {
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a single function call was considered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
refreshProject(projectId);
updateMappedTaskPerUser(projectId);
vm.clearCurrentSelection();
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
}, function (error) {
onUnLockError(projectId, error);
});
};
/**
* Call api to unlock currently locked task after validation. Will pass the comment and new status to api. Will update view and map after unlock.
* @param comment
* @param status
*/
vm.unLockTaskValidation = function (comment, status) {
var projectId = vm.projectData.projectId;
var taskId = vm.lockedTaskData.taskId;
var tasks = [{
comment: comment,
status: status,
taskId: taskId
}];
var unLockPromise = taskService.unLockTaskValidation(projectId, tasks);
vm.comment = '';
unLockPromise.then(function (data) {
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a single function call was considered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
refreshProject(projectId);
updateMappedTaskPerUser(projectId);
vm.clearCurrentSelection();
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
}, function (error) {
onUnLockError(projectId, error);
});
};
/**
* Call api to stop validating locked task. Will pass the comment to api. Will update view and map after unlock.
* @param comment
*/
vm.stopValidating = function (comment) {
var projectId = vm.projectData.projectId;
var taskId = vm.lockedTaskData.taskId;
var tasks = [{
comment: comment,
taskId: taskId
}];
var unLockPromise = taskService.stopValidating(projectId, tasks);
vm.comment = '';
unLockPromise.then(function (data) {
//TODO - The following reset lines are repeated in several places in this file.
//Refactoring to a single function call was considered, however it was decided that the ability to
//call the resets individually was desirable and would help readability.
//The downside is that any change will have to be replicated in several places.
//A fundamental refactor of this controller should be considered at some stage.
vm.resetErrors();
vm.resetStatusFlags();
vm.resetTaskData();
refreshProject(projectId);
updateMappedTaskPerUser(projectId);
vm.clearCurrentSelection();
vm.mappingStep = 'selecting';
vm.validatingStep = 'selecting';
}, function (error) {
onUnLockError(projectId, error);
});
};
/**
* Call api to unlock currently locked tasks after validation. Will pass the comment and new status to api. Will update view and map after unlock.
* @param comment
* @param status
*/
vm.unLockMultiTaskValidation = function (comment, status) {
var projectId = vm.projectData.projectId;
var data = vm.multiLockedTasks;
var tasks = data.map(function (task) {
return {
comment: comment,
status: status,
taskId: task.taskId
};
});