Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Add evented Node handler to meta #666

Merged
merged 27 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/NodeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class NodeHandler extends Evented implements NodeHandlerInterface {
this.emit({ type: NodeEventType.Widget });
}

public addProjector(element: HTMLElement, properties: VNodeProperties): void {
public addProjector(): void {
this.emit({ type: NodeEventType.Projector });
}

Expand Down
39 changes: 13 additions & 26 deletions src/WidgetBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import {
WidgetMetaConstructor,
WidgetBaseConstructor,
WidgetBaseInterface,
WidgetProperties,
ProjectorRenderedEvent
WidgetProperties
} from './interfaces';
import RegistryHandler from './RegistryHandler';
import NodeHandler from './NodeHandler';
Expand Down Expand Up @@ -102,16 +101,6 @@ export function diffProperty(propertyName: string, diffFunction: DiffPropertyFun
});
}

/**
* WeakMap containing the current root node number
*/
export const currentRootNodeMap = new WeakMap<WidgetBase, number>();

/**
* Weakmap containing the number of root nodes
*/
export const numRootNodesMap = new WeakMap<WidgetBase, number>();

/**
* Generic decorator handler to take care of whether or not the decorator was called at the class level
* or the method level.
Expand Down Expand Up @@ -200,6 +189,9 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E

private _projectorAttachEvent: Handle;

private _currentRootNode = 0;
private _numRootNodes = 0;

/**
* @constructor
*/
Expand Down Expand Up @@ -251,7 +243,7 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
this.onElementCreated(element, String(properties.key));
}

protected afterRootCreateCallback(
private afterRootCreateCallback(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs the _ prefix

element: HTMLElement,
projectionOptions: ProjectionOptions,
vnodeSelector: string,
Expand All @@ -274,7 +266,7 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
this.onElementUpdated(element, String(properties.key));
}

protected afterRootUpdateCallback(
private afterRootUpdateCallback(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs the _ prefix

element: HTMLElement,
projectionOptions: ProjectionOptions,
vnodeSelector: string,
Expand All @@ -285,18 +277,13 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
}

private _addElementToNodeHandler(element: HTMLElement, projectionOptions: ProjectionOptions, properties: VNodeProperties) {
let currentRootNode = currentRootNodeMap.get(this) || 0;
const numRootNodes = numRootNodesMap.get(this);

currentRootNode += 1;
currentRootNodeMap.set(this, currentRootNode);
const isLastRootNode = (currentRootNode === numRootNodes);
this._currentRootNode += 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not this._currentRootNode++

const isLastRootNode = (this._currentRootNode === this._numRootNodes);

if (this._projectorAttachEvent === undefined) {
this._projectorAttachEvent = projectionOptions.nodeEvent.on('rendered',
({ element, properties }: ProjectorRenderedEvent) => {
this._nodeHandler.addProjector(element, properties);
});
this._projectorAttachEvent = projectionOptions.nodeEvent.on('rendered', () => {
this._nodeHandler.addProjector();
});
this.own(this._projectorAttachEvent);
}

Expand Down Expand Up @@ -475,8 +462,8 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
private _decorateNodes(node: DNode | DNode[]) {
let nodes = Array.isArray(node) ? [ ...node ] : [ node ];

numRootNodesMap.set(this, nodes.length);
currentRootNodeMap.set(this, 0);
this._numRootNodes = nodes.length;
this._currentRootNode = 0;
const rootNodes: DNode[] = [];

nodes.forEach(node => {
Expand Down
8 changes: 0 additions & 8 deletions src/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ export interface TypedTargetEvent<T extends EventTarget> extends Event {
target: T;
}

/**
* Projector rendered event
*/
export interface ProjectorRenderedEvent extends EventTargettedObject<Evented> {
element: HTMLElement;
properties: VNodeProperties;
}

/*
These are the event handlers exposed by Maquette.
*/
Expand Down
37 changes: 5 additions & 32 deletions src/mixins/Projector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import global from '@dojo/shim/global';
import { createHandle } from '@dojo/core/lang';
import { Handle } from '@dojo/interfaces/core';
import { Evented } from '@dojo/core/Evented';
import { VNode, VNodeProperties } from '@dojo/interfaces/vdom';
import { VNode } from '@dojo/interfaces/vdom';
import { ProjectionOptions } from '../interfaces';
import { dom, h, Projection } from 'maquette';
import 'pepjs';
import cssTransitions from '../animations/cssTransitions';
import { Constructor, DNode } from './../interfaces';
import { WidgetBase, currentRootNodeMap, numRootNodesMap } from './../WidgetBase';
import { WidgetBase } from './../WidgetBase';
import eventHandlerInterceptor from '../util/eventHandlerInterceptor';

/**
Expand Down Expand Up @@ -289,36 +289,6 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)
return this._projection.domNode.outerHTML;
}

protected afterRootCreateCallback(
element: HTMLElement,
projectionOptions: ProjectionOptions,
vnodeSelector: string,
properties: VNodeProperties
): void {
super.afterRootCreateCallback(element, projectionOptions, vnodeSelector, properties);
this._emitProjectorRenderedEvent(element, properties, projectionOptions.nodeEvent);
}

protected afterRootUpdateCallback(
element: HTMLElement,
projectionOptions: ProjectionOptions,
vnodeSelector: string,
properties: VNodeProperties
): void {
super.afterRootUpdateCallback(element, projectionOptions, vnodeSelector, properties);
this._emitProjectorRenderedEvent(element, properties, projectionOptions.nodeEvent);
}

private _emitProjectorRenderedEvent(element: HTMLElement, properties: VNodeProperties, nodeEvent: Evented) {
const currentRootNode = currentRootNodeMap.get(this) || 0;
const numRootNodes = numRootNodesMap.get(this);
const isLastRootNode = (currentRootNode === numRootNodes);

if (isLastRootNode) {
nodeEvent.emit({ type: 'rendered', element, properties });
}
}

public __render__(): VNode {
if (this._projectorChildren) {
this.setChildren(this._projectorChildren);
Expand Down Expand Up @@ -360,6 +330,7 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)

if (this._projection) {
this._projection.update(this._boundRender());
this._projectionOptions.nodeEvent.emit({ type: 'rendered' });
}
}

Expand Down Expand Up @@ -401,6 +372,8 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)
break;
}

this._projectionOptions.nodeEvent.emit({ type: 'rendered' });

return this._attachHandle;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/NodeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ registerSuite({
assert.isTrue(projectorStub.notCalled);
},
'add projector emits Projector event'() {
nodeHandler.addProjector(element, { key: 'foo' });
nodeHandler.addProjector();

assert.isTrue(widgetStub.notCalled);
assert.isTrue(elementStub.notCalled);
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/meta/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ registerSuite({
const div = document.createElement('div');
widget.append(div);

assert.isTrue(meta.has('foo'));
assert.isTrue(meta.has('bar'));
assert.isTrue(onFoo.calledOnce);
assert.isTrue(onBar.calledOnce);
assert.isTrue(onWidget.calledOnce);
assert.isTrue(onProjector.calledOnce);
assert.isTrue(onFoo.calledBefore(onWidget));
assert.isTrue(onFoo.calledBefore(onProjector));
assert.isTrue(meta.has('foo'), '1');
assert.isTrue(meta.has('bar'), '2');
assert.isTrue(onFoo.calledOnce, '3');
assert.isTrue(onBar.calledOnce, '4');
assert.isTrue(onWidget.calledOnce, '5');
assert.isTrue(onProjector.calledOnce, '6');
assert.isTrue(onFoo.calledBefore(onWidget), '7');
assert.isTrue(onFoo.calledBefore(onProjector), '8');
},
'integration with multiple root node'() {
class MyMeta extends MetaBase {
Expand Down