7
7
var data_url = "https://data.transformap.co/raw/5d6b9d3d32097fd6832200874402cfc3" ;
8
8
var fallback_data_url = "susydata-fallback.json" ;
9
9
10
+ var redundant_data_urls = [ "https://data.transformap.co/raw/5d6b9d3d32097fd6832200874402cfc3" , "susydata-fallback.json" ] ;
11
+
10
12
/* fix for leaflet scroll on devices that fire scroll too fast, e.g. Macs
11
13
see https://github.com/Leaflet/Leaflet/issues/4410#issuecomment-234133427
12
14
@@ -176,153 +178,7 @@ function addPOIsToMap(geoJSONfeatureCollection) {
176
178
return true ;
177
179
}
178
180
179
- /* new version of getting map data with promises
180
- should fetch data_url, and in case it doesn't respond in a timeout, fetch fallback_data_url
181
-
182
- taken from https://blog.hospodarets.com/fetch_in_action
183
- */
184
-
185
- var processStatus = function ( response ) {
186
- // status "0" to handle local files fetching (e.g. Cordova/Phonegap etc.)
187
- if ( response . status === 200 || response . status === 0 ) {
188
- return Promise . resolve ( response )
189
- } else {
190
- return Promise . reject ( new Error ( response . statusText ) )
191
- }
192
- } ;
193
-
194
- var parseJson = function ( response ) {
195
- return response . json ( ) ;
196
- } ;
197
-
198
- /* @returns {wrapped Promise } with .resolve/.reject/.catch methods */
199
- // It goes against Promise concept to not have external access to .resolve/.reject methods, but provides more flexibility
200
- var getWrappedPromise = function ( ) {
201
- var wrappedPromise = { } ,
202
- promise = new Promise ( function ( resolve , reject ) {
203
- wrappedPromise . resolve = resolve ;
204
- wrappedPromise . reject = reject ;
205
- } ) ;
206
- wrappedPromise . then = promise . then . bind ( promise ) ;
207
- wrappedPromise . catch = promise . catch . bind ( promise ) ;
208
- wrappedPromise . promise = promise ; // e.g. if you want to provide somewhere only promise, without .resolve/.reject/.catch methods
209
- return wrappedPromise ;
210
- } ;
211
-
212
- /* @returns {wrapped Promise } with .resolve/.reject/.catch methods */
213
- var getWrappedFetch = function ( ) {
214
- var wrappedPromise = getWrappedPromise ( ) ;
215
- var args = Array . prototype . slice . call ( arguments ) ; // arguments to Array
216
-
217
- fetch . apply ( null , args ) // calling original fetch() method
218
- . then ( function ( response ) {
219
- wrappedPromise . resolve ( response ) ;
220
- } , function ( error ) {
221
- wrappedPromise . reject ( error ) ;
222
- } )
223
- . catch ( function ( error ) {
224
- wrappedPromise . catch ( error ) ;
225
- } ) ;
226
- return wrappedPromise ;
227
- } ;
228
-
229
- /**
230
- * Fetch JSON by url
231
- * @param { {
232
- * url: {String},
233
- * [cacheBusting]: {Boolean}
234
- * } } params
235
- * @returns {Promise }
236
- */
237
-
238
- var MAX_WAITING_TIME = 5000 ; // in ms
239
-
240
- var getJSON = function ( params ) {
241
- var wrappedFetch = getWrappedFetch (
242
- params . cacheBusting ? params . url + '?' + new Date ( ) . getTime ( ) : params . url ,
243
- {
244
- method : 'get' , // optional, "GET" is default value
245
- headers : {
246
- 'Accept' : 'application/json'
247
- }
248
- } ) ;
249
-
250
- var timeoutId = setTimeout ( function ( ) {
251
- wrappedFetch . reject ( new Error ( 'Load timeout for resource: ' + params . url ) ) ; // reject on timeout
252
- } , MAX_WAITING_TIME ) ;
253
-
254
- return wrappedFetch . promise // getting clear promise from wrapped
255
- . then ( function ( response ) {
256
- clearTimeout ( timeoutId ) ;
257
- return response ;
258
- } )
259
- . then ( processStatus )
260
- . then ( parseJson ) ;
261
- } ;
262
-
263
- /*--- TEST --*/
264
- var onComplete = function ( ) {
265
- console . log ( 'I\'m invoked in any case after success/error' ) ;
266
- } ;
267
-
268
- /*
269
- getJSON({
270
- url: data_url,
271
- cacheBusting: true
272
- }).then(function (data) {// on success
273
- console.log('JSON parsed successfully!');
274
- console.log(data);
275
- addPOIsToMap(data);
276
- // onComplete(data);
277
- }, function (error) {// on reject
278
- console.error('An error occured!');
279
- console.error(error.message ? error.message : error);
280
- // onComplete(error);
281
- });*/
282
-
283
- function myGetJSON ( url , success_function , error_function ) {
284
- var getJSONparams = { url : url , cacheBusting : true } ;
285
-
286
- getJSON ( getJSONparams ) . then (
287
- function ( data ) { success_function ( data ) } ,
288
- function ( error ) { error_function ( error ) }
289
- ) ;
290
- }
291
-
292
- myGetJSON ( data_url ,
293
- function ( data ) {
294
- var adding_pois_successful = addPOIsToMap ( data ) ;
295
- if ( adding_pois_successful ) {
296
- console . log ( "1st try to fetch POI data from API (" + data_url + ") was successful" ) ;
297
- return ;
298
- }
299
- console . log ( "API didn't return useful data from " + data_url + ", try static file" ) ;
300
- myGetJSON ( fallback_data_url ,
301
- function ( data ) {
302
- if ( ! addPOIsToMap ( data ) )
303
- console . error ( "2nd try to fetch POI data from " + fallback_data_url + " failed too" ) ;
304
- } ,
305
- function ( error ) {
306
- console . error ( "2nd try to fetch POI data from " + fallback_data_url + " failed too" ) ;
307
- console . error ( error . message ? error . message : error ) ;
308
- } ) ;
309
- } ,
310
- function ( error ) {
311
- console . log ( "1st try to fetch POI data from API (" + data_url + ") failed, try fallback" ) ;
312
- myGetJSON ( fallback_data_url ,
313
- function ( data ) {
314
- var adding_pois_successful = addPOIsToMap ( data ) ;
315
- if ( adding_pois_successful )
316
- console . log ( "2nd try to fetch POI data from " + fallback_data_url + " successful." ) ;
317
- else
318
- console . error ( "2nd try to fetch POI data from " + fallback_data_url + " failed too" ) ;
319
- } ,
320
- function ( error ) {
321
- console . error ( "2nd try to fetch POI data from " + fallback_data_url + " failed too" ) ;
322
- console . error ( error . message ? error . message : error ) ;
323
- } ) ;
324
- } ) ;
325
-
181
+ redundantFetch ( redundant_data_urls , addPOIsToMap , function ( error ) { console . error ( "none of the POI data urls available" ) ; } ) ;
326
182
327
183
/* get taxonomy stuff */
328
184
var taxonomy_url = "http://viewer.transformap.co/taxonomy.json" ;
@@ -364,8 +220,7 @@ function getLangTaxURL(lang) {
364
220
'SERVICE wikibase:label {bd:serviceParam wikibase:language "' + lang + '" }' +
365
221
'}' ;
366
222
367
- // return 'https://query.base.transformap.co/bigdata/namespace/transformap/sparql?query=' +encodeURIComponent(tax_query) + "&format=json"; // server not CORS ready yet
368
- return "https://raw.githubusercontent.com/TransforMap/transformap-viewer-translations/master/taxonomy-backup/susy/taxonomy." + lang + ".json" ;
223
+ return 'https://query.base.transformap.co/bigdata/namespace/transformap/sparql?query=' + encodeURIComponent ( tax_query ) + "&format=json" ; // server not CORS ready yet
369
224
}
370
225
371
226
function setFilterLang ( lang ) {
@@ -377,11 +232,9 @@ function setFilterLang(lang) {
377
232
if ( multilang_taxonomies [ lang ] ) {
378
233
setTaxonomy ( multilang_taxonomies [ lang ] ) ;
379
234
} else {
380
- $ . ajax ( {
381
- url : getLangTaxURL ( lang ) ,
382
- dataType : "json" ,
383
- success : applyOrAddTaxonomyLang ,
384
- } ) ;
235
+ redundantFetch ( [ getLangTaxURL ( lang ) , "https://raw.githubusercontent.com/TransforMap/transformap-viewer-translations/master/taxonomy-backup/susy/taxonomy." + lang + ".json" ] ,
236
+ applyOrAddTaxonomyLang ,
237
+ function ( error ) { console . error ( "none of the taxonomy data urls available" ) } ) ;
385
238
}
386
239
}
387
240
@@ -1169,7 +1022,8 @@ setFallbackLangs();
1169
1022
1170
1023
1171
1024
/* get languages for UI from our Wikibase, and pick languages that are translated there */
1172
- $ . getJSON ( "https://base.transformap.co/wiki/Special:EntityData/Q5.json" , function ( returned_data ) {
1025
+
1026
+ function initializeLanguageSwitcher ( returned_data ) {
1173
1027
for ( lang in returned_data . entities . Q5 . labels ) { //Q5 is arbitrary. Choose one that gets translated for sure.
1174
1028
supported_languages . push ( lang ) ;
1175
1029
}
@@ -1210,7 +1064,11 @@ $.getJSON("https://base.transformap.co/wiki/Special:EntityData/Q5.json", functio
1210
1064
$ ( "#languageSelector ul" ) . append ( "<li targetlang=" + langcode + is_default + " onClick='switchToLang(\"" + langcode + "\");'>" + item + "</li>" ) ;
1211
1065
} ) ;
1212
1066
} ) ;
1213
- } ) ;
1067
+ }
1068
+ redundantFetch ( [ "https://base.transformap.co/wiki/Special:EntityData/Q5.json" , "https://raw.githubusercontent.com/TransforMap/transformap-viewer/Q5-fallback.json" , "Q5-fallback.json" ] ,
1069
+ initializeLanguageSwitcher ,
1070
+ function ( error ) { console . error ( "none of the lang init data urls available" ) } ) ;
1071
+
1214
1072
1215
1073
function switchToLang ( lang ) {
1216
1074
$ ( "#languageSelector li.default" ) . removeClass ( "default" ) ;
0 commit comments