|
| 1 | + export function |
| 2 | +wait4DomUpdated( cb ) |
| 3 | +{ return new Promise( resolve => |
| 4 | + { const assureDom = window.requestIdleCallback || window.requestAnimationFrame |
| 5 | + , done = ()=> resolve(cb && cb() ); |
| 6 | + assureDom ? assureDom( done ) : setTimeout( done, 100 ); |
| 7 | + }) |
| 8 | +} |
| 9 | + export function |
| 10 | +toKebbabCase(s){ return (s||'').toLowerCase().replaceAll(/\s/g ,'-')} |
| 11 | + export class |
| 12 | +FetchElement extends HTMLElement |
| 13 | +{ |
| 14 | + static get observedAttributes(){ return [ 'src', 'method', 'headers', 'state', 'status', 'error' ]; } |
| 15 | + |
| 16 | + get headers(){ return {} } |
| 17 | + abort(){} |
| 18 | + fetch(){} |
| 19 | + |
| 20 | + constructor() |
| 21 | + { super(); |
| 22 | + let promise = Promise.resolve(); |
| 23 | + const controller = new AbortController(); |
| 24 | + const { signal } = controller; |
| 25 | + |
| 26 | + this.abort = ()=> controller.abort(); |
| 27 | + this.fetch = async ( url, options )=> |
| 28 | + { |
| 29 | + this.state = 'loading'; |
| 30 | + this.status = ''; |
| 31 | + |
| 32 | + const opt = { method : this.getAttribute( 'method' ) || 'GET' |
| 33 | + , headers : this.headers |
| 34 | + , ...options |
| 35 | + , signal |
| 36 | + }; |
| 37 | + |
| 38 | + promise = new Promise(async (resolve, reject) => |
| 39 | + { try |
| 40 | + { const response = await fetch( url, opt ); |
| 41 | + const ret = await this.onResponse( response ) |
| 42 | + const res = await this.onResult( ret ); |
| 43 | + this.error ? reject( this.error ) : resolve( res ); |
| 44 | + }catch( ex ) |
| 45 | + { reject( this.onError( ex ) ); } |
| 46 | + }) |
| 47 | + }; |
| 48 | + Object.defineProperty( this, 'promise', { get(){ return promise; } } ); |
| 49 | + FetchElement.observedAttributes |
| 50 | + .filter( prop => prop !=='headers' ) |
| 51 | + .forEach( prop => Object.defineProperty( this, prop, |
| 52 | + { get : () => this.getAttribute( prop ) |
| 53 | + , set: val => this.setAttribute( prop, val ) |
| 54 | + } )); |
| 55 | + } |
| 56 | + |
| 57 | + attributeChangedCallback( name, oldValue, newValue ) |
| 58 | + { |
| 59 | + switch( name ) |
| 60 | + { case 'headers': |
| 61 | + this[ name ] = eval( newValue ); |
| 62 | + break; |
| 63 | + case 'src': |
| 64 | + this.fetch( newValue ); |
| 65 | + // setTimeout( () => this.fetch( newValue ),0 ); |
| 66 | + break; |
| 67 | + default: |
| 68 | + if( this[ name ] !== newValue ) |
| 69 | + this[ name ] = newValue; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + async onResponse( response ) |
| 74 | + { const s = 1*( this.status = response.status); |
| 75 | + if( s<200 || s>=300 ) |
| 76 | + this.error = 'network error'; |
| 77 | + this.contentType = response.headers.get( 'content-type' ); |
| 78 | + this.responseHeaders = response.headers; |
| 79 | + if( this.contentType.includes( 'json' ) ) |
| 80 | + return response.json(); |
| 81 | + return response.text(); |
| 82 | + } |
| 83 | + |
| 84 | + async onResult( result ) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + this.state = 'rendering'; |
| 89 | + |
| 90 | + if( this.contentType.includes( 'xml' ) ) |
| 91 | + { |
| 92 | + const xml = ( new window.DOMParser() ).parseFromString( result, "text/xml" ); |
| 93 | + this.data2Html( xml, this.contentType, this.status, this.responseHeaders ); |
| 94 | + // todo xslt from xml |
| 95 | + }else if( this.contentType.includes( 'html' ) ) |
| 96 | + { |
| 97 | + this.innerHTML = result; |
| 98 | + await wait4DomUpdated(); |
| 99 | + this.data2Html( result, this.contentType, this.status, this.responseHeaders ); |
| 100 | + await wait4DomUpdated(); |
| 101 | + }else if( this.contentType.includes( 'json' ) ) |
| 102 | + { await wait4DomUpdated(); |
| 103 | + const html = this.data2Html( result, this.contentType, this.status, this.responseHeaders ); |
| 104 | + this.innerHTML = html || this.json2table(result); |
| 105 | + await wait4DomUpdated(); |
| 106 | + } |
| 107 | + }finally |
| 108 | + { |
| 109 | + this.state = 'loaded'; |
| 110 | + } |
| 111 | + } |
| 112 | + data2Html( data, contentType, code ){} |
| 113 | + onError( error ){ this.state = 'error'; return error; } |
| 114 | + |
| 115 | + getKeys( obj ){ return Object.keys(obj); } |
| 116 | + |
| 117 | + json2table( data ) |
| 118 | + { |
| 119 | + if( Array.isArray(data) ) |
| 120 | + { if( !data.length ) |
| 121 | + return '' |
| 122 | + const keys = this.getKeys( data[0] ); |
| 123 | + |
| 124 | + return ` |
| 125 | +<table> |
| 126 | + <tr>${keys.map( k=>`<th>${k}</th>` ).join('\n')}</tr> |
| 127 | + ${data.map( r=>` |
| 128 | + <tr>${ keys.map(k=>` |
| 129 | + <td key="${toKebbabCase(k)}"> |
| 130 | + ${this.json2table(r[k])} |
| 131 | + </td>`).join('') |
| 132 | + } |
| 133 | + </tr>`).join('\n')} |
| 134 | +</table> |
| 135 | +` } |
| 136 | + if( typeof data !== 'object' || data === null ) |
| 137 | + return data; |
| 138 | + const keys = this.getKeys( data ); |
| 139 | + return ` |
| 140 | +<table> |
| 141 | + ${ keys.map( k=>` |
| 142 | +<tr><th>${k}</th> |
| 143 | + <td key="${toKebbabCase(k)}">${ this.json2table(data[k]) }</td> |
| 144 | +</tr>` ).join('')} |
| 145 | +</table> |
| 146 | +` |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | +export default FetchElement; |
0 commit comments