-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetails.js
266 lines (232 loc) · 9.31 KB
/
details.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
/*!
GeoPortal Genie by Stephen Lead
GeoPortalGenie.com
*/
//It shouldn't be necessary to modify any of these scripts, as doing so will complicate the upgrade path.
//Namespace
if (!this.gpGenie || typeof this.gpGenie !== 'object') {
this.gpGenie = {};
}
//A model to handle fetching Details from an individual record
gpGenie.DetailsModel = Backbone.Model.extend({
initialize: function (args) {
_.bindAll(this, 'fetchDetails', 'handleDetails');
},
fetchDetails: function(id) {
//Retrieve the details for an individual entry, based on its UUID value
var query = "rest/document/" + id;
var request = $.ajax({
url: configOptions.proxy,
data: {requrl: configOptions.geoportalServer + query},
context: this,
success: function(response) {
//Handle the details
this.handleDetails(response);
},
error: function(error) {
var message = "error with Details query"
alert(message);
}
});
},
handleDetails: function(details) {
try {
//Clear the modal from its last values.
//TODO: replace with something which clears the view
$("#modalContent").html(null);
$("#modalTitle").html(null);
//Create an object which will be returned to the Details view.
//It will contain the Title, Summary and Agency (perhaps more to come in future)
this.detailsData = {
"title": "Unable to retrieve title",
"summary": "Unable to retrieve summary",
"agency": "Unable to retrieve agency",
"contact": "Unable to retrieve contact details"
};
//Convert the XML response into JSON, so it's easier to work with
var jsonDetails = $.xml2json(details);
//Different XML formats are possible, so it's necessary to try a few options to detect the valid syntax
if(jsonDetails.identificationInfo !== undefined) {
//Title
try {
this.detailsData.title = jsonDetails.identificationInfo.MD_DataIdentification.citation.CI_Citation.title.CharacterString;
} catch (error) {
console.log("unable to retrieve title");
}
//Summary
try {
this.detailsData.summary = jsonDetails.identificationInfo.MD_DataIdentification["abstract"].CharacterString;
} catch (error) {
console.log("unable to retrieve summary");
}
//Agency
try {
if(jsonDetails.identificationInfo.MD_DataIdentification.pointOfContact !== undefined) {
var contact = jsonDetails.identificationInfo.MD_DataIdentification.pointOfContact;
if(contact.CI_ResponsibleParty !== undefined) {
this.detailsData.agency = contact.CI_ResponsibleParty.organisationName.CharacterString;
//Sometimes this form of contact may contain multiple entries - append them
} else if (contact.length > 0) {
this.detailsData.agency = contact[0].CI_ResponsibleParty.organisationName.CharacterString;
for (var i = 1; i < contact.length; i++) {
this.detailsData.agency = this.detailsData.agency + ", " + contact[i].CI_ResponsibleParty.organisationName.CharacterString;
}
}
}
} catch (error) {
console.log("unable to retrieve agency");
}
//contact
try {
if(jsonDetails.contact.CI_ResponsibleParty.contactInfo.CI_Contact.address.CI_Address !== undefined) {
var shortAddr = jsonDetails.contact.CI_ResponsibleParty.contactInfo.CI_Contact.address.CI_Address;
if(shortAddr.electronicMailAddress !== undefined) {
var contact = shortAddr.electronicMailAddress.CharacterString;
this.detailsData.contact = "<a href='mailto:" + contact + "'>" + contact + "</a>";
} else {
if (shortAddr.deliveryPoint !== undefined && shortAddr.deliveryPoint.CharacterString !== undefined) {
this.detailsData.contact = shortAddr.deliveryPoint.CharacterString;
}
if (shortAddr.city !== undefined && shortAddr.city.CharacterString !== undefined) {
if(this.detailsData.contact.length > 0) {
this.detailsData.contact = this.detailsData.contact + ", " + shortAddr.city.CharacterString;
} else {
this.detailsData.contact = shortAddr.city.CharacterString;
}
}
if (shortAddr.country !== undefined && shortAddr.country.CharacterString !== undefined) {
if(this.detailsData.contact.length > 0) {
this.detailsData.contact = this.detailsData.contact + ", " + shortAddr.country.CharacterString;
} else {
this.detailsData.contact = shortAddr.country.CharacterString;
}
}
if (shortAddr.postalCode !== undefined && shortAddr.postalCode.CharacterString !== undefined) {
if(this.detailsData.contact.length > 0) {
this.detailsData.contact = this.detailsData.contact + ", " + shortAddr.postalCode.CharacterString;
} else {
this.detailsData.contact = shortAddr.postalCode.CharacterString;
}
}
}
}
} catch (error) {
console.log("unable to retrieve contact details");
}
//Another style
} else if(jsonDetails.idinfo !== undefined) {
//Title
try {
this.detailsData.title = jsonDetails.idinfo.citation.citeinfo.title;
} catch (error) {
console.log("unable to retrieve title");
}
//Summary
try {
this.detailsData.summary = jsonDetails.idinfo.descript["abstract"];
} catch (error) {
console.log("unable to retrieve summary");
}
//Agency
try {
if(jsonDetails.metainfo !== undefined) {
if (jsonDetails.metainfo.metc.cntinfo !== undefined) {
if (jsonDetails.metainfo.metc.cntinfo.cntperp !== undefined) {
if (jsonDetails.metainfo.metc.cntinfo.cntperp.cntorg !== undefined) {
this.detailsData.agency = jsonDetails.metainfo.metc.cntinfo.cntperp.cntorg;
} else if (jsonDetails.metainfo.metc.cntinfo.cntperp.cntper !== undefined) {
this.detailsData.agency = jsonDetails.metainfo.metc.cntinfo.cntperp.cntper;
}
} else if (jsonDetails.metainfo.metc.cntinfo.cntorgp !== undefined) {
if (jsonDetails.metainfo.metc.cntinfo.cntorgp.cntorg !== undefined) {
this.detailsData.agency = jsonDetails.metainfo.metc.cntinfo.cntorgp.cntorg;
}
}
}
}
} catch (error) {
console.log("unable to retrieve agency");
}
//contact
try {
if(jsonDetails.metainfo.metc.cntinfo !== undefined) {
if (jsonDetails.metainfo.metc.cntinfo.cntaddr !== undefined) {
if(jsonDetails.metainfo.metc.cntinfo.cntaddr.address !== undefined) {
this.detailsData.contact = jsonDetails.metainfo.metc.cntinfo.cntaddr.address;
}
if(jsonDetails.metainfo.metc.cntinfo.cntaddr.city !== undefined) {
if(this.detailsData.contact.length > 0) {
this.detailsData.contact = this.detailsData.contact + ", " + jsonDetails.metainfo.metc.cntinfo.cntaddr.city;
} else {
this.detailsData.contact = jsonDetails.metainfo.metc.cntinfo.cntaddr.city;
}
}
if(jsonDetails.metainfo.metc.cntinfo.cntaddr.state !== undefined) {
if(this.detailsData.contact.length > 0) {
this.detailsData.contact = this.detailsData.contact + ", " + jsonDetails.metainfo.metc.cntinfo.cntaddr.state;
} else {
this.detailsData.contact = jsonDetails.metainfo.metc.cntinfo.cntaddr.state;
}
}
}
}
} catch (error) {
console.log("Unable to retrieve contact details");
}
//Another style
} else if (jsonDetails.Description !== undefined) {
//Title
try {
this.detailsData.title = jsonDetails.Description.title;
} catch (error) {
console.log("unable to retrieve title");
}
//Summary
try {
this.detailsData.summary = jsonDetails.Description.description;
} catch (error) {
console.log("unable to retrieve summary");
}
//It seems that this format doesn't include contact or agency details(?)
} else {
//TODO: replace with Backbone error handling
$("#modalContent").html("Sorry, there was a problem retrieving the details for this record");
}
//Send the results to the Details modal
this.trigger('detailsReturned', this.detailsData, this);
} catch (error) {
//TODO: replace with Backbone error handling
alert("Sorry, there was an error loading the stylesheet. Please contact your GeoPortal Genie administrator");
}
//Reset the loading button
$(".btnDetailsLink").button('reset');
}
});
//A View to handle display of the Details for an individual record
gpGenie.DetailsView = Backbone.View.extend({
initialize: function (args) {
_.bindAll(this, 'onDetailsReturned');
this.detailsModel = args.detailsModel;
this.detailsModel.on('detailsReturned', this.onDetailsReturned);
this.detailsTemplate = ich[args.detailsTemplate];
//Add the elements to display the results
$(this.options.detailsContainer).append(this.el);
},
className: 'details',
onDetailsReturned: function(detailsData) {
//Fires when the REST API has returned Details about an entry
if(detailsData !== undefined) {
//Populate the modal body and title with the relevant content
var output = this.render();
//Display the modal
$(output.options.detailsContainer).modal();
} //TODO: add an Else with error handling
},
render: function () {
// Compile the template using underscore
var template = _.template( $(this.options.detailsTemplate).html(), this.options.detailsModel.detailsData);
// Load the compiled HTML into the Backbone "el"
this.$el.html( template );
return this;
}
});