266 lines
No EOL
9.4 KiB
JavaScript
266 lines
No EOL
9.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
var Enums = require("./enums");
|
|
var Shared = require("./shared");
|
|
function generateUniqueId() {
|
|
return "__ac-" + Shared.UUID.generate();
|
|
}
|
|
exports.generateUniqueId = generateUniqueId;
|
|
function isNullOrEmpty(value) {
|
|
return value === undefined || value === null || value === "";
|
|
}
|
|
exports.isNullOrEmpty = isNullOrEmpty;
|
|
function appendChild(node, child) {
|
|
if (child != null && child != undefined) {
|
|
node.appendChild(child);
|
|
}
|
|
}
|
|
exports.appendChild = appendChild;
|
|
function getStringValue(obj, defaultValue) {
|
|
if (defaultValue === void 0) { defaultValue = undefined; }
|
|
return typeof obj === "string" ? obj.toString() : defaultValue;
|
|
}
|
|
exports.getStringValue = getStringValue;
|
|
function getNumberValue(obj, defaultValue) {
|
|
if (defaultValue === void 0) { defaultValue = undefined; }
|
|
return typeof obj === "number" ? obj : defaultValue;
|
|
}
|
|
exports.getNumberValue = getNumberValue;
|
|
function getBoolValue(value, defaultValue) {
|
|
if (typeof value === "boolean") {
|
|
return value;
|
|
}
|
|
else if (typeof value === "string") {
|
|
switch (value.toLowerCase()) {
|
|
case "true":
|
|
return true;
|
|
case "false":
|
|
return false;
|
|
default:
|
|
return defaultValue;
|
|
}
|
|
}
|
|
return defaultValue;
|
|
}
|
|
exports.getBoolValue = getBoolValue;
|
|
function getEnumValue(targetEnum, name, defaultValue) {
|
|
if (isNullOrEmpty(name)) {
|
|
return defaultValue;
|
|
}
|
|
for (var key in targetEnum) {
|
|
var isValueProperty = parseInt(key, 10) >= 0;
|
|
if (isValueProperty) {
|
|
var value = targetEnum[key];
|
|
if (value && typeof value === "string") {
|
|
if (value.toLowerCase() === name.toLowerCase()) {
|
|
return parseInt(key, 10);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return defaultValue;
|
|
}
|
|
exports.getEnumValue = getEnumValue;
|
|
function setProperty(target, propertyName, propertyValue, defaultValue) {
|
|
if (defaultValue === void 0) { defaultValue = undefined; }
|
|
if (propertyValue === null || propertyValue === undefined || propertyValue === defaultValue) {
|
|
delete target[propertyName];
|
|
}
|
|
else {
|
|
target[propertyName] = propertyValue;
|
|
}
|
|
}
|
|
exports.setProperty = setProperty;
|
|
function setNumberProperty(target, propertyName, propertyValue, defaultValue) {
|
|
if (defaultValue === void 0) { defaultValue = undefined; }
|
|
if (propertyValue === null || propertyValue === undefined || propertyValue === defaultValue || isNaN(propertyValue)) {
|
|
delete target[propertyName];
|
|
}
|
|
else {
|
|
target[propertyName] = propertyValue;
|
|
}
|
|
}
|
|
exports.setNumberProperty = setNumberProperty;
|
|
function setEnumProperty(enumType, target, propertyName, propertyValue, defaultValue) {
|
|
if (defaultValue === void 0) { defaultValue = undefined; }
|
|
var targetValue = target[propertyName];
|
|
var canDeleteTarget = targetValue == undefined ? true : enumType[targetValue] !== undefined;
|
|
if (propertyValue == defaultValue) {
|
|
if (canDeleteTarget) {
|
|
delete target[propertyName];
|
|
}
|
|
}
|
|
else {
|
|
if (propertyValue == undefined) {
|
|
if (canDeleteTarget) {
|
|
delete target[propertyName];
|
|
}
|
|
}
|
|
else {
|
|
target[propertyName] = enumType[propertyValue];
|
|
}
|
|
}
|
|
}
|
|
exports.setEnumProperty = setEnumProperty;
|
|
function setArrayProperty(target, propertyName, propertyValue) {
|
|
var items = [];
|
|
if (propertyValue) {
|
|
for (var _i = 0, propertyValue_1 = propertyValue; _i < propertyValue_1.length; _i++) {
|
|
var item = propertyValue_1[_i];
|
|
items.push(item.toJSON());
|
|
}
|
|
}
|
|
if (items.length == 0) {
|
|
if (target.hasOwnProperty(propertyName) && Array.isArray(target[propertyName])) {
|
|
delete target[propertyName];
|
|
}
|
|
}
|
|
else {
|
|
setProperty(target, propertyName, items);
|
|
}
|
|
}
|
|
exports.setArrayProperty = setArrayProperty;
|
|
function parseHostConfigEnum(targetEnum, value, defaultValue) {
|
|
if (typeof value === "string") {
|
|
return getEnumValue(targetEnum, value, defaultValue);
|
|
}
|
|
else if (typeof value === "number") {
|
|
return value;
|
|
}
|
|
else {
|
|
return defaultValue;
|
|
}
|
|
}
|
|
exports.parseHostConfigEnum = parseHostConfigEnum;
|
|
function renderSeparation(hostConfig, separationDefinition, orientation) {
|
|
if (separationDefinition.spacing > 0 || separationDefinition.lineThickness > 0) {
|
|
var separator = document.createElement("div");
|
|
separator.className = hostConfig.makeCssClassName("ac-" + (orientation == Enums.Orientation.Horizontal ? "horizontal" : "vertical") + "-separator");
|
|
if (orientation == Enums.Orientation.Horizontal) {
|
|
if (separationDefinition.lineThickness) {
|
|
separator.style.paddingTop = (separationDefinition.spacing / 2) + "px";
|
|
separator.style.marginBottom = (separationDefinition.spacing / 2) + "px";
|
|
separator.style.borderBottom = separationDefinition.lineThickness + "px solid " + stringToCssColor(separationDefinition.lineColor);
|
|
}
|
|
else {
|
|
separator.style.height = separationDefinition.spacing + "px";
|
|
}
|
|
}
|
|
else {
|
|
if (separationDefinition.lineThickness) {
|
|
separator.style.paddingLeft = (separationDefinition.spacing / 2) + "px";
|
|
separator.style.marginRight = (separationDefinition.spacing / 2) + "px";
|
|
separator.style.borderRight = separationDefinition.lineThickness + "px solid " + stringToCssColor(separationDefinition.lineColor);
|
|
}
|
|
else {
|
|
separator.style.width = separationDefinition.spacing + "px";
|
|
}
|
|
}
|
|
separator.style.overflow = "hidden";
|
|
return separator;
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
exports.renderSeparation = renderSeparation;
|
|
function stringToCssColor(color) {
|
|
var regEx = /#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})?/gi;
|
|
var matches = regEx.exec(color);
|
|
if (matches && matches[4]) {
|
|
var a = parseInt(matches[1], 16) / 255;
|
|
var r = parseInt(matches[2], 16);
|
|
var g = parseInt(matches[3], 16);
|
|
var b = parseInt(matches[4], 16);
|
|
return "rgba(" + r + "," + g + "," + b + "," + a + ")";
|
|
}
|
|
else {
|
|
return color;
|
|
}
|
|
}
|
|
exports.stringToCssColor = stringToCssColor;
|
|
function truncate(element, maxHeight, lineHeight) {
|
|
var fits = function () {
|
|
// Allow a one pixel overflow to account for rounding differences
|
|
// between browsers
|
|
return maxHeight - element.scrollHeight >= -1.0;
|
|
};
|
|
if (fits())
|
|
return;
|
|
var fullText = element.innerHTML;
|
|
var truncateAt = function (idx) {
|
|
element.innerHTML = fullText.substring(0, idx) + '...';
|
|
};
|
|
var breakableIndices = findBreakableIndices(fullText);
|
|
var lo = 0;
|
|
var hi = breakableIndices.length;
|
|
var bestBreakIdx = 0;
|
|
// Do a binary search for the longest string that fits
|
|
while (lo < hi) {
|
|
var mid = Math.floor((lo + hi) / 2);
|
|
truncateAt(breakableIndices[mid]);
|
|
if (fits()) {
|
|
bestBreakIdx = breakableIndices[mid];
|
|
lo = mid + 1;
|
|
}
|
|
else {
|
|
hi = mid;
|
|
}
|
|
}
|
|
truncateAt(bestBreakIdx);
|
|
// If we have extra room, try to expand the string letter by letter
|
|
// (covers the case where we have to break in the middle of a long word)
|
|
if (lineHeight && maxHeight - element.scrollHeight >= lineHeight - 1.0) {
|
|
var idx = findNextCharacter(fullText, bestBreakIdx);
|
|
while (idx < fullText.length) {
|
|
truncateAt(idx);
|
|
if (fits()) {
|
|
bestBreakIdx = idx;
|
|
idx = findNextCharacter(fullText, idx);
|
|
}
|
|
else {
|
|
break;
|
|
}
|
|
}
|
|
truncateAt(bestBreakIdx);
|
|
}
|
|
}
|
|
exports.truncate = truncate;
|
|
function findBreakableIndices(html) {
|
|
var results = [];
|
|
var idx = findNextCharacter(html, -1);
|
|
while (idx < html.length) {
|
|
if (html[idx] == ' ') {
|
|
results.push(idx);
|
|
}
|
|
idx = findNextCharacter(html, idx);
|
|
}
|
|
return results;
|
|
}
|
|
function findNextCharacter(html, currIdx) {
|
|
currIdx += 1;
|
|
// If we found the start of an HTML tag, keep advancing until we get
|
|
// past it, so we don't end up truncating in the middle of the tag
|
|
while (currIdx < html.length && html[currIdx] == '<') {
|
|
while (currIdx < html.length && html[currIdx++] != '>')
|
|
;
|
|
}
|
|
return currIdx;
|
|
}
|
|
function getFitStatus(element, containerEnd) {
|
|
var start = element.offsetTop;
|
|
var end = start + element.clientHeight;
|
|
if (end <= containerEnd) {
|
|
return Enums.ContainerFitStatus.FullyInContainer;
|
|
}
|
|
else if (start < containerEnd) {
|
|
return Enums.ContainerFitStatus.Overflowing;
|
|
}
|
|
else {
|
|
return Enums.ContainerFitStatus.FullyOutOfContainer;
|
|
}
|
|
}
|
|
exports.getFitStatus = getFitStatus;
|
|
//# sourceMappingURL=utils.js.map
|