Commit f6d850e1 authored by Fred Chasen's avatar Fred Chasen

Add missing files, firefox overflow / undeflow support

parent a2f6c64c
import { getBoundingClientRect } from "../utils/utils"; import { getBoundingClientRect } from "../utils/utils";
import { walk, after, stackChildren } from "../utils/dom"; import { walk, after, stackChildren } from "../utils/dom";
import Parser from "./parser";
/** /**
* Layout * Layout
...@@ -11,7 +10,6 @@ class Layout { ...@@ -11,7 +10,6 @@ class Layout {
constructor(element, wrapper, parser) { constructor(element, wrapper, parser) {
this.element = element; this.element = element;
this.wrapper = wrapper; this.wrapper = wrapper;
this.parser = parser || new Parser();
let space = this.element.getBoundingClientRect(); let space = this.element.getBoundingClientRect();
this.width = Math.round(space.width); this.width = Math.round(space.width);
...@@ -483,11 +481,41 @@ class Layout { ...@@ -483,11 +481,41 @@ class Layout {
} }
} }
getOverflow() {
let overflow = this.overflow(this.element);
if (overflow) {
let extracted = overflow.extractContents();
this._onOverflow && this._onOverflow(extracted);
}
}
getUnderflow() {
this._onUnderflow && this._onUnderflow();
}
listeners() { listeners() {
if (typeof ResizeObserver !== "undefined") {
this.addResizeObserver();
} else {
this.element.addEventListener("overflow", this.getOverflow.bind(this), false);
this.element.addEventListener("underflow", this.getUnderflow.bind(this), false);
}
// TODO: fall back to mutation observer?
// Key scroll width from changing
this.element.addEventListener("scroll", () => {
this.element.scrollLeft = 0;
});
return true;
}
addResizeObserver() {
let wrapper = this.wrapper; let wrapper = this.wrapper;
let prevHeight = wrapper.getBoundingClientRect().height; let prevHeight = wrapper.getBoundingClientRect().height;
this.ro = new ResizeObserver( entries => {
let ro = new ResizeObserver( entries => {
for (let entry of entries) { for (let entry of entries) {
const cr = entry.contentRect; const cr = entry.contentRect;
...@@ -514,12 +542,17 @@ class Layout { ...@@ -514,12 +542,17 @@ class Layout {
} }
}); });
ro.observe(wrapper); this.ro.observe(wrapper);
}
// Key scroll width from changing destroy() {
this.element.addEventListener("scroll", () => { this.element.removeEventListener("overflow", this.getOverflow.bind(this), false);
this.element.scrollLeft = 0; this.element.removeEventListener("underflow", this.getUnderflow.bind(this), false);
})
this.ro.disconnect();
this.element = element;
this.wrapper = wrapper;
} }
} }
......
export function *walk(start, limiter) {
let node = start;
while (node) {
yield node;
if (node.childNodes.length) {
node = node.firstChild;
} else if (node.nextSibling) {
if (limiter && node === limiter) {
node = undefined;
break;
}
node = node.nextSibling;
} else {
while (node) {
node = node.parentNode;
if (limiter && node === limiter) {
node = undefined;
break;
}
if (node && node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
}
export function after(node, limiter) {
let after = node;
if (after.nextSibling) {
if (limiter && node === limiter) {
return;
}
after = after.nextSibling;
} else {
while (after) {
after = after.parentNode;
if (limiter && after === limiter) {
after = undefined;
break;
}
if (after && after.nextSibling) {
after = after.nextSibling;
break;
}
}
}
return after;
}
export function stackChildren(currentNode, stacked) {
let stack = stacked || [];
stack.unshift(currentNode);
let children = currentNode.children;
for (var i = 0, length = children.length; i < length; i++) {
stackChildren(children[i], stack);
}
return stack;
}
export function getBoundingClientRect(element) {
if (!element) {
return;
}
let rect;
if (element.getBoundingClientRect) {
rect = element.getBoundingClientRect();
} else {
let range = document.createRange();
range.selectNode(element);
rect = range.getBoundingClientRect();
}
return rect;
}
/**
* Generates a UUID
* based on: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
* @returns {string} uuid
*/
export function UUID() {
var d = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
d += performance.now(); //use high-precision timer if available
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment