@up-group/react-controls
Version:
We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get
343 lines • 13.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var moment = require("moment");
var EPSILON = 0.001;
function _get(object, key) {
return typeof object.get === 'function' ? object.get(key) : object[key];
}
exports._get = _get;
function _length(object) {
return typeof object.count === 'function' ? object.count() : object.length;
}
exports._length = _length;
function arraysEqual(array1, array2) {
return (_length(array1) === _length(array2)) && array1.every(function (element, index) {
return element === _get(array2, index);
});
}
exports.arraysEqual = arraysEqual;
function iterateTimes(start, end, unit, timeSteps, callback) {
var time = moment(start).startOf(unit);
if (timeSteps[unit] && timeSteps[unit] > 1) {
var value = time.get(unit);
time.set(unit, value - (value % timeSteps[unit]));
}
while (time.valueOf() < end) {
var nextTime = moment(time).add(timeSteps[unit] || 1, unit + "s");
callback(time, nextTime);
time = nextTime;
}
}
exports.iterateTimes = iterateTimes;
function getMinUnit(zoom, width, timeSteps) {
var timeDividers = {
second: 1000,
minute: 60,
hour: 60,
day: 24,
month: 30,
year: 12
};
var minUnit = 'year';
var breakCount = zoom;
var minCellWidth = 17;
Object.keys(timeDividers).some(function (unit) {
breakCount = breakCount / timeDividers[unit];
var cellCount = breakCount / timeSteps[unit];
var countNeeded = width / (timeSteps[unit] && timeSteps[unit] > 1 ? 3 * minCellWidth : minCellWidth);
if (cellCount < countNeeded) {
minUnit = unit;
return true;
}
});
return minUnit;
}
exports.getMinUnit = getMinUnit;
function getNextUnit(unit) {
var nextUnits = {
second: 'minute',
minute: 'hour',
hour: 'day',
day: 'month',
month: 'year'
};
return nextUnits[unit] || '';
}
exports.getNextUnit = getNextUnit;
function getParentPosition(element) {
var xPosition = 0;
var yPosition = 0;
var first = true;
while (element) {
xPosition += (element.offsetLeft - (first ? 0 : element.scrollLeft) + element.clientLeft);
yPosition += (element.offsetTop - (first ? 0 : element.scrollTop) + element.clientTop);
element = element.offsetParent;
first = false;
}
return { x: xPosition, y: yPosition };
}
exports.getParentPosition = getParentPosition;
function coordinateToTimeRatio(canvasTimeStart, canvasTimeEnd, canvasWidth) {
return (canvasTimeEnd - canvasTimeStart) / canvasWidth;
}
exports.coordinateToTimeRatio = coordinateToTimeRatio;
function calculateDimensions(_a) {
var item = _a.item, order = _a.order, keys = _a.keys, canvasTimeStart = _a.canvasTimeStart, canvasTimeEnd = _a.canvasTimeEnd, canvasWidth = _a.canvasWidth, dragSnap = _a.dragSnap, lineHeight = _a.lineHeight, draggingItem = _a.draggingItem, dragTime = _a.dragTime, resizingItem = _a.resizingItem, resizingEdge = _a.resizingEdge, resizeTime = _a.resizeTime, newGroupOrder = _a.newGroupOrder, itemHeightRatio = _a.itemHeightRatio, fullUpdate = _a.fullUpdate, visibleTimeStart = _a.visibleTimeStart, visibleTimeEnd = _a.visibleTimeEnd;
var itemId = _get(item, keys.itemIdKey);
var itemTimeStart = _get(item, keys.itemTimeStartKey);
var itemTimeEnd = _get(item, keys.itemTimeEndKey);
var isDragging = itemId === draggingItem;
var isResizing = itemId === resizingItem;
var itemStart = (isResizing && resizingEdge === 'left' ? resizeTime : itemTimeStart);
var itemEnd = (isResizing && resizingEdge === 'right' ? resizeTime : itemTimeEnd);
var x = isDragging ? dragTime : itemStart;
var w = Math.max(itemEnd - itemStart, dragSnap);
var collisionX = itemStart;
var collisionW = w;
if (isDragging) {
if (itemTimeStart >= dragTime) {
collisionX = dragTime;
collisionW = Math.max(itemTimeEnd - dragTime, dragSnap);
}
else {
collisionW = Math.max(dragTime - itemTimeStart + w, dragSnap);
}
}
var clippedLeft = false;
var clippedRight = false;
if (fullUpdate) {
if (!isDragging && (visibleTimeStart > x + w || visibleTimeEnd < x)) {
return null;
}
if (visibleTimeStart > x) {
w -= (visibleTimeStart - x);
x = visibleTimeStart;
if (isDragging && w < 0) {
x += w;
w = 0;
}
clippedLeft = true;
}
if (x + w > visibleTimeEnd) {
w -= ((x + w) - visibleTimeEnd);
clippedRight = true;
}
}
var ratio = 1 / coordinateToTimeRatio(canvasTimeStart, canvasTimeEnd, canvasWidth);
var h = lineHeight * itemHeightRatio;
var dimensions = {
left: (x - canvasTimeStart) * ratio,
top: null,
width: Math.max(w * ratio, 3),
height: h,
order: isDragging ? newGroupOrder : order,
stack: true,
collisionLeft: collisionX,
originalLeft: itemTimeStart,
collisionWidth: collisionW,
lineHeight: lineHeight,
isDragging: isDragging,
clippedLeft: clippedLeft,
clippedRight: clippedRight
};
return dimensions;
}
exports.calculateDimensions = calculateDimensions;
function getGroupOrders(groups, keys) {
var groupIdKey = keys.groupIdKey;
var groupOrders = {};
for (var i = 0; i < groups.length; i++) {
groupOrders[_get(groups[i], groupIdKey)] = i;
}
return groupOrders;
}
exports.getGroupOrders = getGroupOrders;
function getVisibleItems(items, canvasTimeStart, canvasTimeEnd, keys) {
var itemTimeStartKey = keys.itemTimeStartKey, itemTimeEndKey = keys.itemTimeEndKey;
return items.filter(function (item) {
return _get(item, itemTimeStartKey) <= canvasTimeEnd && _get(item, itemTimeEndKey) >= canvasTimeStart;
});
}
exports.getVisibleItems = getVisibleItems;
function collision(a, b, lineHeight) {
var verticalMargin = 0;
return ((a.collisionLeft + EPSILON) < (b.collisionLeft + b.collisionWidth) &&
(a.collisionLeft + a.collisionWidth - EPSILON) > b.collisionLeft &&
(a.top - verticalMargin + EPSILON) < (b.top + b.height) &&
(a.top + a.height + verticalMargin - EPSILON) > b.top);
}
exports.collision = collision;
function stack(items, groupOrders, lineHeight, headerHeight, force) {
var i, iMax;
var totalHeight = headerHeight;
var groupHeights = {};
var groupTops = {};
var groupedItems = getGroupedItems(items, groupOrders);
if (force) {
for (i = 0, iMax = items.length; i < iMax; i++) {
items[i].dimensions.top = null;
}
}
groupedItems.forEach(function (group, index, array) {
groupTops[index] = totalHeight;
var groupHeight = 0;
var verticalMargin = 0;
for (i = 0, iMax = group.length; i < iMax; i++) {
var item = group[i];
verticalMargin = (item.dimensions.lineHeight - item.dimensions.height);
if (item.dimensions.stack && item.dimensions.top === null) {
item.dimensions.top = totalHeight + verticalMargin;
groupHeight = Math.max(groupHeight, item.dimensions.lineHeight);
do {
var collidingItem = null;
for (var j = 0, jj = group.length; j < jj; j++) {
var other = group[j];
if (other.top !== null && other !== item && other.dimensions.stack && collision(item.dimensions, other.dimensions, item.dimensions.lineHeight)) {
collidingItem = other;
break;
}
else {
}
}
if (collidingItem != null) {
item.dimensions.top = collidingItem.dimensions.top + collidingItem.dimensions.lineHeight;
groupHeight = Math.max(groupHeight, item.dimensions.top + item.dimensions.height - totalHeight);
}
} while (collidingItem);
}
}
groupHeights[index] = Math.max(groupHeight + verticalMargin, lineHeight);
totalHeight += Math.max(groupHeight + verticalMargin, lineHeight);
});
return {
height: totalHeight,
groupHeights: groupHeights,
groupTops: groupTops
};
}
exports.stack = stack;
function nostack(items, groupOrders, lineHeight, headerHeight, force) {
var i, iMax;
var totalHeight = headerHeight;
var groupHeights = {};
var groupTops = {};
var groupedItems = getGroupedItems(items, groupOrders);
if (force) {
for (i = 0, iMax = items.length; i < iMax; i++) {
items[i].dimensions.top = null;
}
}
groupedItems.forEach(function (group, index, array) {
groupTops[index] = totalHeight;
var groupHeight = 0;
for (i = 0, iMax = group.length; i < iMax; i++) {
var item = group[i];
var verticalMargin = (item.dimensions.lineHeight - item.dimensions.height) / 2;
if (item.dimensions.top === null) {
item.dimensions.top = totalHeight + verticalMargin;
groupHeight = Math.max(groupHeight, item.dimensions.lineHeight);
}
}
groupHeights[index] = Math.max(groupHeight, lineHeight);
totalHeight += Math.max(groupHeight, lineHeight);
});
return {
height: totalHeight,
groupHeights: groupHeights,
groupTops: groupTops
};
}
exports.nostack = nostack;
function keyBy(value, key) {
var obj = {};
value.forEach(function (element, index, array) {
obj[element[key]] = element;
});
return obj;
}
exports.keyBy = keyBy;
function getGroupedItems(items, groupOrders) {
var arr = [];
for (var i = 0; i < Object.keys(groupOrders).length; i++) {
arr[i] = [];
}
for (var i = 0; i < items.length; i++) {
arr[items[i].dimensions.order].push(items[i]);
}
return arr;
}
exports.getGroupedItems = getGroupedItems;
function hasSomeParentTheClass(element, classname) {
if (element.className && element.className.split(' ').indexOf(classname) >= 0)
return true;
return element.parentNode && hasSomeParentTheClass(element.parentNode, classname);
}
exports.hasSomeParentTheClass = hasSomeParentTheClass;
function createGradientPattern(lineHeight, color1, color2, borderColor) {
if (borderColor) {
if (!color2 || color1 === color2) {
return 'repeating-linear-gradient(to bottom, ' +
(color1 + ",") +
(color1 + " " + (lineHeight - 1) + "px,") +
(borderColor + " " + (lineHeight - 1) + "px,") +
(borderColor + " " + lineHeight + "px") +
')';
}
else {
return 'repeating-linear-gradient(to bottom, ' +
(color1 + ",") +
(color1 + " " + (lineHeight - 1) + "px,") +
(borderColor + " " + (lineHeight - 1) + "px,") +
(borderColor + " " + lineHeight + "px,") +
(color2 + " " + lineHeight + "px,") +
(color2 + " " + (lineHeight * 2 - 1) + "px,") +
(borderColor + " " + (lineHeight * 2 - 1) + "px,") +
(borderColor + " " + lineHeight * 2 + "px") +
')';
}
}
else {
if (!color2 || color1 === color2) {
return color1;
}
else {
return "repeating-linear-gradient(to bottom," + color1 + "," + color1 + " " + lineHeight + "px," + color2 + " " + lineHeight + "px," + color2 + " " + lineHeight * 2 + "px)";
}
}
}
exports.createGradientPattern = createGradientPattern;
function deepObjectCompare(obj1, obj2) {
for (var p in obj1) {
if (obj1.hasOwnProperty(p) !== obj2.hasOwnProperty(p))
return false;
switch (typeof (obj1[p])) {
case 'object':
if (!compare(obj1[p], obj2[p]))
return false;
break;
case 'function':
if (typeof (obj2[p]) === 'undefined' || (p !== 'compare' && obj1[p].toString() !== obj2[p].toString()))
return false;
break;
default:
if (obj1[p] !== obj2[p])
return false;
}
}
for (var r in obj2) {
if (typeof (obj1[r]) === 'undefined')
return false;
}
return true;
}
exports.deepObjectCompare = deepObjectCompare;
;
var compare = function (v1, v2) {
if (v1 === v2) {
return v1 !== 0 || 1 / v1 === 1 / v2;
}
else {
return v1 !== v1 && v2 !== v2;
}
};
//# sourceMappingURL=utils.js.map