nativescript-keyboard-toolbar
Version:
NativeScript Keyboard Toolbar plugin
143 lines (142 loc) • 7.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var application = require("tns-core-modules/application");
var platform_1 = require("tns-core-modules/platform");
var editable_text_base_1 = require("tns-core-modules/ui/editable-text-base");
var enums_1 = require("tns-core-modules/ui/enums");
var page_1 = require("tns-core-modules/ui/page");
var frame_1 = require("tns-core-modules/ui/frame");
var keyboard_toolbar_common_1 = require("./keyboard-toolbar.common");
var Toolbar = (function (_super) {
__extends(Toolbar, _super);
function Toolbar() {
return _super !== null && _super.apply(this, arguments) || this;
}
Toolbar.prototype._loaded = function () {
var _this = this;
this.keyboardNotificationObserver = application.ios.addNotificationObserver(UIKeyboardWillChangeFrameNotification, function (notification) {
var newKeyboardHeight = notification.userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey).CGRectValue.size.height;
if (newKeyboardHeight === _this.lastKeyboardHeight) {
return;
}
var isFirstAnimation = _this.lastKeyboardHeight === undefined;
_this.lastKeyboardHeight = newKeyboardHeight;
if (!isFirstAnimation && _this.hasFocus) {
var parent_1 = _this.content.parent;
parent_1.translateY = _this.startPositionY - newKeyboardHeight - (_this.lastHeight / platform_1.screen.mainScreen.scale);
}
});
var onViewForIdFound = function (forView) {
var parent = _this.content.parent;
var isText = forView instanceof editable_text_base_1.EditableTextBase;
var hasIQKeyboardManagerInstalled = typeof (IQKeyboardManager) !== "undefined";
var iqKeyboardManagerOriginalDistance = hasIQKeyboardManagerInstalled ? IQKeyboardManager.sharedManager().keyboardDistanceFromTextField : 0;
if (isText) {
forView.on("focus", function () {
if (hasIQKeyboardManagerInstalled) {
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = iqKeyboardManagerOriginalDistance + parent.height;
}
_this.hasFocus = true;
setTimeout(function () {
var animateToY = _this.startPositionY - _this.lastKeyboardHeight - (_this.showWhenKeyboardHidden === true ? 0 : (_this.lastHeight / platform_1.screen.mainScreen.scale));
_this.log("focus, animateToY: " + animateToY);
parent.animate({
translate: { x: 0, y: animateToY },
curve: enums_1.AnimationCurve.cubicBezier(.32, .49, .56, 1),
duration: 370
}).then(function () {
});
});
});
forView.on("blur", function () {
if (hasIQKeyboardManagerInstalled) {
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = iqKeyboardManagerOriginalDistance;
}
_this.hasFocus = false;
var animateToY = _this.showWhenKeyboardHidden === true && _this.showAtBottomWhenKeyboardHidden !== true ? 0 : _this.startPositionY;
_this.log("blur, animateToY: " + animateToY);
parent.animate({
translate: { x: 0, y: animateToY },
curve: enums_1.AnimationCurve.cubicBezier(.32, .49, .56, 1),
duration: 370
}).then(function () {
});
});
}
else {
forView.on("tap", function () {
var animateToY = _this.startPositionY - (_this.lastHeight / platform_1.screen.mainScreen.scale);
_this.log("tap, animateToY: " + animateToY);
parent.animate({
translate: { x: 0, y: animateToY },
curve: enums_1.AnimationCurve.cubicBezier(.32, .49, .56, 1),
duration: 370
}).then(function () {
});
});
}
};
this.getViewForId(10)
.then(function (view) { return onViewForIdFound(view); })
.catch(function () { return 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=\"myId\" height=\"44\">\n\n"); });
};
Toolbar.prototype.getViewForId = function (attemptsLeft) {
var _this = this;
return new Promise(function (resolve, reject) {
if (attemptsLeft-- > 0) {
setTimeout(function () {
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;
}
}
var page = pg;
var found = page && page.modal ? page.modal.getViewById(_this.forId) : page && page.getViewById(_this.forId);
if (found) {
resolve(found);
}
else {
_this.getViewForId(attemptsLeft).then(resolve).catch(reject);
}
}, 200);
}
else {
reject();
}
});
};
Toolbar.prototype._unloaded = function () {
application.ios.removeNotificationObserver(this.keyboardNotificationObserver, UIKeyboardWillChangeFrameNotification);
};
Toolbar.prototype._layout = function (left, top, right, bottom) {
var parent = this.content.parent;
var newHeight = parent.getMeasuredHeight();
if (newHeight === this.lastHeight) {
return;
}
var y = parent.getLocationOnScreen().y;
this.startPositionY = platform_1.screen.mainScreen.heightDIPs - y - ((this.showWhenKeyboardHidden === true ? newHeight : 0) / platform_1.screen.mainScreen.scale);
this.log("_layout, startPositionY: " + this.startPositionY);
if (this.lastHeight === undefined) {
if (this.showWhenKeyboardHidden === true) {
if (this.showAtBottomWhenKeyboardHidden === true) {
parent.translateY = this.startPositionY;
}
}
else {
parent.translateY = this.startPositionY;
}
}
else if (this.lastHeight !== newHeight) {
parent.translateY = this.startPositionY;
}
this.lastHeight = newHeight;
};
return Toolbar;
}(keyboard_toolbar_common_1.ToolbarBase));
exports.Toolbar = Toolbar;