@drozdik.m/image-gallery
Version:
Efficient native modal image gallery.
87 lines (86 loc) • 3.2 kB
JavaScript
exports.__esModule = true;
exports.GalleryImage = void 0;
var GalleryImageLoadArgs_1 = require("../args/GalleryImageLoadArgs");
var event_1 = require("@drozdik.m/event");
var GalleryImage = /** @class */ (function () {
function GalleryImage(imageSource, imageTitle, imageAlt) {
if (imageAlt === void 0) { imageAlt = imageTitle; }
this.isLoaded = false;
this.imageElement = null;
this.OnLoad = new event_1.Event();
this.imageSrouce = imageSource;
this.imageTitle = imageTitle;
}
/**
* Preloads and prepares the image
* */
GalleryImage.prototype.Preload = function () {
if (this.imageElement == null)
this.imageElement = this.MakeResultImageElement();
};
/**
* Tells if this image is already loaded
* */
GalleryImage.prototype.IsLoaded = function () {
return this.isLoaded;
};
/**
* Simply creates the image element
* */
GalleryImage.prototype.MakeResultImageElement = function () {
var object = this;
var res = document.createElement("img");
res.onload = function () {
object.isLoaded = true;
object.OnLoad.Invoke(object, new GalleryImageLoadArgs_1.GalleryImageLoadArgs());
};
res.src = this.imageSrouce;
res.title = this.imageTitle;
res.classList.add("currentGalleryImage");
return res;
};
/**
* Returns the result element that holds the image
* */
GalleryImage.prototype.GetResultElement = function () {
if (this.imageElement == null)
this.imageElement = this.MakeResultImageElement();
var wrapper = document.createElement("div");
wrapper.classList.add("currentGalleryImageWrapper");
wrapper.appendChild(this.imageElement);
var title = document.createElement("span");
title.classList.add("currentGalleryImageTitle");
title.innerHTML = this.imageTitle;
wrapper.appendChild(title);
return wrapper;
};
/**
* Returns only the image element
* */
GalleryImage.prototype.GetImageElement = function () {
if (this.imageElement == null)
this.imageElement = this.MakeResultImageElement();
return this.imageElement;
};
GalleryImage.prototype.GetImageNaturalWidth = function () {
if (this.imageElement != null)
return this.imageElement.naturalWidth;
return -1;
};
GalleryImage.prototype.GetImageNaturalHeight = function () {
if (this.imageElement != null)
return this.imageElement.naturalHeight;
return -1;
};
/**
* Creates and returns a gallery image object based on a <a> elements attributes
* @param linkElement
*/
GalleryImage.FromLinkElement = function (linkElement) {
var imageSource = linkElement.getAttribute("href") || "";
var imageTitle = linkElement.getAttribute("title") || "";
return new GalleryImage(imageSource, imageTitle);
};
return GalleryImage;
}());
exports.GalleryImage = GalleryImage;