Ero sivun ”Widget:JwObsCharts” versioiden välillä
Järvi-meriwikistä
Rivi 68: | Rivi 68: | ||
memo.graphType = jQuery( this ).attr( 'data-graph-type' ); | memo.graphType = jQuery( this ).attr( 'data-graph-type' ); | ||
memo.editUrl = jQuery( this ).attr( 'data-edit-url' ); if (typeof memo.editUrl == 'undefined') { memo.editUrl = '-'; } | memo.editUrl = jQuery( this ).attr( 'data-edit-url' ); if (typeof memo.editUrl == 'undefined') { memo.editUrl = '-'; } | ||
+ | memo.download = true; if ( typeof jQuery( this ).attr( 'data-download' ) != 'undefined' ) { memo.download = ( jQuery( this ).attr( 'data-download' ) == '1' || jQuery( this ).attr( 'data-download' ) == 'true' ); } | ||
+ | |||
memo.csvfile = jQuery( this ).attr( 'data-csv-filename' ); if (typeof memo.csvfile == 'undefined') { memo.csvfile = i18n[ memo.obscode ].title[ memo.lang ] + '_SiteID-' + memo.siteid + '_Downloaded-' + today.toISOString().split( 'T' )[0] + '.csv'; } | memo.csvfile = jQuery( this ).attr( 'data-csv-filename' ); if (typeof memo.csvfile == 'undefined') { memo.csvfile = i18n[ memo.obscode ].title[ memo.lang ] + '_SiteID-' + memo.siteid + '_Downloaded-' + today.toISOString().split( 'T' )[0] + '.csv'; } | ||
if ( typeof jQuery( this ).attr( 'data-maintainer' ) == 'undefined' ) { memo.maintainerShow = false; } | if ( typeof jQuery( this ).attr( 'data-maintainer' ) == 'undefined' ) { memo.maintainerShow = false; } | ||
Rivi 139: | Rivi 141: | ||
} | } | ||
− | // add download data button | + | // add download data button |
− | memo.headerEl.append( '<div class="obschartbutton download-button" style="display: none; margin-left: 10px; float: right; Xbackground-color: rgba( 255, 255, 255, 1 ); border-radius: 50%; Xborder: 1px solid rgba( 0, 0, 0, 0.5 ); Xpadding: 1px;"><button title="' + i18n['download'][memo.lang] + '" style="width: 24px; height: 24px; padding: 0; margin: 0; color: #1B599B; background: none; border: none;"><i class="fas fa-download"></i></button></div>' ); | + | if ( memo.download ) { |
− | + | memo.headerEl.append( '<div class="obschartbutton download-button" style="display: none; margin-left: 10px; float: right; Xbackground-color: rgba( 255, 255, 255, 1 ); border-radius: 50%; Xborder: 1px solid rgba( 0, 0, 0, 0.5 ); Xpadding: 1px;"><button title="' + i18n['download'][memo.lang] + '" style="width: 24px; height: 24px; padding: 0; margin: 0; color: #1B599B; background: none; border: none;"><i class="fas fa-download"></i></button></div>' ); | |
+ | memo.downloadEl = jQuery( this ).find( '.download-button' ); | ||
− | + | memo.downloadEl.on( 'click', function() { | |
− | + | var header = ''; | |
− | + | header += '\uFEFF'; // BOM | |
− | + | var csv = ''; | |
− | + | //csv += '\r\nsep=,\r\n'; | |
− | + | var headerCols = []; | |
− | + | var csvCols = []; | |
− | + | if ( i18n[memo.obscode].type == 'num' ) { | |
− | + | //headerCols.push( i18n[ 'Date' ][ memo.lang ] ); | |
− | + | //headerCols.push( i18n[ 'Date' ][ memo.lang ] ); | |
− | + | headerCols.push( '"' + i18n[memo.obscode].axisTitleV[memo.lang] + '"' ); | |
− | + | headerCols.push( 'Lisätiedot' ); | |
− | + | headerCols.push( 'Valokuva' ); | |
− | + | headerCols.push( 'Havainnoijan status' ); | |
+ | |||
+ | header += headerCols.join( i18n['sep'][memo.lang] ); | ||
+ | |||
+ | for ( var o in memo.obses ) { | ||
+ | |||
+ | csvCols = []; | ||
− | + | csvCols.push( memo.obses[o].obsdatetime.getFullYear() + '-' + ( memo.obses[o].obsdatetime.getMonth() + 1 ) + '-' + memo.obses[o].obsdatetime.getDate() ); | |
− | + | if ( memo.obses[o].obsdatetime.getUTCHours() == 0 && memo.obses[o].obsdatetime.getUTCMinutes() == 0 && memo.obses[o].obsdatetime.getUTCSeconds() == 0 ) { | |
+ | csvCols.push( '' ); | ||
+ | } else { | ||
+ | csvCols.push( ( "0" + memo.obses[o].obsdatetime.getUTCHours() ).slice(-2) + ':' + ( "0" + memo.obses[o].obsdatetime.getUTCMinutes() ).slice(-2) ); | ||
+ | } | ||
− | + | csvCols.push( '"' + memo.obses[o].value.toString().replace( '.', i18n['dec'][memo.lang] ) + '"' ); | |
+ | csvCols.push( memo.obses[o].addInfo ); | ||
+ | csvCols.push( '' ); | ||
+ | csvCols.push( memo.obses[o].maintainer ); | ||
− | + | csv += '\r\n' + csvCols.join( i18n['sep'][memo.lang] ); | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | + | } else if ( i18n[memo.obscode].type == 'date' ) { | |
− | + | if ( i18n[memo.obscode].season == 'winter' ) { | |
+ | header += i18n[ 'season' ][ i18n[memo.obscode].season ][ memo.lang ]; | ||
+ | } else { | ||
+ | header += i18n[ 'year' ][ memo.lang ]; | ||
+ | } | ||
+ | |||
+ | header += ';' + i18n[ 'Observation' ][ memo.lang ]; | ||
+ | header += ';' + i18n[ 'Value' ][ memo.lang ]; | ||
+ | header += ';' + i18n[ 'ValueUnit' ][ memo.lang ]; | ||
+ | header += ';' + i18n[ 'Additionalinfo' ][ memo.lang ]; | ||
+ | header += ';' + i18n[ 'Image' ][ memo.lang ]; | ||
+ | header += ';' + i18n[ 'ObserverStatus' ][ memo.lang ]; | ||
+ | |||
+ | for ( var s in memo.seriesA ) { | ||
+ | |||
+ | for ( var o in memo.seriesA[ s ].obses ) { | ||
+ | |||
+ | obs = memo.seriesA[ s ].obses[ o ]; | ||
+ | csv += '\r\n'; | ||
− | + | csv += obs.season + '-' + (obs.season+1); | |
− | + | csv += ';' + i18n[ memo.seriesA[ s ].id ].title[ memo.lang ]; | |
− | |||
− | |||
− | |||
− | |||
− | + | csv += ';' + obs.value; | |
− | + | csv += ';' + obs.valueUnit; | |
− | + | ||
− | + | csv += ';' + obs.addInfo; | |
− | + | csv += ';'; | |
− | + | csv += ';' + obs.maintainer; | |
+ | |||
+ | } | ||
− | + | } | |
− | + | } else if ( i18n[memo.obscode].type == 'cat' ) { | |
− | + | header += 'Päivämäärä'; | |
+ | header += ';' + 'Kellonaika'; | ||
+ | header += ';' + i18n[memo.obscode].title[memo.lang]; | ||
+ | header += ';' + i18n[memo.obscode].title[memo.lang]; | ||
+ | header += ';' + utf8StringToUtf16String( 'Lisätiedot' ); | ||
+ | header += ';' + 'Valokuva'; | ||
+ | header += ';' + 'Havainnoijan status'; | ||
+ | |||
+ | for ( var o in memo.obses ) { | ||
csv += '\r\n'; | csv += '\r\n'; | ||
− | csv += | + | csv += memo.obses[o].obsdatetime.toLocaleDateString(); //.toISOString(); |
− | csv += ';' + | + | if ( memo.obses[o].obsdatetime.getUTCHours() == 0 && memo.obses[o].obsdatetime.getUTCMinutes() == 0 && memo.obses[o].obsdatetime.getUTCSeconds() == 0 ) { |
+ | csv += ';'; | ||
+ | } else { | ||
+ | csv += ';' + ( "0" + memo.obses[o].obsdatetime.getUTCHours() ).slice(-2) + ':' + ( "0" + memo.obses[o].obsdatetime.getUTCMinutes() ).slice(-2); | ||
+ | } | ||
− | csv += ';' + | + | csv += ';' + memo.obses[o].value; |
− | csv += ';' + | + | csv += ';' + i18n[memo.obscode].catNames[memo.obses[o].value][memo.lang]; |
− | csv += ';' + | + | csv += ';' + memo.obses[o].addInfo; |
csv += ';'; | csv += ';'; | ||
− | csv += ';' + | + | csv += ';' + memo.obses[o].maintainer; |
} | } | ||
Rivi 224: | Rivi 260: | ||
} | } | ||
− | + | csv = header + csv; | |
− | + | var exportedFilename = memo.csvfile; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | var blob = new Blob( [csv], { type: 'text/csv;charset=utf-16LE;' }); | |
− | + | if (navigator.msSaveBlob) { // IE 10+ | |
− | + | navigator.msSaveBlob(blob, exportedFilename); | |
− | + | } else { | |
− | + | var link = document.createElement("a"); | |
− | + | if (link.download !== undefined) { // feature detection | |
− | if ( | + | // Browsers that support HTML5 download attribute |
− | + | var url = URL.createObjectURL(blob); | |
− | + | link.setAttribute("href", url); | |
− | + | link.setAttribute("download", exportedFilename); | |
+ | link.style.visibility = 'hidden'; | ||
+ | document.body.appendChild(link); | ||
+ | link.click(); | ||
+ | document.body.removeChild(link); | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | } | + | }); |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
} | } |