Skip to content

Commit

Permalink
simplify OverlayChangeEvent and generalize forEachNodeList
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mrowetz committed May 6, 2017
1 parent 79c1ae3 commit caf8f25
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/ts/helpers/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ export function getLastItemOfNodeList<T extends Node>(list: NodeListOf<T>) {
}
return list.item(list.length - 1);
}

// /** Calls `fn` with each element of `els` */
export function forEachNodeList<T extends Node>(els: NodeListOf<T>, fn: (el: T, index: number) => any) {
Array.prototype.forEach.call(els, fn);
}
3 changes: 0 additions & 3 deletions src/ts/typing/open-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ export interface OpenOverlay {

export interface OverlayChangeEvent {
type: EventType;
/** list of currenly open overlays */
openOverlays: OpenOverlay[];
/** index that triggerd the change */
changedIndex?: number;
changedOverlay: OpenOverlay;
combinedOverlayHeight: number;
}

Expand Down
5 changes: 1 addition & 4 deletions src/ts/waterfall/details-overlay/overlay-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ class OverlayManager {
this.renderOverlays(detailsHeight, rowItems);
this.context.pubSub.publishToOverlayChanges({
"changedIndex": index,
"changedOverlay": newOverlay,
"combinedOverlayHeight": self.getCombinedOverlayHeight(),
"openOverlays": self.openOverlays,
"type": "open",
} as OverlayChangeEvent);
}
Expand Down Expand Up @@ -112,7 +110,6 @@ class OverlayManager {
this.context.pubSub.publishToOverlayChanges({
"changedIndex": index,
"combinedOverlayHeight": self.getCombinedOverlayHeight(),
"openOverlays": self.openOverlays,
"type": "closed",
} as OverlayChangeEvent);
}
Expand Down Expand Up @@ -158,7 +155,7 @@ class OverlayManager {
overlayHolder.appendChild(infoOverlay);
updateHeight(overlay, y, infoOverlay.getBoundingClientRect().height);
};
let updateRow = (rowItem, index) => {
let updateRow = (rowItem: SVGAElement, index: number) => {
const overlay = find(this.openOverlays, (o) => o.index === index);
const overlayEl = rowItem.nextElementSibling.firstElementChild as SVGGElement;
this.realignRow(rowItem, currY);
Expand Down
9 changes: 3 additions & 6 deletions src/ts/waterfall/details-overlay/svg-details-overlay.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { forEachNodeList } from "../../helpers/dom";
import * as svg from "../../helpers/svg";
import { OpenOverlay } from "../../typing/open-overlay";
import { createDetailsBody } from "./html-details-body";

export function forEach(els: NodeListOf<Element>, fn: (el: Element, index: number) => any) {
Array.prototype.forEach.call(els, fn);
}

function createCloseButtonSvg(y: number): SVGGElement {
let closeBtn = svg.newA("info-overlay-close-btn");

Expand Down Expand Up @@ -63,13 +60,13 @@ export function createRowInfoOverlay(overlay: OpenOverlay, y: number, detailsHei

let setTabStatus = (tabIndex: number) => {
overlay.openTabIndex = tabIndex;
forEach(tabs, (tab: HTMLDivElement, j) => {
forEachNodeList(tabs, (tab: HTMLDivElement, j) => {
tab.style.display = (tabIndex === j) ? "block" : "none";
buttons.item(j).classList.toggle("active", (tabIndex === j));
});
};

forEach(buttons, (btn, tabIndex) => {
forEachNodeList(buttons, (btn, tabIndex) => {
btn.addEventListener("click", () => setTabStatus(tabIndex));
});

Expand Down

0 comments on commit caf8f25

Please sign in to comment.