/* This is a demo application. It uses jQuery. Use it on your on risk. No warranties are made. It is intended as a working example on how to use the API. */ /* Declaration of smhi and functions for retrieval of data in json format */ (function(smhi, $, undefined) { /* * The API end point. This is the only hardcoded url. All other urls are * returned in responses. */ smhi.endPoint = "https://opendata-download-icemap.smhi.se/api.json"; /* * The version of the API used. */ smhi.version = "1.0"; /* * Get all specifications - 'callback(specification)' is called on completion. * No error handling! */ smhi.getYears = function(callback) { // first of, get the category object with links to all versions. $.getJSON(smhi.endPoint).done(function(data) { var href = undefined; // find our version in json format for (var i = 0; i < data.version.length; i++) { var version = data.version[i]; if (version.key == smhi.version) { for (var j = 0; j < version.link.length; j++) { if (version.link[j].rel === 'version' && version.link[j].type === 'application/json') { href = version.link[j].href; } } } } // get the year object with links to all // months $.getJSON(href).done(function(data) { var years = []; for (var i = 0; i < data.resource.length; i++) { var year = data.resource[i]; var obj = { key : year.key, title : year.title, href : undefined, }; // find the appropriate link for (var j = 0; j < year.link.length; j++) { if (year.link[j].rel === 'year' && year.link[j].type === 'application/json') { obj.href = year.link[j].href; } } years.push(obj); } // callback a list of // years callback(years); }); }); }; /* * Get all months for a year - * 'callback(months)' is called on completion. No error handling! */ smhi.getMonths = function(year, callback) { // get the specification object with links to all the drainage basins $.getJSON(year.href).done(function(data) { var months = []; if (data.month !== null) { for (var i = 0; i < data.month.length; i++) { var month = data.month[i]; var monthKey = month.key; var monthTitle = month.title; var obj = { key : monthKey, title : monthTitle, href : undefined }; // find the appropriate link for (var j = 0; j < month.link.length; j++) { if (month.link[j].rel === 'month' && month.link[j].type === 'application/json') { obj.href = month.link[j].href; } } months.push(obj); } } // callback a list of months callback(months); }); }; /* * Get all days for a month - * 'callback(days)' is called on completion. No error handling! */ smhi.getDays = function(month, callback) { // get the month object with links to all the days $.getJSON(month.href).done(function(data) { var days = []; if (data.day !== null) { for (var i = 0; i < data.day.length; i++) { var day = data.day[i]; var dayKey = day.key; var dayTitle = day.title; var obj = { key : dayKey, title : dayTitle, href : undefined }; // find the appropriate link for (var j = 0; j < day.link.length; j++) { if (day.link[j].rel === 'day' && day.link[j].type === 'application/json') { obj.href = day.link[j].href; } } days.push(obj); } } callback(days); }); }; /* * Get the name and link for the zip file for a specific day - * 'callback(day)' is called on completion. No error handling! */ smhi.getDataFiles = function(day, callback) { // get the data object with link to the zip file $.getJSON(day.href).done(function(data) { var day = []; if (data.data !== null) { for (var i = 0; i < data.data.length; i++) { var dataInfo = data.data[i]; var dataFileKey = dataInfo.key; var dataFileTitle = dataInfo.title; var obj = { key : dataFileKey, title : dataFileKey+'.zip', href : undefined }; // find the appropriate link for (var j = 0; j < dataInfo.link.length; j++) { if (dataInfo.link[j].rel === 'data' && dataInfo.link[j].type === 'application/zip') { obj.href = dataInfo.link[j].href; } } day.push(obj); } } callback(day); }); }; }(window.smhi = window.smhi || {}, jQuery)); /* * faded loading on ajax requests */ $(document).ajaxStart(function() { $('.demo-content').hide(); $('.demo-loading').show(); }).ajaxStop(function() { $('.demo-content').show(); $('.demo-loading').hide(); }); /* * on ready - load all parameters when dom is done. */ $(document).ready(function() { // clear previous content $('#demo-years').html(''); $('#demo-months').html(''); $('#demo-days').html(''); $('#demo-data').html(''); // get specifications smhi.getYears(function(years) { for (var i = 0; i < years.length; i++) { // add new clickable list item per specification. var year = years[i]; var item = $('