esolution-nativescript-photoviewer
Version:
A simple Photo-viewer component for NativeScript based on NYTPhotoViewer pod for iOS and ImageGalleryViewer for Android.
225 lines • 9.63 kB
JavaScript
import { Color, Utils, ImageSource } from "@nativescript/core";
export * from './photoviewer.common';
var _dataSource;
const background_queue = dispatch_get_global_queue(21, 0);
const main_queue = dispatch_get_current_queue();
export class PhotoViewer {
constructor() {
this._finishedLoading = false;
this._didReload = false;
let photosArray = [];
_dataSource = NYTPhotoViewerArrayDataSource.alloc().initWithPhotos(photosArray);
}
get ios() {
return this.nativeView;
}
showGallery(imagesArray, options) {
if (!options)
options = {};
if (!options.ios)
options.ios = {};
if (!options.android)
options.android = {};
let photosArray = [];
let startIndex = options.startIndex || 0;
let iosCompletionCallback = options.ios.completionCallback || null;
imagesArray.forEach((imageItem, index) => {
let imageToAdd = NYTImage.alloc().init();
let fontFamily = options.ios.fontFamily || "HelveticaNeue";
let titleFontSize = options.ios.titleFontSize || 16;
let summaryFontSize = options.ios.summaryFontSize || 14;
let creditFontSize = options.ios.creditFontSize || 14;
let titleColor = options.ios.titleColor || new Color("white").ios;
let summaryColor = options.ios.summaryColor || new Color("lightgray").ios;
let creditColor = options.ios.creditColor || new Color("gray").ios;
if (isNYTPhotoItem(imageItem)) {
if (imageItem.imageURL)
dispatch_async(background_queue, () => {
imageToAdd.image = getUIImage(imageItem.imageURL);
if (index == imagesArray.length - 1) {
dispatch_async(main_queue, () => {
this._finishedLoading = true;
if (this.nativeView) {
this.nativeView.reloadPhotosAnimated(false);
this._didReload = true;
}
});
}
});
else
imageToAdd.image = imageItem.image;
imageToAdd.placeholderImage = imageItem.placeholderImage;
imageToAdd.attributedCaptionTitle = this.attributedString(imageItem.title, titleColor, fontFamily, titleFontSize);
imageToAdd.attributedCaptionSummary = this.attributedString(imageItem.summary, summaryColor, fontFamily, summaryFontSize);
imageToAdd.attributedCaptionCredit = this.attributedString(imageItem.credit, creditColor, fontFamily, creditFontSize);
}
else if (typeof imageItem === 'string') {
dispatch_async(background_queue, () => {
let img = getUIImage(imageItem);
imageToAdd.image = img;
if (index == imagesArray.length - 1) {
dispatch_async(main_queue, () => {
this._finishedLoading = true;
if (this.nativeView) {
this.nativeView.reloadPhotosAnimated(false);
this._didReload = true;
}
});
}
});
}
else {
console.log("ERROR: Passed object is not a image path/url or NYTPhotoItem object!", imageItem);
}
photosArray.push(imageToAdd);
});
_dataSource = NYTPhotoViewerArrayDataSource.alloc().initWithPhotos(photosArray);
this.nativeView = NYTPhotosViewController.alloc().initWithDataSourceInitialPhotoIndexDelegate(_dataSource, startIndex, null);
if (!this._didReload && this._finishedLoading) {
if (!NSThread.isMainThread) {
dispatch_async(main_queue, () => {
this.nativeView.reloadPhotosAnimated(false);
this._didReload = true;
});
}
else {
this.nativeView.reloadPhotosAnimated(false);
this._didReload = true;
}
}
if (options.ios.showShareButton == false) {
this.nativeView.rightBarButtonItem = null;
}
this.topViewController.presentViewControllerAnimatedCompletion(this.nativeView, true, iosCompletionCallback);
return new Promise((resolve) => {
this._delegate = PhotoViewerDelegateImpl.initWithResolve(resolve);
this.nativeView.delegate = this._delegate;
});
}
attributedString(text, color, fontFamily, fontSize) {
var attributeOptions = {
[NSForegroundColorAttributeName]: color,
[NSFontAttributeName]: UIFont.fontWithNameSize(fontFamily, fontSize)
};
return NSAttributedString.alloc().initWithStringAttributes(text || "", attributeOptions);
}
static get rootViewController() {
const keyWindow = UIApplication.sharedApplication.keyWindow;
return keyWindow != null ? keyWindow.rootViewController : undefined;
}
get topViewController() {
const root = PhotoViewer.rootViewController;
if (root == null) {
return undefined;
}
return this.findTopViewController(root);
}
findTopViewController(root) {
const presented = root.presentedViewController;
if (presented != null) {
return this.findTopViewController(presented);
}
if (root instanceof UISplitViewController) {
const last = root.viewControllers.lastObject;
if (last == null) {
return root;
}
return this.findTopViewController(last);
}
else if (root instanceof UINavigationController) {
const top = root.topViewController;
if (top == null) {
return root;
}
return this.findTopViewController(top);
}
else if (root instanceof UITabBarController) {
const selected = root.selectedViewController;
if (selected == null) {
return root;
}
return this.findTopViewController(selected);
}
else {
return root;
}
}
}
function getImageData(imageURL) {
let nsURL = NSURL.URLWithString(imageURL);
return NSData.dataWithContentsOfURL(nsURL);
}
function getUIImage(imageURL) {
if (Utils.isFileOrResourcePath(imageURL)) {
return ImageSource.fromFileOrResourceSync(imageURL).ios;
}
else {
let nsURL = NSURL.URLWithString(imageURL);
let imageData = NSData.dataWithContentsOfURL(nsURL);
return UIImage.imageWithData(imageData);
}
}
function isNYTPhotoItem(item) {
return typeof item.image === 'object' || typeof item.imageURL === 'string';
}
var NYTImage = /** @class */ (function (_super) {
__extends(NYTImage, _super);
function NYTImage() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(NYTImage.prototype, "image", {
get: function () { return this._image; },
set: function (value) { this._image = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(NYTImage.prototype, "imageData", {
get: function () { return this._imageData; },
set: function (value) { this._imageData = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(NYTImage.prototype, "placeholderImage", {
get: function () { return this._placeholderImage; },
set: function (value) { this._placeholderImage = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(NYTImage.prototype, "attributedCaptionTitle", {
get: function () { return this._attributedCaptionTitle; },
set: function (value) { this._attributedCaptionTitle = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(NYTImage.prototype, "attributedCaptionSummary", {
get: function () { return this._attributedCaptionSummary; },
set: function (value) { this._attributedCaptionSummary = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(NYTImage.prototype, "attributedCaptionCredit", {
get: function () { return this._attributedCaptionCredit; },
set: function (value) { this._attributedCaptionCredit = value; },
enumerable: true,
configurable: true
});
NYTImage.ObjCProtocols = [NYTPhoto];
return NYTImage;
}(NSObject));
var PhotoViewerDelegateImpl = /** @class */ (function (_super) {
__extends(PhotoViewerDelegateImpl, _super);
function PhotoViewerDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
PhotoViewerDelegateImpl.initWithResolve = function (resolve) {
var delegate = PhotoViewerDelegateImpl.new();
delegate._resolve = resolve;
return delegate;
};
PhotoViewerDelegateImpl.prototype.photosViewControllerDidDismiss = function (photosViewController) {
this._resolve();
};
PhotoViewerDelegateImpl.ObjCProtocols = [NYTPhotosViewControllerDelegate];
return PhotoViewerDelegateImpl;
}(NSObject));
//# sourceMappingURL=photoviewer.ios.js.map