@pilotlab/lux-tools
Version:
A luxurious user experience framework, developed by your friends at Pilot.
140 lines • 5.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const is_1 = require("@pilotlab/is");
const result_1 = require("@pilotlab/result");
const attributes_1 = require("@pilotlab/attributes");
class WebTools {
static loadJavascript(url) {
let result = new result_1.default();
let script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
if (is_1.default.notEmpty(script)) {
script.onload = () => { result.resolve(true); };
let firstScript = document.body.getElementsByTagName('script')[0];
if (is_1.default.empty(firstScript))
document.body.appendChild(script);
else
document.body.insertBefore(script, firstScript);
}
else
result.resolve(false);
return result;
}
static calculateContentSize(node) {
let realWidth, realHeight;
let tmpDiv;
let previousDisplay;
tmpDiv = document.createElement('div');
tmpDiv.style.visibility = 'hidden';
if (typeof node == 'object' && node != null) {
let parent = node.parent;
previousDisplay = node.style.display;
node.style.display = 'inline';
tmpDiv.appendChild(node);
tmpDiv.style.display = 'inline';
document.body.appendChild(tmpDiv);
realWidth = tmpDiv.offsetWidth;
realHeight = tmpDiv.offsetHeight;
node.style.display = previousDisplay;
document.body.removeChild(tmpDiv);
if (typeof parent == 'object' && parent != null) {
parent.appendChild(node);
}
}
else if (typeof node == 'string' && node != '') {
previousDisplay = tmpDiv.style.display;
tmpDiv.innerHTML = node;
tmpDiv.style.display = 'inline';
document.body.appendChild(tmpDiv);
realWidth = tmpDiv.offsetWidth;
realHeight = tmpDiv.offsetHeight;
tmpDiv.style.display = previousDisplay;
document.body.removeChild(tmpDiv);
}
else {
realWidth = 0;
realHeight = 0;
}
return { width: realWidth, height: realHeight };
}
static calculateHeight(strHtml, width) {
let realHeight;
let tmpDiv;
tmpDiv = document.createElement('div');
tmpDiv.style.visibility = 'hidden';
tmpDiv.style.position = 'absolute';
tmpDiv.style.display = 'block';
if (width !== undefined)
tmpDiv.style.width = width + 'px';
tmpDiv.innerHTML = strHtml;
document.body.appendChild(tmpDiv);
realHeight = tmpDiv.offsetHeight;
document.body.removeChild(tmpDiv);
return realHeight;
}
static calculateHtmlSize(strHtml, width) {
let realWidth;
let realHeight;
let tmpDiv;
tmpDiv = document.createElement('div');
tmpDiv.style.visibility = 'hidden';
tmpDiv.style.position = 'absolute';
tmpDiv.style.display = 'block';
if (width !== undefined)
tmpDiv.style.width = width + 'px';
tmpDiv.innerHTML = strHtml;
document.body.appendChild(tmpDiv);
realWidth = tmpDiv.offsetWidth;
realHeight = tmpDiv.offsetHeight;
document.body.removeChild(tmpDiv);
return new attributes_1.Size(realWidth, realHeight);
}
static autoEllipseText(text, width, height, className) {
let tmpDiv;
let returnText = text;
tmpDiv = document.createElement('span');
tmpDiv.style.maxWidth = 'none';
tmpDiv.style.maxHeight = 'none';
tmpDiv.style.visibility = 'hidden';
tmpDiv.style.position = 'absolute';
tmpDiv.style.display = 'block';
tmpDiv.style.width = width + 'px';
tmpDiv.className = className;
document.body.appendChild(tmpDiv);
let i = 1;
tmpDiv.innerHTML = text;
if (tmpDiv.offsetHeight > height) {
tmpDiv.innerHTML = '';
while (tmpDiv.offsetHeight < height && i < text.length) {
tmpDiv.innerHTML = text.substr(0, i) + '...';
i++;
}
returnText = tmpDiv.innerHTML;
}
document.body.removeChild(tmpDiv);
return returnText;
}
static imageToCanvas(image) {
if (is_1.default.empty(image))
return;
document.body.appendChild(image);
let imageWidth = image.offsetWidth;
let imageHeight = image.offsetHeight;
let canvas = document.createElement('canvas');
let context = canvas.getContext('2d');
canvas.width = imageWidth;
canvas.height = imageHeight;
context.drawImage(image, 0, 0);
document.body.removeChild(image);
return canvas;
}
static canvasToImage(canvas) {
let image = new Image();
image.src = canvas.toDataURL('image/png');
return image;
}
}
exports.WebTools = WebTools;
exports.default = WebTools;
//# sourceMappingURL=index.js.map