Skip to content

Commit

Permalink
fix(templates): Safari polyfilled templates not recognizing parent as…
Browse files Browse the repository at this point in the history
… element

Fixes #5
  • Loading branch information
hotforfeature committed Apr 10, 2017
1 parent a6c8f4b commit 7c857c0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/events/polymer.directive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Directive, ElementRef, OnInit } from '@angular/core';

import { getCustomElementClass } from '../util/getCustomElementClass';
import { Polymer } from '../util/Polymer';
import '../util/Polymer';

@Directive({
selector: '[polymer]'
Expand All @@ -22,7 +22,7 @@ export class PolymerDirective implements OnInit {
}

notify.forEach(property => {
const eventName = `${Polymer.CaseMap.camelToDashCase(property)}-changed`;
const eventName = `${window.Polymer.CaseMap.camelToDashCase(property)}-changed`;
this.elementRef.nativeElement.addEventListener(eventName, event => {
this.elementRef.nativeElement.dispatchEvent(new CustomEvent(`${property}Change`, {
detail: event.detail
Expand Down
17 changes: 8 additions & 9 deletions src/templates/polymer-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@ import { Directive, TemplateRef, ViewContainerRef } from '@angular/core';
})
export class PolymerTemplateDirective {
constructor(public view: ViewContainerRef, public templateRef: TemplateRef<any>) {
const parentElement: HTMLElement = view.element.nativeElement.parentElement;
const parentNode = (<HTMLElement>view.element.nativeElement).parentNode;
const template = document.createElement('template');
const viewRef = view.createEmbeddedView(templateRef);
viewRef.rootNodes.forEach(rootNode => {
parentElement.removeChild(rootNode);
parentNode.removeChild(rootNode);
template.content.appendChild(rootNode);
});

parentElement.appendChild(template);
parentNode.appendChild(template);

// Detach and re-attach the parent element. This will trigger any template attaching logic
// that a custom elements needs which Angular skipped when using <ng-template>
const hostElement = parentElement.parentElement;
const parentElementIndex = Array.from(hostElement.children).indexOf(parentElement);
const parentSibling = parentElement.nextSibling;
hostElement.removeChild(parentElement);
const hostNode = parentNode.parentNode;
const parentSibling = parentNode.nextSibling;
hostNode.removeChild(parentNode);
if (parentSibling) {
hostElement.insertBefore(parentSibling, parentElement);
hostNode.insertBefore(parentSibling, parentNode);
} else {
hostElement.appendChild(parentElement);
hostNode.appendChild(parentNode);
}
}
}
2 changes: 1 addition & 1 deletion src/util/Polymer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ declare global {
}
}

export const Polymer = window.Polymer;
export { };
2 changes: 1 addition & 1 deletion src/util/customElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ declare global {
}
}

export const customElements = window.customElements;
export { };
4 changes: 2 additions & 2 deletions src/util/getCustomElementClass.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ElementRef } from '@angular/core';

import { customElements } from './customElements';
import './customElements';
import { getTagName } from './getTagName';

export function getCustomElementClass(elementRef: ElementRef): Function {
if (elementRef && elementRef.nativeElement) {
const htmlElement = Object.getPrototypeOf(elementRef.nativeElement);
if (htmlElement && htmlElement.is) {
return customElements.get(htmlElement.is);
return window.customElements.get(htmlElement.is);
} else {
console.warn(`${getTagName(elementRef)} is not registered`);
}
Expand Down

0 comments on commit 7c857c0

Please sign in to comment.