-
Notifications
You must be signed in to change notification settings - Fork 0
/
cswResultsStore.js
415 lines (384 loc) · 13.7 KB
/
cswResultsStore.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
/*
* buildToolbar constructs the tools appropriate for the record passed in
* Much of the logic here is for determining which buttons to make available
* and is determined based on the "protocol" attribute of the record passed in
*/
function buildToolbar(record, saved) {
spacerWidth = 3;
// Everything has a details button
detailsButton = new Ext.Button({
handler: function(btn, e) {
detailsRecord(btn.fileid); // to be defined
},
text: 'Details',
fileid: record.fileid,
icon: 'csw/img/magnifier.png'
});
if (saved) {
// Locked results have an unlock button
unlockButton = new Ext.Button({
handler: function(btn, e) {
unlockRecord(btn.fileid);
},
text: "Unlock",
fileid: record.fileid,
icon: 'csw/img/lock_open.png'
});
items = [ unlockButton, new Ext.Toolbar.Spacer({ width: spacerWidth }), detailsButton ];
}
else {
// these two buttons are standard for search results.
lockButton = new Ext.Button({
handler: function(btn, e) {
lockRecord(btn.fileid);
},
text: 'Lock',
fileid: record.fileid,
icon: 'csw/img/lock.png'
});
removeButton = new Ext.Button({
handler: function(btn, e) {
removeRecord(btn.fileid);
},
text: 'Remove',
fileid: record.fileid,
icon: 'csw/img/cancel.png'
});
items = [ lockButton, new Ext.Toolbar.Spacer({ width: spacerWidth }), removeButton, new Ext.Toolbar.Spacer({ width: spacerWidth }), detailsButton ];
}
// Begin constructing the toolbar
toolbar = new Ext.Toolbar({
buttonAlign: 'center',
items: items,
style: "border: none; border-top: 1px solid #D0D0D0;"
});
// Add conditional buttons
var availableProtocols = record.protocols;
zoomButton = new Ext.Button({
handler: function(btn, e) {
zoomRecord(btn.fileid);
},
text: 'Zoom To',
fileid: record.fileid,
icon: 'csw/img/arrow_in.png'
});
toolbar.insertButton(0, zoomButton);
toolbar.insert(1, new Ext.Toolbar.Spacer({ width: spacerWidth }));
if (availableProtocols.hasOwnProperty('wms') || availableProtocols.hasOwnProperty('wfs') || availableProtocols.hasOwnProperty('esri')) {
addButton = new Ext.Button({
id: record.fileid + "-add-button",
handler: function(btn, e) {
btn.setIcon("csw/img/loading.gif");
addRecord(btn.fileid, availableProtocols);
},
text: 'Add to Map',
fileid: record.fileid,
icon: 'csw/img/map_add.png'
});
insertIndex = toolbar.items.indexOf(detailsButton) - 1;
toolbar.insertButton(insertIndex, addButton);
toolbar.insert(insertIndex + 1, new Ext.Toolbar.Spacer({ width: spacerWidth }));
}
if (availableProtocols.hasOwnProperty('esri')) {
downloadButton = new Ext.Button({
handler: function(btn, e) {
arcmapRecord(btn.fileid, availableProtocols.esri);
},
text: 'Add to ArcMap',
fileid: record.fileid,
icon: 'csw/img/world_add.png'
});
insertIndex = toolbar.items.indexOf(detailsButton) - 1;
toolbar.insertButton(insertIndex, downloadButton);
toolbar.insert(insertIndex + 1, new Ext.Toolbar.Spacer({ width: spacerWidth }));
}
if (availableProtocols.hasOwnProperty('download')) {
downloadButton = new Ext.Button({
handler: function(btn, e) {
downloadRecord(btn.fileid, availableProtocols.download); // to be defined
},
text: 'Download',
fileid: record.fileid,
icon: 'csw/img/arrow_down.png'
});
insertIndex = toolbar.items.indexOf(detailsButton) - 1;
toolbar.insertButton(insertIndex, downloadButton);
toolbar.insert(insertIndex + 1, new Ext.Toolbar.Spacer({ width: spacerWidth }));
}
if (availableProtocols.hasOwnProperty('contact')) {
contactButton = new Ext.Button({
handler: function(btn, e) {
//TODO: Implement contact functionality
notImplementedYet().show();
},
text: 'Contact',
fileid: record.fileid,
icon: 'csw/img/email_open.png'
});
insertIndex = toolbar.items.indexOf(detailsButton) - 1;
toolbar.insertButton(insertIndex, contactButton);
toolbar.insert(insertIndex + 1, new Ext.Toolbar.Spacer({ width: spacerWidth }));
}
// return the toolbar
return toolbar;
}
/*
* keywords function takes the csw output, having been parsed by OpenLayers.Format.CSWRecords,
* and returns a simpler array of keywords for the data store.
*/
keywords = function(v, rec) {
keywordArray = [];
if (typeof(rec.subject) == 'undefined') { return keywordArray; }
for (var i = 0; i < rec.subject.length; i++) {
keywordArray.push(rec.subject[i].value);
}
return keywordArray;
};
/*
* projectBounds is a function to reproject the bounds object created during parsing of csw output
* by OpenLayers.Format.CSWRecords. The OpenLayers.Bounds object is reprojected from WGS84 to
* standard Web Mercator.
*/
projectBounds = function(v, rec) {
wgs84Bounds = rec.BoundingBox[1].bounds;
destProj = new OpenLayers.Projection("EPSG:3857");
sourceProj = new OpenLayers.Projection("EPSG:4326");
return wgs84Bounds.transform(sourceProj, destProj);
};
/*
* parseOgcCapabilitiesUrl is an attempt to parse two common forms of OGC url
* It is supposed to recognize MapServer URLs which have a map=... parameter
* which is required in order to function.
*/
parseOgcCapabilitiesUrl = function(inputUrl) {
var urlBits = inputUrl.split('?');
var finalUrl = urlBits[0] + "?";
var parameters = urlBits[1].split('&');
for (var i = 0; i < parameters.length; i++) {
if (parameters[i].indexOf("map=") == 0) {
finalUrl = finalUrl + parameters[i];
continue;
}
}
return finalUrl;
};
/*
* guessAccessProtocol is a function to try and get access points out of the csw ouput that has
* been parsed by OpenLayers.Format.CSWRecords already. Returns a dictionary keyed on general
* protocol type (contact, download, wms, wfs, esri) and valued with URLs
*
* Right now this is dependent on Geoportal's assigned "type".
* TODO: Generalize guessAccessProtocols to work with GeoNetwork csw:Record
*/
guessAccessProtocol = function(v, rec) {
// Get a couple of references from the rTecord passed in
geoportalAssigned = rec.type[0].value; // Geoportal-assigned "type"
allUrls = rec.references; // List of urls that geoportal offered
results = {}; // Dictionary of protocols guessed
needsContact = true; // Boolean to determine if we need to offer a contact button (no URLs found)
// Loop through the URLs in the record
for (var i = 0; i < allUrls.length; i++) {
thisUrl = allUrls[i];
// See if this is the link directly to the metadata record itself
if (thisUrl.indexOf("catalog.usgin.org") != -1) {
results['details'] = thisUrl;
continue;
}
// Different things to look for depending on the type that geoportal assigned
if (geoportalAssigned == 'downloadableData') {
/* downloadableData records tend to only have a download link and/or a details link
* so just add this one. If we come across a record with more than one link, we'll
* have to deal with it
*/
results['download'] = thisUrl;
needsContact = false;
}
else if (geoportalAssigned == 'liveData') {
// If the URL has GetCapabilities in it then it is probably for a WMS or WFS
if (thisUrl.indexOf("GetCapabilities") != -1 || thisUrl.indexOf("getcapabilities") != -1) {
// Look for a WFS
if (thisUrl.indexOf("wfs") != -1 || thisUrl.indexOf("WFS") != -1) {
results['wfs'] = parseOgcCapabilitiesUrl(thisUrl);
needsContact = false;
}
// Look for a WMS
if (thisUrl.indexOf("wms") != -1 || thisUrl.indexOf("WMS") != -1) {
results['wms'] = parseOgcCapabilitiesUrl(thisUrl);
needsContact = false;
}
}
// Look for what looks like an ESRI service -- that is, says "MapServer" somewhere in it
if (thisUrl.indexOf("mapserver") != -1 || thisUrl.indexOf("MapServer") != -1) {
lowCase = thisUrl.indexOf("mapserver");
camelCase = thisUrl.indexOf("MapServer");
index = camelCase != -1 ? camelCase : lowCase;
basicUrl = thisUrl.substring(0, index + 9);
if (basicUrl.indexOf("rest") == -1) {
serviceIndex = basicUrl.indexOf("/services/");
finalUrl = basicUrl.substring(0, serviceIndex) + "/rest" + basicUrl.substring(serviceIndex);
}
results['esri'] = finalUrl || basicUrl;
needsContact = false;
}
}
}
// If we didn't find any acceptable URLs, then just give a contact protocol
if (needsContact) {
results['contact'] = "Please contact this dataset's curator at: NOT IMPLEMENTED";
}
// Return the results!
return results;
};
function paginatorButtonHandler(btn, e) {
performSearch(btn.url, Ext.getCmp("csw-search-field").getSearchTerms(), Ext.getCmp("csw-search-field").getBbox(), btn.start);
}
/*
* cswResultsStore is an Ext.data.JsonStore containing the results of the current csw output.
* Its loadData method expects an object that is the parsed output from OpenLayers.Format.CSWRecords
*
* TODO: Make sure fields are valid in GeoNetwork responses
*/
function cswResultsStore(saved, map, url) {
if (saved) {
tableTitle = "Saved Results";
tableId = "csw-saved-table";
storeId = "csw-saved-store";
bottomBar = ["->",
new Ext.Button({
text: "Save these results",
icon: "csw/img/disk.png",
handler: function() {
//TODO: Implement saving record functionality
notImplementedYet().show();
}
})
];
}
else {
tableTitle = "Search Results";
tableId = "csw-search-table";
storeId = "csw-search-store";
bottomBar = ["->",
new Ext.Button({
id: "csw-results-first",
icon: "csw/img/resultset_first.png",
url: url,
start: 1,
handler: paginatorButtonHandler
}),
new Ext.Button({
id: "csw-results-previous",
icon: "csw/img/resultset_previous.png",
url: url,
start: 1,
handler: paginatorButtonHandler
}),
{
xtype: "tbtext",
id: "csw-results-text",
text: "Hello, here are some records!"
},
new Ext.Button({
id: "csw-results-next",
icon: "csw/img/resultset_next.png",
url: url,
start: 1,
handler: paginatorButtonHandler
})
];
}
return new Ext.data.JsonStore({
id: storeId,
root: "records",
tableId: tableId,
viewerMap: map,
saved: saved,
url: url,
fields: [
{name: 'title', mapping: 'title[0].value'},
{name: 'abstract', mapping: 'abstract[0]'},
{name: 'subject', convert: keywords},
{name: 'type', mapping: 'type[0].value'},
{name: 'bbox', convert: projectBounds},
{name: 'fileid', mapping: 'identifier[0].value'},
{name: 'docid', mapping: 'identifier[1].value'},
{name: 'modified', mapping: 'modified[0]', type: 'date', dateFormat: 'Y-m-d\\TG:i:sP'},
{name: 'protocols', convert: guessAccessProtocol}
],
listeners: {
load: function(store, records, options) {
table = store.getTable();
Ext.getCmp("csw-tab-container").activate(table);
for (var i = 0; i < store.getCount(); i++) {
record = store.getAt(i);
table.add(store.getRecordPanel(record));
}
table.doLayout();
},
add: function(store, records, index) {
table = store.getTable();
Ext.getCmp("csw-tab-container").activate(table);
for (var i = 0; i < records.length; i++) {
lockedRow = store.getRecordPanel(records[i]);
table.add(lockedRow);
}
table.doLayout();
},
remove: function(store, record, index) {
table = store.getTable();
panel = store.getRecordPanel(record);
table.remove(panel);
},
clear: function(store, records) {
store.getTable().removeAll();
}
},
panel: {
xtype: "panel",
title: tableTitle,
layout: 'table',
map: map,
id: tableId,
padding: 5,
autoScroll: true,
layoutConfig: { columns: 1 },
bbar: bottomBar
},
getTable: function() {
return Ext.getCmp(this.tableId);
},
getRecordPanel: function(record) {
if (this.saved) {
id = record.id + "-saved-row";
}
else {
id = record.id + "-search-row";
}
return Ext.getCmp(id) || new Ext.Panel({
id: id,
cls: 'result-container',
html: '<div class="result-heading x-panel-header" onclick="zoomRecord(\'' + record.get("fileid") + '\');">' + record.get('title') + '</div><div class="result-abstract">' + record.get('abstract') + '</div>',
bodyStyle: "border: none;",
style: "border-width: 1px;",
bbar: buildToolbar(record.data, saved),
record: record,
feature: new OpenLayers.Feature.Vector(record.get('bbox').toGeometry(), record.data)
});
},
adjustPaginator: function(resultsInfo) {
firstRecordShown = resultsInfo.nextRecord - resultsInfo.numberOfRecordsReturned;
lastRecordShown = (resultsInfo.nextRecord - 1) > 0 ? (resultsInfo.nextRecord - 1) : 0;
theText = "Showing " + firstRecordShown + " - " + lastRecordShown + " of " + resultsInfo.numberOfRecordsMatched + " results";
// Adjust text
Ext.getCmp("csw-results-text").setText(theText);
terms = Ext.getCmp("csw-search-field").getSearchTerms();
bbox = Ext.getCmp("csw-search-field").getBbox();
// Adjust button starts
moveBack = firstRecordShown - resultsInfo.numberOfRecordsReturned;
Ext.getCmp("csw-results-previous").start = moveBack > 0 ? moveBack : 1;
Ext.getCmp("csw-results-next").start = resultsInfo.nextRecord;
// Adjust the table Title
Ext.getCmp("csw-search-table").setTitle("Search Results (" + resultsInfo.numberOfRecordsMatched + ")");
}
});
}