Skip to content

Commit

Permalink
initial refactor according to #3 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashafirsov committed Dec 1, 2019
1 parent fe29aea commit 66006a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ Proof of concept for
[Embeddable Progressive Application](https://github.com/EPA-WG/EPA-concept)
- a microapplication container, a WebComponent acting as seamless IFRAME and html include

[![git](https://cdnjs.cloudflare.com/ajax/libs/octicons/8.5.0/svg/mark-github.svg) GitHub](https://github.com/EPA-WG/embed-page)
| [demo](https://cdn.xml4jquery.com/ajax/libs/embed-page/0.0.21/build/esm-unbundled/demo/index.html)
[![NPM version][npm-image]][npm-url]
[![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg)](https://www.webcomponents.org/element/embed-page)
[![Join the chat at https://gitter.im/embed-page/Lobby](https://badges.gitter.im/embed-page/Lobby.svg)](https://gitter.im/embed-page/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

## Where to use?
<code>&lt;embed-page/&gt;</code> covers 2 extreme cases.
Expand Down
36 changes: 18 additions & 18 deletions embed-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,12 @@ import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';

_loadHtml( htmlStr )
{
{ let $b= css=> [ ...this.$.body.querySelectorAll( css ) ];
$b( "*[id]" ).map( el => delete this.globals[ el.id ] );
$b( "script" ).map( el =>
{
el.textContent=""; el.src=""; el.remove()
});
}
$( "script", this.$.body ).map( el => el.remove() );

Object.keys( this.globals_removable ||{} ).forEach( k=>{ delete this.globals_removable[ k ]; delete this.globals[ k ] });
epa.globals_removable = {};


this.loadCount++;
try
{ this.watchHtml(0);
Expand Down Expand Up @@ -518,10 +517,9 @@ import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';

f.lastElementChild ? f.replaceChild( el, f.lastElementChild ).remove() : f.appendChild( el );
globalThis[ 'EPA_'+this.uid ] = this;
this._extractVars( $( "*[id]", this.$.body ).map( n=>n.id ) );
[...this.window.document.querySelectorAll("*[id]")].map( el=> this.globals[ el.id ] = el );
$( "*[id]", this.$.body ).map( n=> this.globals[n.id] = this.globals_removable = n );

this._initEventHadlers();
$s.unshift( this._initEventHadlers() );
return this._loadScriptsCode($s)
.then( arr => this._extractImports($s) )
.then( arr => this._extractVars(arr) )
Expand Down Expand Up @@ -581,22 +579,24 @@ import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
Object.assign( this.globals, vars );
}
_initEventHadlers()
{ const epc = this;
{ const epc = this, codeArr = [];

$( EVENTS_SELECTOR, epc.$.body ).map( node=>
node.getAttributeNames().filter(a=>a.startsWith('on')).forEach( a=>
node.getAttributeNames().filter( a=>a.startsWith('on') ).forEach( a=>
{ const code = node.getAttribute( a );
epc._extractVars( [code] );
codeArr.push( code );
node.removeAttribute(a);
node.addEventListener( a.substring(2)
, ev=> epc._handleEvent.call( node, ev, code, a ) )
node.addEventListener( a.substring(2), ev=> epc._handleEvent.call( node, ev, code, a ) )
}) );
$( HREF_JS_SELECTOR, epc.$.body ).map( node=>
{ const code = node.getAttribute( 'href' ).substring( 'javascript:'.length );
epc._extractVars( [code] );

codeArr.push( code );
node.removeAttribute('href');
node.addEventListener( 'click', ev=>( ev.preventDefault(), (code !=='void(0)' && epc._handleEvent.call( node, ev, code, 'href' ) ) ) )
node.addEventListener( 'click', ev=>{ ev.preventDefault(); (code !=='void(0)' && epc._handleEvent.call( node, ev, code, 'href' ) ) } )
});
let s = doc.createElement('script');
s.textContent = codeArr.join(";");
return s;
}
_handleEvent( node, ev, code, eventAttr ){}// stub to be replaced by scoped implementation after _loadHtml

Expand Down

0 comments on commit 66006a4

Please sign in to comment.