@@ -89,27 +89,11 @@ <h1 data-i18n="Countries"></h1>
8989 < nav >
9090 < a href ="#/en/search " data-i18n ="Search " data-i18n-href > </ a >
9191 < a href ="#/en/info " data-i18n ="Info " data-i18n-href > </ a >
92- < div class ="download-links ml-20 ">
93- < span class ="download " data-i18n ="Download "> </ span >
94- < img class ="download "
95- src ="img/Excel.svg "
96- height ="16 "
97- width ="16 "
98- alt ="Download to Excel "
99- title ="Download to Excel "
100- data-export-excel-selector ="table "
101- data-export-file-name ="[[Countries]].xlsx "
102- data-i18n-attr ="alt, title, data-export-file-name ">
103- < img class ="download "
104- src ="img/CSV.svg "
105- height ="16 "
106- width ="16 "
107- alt ="Download to CSV "
108- title ="Download to CSV "
109- data-export-csv-selector ="table "
110- data-export-file-name ="[[Countries]].csv "
111- data-i18n-attr ="alt, title, data-export-file-name ">
112- </ div >
92+ <!--
93+ <download-links> is a custom Web Component defined
94+ for this app near the bottom of this file.
95+ -->
96+ < download-links file-name ="Countries "> </ download-links >
11397 </ nav >
11498
11599 < json-data url ="{rootApiUrl}/countries " load-only-once onready ="showMessage('Countries List is ready') ">
@@ -336,5 +320,95 @@ <h1 data-i18n="Countries"></h1>
336320 } ) ;
337321 } ) ( ) ;
338322 </ script >
323+
324+ <!--
325+ Define Custom Web Component <download-links>
326+
327+ This uses the DataFormsJS Class `Component` which was created so that
328+ custom Web Components can be quickly defined for an app.
329+ -->
330+ < script type ="module ">
331+ // import { Component, html } from 'https://cdn.jsdelivr.net/npm/dataformsjs@5/js/web-components/Component.min.js';
332+ import { Component , html } from '../js/web-components/Component.js' ;
333+
334+ // Define class for the <download-links> element
335+ class DownloadLinks extends Component {
336+ // Props end up being assigned to both HTML observable attributes
337+ // and JavaScript properties. Example usage:
338+ // document.querySelector('download-links').fileName = 'Report';
339+ // document.querySelector('download-links').setAttribute('file-name', 'Report');
340+ static get props ( ) {
341+ return {
342+ fileName : null ,
343+ marginLeft : true ,
344+ }
345+ }
346+
347+ render ( ) {
348+ return html `
349+ < div class ="download-links ${ this . marginLeft ? 'ml-20' : '' } ">
350+ < span class ="download "> ${ window . i18nText ( 'Download' ) } </ span >
351+ < img class ="download "
352+ src ="img/Excel.svg "
353+ height ="16 "
354+ width ="16 "
355+ alt ="${ window . i18nText ( 'Download to Excel' ) } "
356+ title ="${ window . i18nText ( 'Download to Excel' ) } "
357+ data-export-excel-selector ="table "
358+ data-export-file-name ="${ window . i18nText ( this . fileName ) } .xlsx ">
359+ < img class ="download "
360+ src ="img/CSV.svg "
361+ height ="16 "
362+ width ="16 "
363+ alt ="${ window . i18nText ( 'Download to CSV' ) } "
364+ title ="${ window . i18nText ( 'Download to CSV' ) } "
365+ data-export-csv-selector ="table "
366+ data-export-file-name ="${ window . i18nText ( this . fileName ) } .csv ">
367+ </ div >
368+ ` ;
369+ }
370+ }
371+
372+ // Add <download-links> element to the page
373+ window . customElements . define ( 'download-links' , DownloadLinks ) ;
374+ </ script >
375+ < script nomodule >
376+ // Only modern browsers support Web Components so for IE and older browsers
377+ // handle the document event 'app:routeChanged' which gets triggered from
378+ // [polyfill.js] and then manually render HTML for the Web Component.
379+ ( function ( ) {
380+ function displayDownloadLinks ( ) {
381+ var element = document . querySelector ( 'download-links' ) ;
382+ if ( element === null ) {
383+ return ;
384+ }
385+
386+ var fileName = element . getAttribute ( 'file-name' ) ;
387+ var marginLeft = ! ( element . getAttribute ( 'margin-left' ) === 'false' ) ;
388+ var html = '<div class="download-links ' + ( marginLeft ? 'ml-20' : '' ) + '">' ;
389+ html += '<span class="download">' + window . i18nText ( 'Download' ) + '</span>' ;
390+ html += '<img class="download"' ;
391+ html += ' src="img/Excel.svg"' ;
392+ html += ' height="16"' ;
393+ html += ' width="16"' ;
394+ html += ' alt="' + window . i18nText ( 'Download to Excel' ) + '"' ;
395+ html += ' title="' + window . i18nText ( 'Download to Excel' ) + '"' ;
396+ html += ' data-export-excel-selector="table"' ;
397+ html += ' data-export-file-name="' + window . i18nText ( fileName ) + '.xlsx">' ;
398+ html += '<img class="download"' ;
399+ html += ' src="img/CSV.svg"' ;
400+ html += ' height="16"' ;
401+ html += ' width="16"' ;
402+ html += ' alt="' + window . i18nText ( 'Download to CSV' ) + '"' ;
403+ html += ' title="' + window . i18nText ( 'Download to CSV' ) + '"' ;
404+ html += ' data-export-csv-selector="table"' ;
405+ html += ' data-export-file-name="' + window . i18nText ( fileName ) + '.csv">' ;
406+ html += '</div>' ;
407+ element . innerHTML = html ;
408+ }
409+
410+ document . addEventListener ( 'app:routeChanged' , displayDownloadLinks ) ;
411+ } ) ( ) ;
412+ </ script >
339413 </ body >
340414</ html >
0 commit comments