-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
229 lines (191 loc) · 8.75 KB
/
init.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
/*!
GeoPortal Genie by Stephen Lead
GeoPortalGenie.com
*/
//Initialization script. The user may choose to use a local copy of this file in order to control behaviors
var geoPortalGenie = {};
require(["esri/map", "dojo/domReady!", "esri/dijit/Geocoder", "esri/layers/graphics", "esri/virtualearth/VETiledLayer"], function() {
//Read in the options from the configuration file
if(!configOptions.mapOptions || !configOptions.arcgisGeocoder) {
//TODO: replace with a nicer error message
alert("please check your configuration file for errors");
return null;
}
/*********************************************************************/
//Backbone MVC:
//Create the new map model, based on these options
if(document.getElementById("mapDiv") !== null) {
geoPortalGenie.mapModel = new gpGenie.MapModel({
mapDiv: "mapDiv", //ID of the DIV holding the Esri map
mapOptions: configOptions.mapOptions,
geocoderOptions: configOptions.arcgisGeocoder,
geocoderDiv: "geocoderDiv", //ID of the DIV for the geocoder widget
bingMapsOptions: configOptions.bingMapsOptions || null
});
//Create a graphics layer, which shows the extents on the map
dojo.connect(geoPortalGenie.mapModel.map, "onLoad",geoPortalGenie.mapModel.buildExtentLayers);
} else {
geoPortalGenie.mapModel = null;
}
//Create a new REST API model, to interact with the GeoPortal REST API
geoPortalGenie.restModel = new gpGenie.RestModel({
mapModel: geoPortalGenie.mapModel,
gpServer: configOptions.geoPortalServer,
infoMessageContainer: '#infoMessage' //ID of the DIV holding the information message
});
//Create a new Details model, to handle fetching Details for an individual record
geoPortalGenie.detailsModel = new gpGenie.DetailsModel({
mapModel: geoPortalGenie.mapModel,
restModel: geoPortalGenie.restModel
});
//Create a new Details view, to handle displaying the details for an individual record
geoPortalGenie.detailsView = new gpGenie.DetailsView({
detailsModel: geoPortalGenie.detailsModel,
detailsContainer: "#modalResults",
detailsTemplate: "#details-template"
});
//Results collection
geoPortalGenie.resultsCollection = new gpGenie.ResultsCollection({
mapModel: geoPortalGenie.mapModel,
restModel: geoPortalGenie.restModel
});
//Results view
geoPortalGenie.resultsView = new gpGenie.ResultsView({
mapModel: geoPortalGenie.mapModel,
restModel: geoPortalGenie.restModel,
collection: geoPortalGenie.resultsCollection,
resultsContainer: '#searchResults', //ID of the DIV for the results
resultSetTemplate: 'results-set-template' //ID of the HTML template for the results set
//TODO: the following is currently hard-coded in geoportalGenie.js and should instead be a parameter:
// resultItemTemplate: "#result-item-template" //ID of the HTML template for each individual result
});
/*********************************************************************/
//Add some event listeners. These are separated from the Backbone scripts to enable
//easy customization by developers, in order to personalize the page.
//Customise this section as necessary to show/hide the Advanced Search Parameters in your system.
//Eg, you could use a checkbox or button - or just leave the Advanced Search Parameters on the whole time.
//The key point is to change the value of geoPortalGenie.useAdvanced to true/false
$("#advancedToggle").click(function() {toggleAdvancedSearch();});
function toggleAdvancedSearch() {
//Show/Hide the advanced search panel depending on the status of the button
if($("#advancedToggle").hasClass("advancedOn")) {
$("#advancedSearchParameters").fadeOut(100);
$("#advancedStatus").addClass("icon-caret-down");
$("#advancedStatus").removeClass("icon-caret-up");
$("#advancedToggle").removeClass("advancedOn");
geoPortalGenie.useAdvanced = false;
} else {
$("#advancedStatus").addClass("icon-caret-up");
$("#advancedStatus").removeClass("icon-caret-down");
$("#advancedSearchParameters").fadeIn(300);
$("#advancedToggle").addClass("advancedOn");
geoPortalGenie.useAdvanced = true;
}
}
//Set the useAdvanced property for the first time. TODO: read this from local storage/cookie
geoPortalGenie.useAdvanced = $("#advancedToggle").hasClass("advancedOn");
//Enable tooltips
$('.hasTooltip').tooltip({delay: { show: 1000, hide: 100 }});
//Support placeholder text on older browsers
$('input, textarea').placeholder();
//Enable the date picker
$('.datepicker').datepicker({format: 'yyyy-mm-dd'});
$('#txtFromDate').click(function() {$('#txtToDate').datepicker('hide');});
$('#txtToDate').click(function() {$('#txtFromDate').datepicker('hide');});
//Build the Advanced theme search
buildAdvancedSearchCategories();
// $("#filterThemes").selectpicker();
//validate the date on change
$(".datepicker").on('changeDate', function(){
$(this).removeClass("error");
});
$(".datepicker").change(function() {
var datePattern = new RegExp(/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/);
if(datePattern.test(this.value)) {
$(this).removeClass("error");
} else {
$(this).addClass("error");
}
});
//Build the query when the user hits Enter, or uses the Search button. You could add other
//triggers as required
$('#btnRunSearch').click(function() {buildSearchQuery();});
$('#txtSearch').keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
buildSearchQuery();
}
});
//When the user changes any of the Advanced Search parameters, inform the model so that the
//search may be re-run. You could optionally run the search automatically at this point (although
//most users find this annoying)
$(".advancedSearch").on("change", function() {geoPortalGenie.restModel.trigger("searchTermsUpdated");});
//Set a listener for the Zoom To button
$(document).on('click','.btnZoomTo',function(event) {
var bbox = this.getAttribute("data-bbox");
if(bbox !== null && bbox !== undefined) {
bbox = bbox.split(",");
var extent = new esri.geometry.Extent(parseFloat(bbox[0]),parseFloat(bbox[1]),parseFloat(bbox[2]),parseFloat(bbox[3]),{"wkid":4326});
var extentWM = esri.geometry.geographicToWebMercator(extent);
var zoomFactor = configOptions.zoomFactor || 1.5;
geoPortalGenie.mapModel.map.setExtent(extentWM.expand(zoomFactor), true);
}
});
//Set a listener for the Quick Preview button, which sends the preview layer to the main map
$(document).on('click','.btnQuickPreview',function(event) {
var url = this.getAttribute("data-url");
if(url !== null && url !== undefined) {
alert(url);
}
});
//Configure the modal results dialog
$("#modalResults").modal({
show: false,
keyboard: true,
remote: true
});
//Thumbnails are returned in the modal popup
$(document).on("click", ".btnThumbnailLink", function() {
//Display the loading indicator
$(this).button('loading');
//Retrieve the image's source
var img = this.getAttribute("data-img");
//Insert a link to the original image, plus the embedded image
var title = "";
var contents = "<a class='thumb' target='_blank' alt='thumbnail image' title='Click the image to open in a new window' href='" + img + "'><img class='thumb' alt='thumbnail image' src='" + img + "'/></a>";
//Show the modal dialog
$("#modalTitle").html(title);
$("#modalContent").html(contents);
$("#modalResults").modal("show");
$(this).button('reset');
});
//Details are returned in the modal popup based on the element's UUID
$(document).on("click", ".btnDetailsLink", function() {
$(this).button('loading');
//Retrieve the details from the GeoPortal server
var id = this.getAttribute("data-uuid");
geoPortalGenie.detailsModel.fetchDetails(id);
});
function buildSearchQuery() {
//Submit this query to the REST model. Thereafter, the Backbone models, views and collections
//handle the results
geoPortalGenie.restModel.buildSearchQuery();
}
function buildAdvancedSearchCategories() {
//Build the Filter By Theme dropdown for the Advanced Search bar, based on the themes and subthemes in the config file
var allCategoriesText = configOptions.allCategoriesText || "All themes";
var html = '<option class="allCategories" value="all">' + allCategoriesText + '</option>';
for (var t = 0; t < configOptions.categories.length; t++) {
var category = configOptions.categories[t];
html += '<optgroup label="' + category.heading + '">';
for (var i = 0; i < category.subcategories.length; i++) {
var subcategory = category.subcategories[i];
var label = subcategory.name;
if(subcategory.alias) {label = subcategory.alias;}
html += '<option value="' + subcategory.name + '">' + label + '</option>';
}
html += '</optgroup>';
}
jQuery("#filterThemes").html(html);
}
});