nativescript-advanced-webview
Version:
An advanced webview using Chrome CustomTabs on Android and SFSafariViewController on iOS.
70 lines (69 loc) • 2.98 kB
JavaScript
import { Application, Color, Observable, Utils } from '@nativescript/core';
import { AdvancedWebviewEvents } from './interfaces';
export { AdvancedWebviewEvents } from './interfaces';
export const NSAdvancedWebViewEventEmitter = new Observable();
export function init() {
co.fitcom.fancywebview.AdvancedWebView.AdvancedWebViewStatics.init(Utils.android.getApplicationContext(), true);
}
export function openAdvancedUrl(options) {
if (!options.url) {
throw new Error('No url set in the Advanced WebView Options object.');
}
const activity = Application.android.startActivity || Application.android.foregroundActivity;
const i = new co.fitcom.fancywebview.AdvancedWebViewListener({
onCustomTabsServiceConnected(componentName, client) { },
onServiceDisconnected(componentName) { },
onNavigationEvent: function (navigationEvent, extras) {
switch (navigationEvent) {
case 1:
NSAdvancedWebViewEventEmitter.notify({
eventName: AdvancedWebviewEvents.LoadStarted
});
break;
case 2:
NSAdvancedWebViewEventEmitter.notify({
eventName: AdvancedWebviewEvents.LoadFinished
});
break;
case 3:
NSAdvancedWebViewEventEmitter.notify({
eventName: AdvancedWebviewEvents.LoadError
});
break;
case 4:
break;
case 5:
break;
case 6:
NSAdvancedWebViewEventEmitter.notify({
eventName: AdvancedWebviewEvents.Closed
});
break;
}
}
});
const wv = new co.fitcom.fancywebview.AdvancedWebView(activity, i);
const intentBuilder = wv.getBuilder();
if (options.toolbarColor) {
const defaultColors = new androidx.browser.customtabs.CustomTabColorSchemeParams.Builder()
.setToolbarColor(new Color(options.toolbarColor).android)
.build();
const darkParams = new androidx.browser.customtabs.CustomTabColorSchemeParams.Builder()
.setToolbarColor(new Color('#222').android)
.build();
intentBuilder
.setColorScheme(androidx.browser.customtabs.CustomTabsIntent
.COLOR_SCHEME_SYSTEM)
.setColorSchemeParams(androidx.browser.customtabs.CustomTabsIntent.COLOR_SCHEME_DARK, darkParams)
.setDefaultColorSchemeParams(defaultColors);
}
intentBuilder
.setShowTitle(options.showTitle ? options.showTitle : true)
.setDefaultShareMenuItemEnabled(true)
.setInstantAppsEnabled(true)
.setUrlBarHidingEnabled(true);
wv.setBuilder(intentBuilder);
wv.loadUrl(options.url);
}
export function close() {
}