nativescript-keyboard-toolbar
Version:
NativeScript Keyboard Toolbar plugin
215 lines (214 loc) • 9.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var application_1 = require("tns-core-modules/application");
var platform_1 = require("tns-core-modules/platform");
var enums_1 = require("tns-core-modules/ui/enums");
var page_1 = require("tns-core-modules/ui/page");
var tab_view_1 = require("tns-core-modules/ui/tab-view");
var utils_1 = require("tns-core-modules/utils/utils");
var keyboard_toolbar_common_1 = require("./keyboard-toolbar.common");
var frame_1 = require("tns-core-modules/ui/frame");
var Toolbar = (function (_super) {
__extends(Toolbar, _super);
function Toolbar() {
var _this = _super.call(this) || this;
_this.verticalAlignment = "top";
return _this;
}
Toolbar.prototype._loaded = function () {
var _this = this;
setTimeout(function () { return _this.applyInitialPosition(); }, 300);
setTimeout(function () {
var prepFocusEvents = function (forView) {
forView.on("focus", function () {
_this.hasFocus = true;
if (that.lastKeyboardHeight) {
_this.showToolbar(_this.content.parent);
}
});
forView.on("blur", function () {
_this.hasFocus = false;
_this.hideToolbar(_this.content.parent);
});
};
var pg;
if (frame_1.topmost()) {
pg = frame_1.topmost().currentPage;
}
else {
pg = _this.content.parent;
while (pg && !(pg instanceof page_1.Page)) {
pg = pg.parent;
}
}
_this.thePage = pg;
var forView = _this.thePage.getViewById(_this.forId);
if (forView) {
prepFocusEvents(forView);
return;
}
var p = _this.thePage.parent;
while (p && !(p instanceof tab_view_1.TabView)) {
p = p.parent;
}
if (p instanceof tab_view_1.TabView) {
p.on(tab_view_1.TabView.selectedIndexChangedEvent, function () {
var forView2 = p.getViewById(_this.forId);
if (forView2) {
prepFocusEvents(forView2);
p.off(tab_view_1.TabView.selectedIndexChangedEvent);
}
});
}
else {
console.log("\n\u2328 \u2328 \u2328 Please make sure forId=\"<view id>\" resolves to a visible view, or the toolbar won't render correctly! Example: <Toolbar forId=\"" + (_this.forId || "myId") + "\" height=\"44\">\n\n");
}
}, 500);
var that = this;
this.onGlobalLayoutListener = new android.view.ViewTreeObserver.OnGlobalLayoutListener({
onGlobalLayout: function () {
if (!that.content.android) {
return;
}
var rect = new android.graphics.Rect();
that.content.android.getWindowVisibleDisplayFrame(rect);
var newKeyboardHeight = (Toolbar.getUsableScreenSizeY() - rect.bottom) / platform_1.screen.mainScreen.scale;
if (newKeyboardHeight <= 0 && that.lastKeyboardHeight === undefined) {
return;
}
if (newKeyboardHeight === that.lastKeyboardHeight) {
return;
}
that.lastKeyboardHeight = newKeyboardHeight;
if (that.hasFocus) {
if (newKeyboardHeight <= 0) {
that.hideToolbar(that.content.parent);
}
else {
that.showToolbar(that.content.parent);
}
}
}
});
that.content.android.getViewTreeObserver().addOnGlobalLayoutListener(that.onGlobalLayoutListener);
};
Toolbar.prototype._unloaded = function () {
this.content.android.getViewTreeObserver().removeOnGlobalLayoutListener(this.onGlobalLayoutListener);
this.onGlobalLayoutListener = undefined;
};
Toolbar.prototype.showToolbar = function (parent) {
var navbarHeight = this.isNavbarVisible ? 0 : this.navbarHeight;
if (!this.isNavbarVisible) {
var isNavbarVisibleWhenKeyboardOpen = this.thePage.getMeasuredHeight() < Toolbar.getUsableScreenSizeY() &&
(Toolbar.isVirtualNavbarHidden_butShowsWhenKeyboardIsOpen() || Toolbar.hasPermanentMenuKey());
if (isNavbarVisibleWhenKeyboardOpen) {
if (!this.navbarHeightWhenKeyboardOpen) {
this.navbarHeightWhenKeyboardOpen = Toolbar.getNavbarHeightWhenKeyboardOpen();
}
navbarHeight = this.navbarHeightWhenKeyboardOpen;
}
}
var animateToY = this.startPositionY - this.lastKeyboardHeight - (this.showWhenKeyboardHidden === true ? 0 : (this.lastHeight / platform_1.screen.mainScreen.scale)) - navbarHeight;
parent.animate({
translate: { x: 0, y: animateToY },
curve: enums_1.AnimationCurve.cubicBezier(.32, .49, .56, 1),
duration: 370
}).then(function () {
});
};
Toolbar.prototype.hideToolbar = function (parent) {
var animateToY = this.showWhenKeyboardHidden === true && this.showAtBottomWhenKeyboardHidden !== true ? 0 : (this.startPositionY + this.navbarHeight);
parent.animate({
translate: { x: 0, y: animateToY },
curve: enums_1.AnimationCurve.cubicBezier(.32, .49, .56, 1),
duration: 370
}).then(function () {
});
};
Toolbar.prototype.applyInitialPosition = function () {
if (this.startPositionY !== undefined) {
return;
}
var parent = this.content.parent;
this.thePage = parent;
while (!this.thePage && !this.thePage.frame) {
this.thePage = this.thePage.parent;
}
var loc = parent.getLocationOnScreen();
if (!loc) {
return;
}
var y = loc.y;
var newHeight = parent.getMeasuredHeight();
this.navbarHeight = Toolbar.getNavbarHeight();
this.isNavbarVisible = !!this.navbarHeight;
this.startPositionY = platform_1.screen.mainScreen.heightDIPs - y - ((this.showWhenKeyboardHidden === true ? newHeight : 0) / platform_1.screen.mainScreen.scale) - (this.isNavbarVisible ? this.navbarHeight : 0);
if (this.lastHeight === undefined) {
if (this.showWhenKeyboardHidden === true) {
if (this.showAtBottomWhenKeyboardHidden === true) {
parent.translateY = this.startPositionY;
}
}
else {
parent.translateY = this.startPositionY + this.navbarHeight;
}
}
else if (this.lastHeight !== newHeight) {
parent.translateY = this.startPositionY + this.navbarHeight;
}
this.lastHeight = newHeight;
};
Toolbar.getNavbarHeight = function () {
var context = utils_1.ad.getApplicationContext();
var navBarHeight = 0;
var windowManager = context.getSystemService(android.content.Context.WINDOW_SERVICE);
var d = windowManager.getDefaultDisplay();
var realDisplayMetrics = new android.util.DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);
var realHeight = realDisplayMetrics.heightPixels;
var realWidth = realDisplayMetrics.widthPixels;
var displayMetrics = new android.util.DisplayMetrics();
d.getMetrics(displayMetrics);
var displayHeight = displayMetrics.heightPixels;
var displayWidth = displayMetrics.widthPixels;
if ((realHeight - displayHeight) > 0) {
navBarHeight = realHeight - displayHeight;
}
else if ((realWidth - displayWidth) > 0) {
navBarHeight = realWidth - displayWidth;
}
return navBarHeight / context.getResources().getDisplayMetrics().density;
};
Toolbar.getNavbarHeightWhenKeyboardOpen = function () {
var resources = utils_1.ad.getApplicationContext().getResources();
var resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId) / platform_1.screen.mainScreen.scale;
}
return 0;
};
Toolbar.hasPermanentMenuKey = function () {
return android.view.ViewConfiguration.get(utils_1.ad.getApplicationContext()).hasPermanentMenuKey();
};
Toolbar.isVirtualNavbarHidden_butShowsWhenKeyboardIsOpen = function () {
if (Toolbar.supportVirtualKeyboardCheck !== undefined) {
return Toolbar.supportVirtualKeyboardCheck;
}
var SAMSUNG_NAVIGATION_EVENT = "navigationbar_hide_bar_enabled";
try {
Toolbar.supportVirtualKeyboardCheck = android.provider.Settings.Global.getInt(application_1.android.foregroundActivity.getContentResolver(), SAMSUNG_NAVIGATION_EVENT) === 1;
}
catch (e) {
console.log(">> e: " + e);
Toolbar.supportVirtualKeyboardCheck = false;
}
return Toolbar.supportVirtualKeyboardCheck;
};
Toolbar.getUsableScreenSizeY = function () {
var screenSize = new android.graphics.Point();
application_1.android.foregroundActivity.getWindowManager().getDefaultDisplay().getSize(screenSize);
return screenSize.y;
};
return Toolbar;
}(keyboard_toolbar_common_1.ToolbarBase));
exports.Toolbar = Toolbar;