uicore-ts
Version:
UICore is a library to build native-like user interfaces using pure Typescript. No HTML is needed at all. Components are described as TS classes and all user interactions are handled explicitly. This library is strongly inspired by the UIKit framework tha
167 lines (166 loc) • 6.1 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var UIImageView_exports = {};
__export(UIImageView_exports, {
UIImageView: () => UIImageView
});
module.exports = __toCommonJS(UIImageView_exports);
var import_UICore = require("./UICore");
var import_UIObject = require("./UIObject");
var import_UIRectangle = require("./UIRectangle");
var import_UIView = require("./UIView");
const _UIImageView = class extends import_UIView.UIView {
constructor(elementID, viewHTMLElement = null) {
super(elementID, viewHTMLElement, "img");
this._hiddenWhenEmpty = import_UIObject.NO;
}
get viewHTMLElement() {
return super.viewHTMLElement;
}
static objectURLFromDataURL(dataURL) {
const blob = dataURLtoBlob(dataURL);
return URL.createObjectURL(blob);
}
static dataURL(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open("get", url);
xhr.responseType = "blob";
xhr.onload = function() {
const fr = new FileReader();
fr.onload = function() {
callback(this.result);
};
fr.readAsDataURL(xhr.response);
};
xhr.send();
}
static dataURLWithMaxSize(URLString, maxSize, completion) {
const imageView = new _UIImageView();
imageView.imageSource = URLString;
imageView.viewHTMLElement.onload = () => {
const originalSize = imageView.intrinsicContentSize();
let multiplier = maxSize / Math.max(originalSize.height, originalSize.width);
multiplier = Math.min(1, multiplier);
const result = imageView.getDataURL((originalSize.height * multiplier).integerValue, (originalSize.width * multiplier).integerValue);
completion(result);
};
}
static dataURLWithSizes(URLString, height, width, completion) {
const imageView = new _UIImageView();
imageView.imageSource = URLString;
imageView.viewHTMLElement.onload = () => {
const result = imageView.getDataURL(height, width);
completion(result);
};
}
getDataURL(height, width) {
const img = this.viewHTMLElement;
const canvas = document.createElement("canvas");
canvas.width = width != null ? width : img.naturalWidth;
canvas.height = height != null ? height : img.naturalHeight;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width != null ? width : img.naturalWidth, height != null ? height : img.naturalHeight);
return canvas.toDataURL("image/png");
}
get imageSource() {
return this.viewHTMLElement.src;
}
set imageSource(sourceString) {
if ((0, import_UIObject.IS_NOT)(sourceString)) {
sourceString = "";
}
this.viewHTMLElement.src = sourceString;
if (this.hiddenWhenEmpty) {
this.hidden = (0, import_UIObject.IS_NOT)(this.imageSource);
}
if (!sourceString || !sourceString.length) {
this.hidden = import_UIObject.YES;
return;
} else {
this.hidden = import_UIObject.NO;
}
this.viewHTMLElement.onload = () => {
var _a;
return (_a = this.superview) == null ? void 0 : _a.setNeedsLayout();
};
}
setImageSource(key, defaultString) {
const languageName = import_UICore.UICore.languageService.currentLanguageKey;
this.imageSource = import_UICore.UICore.languageService.stringForKey(key, languageName, defaultString, void 0);
}
didReceiveBroadcastEvent(event) {
super.didReceiveBroadcastEvent(event);
if (event.name == import_UIView.UIView.broadcastEventName.LanguageChanged || event.name == import_UIView.UIView.broadcastEventName.AddedToViewTree) {
this._setImageSourceFromKeyIfPossible();
}
}
willMoveToSuperview(superview) {
super.willMoveToSuperview(superview);
this._setImageSourceFromKeyIfPossible();
}
_setImageSourceFromKeyIfPossible() {
if (this._sourceKey && this._defaultSource) {
this.setImageSource(this._sourceKey, this._defaultSource);
}
}
get fillMode() {
return this._fillMode;
}
set fillMode(fillMode) {
this._fillMode = fillMode;
this.style.objectFit = fillMode;
}
get hiddenWhenEmpty() {
return this._hiddenWhenEmpty;
}
set hiddenWhenEmpty(hiddenWhenEmpty) {
this._hiddenWhenEmpty = hiddenWhenEmpty;
if (hiddenWhenEmpty) {
this.hidden = (0, import_UIObject.IS_NOT)(this.imageSource);
}
}
didMoveToSuperview(superview) {
super.didMoveToSuperview(superview);
}
layoutSubviews() {
super.layoutSubviews();
}
intrinsicContentSize() {
return new import_UIRectangle.UIRectangle(0, 0, this.viewHTMLElement.naturalHeight, this.viewHTMLElement.naturalWidth);
}
intrinsicContentSizeWithConstraints(constrainingHeight = 0, constrainingWidth = 0) {
const heightRatio = constrainingHeight / this.viewHTMLElement.naturalHeight;
const widthRatio = constrainingWidth / this.viewHTMLElement.naturalWidth;
const multiplier = Math.max(heightRatio, widthRatio);
return new import_UIRectangle.UIRectangle(0, 0, this.viewHTMLElement.naturalHeight * multiplier, this.viewHTMLElement.naturalWidth * multiplier);
}
};
let UIImageView = _UIImageView;
UIImageView.fillMode = {
"stretchToFill": "fill",
"aspectFit": "contain",
"aspectFill": "cover",
"center": "none",
"aspectFitIfLarger": "scale-down"
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
UIImageView
});
//# sourceMappingURL=UIImageView.js.map