-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
223 lines (212 loc) · 6.37 KB
/
index.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
import EsriConfig from 'https://js.arcgis.com/4.27/@arcgis/core/config.js';
import FeatureLayer from "https://js.arcgis.com/4.27/@arcgis/core/layers/FeatureLayer.js";
import Map from 'https://js.arcgis.com/4.27/@arcgis/core/Map.js';
import MapView from 'https://js.arcgis.com/4.27/@arcgis/core/views/MapView.js';
import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
createApp({
data() {
return {
files: [],
snacks: [{ message: "" }],
featureLayerUrl: "https://services7.arcgis.com/vVpN3IL0Y4nustY6/ArcGIS/rest/services/MOHEWorkRecords/FeatureServer/0/",
selectedOid: 4,
currentAttachments: [],
currentWorkRecords: [],
currentWorkRecord: {
action: "",
date: "",
time: ""
},
currentSiteName: "",
currentSiteId: 0,
bigPictureSource: "",
isShowingBigPicture: false,
isShowingPictures: false,
isUploadingPictures: false
}
},
mounted() {
// PWA support: register service worker
if ("sw" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("/sw.js")
.then(res => console.log("service worker registered"))
.catch(err => console.log("service worker not registered", err))
})
}
EsriConfig.apiKey = "AAPK7f0624e696e04015b9c410743b9f9066bBcF0v6zJP3GLijOE8ODrEKaVmIHJE3qAEzxd_gx8fCGtCivYtkqz8DQaHBsqlHu";
const map = new Map({
basemap: "topo-vector"
});
const view = new MapView({
map: map,
center: [-122.4, 47.6],
zoom: 11,
container: "map-view"
});
const workLayer = new FeatureLayer({
visible: false,
portalItem: {
id: "2bbadf2f5ea54cf79c075670f129246f"
}
});
map.add(workLayer);
const sitesLayer = new FeatureLayer({
portalItem: {
id: "79432d649f864ed7aba8a3a925bd2724"
},
outFields: ['*'],
popupEnabled: false
});
map.add(sitesLayer);
view.on("click", event => {
//
const opts = {
include: sitesLayer
}
view.hitTest(event, opts).then(response => {
// clear work records and pictures no matter what
// including clicking on a non-site
this.currentWorkRecord = {
action: "",
date: "",
time: ""
};
this.currentWorkRecords = [];
this.currentAttachments = [];
this.isShowingPictures = false;
if (response.results?.length > 0) {
this.currentSiteName = response.results[0].graphic.attributes.LOC_NAME;
this.currentSiteId = response.results[0].graphic.attributes.SITE_ID;
// call method to get work records for this site
this.showSiteWorkRecords(workLayer, this.currentSiteId);
}
});
});
},
methods: {
showSiteWorkRecords(workLayer, siteId) {
const query = { where: `SITE_ID = ${siteId}`, outFields: ['*'] };
workLayer.queryFeatures(query).then(results => {
results.features.forEach(feature => {
let workDate = new Date(feature.attributes.ACTION_DATE);
const wr = {
oid: feature.attributes.OBJECTID,
date: workDate.toLocaleDateString("medium"),
time: workDate.toLocaleTimeString("medium"),
action: feature.attributes.WORKACTION
}
this.currentWorkRecords.push(wr);
});
});
},
async uploadImages() {
this.isUploadingPictures = true;
// iterate the files backwards so we can remove them after upload (which will reduce the count and remove the item from the UI)
for (let i = this.files.length - 1; i >= 0; i--) {
const file = this.files[i];
let formData = new FormData();
formData.append("file", file);
formData.append("f", "json");
try {
const url = this.featureLayerUrl + this.selectedOid + "/addAttachment";
const response = await fetch(url, {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.addAttachmentResult.success) {
this.snackTime(`Successfully uploaded image: ${file.name}`);
console.log(`Successfully uploaded image: ${file.name}`);
} else {
this.snackTime(`Failed to upload image: ${file.name}`);
console.log(`Failed to upload image: ${file.name}`);
}
}
catch(e) {
console.log(e);
}
// remove from list once processed
this.files.splice(i, 1);
}
// clear the selected files regardless
this.files = [];
this.fetchFeatureAttachments();
this.isUploadingPictures = false;
},
async deleteAttachment(attachment) {
let formData = new FormData();
formData.append("attachmentIds", attachment.id);
formData.append("f", "json");
try {
const url = this.featureLayerUrl + this.selectedOid + "/deleteAttachments?f=json";
const response = await fetch(url, {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.deleteAttachmentResults[0].success) {
this.snackTime(`Successfully deleted image: ${attachment.name}`);
console.log(`Successfully deleted image: ${attachment.name}`);
this.fetchFeatureAttachments();
} else {
this.snackTime(`Failed to deleted image: ${attachment.name}`);
console.log(`Failed to deleted image: ${attachment.name}`);
console.log(result.deleteAttachmentResults[0].error.description);
}
}
catch(e) {
console.log(e);
}
},
getPictures(workRecord) {
this.currentWorkRecord = workRecord;
this.selectedOid = workRecord.oid;
this.isShowingPictures = true;
this.fetchFeatureAttachments();
},
async fetchFeatureAttachments() {
try {
const url = this.featureLayerUrl + this.selectedOid + "/attachments?f=json";
const response = await fetch(url);
const attachments = await response.json();
if (attachments) {
this.currentAttachments = [];
attachments.attachmentInfos.forEach(attachment => {
const a = {
name: attachment.name,
id: attachment.id,
url: `${this.featureLayerUrl}${this.selectedOid}/attachments/${attachment.id}`
}
this.currentAttachments.push(a);
});
}
}
catch(e) {
console.log(e);
}
},
clearFiles() {
// clear the selected files
this.files = [];
},
refreshFiles(event) {
// Convert the FileList to an array and update the 'files' data property
this.files = Array.from(event.target.files);
},
showBigPicture(source) {
this.bigPictureSource = source;
this.isShowingBigPicture = true;
},
snackTime(snackWords) {
// Get the snackbar DIV
var snacky = document.getElementById("snackbar");
snacky.innerHTML = snackWords;
// Add the "show" class
snacky.className = "show";
// After 3 seconds, remove the show class
setTimeout(() => { snacky.className = snacky.className.replace("show", ""); }, 3000);
}
}
}).mount('#app')