-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathEditLog.vue
1176 lines (1089 loc) · 33.1 KB
/
EditLog.vue
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
<template>
<div class="tab-container">
<div class="tab-bar">
<div
class="tab"
:class="{ selected: tabSelected === 'FIRST' }"
@click="tabSelected = 'FIRST'">
<h5>GENERAL</h5>
</div>
<div
class="tab"
:class="{ selected: tabSelected === 'SECOND' }"
@click="tabSelected = 'SECOND'">
<h5>MOVEMENT</h5>
</div>
</div>
<div
class="tab-indicator"
:class="[
{first: tabSelected === 'FIRST' },
{second: tabSelected === 'SECOND' },
]"/>
<div
class="container-fluid tab-content first"
:class="{ selected: tabSelected === 'FIRST' }">
<br>
<div class="form-item form-group">
<toggle-check
label="Done"
labelPosition="after"
:checked="currentLog.done"
@input="updateCurrentLog('done', $event)"/>
</div>
<div class="form-item form-item-name form-group">
<label for="name" class="control-label">Name</label>
<input
:value="currentLog.name"
@input="updateCurrentLog('name', $event.target.value)"
placeholder="Enter name"
type="text"
class="form-control"
autofocus>
</div>
<date-and-time-form
:timestamp="currentLog.timestamp"
@input="updateCurrentLog('timestamp', $event)"/>
<!-- Allow users to change type for logs that have not yet been sent to the server
For logs currently on the server, display type as text -->
<div class="form-item form-item-name form-group">
<label for="type" class="control-label ">Log Type</label>
<div class="input-group" v-if="(currentLog.id === undefined)">
<select
:value="currentLog.type"
@input="updateCurrentLog('type', $event.target.value)"
class="custom-select col-sm-3 ">
<!-- options are defined in the local logTypes variable -->
<option
v-for="(type, typeKey) in logTypes"
:value="typeKey"
:key="`${type.label}-${typeKey}`">
{{ type.label }}
</option>
</select>
</div>
<div class="form-item" v-if="!(currentLog.id === undefined)">
<p> {{ logTypes[currentLog.type].label }} </p>
</div>
</div>
<div class="form-item form-item-name form-group">
<label for="notes" class="control-label ">Notes</label>
<textarea
:value="parseNotes(currentLog.notes)"
@input="updateNotes($event.target.value)"
placeholder="Enter notes"
type="text"
class="form-control">
</textarea>
</div>
<h4>Log Categories</h4>
<div id="categories" class="form-item form-group">
<p v-if="!showAllCategories
&& (!currentLog.log_category
|| currentLog.log_category.length < 1)">
No categories selected
</p>
<select-box
small
v-for="cat in filteredCategories"
:id="`category-${cat.tid}-${cat.name}`"
:selected="currentLog.log_category
&& currentLog.log_category.some(_cat => cat.tid === _cat.id)"
:label="cat.name"
:key="`category-${cat.tid}-${cat.name}`"
@input="
$event
? addCategory(cat.tid)
: removeCategory(currentLog
.log_category.findIndex(_cat => cat.tid === _cat.id))"
/>
<div class="show-hide">
<div v-if="!showAllCategories" @click="showAllCategories = !showAllCategories">
<p><icon-expand-more/>Show More</p>
</div>
<div v-if="showAllCategories" @click="showAllCategories = !showAllCategories">
<p><icon-expand-less/>Show Less</p>
</div>
</div>
</div>
<div v-if="currentLog.quantity !== undefined">
<h4>Quantities</h4>
<label for="quantity" class="control-label ">Add new or edit existing quantity</label>
<div v-if="currentQuant >= 0" class="form-item form-item-name form-group">
<!-- To display a placeholder value ONLY when there are no existing quantities,
we must add the placeholder with an <option> tag and select it using the :value option -->
<select
:value="(currentLog.quantity
&& currentLog.quantity.length > 0
&& currentLog.quantity[currentQuant].measure)
? currentLog.quantity[currentQuant].measure
: 'Select measure'"
@input="updateQuantity('measure', $event.target.value, currentQuant)"
class="custom-select col-sm-3 ">
<option>Select measure</option>
<option
v-for="(measure, i) in quantMeasures"
:value="measure"
:key="`measure-${i}`">
{{ measure }}
</option>
</select>
<input
:value="(currentLog.quantity
&& currentLog.quantity.length > 0)
? currentLog.quantity[currentQuant].value
: null"
@input="updateQuantity('value', $event.target.value, currentQuant)"
placeholder="Enter value"
type="number"
class="form-control"/>
<select
:value="(currentLog.quantity
&& currentLog.quantity.length > 0
&& currentLog.quantity[currentQuant].unit)
? currentLog.quantity[currentQuant].unit.id
: 'Select unit'"
@input="updateQuantity('unit', $event.target.value, currentQuant)"
class="custom-select col-sm-3 ">
<option>Select unit</option>
<option
v-for="(unit, i) in units"
:value="unit.tid"
:key="`unit-${i}`">
{{ (units) ? unit.name : '' }}
</option>
</select>
<input
:value="(currentLog.quantity
&& currentLog.quantity.length > 0)
? currentLog.quantity[currentQuant].label
: null"
@input="updateQuantity('label', $event.target.value, currentQuant)"
placeholder="Enter label"
type="text"
class="form-control"/>
</div>
<div class="form-item form-group">
<ul
v-if="currentLog.quantity
&& currentLog.quantity.length > 0"
class="list-group">
<li
v-for="(quant, i) in currentLog.quantity"
v-bind:key="`quantity-${i}-${Math.floor(Math.random() * 1000000)}`"
@click="currentQuant = i"
class="list-group-item">
{{ quant.measure }}
{{ quant.value }}
{{ (quantUnitNames.length > 0) ? quantUnitNames[i] : '' }}
{{ quant.label }}
<span class="remove-list-item" @click="removeQuant(i); $event.stopPropagation()">
✕
</span>
</li>
</ul>
</div>
<div class="form-item form-group">
<button
type="button"
class="btn btn-success"
@click="updateQuantity(null, null, -1)"
name="addNewQuantity">
Add another quantity
</button>
</div>
</div>
<h4>Assets</h4>
<Autocomplete
:objects="filteredAssets"
searchKey="name"
searchId="id"
:label="assetsRequired() ? 'Seedings must include assets!' : 'Add assets to the log'"
:class="{ invalid: assetsRequired() }"
v-on:results="addAsset($event)">
<template slot="empty">
<div class="empty-slot">
<em>No assets found.</em>
<br>
<button
type="button"
class="btn btn-light"
@click="forceSync"
name="button">
Sync Now
</button>
</div>
</template>
</Autocomplete>
<div class="form-item form-item-name form-group">
<ul class="list-group">
<li
v-for="(asset, i) in selectedAssets"
v-bind:key="`log-${i}-${Math.floor(Math.random() * 1000000)}`"
class="list-group-item">
{{ asset.name }}
<span class="remove-list-item" @click="removeAsset(asset)">
✕
</span>
</li>
</ul>
</div>
<div class="form-item form-item-name form-group">
<label for="type" class="control-label ">Equipment</label>
<div class="input-group">
<select
@input="addEquipment($event.target.value)"
class="custom-select col-sm-3 ">
<option value=""></option>
<option
v-for="(equip, i) in equipment"
:value="equip.id"
:key="`equip-${i}`">
{{ (equip) ? equip.name : '' }}
</option>
</select>
</div>
</div>
<div class="form-item form-group">
<ul v-if="currentLog.equipment" class="list-group">
<li
v-for="(equip, i) in currentLog.equipment"
v-bind:key="`log-${i}-${Math.floor(Math.random() * 1000000)}`"
class="list-group-item">
{{ (equipmentNames.length > 0) ? equipmentNames[i] : '' }}
<span class="remove-list-item" @click="removeEquipment(i)">
✕
</span>
</li>
</ul>
</div>
<div
v-if="!(currentLog.type === 'farm_seeding')"
id="areas-and-location">
<h4>Areas & Location</h4>
<!-- We're using a radio button to choose whether areas are selected
automatically based on device location, or using an Autocomplete.
This will use the useLocalAreas conditional var -->
<div v-if="useGeolocation" class="form-item form-item-name form-group">
<div class="form-check">
<input
v-model="useLocalAreas"
type="radio"
class="form-check-input"
id="dontUseGeo"
name="geoRadioGroup"
v-bind:value="false"
checked>
<label class="form-check-label" for="dontUseGeo">Search areas</label>
</div>
<div class="form-check">
<input
v-model="useLocalAreas"
type="radio"
class="form-check-input"
id="doUseGeo"
name="geoRadioGroup"
v-bind:value="true"
>
<label class="form-check-label" for="doUseGeo">Use my location</label>
</div>
</div>
<!-- If using the user's, show a select menu of nearby locations -->
<div v-if="useLocalAreas" class="form-group">
<label for="areaSelector">Farm areas near your current location</label>
<select
@input="addArea($event.target.value)"
class="form-control"
name="areas">
<option v-if="localAreas.length < 1" value="">No other areas nearby</option>
<option v-if="localAreas.length > 0" value="" selected>-- Select an Area --</option>
<option
v-for="area in localAreas"
:value="area.tid"
v-bind:key="`area-${area.tid}`">
{{area.name}}
</option>
</select>
</div>
<!-- If not using the user's location, show a search bar -->
<Autocomplete
v-if="!useLocalAreas"
:objects="filteredAreas"
searchKey="name"
searchId="tid"
label="Add areas to the log"
v-on:results="addArea($event)">
<template slot="empty">
<div class="empty-slot">
<em>No areas found.</em>
<br>
<button
type="button"
class="btn btn-light"
@click="forceSync"
name="button">
Sync Now
</button>
</div>
</template>
</Autocomplete>
<!-- Display the areas attached to each log -->
<div class="form-item form-item-name form-group">
<ul class="list-group">
<li
v-for="(area, i) in selectedAreas"
v-bind:key="`log-${i}-${Math.floor(Math.random() * 1000000)}`"
class="list-group-item">
{{ area.name }}
<span class="remove-list-item" @click="removeArea(area)">
✕
</span>
</li>
</ul>
</div>
<!-- We're using a button to attach the current location to the log
as a geofield -->
<div v-if="useGeolocation" class="form-item form-item-name form-group">
<button
:disabled='false'
title="Add my GPS location to the log"
@click="addLocation"
type="button"
class="btn btn-success btn-navbar">
Add my GPS location to the log
</button>
</div>
<!-- Display a spinner while getting geolocation, then display the location -->
<div class="form-item form-item-name form-group">
<ul class="list-group">
<li
class="list-group-item"
v-for="(geofield, i) in filteredGeofields"
:key="`geofield-${i}`">
{{ geofield.geom }}
<span class="remove-list-item" @click="removeLocation(i)">
✕
</span>
</li>
<li class="list-item-group" v-if="isWorking">
<icon-spinner/>
</li>
</ul>
</div>
</div>
<h4>Images</h4>
<div
v-if="isNative"
class="form-item form-item-name form-group">
<button
:disabled='false'
title="Take picture with camera"
@click="getPhoto"
class="btn btn-info btn-navbar navbar-right"
type="button">
Take picture with camera
</button>
</div>
<div class="form-item form-item-name form-group">
<div class="input-group ">
<label
class="custom-file-label"
for="customFile">
Select photo from file
</label>
<input
type="file"
accept="image/*"
class="custom-file-input"
ref="photo"
@change="loadPhoto($event.target.files)">
</div>
</div>
<div class="form-item form-item-name form-group">
<!-- NOTE: Display is set to 'none' if the img fails to load. -->
<img
v-for="(url, i) in imageUrls"
:src="url"
:key="`preview-${i}`"
onerror="this.style.display='none'"
class="preview" />
</div>
</div>
<div
class="container-fluid tab-content second"
:class="{ selected: tabSelected === 'SECOND' }"
v-if="currentLog.movement !== undefined">
<br>
<Autocomplete
:objects="filteredAssets"
searchKey="name"
searchId="id"
:label="assetsRequired() ? 'Seedings must include assets!' : 'Add assets to be moved'"
:class="{ invalid: assetsRequired() }"
v-on:results="addAsset($event)">
<template slot="empty">
<div class="empty-slot">
<em>No assets found.</em>
<br>
<button
type="button"
class="btn btn-light"
@click="forceSync"
name="button">
Sync Now
</button>
</div>
</template>
</Autocomplete>
<div class="form-item form-item-name form-group">
<ul class="list-group">
<li
v-for="(asset, i) in selectedAssets"
v-bind:key="`asset-${i}-${Math.floor(Math.random() * 1000000)}`"
class="list-group-item">
{{ asset.name }}
<span class="remove-list-item" @click="removeAsset(asset)">
✕
</span>
</li>
</ul>
</div>
<Autocomplete
:objects="filteredMovementAreas"
searchKey="name"
searchId="tid"
label="Movement to"
v-on:results="addMovementArea($event)">
<template slot="empty">
<div class="empty-slot">
<em>No areas found.</em>
<br>
<button
type="button"
class="btn btn-light"
@click="forceSync"
name="button">
Sync Now
</button>
</div>
</template>
</Autocomplete>
<div class="form-item form-item-name form-group">
<ul class="list-group">
<li
v-for="(area, i) in selectedMovementAreas"
v-bind:key="`log-${i}-${Math.floor(Math.random() * 1000000)}`"
class="list-group-item">
{{ area.name }}
<span class="remove-list-item" @click="removeMovementArea(area)">
✕
</span>
</li>
</ul>
</div>
<router-link :to="{ name: 'edit-map' }">
<Map
id="map"
:overrideStyles="{ height: '90vw' }"
:drawing="false"
:options="{
controls: (defaults) => defaults.filter(def => def.constructor.name === 'Attribution'),
interactions: false,
}"
:wkt=mapLayers
:geojson="{
title: 'areas',
url: areaGeoJSON,
color: 'grey',
}"/>
</router-link>
<br>
</div>
</div>
</template>
<script>
import Autocomplete from '@/components/Autocomplete';
import IconExpandLess from '@/components/icons/icon-expand-less';
import IconExpandMore from '@/components/icons/icon-expand-more';
import IconSpinner from '@/components/icons/icon-spinner';
import Map from '@/components/Map';
import ToggleCheck from '@/components/ToggleCheck';
import SelectBox from '@/components/SelectBox';
import DateAndTimeForm from '@/components/DateAndTimeForm';
import { mergeGeometries, removeGeometry, isNearby } from '@/utils/geometry';
import parseNotes from '@/utils/parseNotes';
export default {
name: 'EditLog',
components: {
Autocomplete,
IconExpandLess,
IconExpandMore,
IconSpinner,
Map,
ToggleCheck,
SelectBox,
DateAndTimeForm,
},
data() {
return {
tabSelected: 'FIRST',
useLocalAreas: false,
isWorking: false,
localAreas: [],
showAllCategories: false,
currentQuant: -1,
quantMeasures: [
'count',
'length',
'weight',
'area',
'volume',
'time',
'temperature',
'water_content',
'value',
'rating',
'ratio',
'probability',
],
};
},
props: [
'id',
'logs',
'logTypes',
'areas',
'assets',
'useGeolocation',
'units',
'categories',
'equipment',
],
beforeMount() {
if (this.$router.currentRoute.params.tab) {
this.tabSelected = this.$router.currentRoute.params.tab;
}
},
methods: {
forceSync() {
if (localStorage.getItem('host') !== null) {
this.$store.dispatch('updateAssets');
this.$store.dispatch('updateAreas');
return;
}
this.$router.push('/login');
},
updateCurrentLog(key, val) {
const props = {
[key]: val,
localID: +this.id,
};
this.$store.dispatch('updateLog', props);
},
updateNotes(value) {
this.updateCurrentLog('notes', { value, format: 'farm_format' });
},
updateQuantity(key, value, index) {
const currentQuants = this.currentLog.quantity || [];
const storedVal = (key === 'unit')
? { id: value, resource: 'taxonomy_term' }
: value;
let updatedQuant; let updatedQuants;
if (index >= 0) {
updatedQuant = { ...currentQuants[index], [key]: storedVal };
updatedQuants = [
...currentQuants.slice(0, index),
updatedQuant,
...currentQuants.slice(index + 1),
];
} else {
updatedQuant = {
measure: null,
value: null,
unit: null,
label: null,
};
updatedQuants = [...currentQuants, updatedQuant];
}
this.updateCurrentLog('quantity', updatedQuants);
if (index < 0) {
this.currentQuant = updatedQuants.length - 1;
}
},
addCategory(id) {
const catReference = { id, resource: 'taxonomy_term' };
const oldCats = this.currentLog.log_category;
const newCats = oldCats
? oldCats.concat(catReference)
: [catReference];
this.updateCurrentLog('log_category', newCats);
},
addEquipment(id) {
if (id !== '') {
const equipReference = { id, resource: 'farm_asset' };
const oldEquip = this.currentLog.equipment;
const newEquip = oldEquip
? oldEquip.concat(equipReference)
: [equipReference];
this.updateCurrentLog('equipment', newEquip);
}
},
addAsset(id) {
const assetReference = { id, resource: 'farm_asset' };
const newAssets = this.currentLog.asset.concat(assetReference);
this.updateCurrentLog('asset', newAssets);
},
addMovementArea(id) {
const areaReference = { id, resource: 'taxonomy_term' };
const areaGeometry = (this.areas.find(area => area.tid === id).geofield[0])
? this.areas.find(area => area.tid === id).geofield[0].geom
: null;
const prevMovement = this.currentLog.movement;
const newGeometry = prevMovement
? mergeGeometries([areaGeometry, prevMovement.geometry])
: areaGeometry;
const newMovement = {
area: prevMovement
? prevMovement.area.concat(areaReference)
: [areaReference],
geometry: newGeometry,
};
this.updateCurrentLog('movement', newMovement);
},
addArea(id) {
if (id !== '') {
const areaReference = { id, resource: 'taxonomy_term' };
const newAreas = this.currentLog.area.concat(areaReference);
this.updateCurrentLog('area', newAreas);
}
},
removeAsset(asset) {
const newAssets = this.currentLog.asset
.filter(_asset => _asset.id !== asset.id);
this.updateCurrentLog('asset', newAssets);
},
removeArea(area) {
// Update the current log with a new array of areas, sans the removed one.
const newAreas = this.currentLog.area
.filter(_area => _area.id !== area.tid);
this.updateCurrentLog('area', newAreas);
// Also remove the area's geofield from the current log.
const removedGeofields = this.areas
.find(_area => _area.tid === area.tid)
?.geofield;
const newGeofields = this.currentLog.geofield
?.filter(geofield => !removedGeofields.some(_geofield => geofield.geom === _geofield.geom));
this.updateCurrentLog('geofield', newGeofields);
},
removeMovementArea(area) {
const newAreas = this.currentLog.movement.area
.filter(_area => _area.id !== area.tid);
const prevGeometry = this.currentLog.movement.geometry;
let areaGeometry = null;
if (area.geofield[0]) {
areaGeometry = area.geofield[0].geom;
}
const newGeometry = removeGeometry(prevGeometry, areaGeometry);
const newMovement = {
geometry: newGeometry,
area: newAreas,
};
this.updateCurrentLog('movement', newMovement);
},
removeQuant(index) {
if (this.currentQuant >= index) {
this.currentQuant = this.currentQuant - 1;
}
const newQuant = [
...this.currentLog.quantity.slice(0, index),
...this.currentLog.quantity.slice(index + 1),
];
this.updateCurrentLog('quantity', newQuant);
},
removeCategory(index) {
const newCat = this.currentLog.log_category;
newCat.splice(index, 1);
this.updateCurrentLog('category', newCat);
},
removeEquipment(index) {
const newEquip = this.currentLog.equipment;
newEquip.splice(index, 1);
this.updateCurrentLog('equipment', newEquip);
},
getPhoto() {
// Obtains an image location from the camera!
return this.$store.dispatch('getPhotoFromCamera', this.currentLog);
},
loadPhoto(files) {
for (let i = 0; i < files.length; i += 1) {
this.$store.dispatch('loadPhotoBlob', {
file: files[i],
log: this.currentLog,
});
}
},
addLocation() {
let props;
function addGeofield(position) {
const geom = `POINT (${position.coords.longitude} ${position.coords.latitude})`;
const oldGeofield = this.currentLog.geofield;
props = oldGeofield
? oldGeofield.concat({ geom })
: [{ geom }];
}
function onError({ message }) {
const errorPayload = { message, level: 'warning', show: false };
this.$store.commit('logError', errorPayload);
this.isWorking = false;
}
const options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0,
};
this.isWorking = true;
const watch = navigator.geolocation.watchPosition(
addGeofield.bind(this),
onError.bind(this),
options,
);
setTimeout(() => {
navigator.geolocation.clearWatch(watch);
this.updateCurrentLog('geofield', props);
this.isWorking = false;
}, 5000);
},
removeLocation(index) {
const oldGeofield = this.currentLog.geofield;
const newGeofield = [
...oldGeofield.slice(0, index),
...oldGeofield.slice(index + 1),
];
this.updateCurrentLog('geofield', newGeofield);
},
getAttached(attribute, resources, resId) {
const logAttached = [];
resources.forEach((resrc) => {
attribute.forEach((attrib) => {
if (resrc[resId] === attrib.id) {
logAttached.push(resrc);
}
});
});
return logAttached;
},
assetsRequired() {
return this.currentLog.type === 'farm_seeding' && this.selectedAssets < 1;
},
parseNotes,
},
computed: {
currentLog() {
return this.logs.find(log => log.localID === +this.id) || this.logs[0];
},
/*
In order to avoid duplicates, filteredAssets & filteredAreas remove
assets/areas from the array of searchable objects if they've already been
added to the current log.
*/
filteredAssets() {
if (this.currentLog.asset) {
const selectAssetRefs = this.currentLog.asset;
return this.assets.filter(asset => (
!selectAssetRefs.some(selAsset => asset.id === selAsset.id)
));
}
return this.assets;
},
filteredAreas() {
if (this.currentLog.area) {
const selectAreaRefs = this.currentLog.area;
return this.areas.filter(area => (
!selectAreaRefs.some(selArea => area.tid === selArea.id)
));
}
return this.areas;
},
filteredMovementAreas() {
const { movement } = this.currentLog;
if (movement && movement && movement.area) {
const selectAreaRefs = this.currentLog.movement.area;
return this.areas.filter(area => (
!selectAreaRefs.some(selArea => area.tid === selArea.id)
));
}
return this.areas;
},
filteredCategories() {
const selectedCats = this.currentLog.log_category;
const noCatsAreSelected = !selectedCats || selectedCats.length === 0;
if (!this.showAllCategories && !noCatsAreSelected) {
return this.categories.filter(cat => (
selectedCats.some(_cat => cat.tid === _cat.id)
));
}
if (this.showAllCategories) {
return this.categories;
}
return [];
},
filteredGeofields() {
const geofields = this.currentLog.geofield;
return this.currentLog.geofield
? geofields.filter(g => g.geom?.includes('POINT'))
: [];
},
selectedAssets() {
if (this.currentLog.asset) {
return this.getAttached(this.currentLog.asset, this.assets, 'id');
}
return [];
},
selectedAreas() {
if (this.currentLog.area) {
return this.getAttached(this.currentLog.area, this.areas, 'tid');
}
return [];
},
selectedMovementAreas() {
const { movement } = this.currentLog;
if (movement && movement.area) {
return this.getAttached(
this.currentLog.movement.area,
this.areas,
'tid',
);
}
return [];
},
quantUnitNames() {
if (this.units.length > 0 && this.currentLog?.quantity.length > 0) {
const unitNames = [];
this.currentLog.quantity.forEach((quant) => {
if (quant.unit) {
this.units.forEach((unit) => {
if (parseInt(unit.tid, 10) === parseInt(quant.unit.id, 10)) {
unitNames.push(unit.name);
}
});
} else {
unitNames.push(null);
}
});
return unitNames;
}
return [];
},
categoryNames() {
if (this.categories.length > 0
&& this.currentLog.log_category.length > 0) {
const catNames = [];
this.currentLog.log_category.forEach((logCat) => {
this.categories.forEach((cat) => {
if (parseInt(cat.tid, 10) === parseInt(logCat.id, 10)) {
catNames.push(cat.name);
}
});
});
return catNames;
}
return [];
},
equipmentNames() {
if (this.equipment.length > 0
&& this.currentLog.equipment
&& this.currentLog.equipment.length > 0) {
const equipNames = [];
this.currentLog.equipment.forEach((logEquip) => {
this.equipment.forEach((equip) => {
if (parseInt(equip.id, 10) === parseInt(logEquip.id, 10)) {
equipNames.push(equip.name);
}
});
});
return equipNames;
}
return [];
},
areaGeoJSON() {
return (process.env.NODE_ENV === 'development')
? 'http://localhost:8080/farm/areas/geojson/all'
: `${localStorage.getItem('host')}/farm/areas/geojson/all`;
},
isNative() {
if (process.env.PLATFORM === 'native' || process.env.PLATFORM === 'dev') {
return true;
}
return false;
},
imageUrls() {
return this.currentLog.images
.filter(img => typeof img === 'string');
},
/*
Assemble layers for display.
The 'previous' layer is assembled from the geofield plus
all area geometires associated with the log.
The 'movement' layer is the geometry in the log's movement field
*/
mapLayers() {
const movement = {
title: 'movement',
wkt: this.currentLog.movement?.geometry,
color: 'orange',
visible: true,
weight: 0,
canEdit: !!this.currentLog.movement?.geometry,
};
const previousGeoms = this.currentLog.asset