From 5ee1e8e1a3a395ba33553c50b02e7635bf5164a5 Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Mon, 24 Apr 2017 02:37:08 +0300 Subject: [PATCH 01/72] feat(menu-types): move functionality from constructor --- src/components/app/menu-interface.ts | 6 +- src/components/menu/menu-types.ts | 88 ++++++++++++++++++---------- 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/src/components/app/menu-interface.ts b/src/components/app/menu-interface.ts index 45384961fe4..a78cdb50137 100644 --- a/src/components/app/menu-interface.ts +++ b/src/components/app/menu-interface.ts @@ -20,14 +20,16 @@ export interface Menu { getBackdropElement(): HTMLElement; _canOpen(): boolean; persistent: boolean; -}; +} export interface MenuType { ani: Animation; isOpening: boolean; + menu: Menu; setOpen(shouldOpen: boolean, animated: boolean, done: Function): void; setProgressStart(isOpen: boolean): void; setProgessStep(stepValue: number): void; setProgressEnd(shouldComplete: boolean, currentStepValue: number, velocity: number, done: Function): void; + updatePosition(): void; destroy(): void; -}; +} diff --git a/src/components/menu/menu-types.ts b/src/components/menu/menu-types.ts index 5543d3c3b20..8b6b9824dfc 100644 --- a/src/components/menu/menu-types.ts +++ b/src/components/menu/menu-types.ts @@ -16,7 +16,7 @@ export class MenuType implements IMenuType { ani: Animation; isOpening: boolean; - constructor(plt: Platform) { + constructor(public menu: Menu, plt: Platform) { this.ani = new Animation(plt); this.ani .easing('cubic-bezier(0.0, 0.0, 0.2, 1)') @@ -67,6 +67,9 @@ export class MenuType implements IMenuType { ani.progressEnd(shouldComplete, currentStepValue, dur); } + updatePosition() { + } + destroy() { this.ani.destroy(); this.ani = null; @@ -82,13 +85,20 @@ export class MenuType implements IMenuType { * The menu itself, which is under the content, does not move. */ class MenuRevealType extends MenuType { + private contentOpen: Animation; + constructor(menu: Menu, plt: Platform) { - super(plt); + super(menu, plt); + + this.contentOpen = new Animation(plt, menu.getContentElement()); + this.ani.add(this.contentOpen); - const openedX = (menu.width() * (menu.isRightSide ? -1 : 1)) + 'px'; - const contentOpen = new Animation(plt, menu.getContentElement()); - contentOpen.fromTo('translateX', '0px', openedX); - this.ani.add(contentOpen); + this.updatePosition(); + } + + updatePosition() { + let openedX = (this.menu.width() * (this.menu.isRightSide ? -1 : 1)) + 'px'; + this.contentOpen.fromTo('translateX', '0px', openedX); } } MenuController.registerType('reveal', MenuRevealType); @@ -101,30 +111,38 @@ MenuController.registerType('reveal', MenuRevealType); * The menu itself also slides over to reveal its bad self. */ class MenuPushType extends MenuType { + private menuAni: Animation; + private contentApi: Animation; + constructor(menu: Menu, plt: Platform) { - super(plt); + super(menu, plt); + this.menuAni = new Animation(plt, menu.getMenuElement()); + this.ani.add(this.menuAni); + + this.contentApi = new Animation(plt, menu.getContentElement()); + this.ani.add(this.contentApi); + + this.updatePosition(); + } + + updatePosition() { let contentOpenedX: string, menuClosedX: string, menuOpenedX: string; - const width = menu.width(); - if (menu.isRightSide) { + + if (this.menu.isRightSide) { // right side - contentOpenedX = -width + 'px'; - menuClosedX = width + 'px'; + contentOpenedX = -this.menu.width() + 'px'; + menuClosedX = this.menu.width() + 'px'; menuOpenedX = '0px'; } else { - contentOpenedX = width + 'px'; + contentOpenedX = this.menu.width() + 'px'; menuOpenedX = '0px'; - menuClosedX = -width + 'px'; + menuClosedX = -this.menu.width() + 'px'; } - const menuAni = new Animation(plt, menu.getMenuElement()); - menuAni.fromTo('translateX', menuClosedX, menuOpenedX); - this.ani.add(menuAni); - - const contentApi = new Animation(plt, menu.getContentElement()); - contentApi.fromTo('translateX', '0px', contentOpenedX); - this.ani.add(contentApi); + this.menuAni.fromTo('translateX', menuClosedX, menuOpenedX); + this.contentApi.fromTo('translateX', '0px', contentOpenedX); } } MenuController.registerType('push', MenuPushType); @@ -137,29 +155,35 @@ MenuController.registerType('push', MenuPushType); * itself, which is under the menu, does not move. */ class MenuOverlayType extends MenuType { + private menuAni: Animation; + constructor(menu: Menu, plt: Platform) { - super(plt); + super(menu, plt); + + const backdropApi = new Animation(plt, menu.getBackdropElement()); + backdropApi.fromTo('opacity', 0.01, 0.35); + this.ani.add(backdropApi); + + this.menuAni = new Animation(plt, menu.getMenuElement()); + this.ani.add(this.menuAni); + this.updatePosition(); + } + + updatePosition() { let closedX: string, openedX: string; - const width = menu.width(); - if (menu.isRightSide) { + if (this.menu.isRightSide) { // right side - closedX = 8 + width + 'px'; + closedX = 8 + this.menu.width() + 'px'; openedX = '0px'; } else { // left side - closedX = -(8 + width) + 'px'; + closedX = -(8 + this.menu.width()) + 'px'; openedX = '0px'; } - const menuAni = new Animation(plt, menu.getMenuElement()); - menuAni.fromTo('translateX', closedX, openedX); - this.ani.add(menuAni); - - const backdropApi = new Animation(plt, menu.getBackdropElement()); - backdropApi.fromTo('opacity', 0.01, 0.35); - this.ani.add(backdropApi); + this.menuAni.fromTo('translateX', closedX, openedX); } } MenuController.registerType('overlay', MenuOverlayType); From 635832c51ec2ef2d8fce3e468404f0b001393c82 Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Mon, 24 Apr 2017 02:41:06 +0300 Subject: [PATCH 02/72] fix(menu): correct side storing and isRightSide --- src/components/menu/menu.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/components/menu/menu.ts b/src/components/menu/menu.ts index da838dfbd0f..96a7f770d7e 100644 --- a/src/components/menu/menu.ts +++ b/src/components/menu/menu.ts @@ -213,11 +213,6 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { */ isOpen: boolean = false; - /** - * @hidden - */ - isRightSide: boolean = false; - /** * @hidden */ @@ -268,16 +263,19 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { */ @Input() get side(): Side { - return this._side; + if (this.isRightSide) { + return 'right'; + } + + return 'left'; } set side(val: Side) { - this.isRightSide = isRightSide(val, this._plt.isRTL); - if (this.isRightSide) { - this._side = 'right'; - } else { - this._side = 'left'; - } + this._side = val; + } + + get isRightSide(): boolean { + return isRightSide(this._side, this._plt.isRTL); } /** From e4573a0f7bf654f7a59d3b085fab7eb90297af66 Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Mon, 24 Apr 2017 02:50:35 +0300 Subject: [PATCH 03/72] fix(menu): update type when direction changes --- src/components/menu/menu.ts | 15 +++++++++++++++ src/platform/platform.ts | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/src/components/menu/menu.ts b/src/components/menu/menu.ts index 96a7f770d7e..09639a15ffb 100644 --- a/src/components/menu/menu.ts +++ b/src/components/menu/menu.ts @@ -207,6 +207,7 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { private _gestureBlocker: BlockerDelegate; private _isPane: boolean = false; private _side: Side; + private _dirChanged: EventEmitter; /** * @hidden @@ -272,6 +273,8 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { set side(val: Side) { this._side = val; + + this._updateType(); } get isRightSide(): boolean { @@ -340,6 +343,8 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { disable: [GESTURE_GO_BACK_SWIPE] }); this.side = 'start'; + + this._dirChanged = Platform.dirChanged.subscribe(this._updateType.bind(this)); } /** @@ -408,6 +413,15 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { return this._type; } + /** + * @hidden + */ + private _updateType(): void { + // Update type's position if type exists + if (this._type) + this._getType().updatePosition(); + } + /** * @hidden */ @@ -727,6 +741,7 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { this._events.destroy(); this._gesture && this._gesture.destroy(); this._type && this._type.destroy(); + this._dirChanged.unsubscribe(); this._gesture = null; this._type = null; diff --git a/src/platform/platform.ts b/src/platform/platform.ts index c8c8977cd8e..aa26169da5b 100644 --- a/src/platform/platform.ts +++ b/src/platform/platform.ts @@ -51,6 +51,8 @@ export class Platform { private _isPortrait: boolean = null; private _uiEvtOpts = false; + static dirChanged: EventEmitter = new EventEmitter(); + /** @hidden */ zone: NgZone; @@ -323,6 +325,8 @@ export class Platform { if (updateDocument !== false) { this._doc['documentElement'].setAttribute('dir', dir); } + + Platform.dirChanged.emit(this._dir); } /** From d7bec876204c5749cf62c23edbc884e88d7ddbb9 Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Sat, 20 May 2017 15:44:50 +0300 Subject: [PATCH 04/72] fix(lint): unused import --- src/components/virtual-scroll/virtual-scroll.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/virtual-scroll/virtual-scroll.ts b/src/components/virtual-scroll/virtual-scroll.ts index 39fb389db89..88bf65ba80c 100644 --- a/src/components/virtual-scroll/virtual-scroll.ts +++ b/src/components/virtual-scroll/virtual-scroll.ts @@ -4,7 +4,7 @@ import { adjustRendered, calcDimensions, estimateHeight, initReadNodes, processR import { Config } from '../../config/config'; import { Content, ScrollEvent } from '../content/content'; import { DomController } from '../../platform/dom-controller'; -import { isBlank, isFunction, isPresent, assert } from '../../util/util'; +import { isFunction, isPresent, assert } from '../../util/util'; import { Platform } from '../../platform/platform'; import { ViewController } from '../../navigation/view-controller'; import { VirtualCell, VirtualData, VirtualNode } from './virtual-util'; From 813e7eefee04a83628152c8a4de12bc6991338ee Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Fri, 26 May 2017 17:15:31 +0300 Subject: [PATCH 05/72] fix(menu): right should be always on the right --- src/components/menu/menu.scss | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/components/menu/menu.scss b/src/components/menu/menu.scss index e6448e2af3f..b1ea884cede 100644 --- a/src/components/menu/menu.scss +++ b/src/components/menu/menu.scss @@ -47,6 +47,12 @@ ion-menu.show-menu { } ion-menu[side=right] > .menu-inner { + // scss-lint:disable PropertySpelling + right: 0; + left: auto; +} + +ion-menu[side=end] > .menu-inner { @include position-horizontal(auto, 0); } From 1ab886a5bfad59b1aade82981a046d3b8dddec5a Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Fri, 26 May 2017 17:16:15 +0300 Subject: [PATCH 06/72] fix(menu): abstract the MenuType class --- src/components/menu/menu-types.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/components/menu/menu-types.ts b/src/components/menu/menu-types.ts index 8b6b9824dfc..7d58c3c126f 100644 --- a/src/components/menu/menu-types.ts +++ b/src/components/menu/menu-types.ts @@ -11,7 +11,7 @@ import { Platform } from '../../platform/platform'; * type will provide their own animations for open and close * and registers itself with Menu. */ -export class MenuType implements IMenuType { +export abstract class MenuType implements IMenuType { ani: Animation; isOpening: boolean; @@ -67,8 +67,7 @@ export class MenuType implements IMenuType { ani.progressEnd(shouldComplete, currentStepValue, dur); } - updatePosition() { - } + abstract updatePosition(): void; destroy() { this.ani.destroy(); @@ -128,17 +127,17 @@ class MenuPushType extends MenuType { updatePosition() { let contentOpenedX: string, menuClosedX: string, menuOpenedX: string; + const width = this.menu.width(); if (this.menu.isRightSide) { // right side - contentOpenedX = -this.menu.width() + 'px'; - menuClosedX = this.menu.width() + 'px'; + contentOpenedX = -width + 'px'; + menuClosedX = width + 'px'; menuOpenedX = '0px'; - } else { - contentOpenedX = this.menu.width() + 'px'; + contentOpenedX = width + 'px'; menuOpenedX = '0px'; - menuClosedX = -this.menu.width() + 'px'; + menuClosedX = -width + 'px'; } this.menuAni.fromTo('translateX', menuClosedX, menuOpenedX); @@ -172,14 +171,16 @@ class MenuOverlayType extends MenuType { updatePosition() { let closedX: string, openedX: string; + const width = this.menu.width(); + if (this.menu.isRightSide) { // right side - closedX = 8 + this.menu.width() + 'px'; + closedX = 8 + width + 'px'; openedX = '0px'; } else { // left side - closedX = -(8 + this.menu.width()) + 'px'; + closedX = -(8 + width) + 'px'; openedX = '0px'; } From 4be9fa1305577c8cbbef74dc566924105169853b Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Fri, 26 May 2017 17:16:46 +0300 Subject: [PATCH 07/72] feat(e2e-menu): add option to change dir on the fly --- .../menu/test/basic/pages/page-two/page-two.html | 3 +-- src/components/menu/test/basic/pages/page-two/page-two.ts | 8 +++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/components/menu/test/basic/pages/page-two/page-two.html b/src/components/menu/test/basic/pages/page-two/page-two.html index a3b5f273c49..34d46c181d9 100644 --- a/src/components/menu/test/basic/pages/page-two/page-two.html +++ b/src/components/menu/test/basic/pages/page-two/page-two.html @@ -11,8 +11,7 @@ - diff --git a/src/components/menu/test/basic/pages/page-two/page-two.ts b/src/components/menu/test/basic/pages/page-two/page-two.ts index 207a6461eec..e22c9d7d9dc 100644 --- a/src/components/menu/test/basic/pages/page-two/page-two.ts +++ b/src/components/menu/test/basic/pages/page-two/page-two.ts @@ -1,5 +1,6 @@ import { Component } from '@angular/core'; import { AlertController, IonicPage, ModalController, NavController } from '../../../../../..'; +import { Platform } from '../../../../../../platform/platform'; @IonicPage() @Component({ @@ -9,10 +10,15 @@ export class PageTwo { constructor( public navCtrl: NavController, public alertCtrl: AlertController, - public modalCtrl: ModalController) { } + public modalCtrl: ModalController, + public platform: Platform) { } myMenu: string = 'right'; + toggleDir() { + this.platform.setDir(this.platform.isRTL ? 'ltr' : 'rtl', true); + } + presentAlert() { let alert = this.alertCtrl.create({ title: 'New Friend!', From 1cda9662e1e8b391cdaf6207c88510935037bb34 Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Tue, 6 Jun 2017 22:22:31 +0300 Subject: [PATCH 08/72] fix(test): change test to start and end --- src/components/app/menu-controller.ts | 11 +++- src/components/menu/test/basic/e2e.ts | 16 ++--- .../test/basic/pages/page-four/page-four.html | 6 +- .../test/basic/pages/page-one/page-one.html | 66 +++++++++---------- .../test/basic/pages/page-one/page-one.ts | 8 +-- .../basic/pages/page-three/page-three.html | 2 +- .../test/basic/pages/page-two/page-two.html | 13 ++-- .../test/basic/pages/page-two/page-two.ts | 2 +- src/components/menu/test/menu.spec.ts | 2 +- 9 files changed, 67 insertions(+), 59 deletions(-) diff --git a/src/components/app/menu-controller.ts b/src/components/app/menu-controller.ts index 275c8da1a55..3b994b731c1 100644 --- a/src/components/app/menu-controller.ts +++ b/src/components/app/menu-controller.ts @@ -1,6 +1,7 @@ import { Menu, MenuType } from './menu-interface'; import { Platform } from '../../platform/platform'; -import { removeArrayItem, assert } from '../../util/util'; +import { removeArrayItem, assert, isRightSide } from '../../util/util'; +import { Injectable } from '@angular/core'; /** @@ -115,9 +116,13 @@ import { removeArrayItem, assert } from '../../util/util'; * @see {@link ../Menu Menu API Docs} * */ +@Injectable() export class MenuController { private _menus: Array = []; + constructor(private plt: Platform) { + } + /** * Programatically open the Menu. * @param {string} [menuId] Optionally get the menu by its id, or side. @@ -244,6 +249,10 @@ export class MenuController { get(menuId?: string): Menu { var menu: Menu; + if (menuId === 'start' || menuId === 'end') { + menuId = isRightSide(menuId, this.plt.isRTL) ? 'right' : 'left'; + } + if (menuId === 'left' || menuId === 'right') { // there could be more than one menu on the same side // so first try to get the enabled one diff --git a/src/components/menu/test/basic/e2e.ts b/src/components/menu/test/basic/e2e.ts index 827fa1cb657..4848830fac6 100644 --- a/src/components/menu/test/basic/e2e.ts +++ b/src/components/menu/test/basic/e2e.ts @@ -1,19 +1,19 @@ import { by, element } from 'protractor'; -it('should toggle left menu', function() { - element(by.css('.e2eContentToggleLeftMenu')).click(); +it('should toggle start menu', function() { + element(by.css('.e2eContentToggleStartMenu')).click(); }); -it('should close left menu', function() { - element(by.css('.e2eCloseLeftMenu')).click(); +it('should close start menu', function() { + element(by.css('.e2eCloseStartMenu')).click(); }); -it('should toggle right menu', function() { - element(by.css('.e2eContentToggleRightMenu')).click(); +it('should toggle end menu', function() { + element(by.css('.e2eContentToggleEndMenu')).click(); }); -it('should close right menu', function() { - element(by.css('.e2eCloseRightMenu')).click(); +it('should close end menu', function() { + element(by.css('.e2eCloseEndMenu')).click(); }); diff --git a/src/components/menu/test/basic/pages/page-four/page-four.html b/src/components/menu/test/basic/pages/page-four/page-four.html index 513b043e824..45a27327f42 100644 --- a/src/components/menu/test/basic/pages/page-four/page-four.html +++ b/src/components/menu/test/basic/pages/page-four/page-four.html @@ -1,13 +1,13 @@ - Menu - @@ -20,7 +20,7 @@

Page 3

- +

diff --git a/src/components/menu/test/basic/pages/page-one/page-one.html b/src/components/menu/test/basic/pages/page-one/page-one.html index c807ae32120..44788f1f653 100644 --- a/src/components/menu/test/basic/pages/page-one/page-one.html +++ b/src/components/menu/test/basic/pages/page-one/page-one.html @@ -1,8 +1,8 @@ - + - Left Menu + Start Menu @@ -14,55 +14,55 @@ {{p.title}} - - - - - - - - - - - - - @@ -78,11 +78,11 @@ - + - Right Menu + End Menu @@ -94,59 +94,59 @@ {{p.title}} - - - - - - - - - - - - - - diff --git a/src/components/menu/test/basic/pages/page-one/page-one.ts b/src/components/menu/test/basic/pages/page-one/page-one.ts index 60115306ef5..9b100a63571 100644 --- a/src/components/menu/test/basic/pages/page-one/page-one.ts +++ b/src/components/menu/test/basic/pages/page-one/page-one.ts @@ -33,12 +33,12 @@ export class PageOne { }); } - openRightMenu() { - this.menuCtrl.open('right'); + openEndMenu() { + this.menuCtrl.open('end'); } - openLeftMenu() { - this.menuCtrl.open('left'); + openStartMenu() { + this.menuCtrl.open('start'); } onDrag(ev: any) { diff --git a/src/components/menu/test/basic/pages/page-three/page-three.html b/src/components/menu/test/basic/pages/page-three/page-three.html index 55cc7ec7df5..d8ddf360504 100644 --- a/src/components/menu/test/basic/pages/page-three/page-three.html +++ b/src/components/menu/test/basic/pages/page-three/page-three.html @@ -20,7 +20,7 @@

Page 2

- +

diff --git a/src/components/menu/test/basic/pages/page-two/page-two.html b/src/components/menu/test/basic/pages/page-two/page-two.html index 34d46c181d9..693ba6ea3ea 100644 --- a/src/components/menu/test/basic/pages/page-two/page-two.html +++ b/src/components/menu/test/basic/pages/page-two/page-two.html @@ -2,7 +2,7 @@ - @@ -11,8 +11,7 @@ - - + @@ -21,7 +20,7 @@ - @@ -36,9 +35,9 @@

Page 1

- + - + @@ -49,7 +48,7 @@

Page 1

- +

diff --git a/src/components/menu/test/basic/pages/page-two/page-two.ts b/src/components/menu/test/basic/pages/page-two/page-two.ts index e22c9d7d9dc..0c8c2ed1a3d 100644 --- a/src/components/menu/test/basic/pages/page-two/page-two.ts +++ b/src/components/menu/test/basic/pages/page-two/page-two.ts @@ -13,7 +13,7 @@ export class PageTwo { public modalCtrl: ModalController, public platform: Platform) { } - myMenu: string = 'right'; + myMenu: string = 'end'; toggleDir() { this.platform.setDir(this.platform.isRTL ? 'ltr' : 'rtl', true); diff --git a/src/components/menu/test/menu.spec.ts b/src/components/menu/test/menu.spec.ts index c90fbf10742..35dc3106850 100644 --- a/src/components/menu/test/menu.spec.ts +++ b/src/components/menu/test/menu.spec.ts @@ -300,7 +300,7 @@ describe('MenuController', () => { let menuCtrl: MenuController; beforeEach(() => { - menuCtrl = new MenuController(); + menuCtrl = new MenuController(mockPlatform()); }); }); From 5b665c47283777aeeeff7774a1903f92c65f0649 Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Thu, 8 Jun 2017 23:38:18 +0300 Subject: [PATCH 09/72] fix(typo): --- src/components/menu/menu.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/menu/menu.ts b/src/components/menu/menu.ts index e82633764ee..7d32c553728 100644 --- a/src/components/menu/menu.ts +++ b/src/components/menu/menu.ts @@ -425,7 +425,7 @@ export class Menu implements RootNode, MenuInterface, OnInit, OnDestroy { * @hidden */ setOpen(shouldOpen: boolean, animated: boolean = true): Promise { - // If the menu is disabled or it is currenly being animated, let's do nothing + // If the menu is disabled or it is currently being animated, let's do nothing if ((shouldOpen === this.isOpen) || !this._canOpen() || this._isAnimating) { return Promise.resolve(this.isOpen); } From a91a68e198fa738ecf2fbe4db859b2d42537901a Mon Sep 17 00:00:00 2001 From: Brandy Carney Date: Mon, 12 Jun 2017 13:51:26 -0400 Subject: [PATCH 10/72] style(util): remove commented out test css --- src/themes/util.scss | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/themes/util.scss b/src/themes/util.scss index dcd542b8946..305698d4fff 100755 --- a/src/themes/util.scss +++ b/src/themes/util.scss @@ -62,8 +62,6 @@ ion-input :focus { opacity: 0; - // background: red; - // opacity: .3; contain: strict; } From 47e3c70bf3fc7ec2d4408074ffeca7dc66931d55 Mon Sep 17 00:00:00 2001 From: Job Date: Mon, 12 Jun 2017 21:05:57 +0200 Subject: [PATCH 11/72] fix(refresher): border should only show when pulled (#12015) fixes #10994 --- src/components/content/content.ts | 10 +++++++++- src/components/refresher/refresher.ts | 2 +- src/components/refresher/test/refresher.spec.ts | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/components/content/content.ts b/src/components/content/content.ts index e579123e09e..df4708e169a 100644 --- a/src/components/content/content.ts +++ b/src/components/content/content.ts @@ -167,7 +167,8 @@ export class EventEmitterProxy extends EventEmitter { '' + '', host: { - '[class.statusbar-padding]': 'statusbarPadding' + '[class.statusbar-padding]': 'statusbarPadding', + '[class.has-refresher]': '_hasRefresher' }, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None @@ -212,6 +213,8 @@ export class Content extends Ion implements OnDestroy, AfterViewInit, IContent { /** @internal */ _fullscreen: boolean; /** @internal */ + _hasRefresher: boolean = false; + /** @internal */ _footerEle: HTMLElement; /** @internal */ _dirty: boolean; @@ -782,6 +785,11 @@ export class Content extends Ion implements OnDestroy, AfterViewInit, IContent { this._cBottom += this._tabbarHeight; } + // Refresher uses a border which should be hidden unless pulled + if (this._hasRefresher) { + this._cTop -= 1; + } + // Fixed content shouldn't include content padding this._fTop = this._cTop; this._fBottom = this._cBottom; diff --git a/src/components/refresher/refresher.ts b/src/components/refresher/refresher.ts index d22114311e7..04580f3fd34 100644 --- a/src/components/refresher/refresher.ts +++ b/src/components/refresher/refresher.ts @@ -202,7 +202,7 @@ export class Refresher { constructor(private _plt: Platform, @Host() private _content: Content, private _zone: NgZone, gestureCtrl: GestureController) { this._events = new UIEventManager(_plt); - _content.setElementClass('has-refresher', true); + _content._hasRefresher = true; this._gesture = gestureCtrl.createGesture({ name: GESTURE_REFRESHER, priority: GESTURE_PRIORITY_REFRESHER diff --git a/src/components/refresher/test/refresher.spec.ts b/src/components/refresher/test/refresher.spec.ts index 6d787c6de84..8f94b5aeb83 100644 --- a/src/components/refresher/test/refresher.spec.ts +++ b/src/components/refresher/test/refresher.spec.ts @@ -224,6 +224,9 @@ describe('Refresher', () => { }); + it('should set hasRefresher on content', () => { + expect(content._hasRefresher).toBeTruthy(); + }); let contentElementRef: any; let refresher: Refresher; From c10f72b1e2614292e547c68f6f26515efb1cd691 Mon Sep 17 00:00:00 2001 From: Manuel Mtz-Almeida Date: Thu, 8 Jun 2017 01:00:40 +0200 Subject: [PATCH 12/72] fix(keyboard): big keyboard/input refactor fixes #9699 fixes #11484 fixes #11389 fixes #11325 fixes #11291 fixes #10828 fixes #11291 fixes #10393 fixes #10257 fixes #9434 fixes #8933 fixes #7178 fixes #7047 fixes #10552 fixes #10393 fixes #10183 fixes #10187 fixes #10852 fixes #11578 --- scripts/gulp/tasks/demos.dev.ts | 6 +- scripts/gulp/tasks/e2e.dev.ts | 6 +- scripts/gulp/util.ts | 9 +- src/components/alert/alert-component.ts | 4 +- src/components/app/app.ts | 62 ++ src/components/checkbox/checkbox.ts | 11 +- src/components/datetime/datetime.ts | 5 +- src/components/input/input.scss | 23 +- src/components/input/input.ts | 988 ++++++++---------- src/components/input/native-input.ts | 242 ----- src/components/input/next-input.ts | 18 - .../highlight/pages/root-page/root-page.html | 14 +- src/components/input/test/text-input.spec.ts | 33 +- src/components/searchbar/searchbar.ts | 5 +- src/components/select/select.ts | 4 - src/components/tabs/tabs.scss | 4 + src/components/tabs/tabs.ts | 40 +- .../advanced/pages/tab1-page1/tab1-page1.html | 1 + src/components/toggle/toggle.ts | 9 +- src/index.ts | 2 - src/module.ts | 6 - src/platform/keyboard.ts | 84 +- src/platform/platform-registry.ts | 10 + src/util/base-input.ts | 107 +- src/util/form.ts | 34 +- src/util/input-tester.ts | 42 + src/util/scroll-view.ts | 35 +- 27 files changed, 847 insertions(+), 957 deletions(-) delete mode 100644 src/components/input/native-input.ts delete mode 100644 src/components/input/next-input.ts diff --git a/scripts/gulp/tasks/demos.dev.ts b/scripts/gulp/tasks/demos.dev.ts index f43f787223a..90575d99469 100644 --- a/scripts/gulp/tasks/demos.dev.ts +++ b/scripts/gulp/tasks/demos.dev.ts @@ -11,14 +11,14 @@ task('demos.watch', ['demos.prepare'], (done: Function) => { done(new Error(`Usage: gulp e2e.watch --folder modal`)); } - serveDemo(folderInfo.componentName).then(() => { + serveDemo(folderInfo.componentName, folderInfo.devApp).then(() => { done(); }).catch((err: Error) => { done(err); }); }); -function serveDemo(folderName: any) { +function serveDemo(folderName: any, devApp: boolean) { const ionicAngularDir = join(PROJECT_ROOT, 'src'); const srcTestRoot = join(DEMOS_ROOT, 'src', folderName); @@ -40,5 +40,5 @@ function serveDemo(folderName: any) { const appNgModulePath = join(srcTestRoot, 'app', 'app.module.ts'); const distDir = join(distDemoRoot, 'www'); - return runAppScriptsServe(folderName, appEntryPoint, appNgModulePath, ionicAngularDir, distDir, pathToWriteFile, ionicAngularDir, sassConfigPath, copyConfigPath, watchConfigPath); + return runAppScriptsServe(folderName, appEntryPoint, appNgModulePath, ionicAngularDir, distDir, pathToWriteFile, ionicAngularDir, sassConfigPath, copyConfigPath, watchConfigPath, devApp); } diff --git a/scripts/gulp/tasks/e2e.dev.ts b/scripts/gulp/tasks/e2e.dev.ts index e9a5237afca..5fd86d41c7d 100644 --- a/scripts/gulp/tasks/e2e.dev.ts +++ b/scripts/gulp/tasks/e2e.dev.ts @@ -13,14 +13,14 @@ task('e2e.watch', ['e2e.prepare'], (done: Function) => { return; } - serveTest(folderInfo).then(() => { + serveTest(folderInfo, folderInfo.devApp).then(() => { done(); }).catch((err: Error) => { done(err); }); }); -function serveTest(folderInfo: any) { +function serveTest(folderInfo: any, devApp: boolean) { const ionicAngularDir = join(PROJECT_ROOT, 'src'); const srcTestRoot = join(PROJECT_ROOT, 'src', 'components', folderInfo.componentName, 'test', folderInfo.componentTest); @@ -47,5 +47,5 @@ function serveTest(folderInfo: any) { const appNgModulePath = join(dirname(appEntryPoint), 'app.module.ts'); const distDir = join(distTestRoot, 'www'); - return runAppScriptsServe(join(folderInfo.componentName, folderInfo.componentTest), appEntryPoint, appNgModulePath, ionicAngularDir, distDir, pathToWriteFile, ionicAngularDir, sassConfigPath, copyConfigPath, null); + return runAppScriptsServe(join(folderInfo.componentName, folderInfo.componentTest), appEntryPoint, appNgModulePath, ionicAngularDir, distDir, pathToWriteFile, ionicAngularDir, sassConfigPath, copyConfigPath, null, devApp); } diff --git a/scripts/gulp/util.ts b/scripts/gulp/util.ts index e87bf09db32..65a0cdb3747 100644 --- a/scripts/gulp/util.ts +++ b/scripts/gulp/util.ts @@ -190,7 +190,7 @@ export function runWebpack(pathToWebpackConfig: string, done: Function) { }); } -export function runAppScriptsServe(testOrDemoName: string, appEntryPoint: string, appNgModulePath: string, srcDir: string, distDir: string, tsConfig: string, ionicAngularDir: string, sassConfigPath: string, copyConfigPath: string, watchConfigPath: string) { +export function runAppScriptsServe(testOrDemoName: string, appEntryPoint: string, appNgModulePath: string, srcDir: string, distDir: string, tsConfig: string, ionicAngularDir: string, sassConfigPath: string, copyConfigPath: string, watchConfigPath: string, devApp: boolean) { console.log('Running ionic-app-scripts serve with', testOrDemoName); const deepLinksDir = dirname(dirname(appNgModulePath)); let scriptArgs = [ @@ -207,6 +207,9 @@ export function runAppScriptsServe(testOrDemoName: string, appEntryPoint: string '--copy', copyConfigPath, '--enableLint', 'false' ]; + if (devApp) { + scriptArgs.push('--bonjour'); + } if (watchConfigPath) { scriptArgs.push('--watch'); @@ -349,9 +352,11 @@ export function getFolderInfo() { componentName = folderSplit[0]; componentTest = (folderSplit.length > 1 ? folderSplit[1] : 'basic'); } + const devApp = argv.devapp !== undefined; return { componentName: componentName, - componentTest: componentTest + componentTest: componentTest, + devApp: devApp }; } diff --git a/src/components/alert/alert-component.ts b/src/components/alert/alert-component.ts index 836f34f7255..abb1771b18f 100644 --- a/src/components/alert/alert-component.ts +++ b/src/components/alert/alert-component.ts @@ -84,6 +84,7 @@ export class AlertCmp { msgId: string; subHdrId: string; mode: string; + keyboardResizes: boolean; gestureBlocker: BlockerDelegate; constructor( @@ -99,6 +100,7 @@ export class AlertCmp { this.gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL); this.d = params.data; this.mode = this.d.mode || config.get('mode'); + this.keyboardResizes = config.getBoolean('keyboardResizes', false); _renderer.setElementClass(_elementRef.nativeElement, `alert-${this.mode}`, true); if (this.d.cssClass) { @@ -178,7 +180,7 @@ export class AlertCmp { } const hasTextInput = (this.d.inputs.length && this.d.inputs.some(i => !(NON_TEXT_INPUT_REGEX.test(i.type)))); - if (hasTextInput && this._plt.is('mobile')) { + if (!this.keyboardResizes && hasTextInput && this._plt.is('mobile')) { // this alert has a text input and it's on a mobile device so we should align // the alert up high because we need to leave space for the virtual keboard // this also helps prevent the layout getting all messed up from diff --git a/src/components/app/app.ts b/src/components/app/app.ts index aa76a863759..3b10041b9cf 100644 --- a/src/components/app/app.ts +++ b/src/components/app/app.ts @@ -30,6 +30,7 @@ export class App { private _titleSrv: Title = new Title(DOCUMENT); private _rootNav: NavController = null; private _disableScrollAssist: boolean; + private _didScroll = false; /** * @hidden @@ -87,6 +88,11 @@ export class App { _plt.registerBackButtonAction(this.goBack.bind(this)); this._disableScrollAssist = _config.getBoolean('disableScrollAssist', false); + const blurring = _config.getBoolean('inputBlurring', false); + if (blurring) { + this._enableInputBlurring(); + } + runInDev(() => { // During developement, navPop can be triggered by calling const win = _plt.win(); @@ -179,6 +185,7 @@ export class App { */ setScrolling() { this._scrollTime = Date.now() + ACTIVE_SCROLLING_TIME; + this._didScroll = true; } /** @@ -289,6 +296,60 @@ export class App { return recursivePop(this.getActiveNav()); } + /** + * @hidden + */ + _enableInputBlurring() { + console.debug('App: _enableInputBlurring'); + let focused = true; + const self = this; + const platform = this._plt; + + platform.registerListener(platform.doc(), 'focusin', onFocusin, { capture: true, zone: false, passive: true }); + platform.registerListener(platform.doc(), 'touchend', onTouchend, { capture: false, zone: false, passive: true }); + + function onFocusin(ev: any) { + focused = true; + } + function onTouchend(ev: any) { + // if app did scroll return early + if (self._didScroll) { + self._didScroll = false; + return; + } + const active = self._plt.getActiveElement(); + if (!active) { + return; + } + // only blur if the active element is a text-input or a textarea + if (SKIP_BLURRING.indexOf(active.tagName) === -1) { + return; + } + + // if the selected target is the active element, do not blur + const tapped = ev.target; + if (tapped === active) { + return; + } + if (SKIP_BLURRING.indexOf(tapped.tagName) >= 0) { + return; + } + + // skip if div is a cover + if (tapped.classList.contains('input-cover')) { + return; + } + + focused = false; + // TODO: find a better way, why 50ms? + platform.timeout(() => { + if (!focused) { + active.blur(); + } + }, 50); + } + } + } function recursivePop(nav: any): Promise { @@ -322,5 +383,6 @@ function findTopNav(nav: NavController) { return nav; } +const SKIP_BLURRING = ['INPUT', 'TEXTAREA', 'ION-INPUT', 'ION-TEXTAREA']; const ACTIVE_SCROLLING_TIME = 100; const CLICK_BLOCK_BUFFER_IN_MILLIS = 64; diff --git a/src/components/checkbox/checkbox.ts b/src/components/checkbox/checkbox.ts index d8aa685e2f3..be8fcc18c2e 100644 --- a/src/components/checkbox/checkbox.ts +++ b/src/components/checkbox/checkbox.ts @@ -118,13 +118,6 @@ export class Checkbox extends BaseInput implements IonicTapInput, OnDes super(config, elementRef, renderer, 'checkbox', false, form, item, null); } - /** - * @hidden - */ - initFocus() { - this._elementRef.nativeElement.querySelector('button').focus(); - } - /** * @hidden */ @@ -145,8 +138,8 @@ export class Checkbox extends BaseInput implements IonicTapInput, OnDes /** * @hidden */ - _inputCheckHasValue(val: boolean) { - this._item && this._item.setElementClass('item-checkbox-checked', val); + _inputUpdated() { + this._item && this._item.setElementClass('item-checkbox-checked', this._value); } } diff --git a/src/components/datetime/datetime.ts b/src/components/datetime/datetime.ts index 27cf3a3b2cf..00b7ec47df6 100644 --- a/src/components/datetime/datetime.ts +++ b/src/components/datetime/datetime.ts @@ -448,6 +448,7 @@ export class DateTime extends BaseInput implements AfterContentIni * @hidden */ _inputUpdated() { + super._inputUpdated(); this.updateText(); } @@ -475,10 +476,6 @@ export class DateTime extends BaseInput implements AfterContentIni @HostListener('click', ['$event']) _click(ev: UIEvent) { - // do not continue if the click event came from a form submit - if (ev.detail === 0) { - return; - } ev.preventDefault(); ev.stopPropagation(); this.open(); diff --git a/src/components/input/input.scss b/src/components/input/input.scss index 4576fed2d40..119a87e8333 100644 --- a/src/components/input/input.scss +++ b/src/components/input/input.scss @@ -93,6 +93,8 @@ input.text-input:-webkit-autofill { width: 100%; height: 100%; + + touch-action: manipulation; } .input[disabled] .input-cover { @@ -127,27 +129,6 @@ input.text-input:-webkit-autofill { } -// Scroll Assist Input -// -------------------------------------------------- -// This input is used to help the app handle -// Next and Previous input tabbing - -[next-input] { - @include padding(0); - - position: absolute; - bottom: 20px; - - width: 1px; - height: 1px; - - border: 0; - background: transparent; - - pointer-events: none; -} - - // Clear Input Icon // -------------------------------------------------- diff --git a/src/components/input/input.ts b/src/components/input/input.ts index f3f7980d77b..7e095188baa 100644 --- a/src/components/input/input.ts +++ b/src/components/input/input.ts @@ -1,19 +1,21 @@ -import { Component, Optional, ElementRef, EventEmitter, Input, Output, Renderer, ViewChild, ViewEncapsulation } from '@angular/core'; +import { + Component, ChangeDetectionStrategy, Optional, ElementRef, EventEmitter, + Input, Output, Renderer, ViewChild, ViewEncapsulation +} from '@angular/core'; import { NgControl } from '@angular/forms'; +import { Subject } from 'rxjs/Subject'; +import 'rxjs/add/operator/takeUntil'; + import { App } from '../app/app'; import { Config } from '../../config/config'; -import { Content, ContentDimensions, ScrollEvent } from '../content/content'; -import { copyInputAttributes, PointerCoordinates, hasPointerMoved, pointerCoord } from '../../util/dom'; +import { Content, ContentDimensions } from '../content/content'; +import { hasPointerMoved, pointerCoord } from '../../util/dom'; import { DomController } from '../../platform/dom-controller'; import { Form, IonicFormInput } from '../../util/form'; -import { Ion } from '../ion'; -import { isString, isTrueProperty } from '../../util/util'; +import { BaseInput } from '../../util/base-input'; +import { isTrueProperty, assert } from '../../util/util'; import { Item } from '../item/item'; -import { NativeInput } from './native-input'; -import { NextInput } from './next-input'; -import { NavController } from '../../navigation/nav-controller'; -import { NavControllerBase } from '../../navigation/nav-controller-base'; import { Platform } from '../../platform/platform'; @@ -84,92 +86,65 @@ import { Platform } from '../../platform/platform'; @Component({ selector: 'ion-input,ion-textarea', template: - '' + - '' + - '' + - '' + - '
', + '' + + + '' + + + '' + + + '
', + encapsulation: ViewEncapsulation.None, + changeDetection: ChangeDetectionStrategy.OnPush }) -export class TextInput extends Ion implements IonicFormInput { - _autoComplete: string; - _autoCorrect: string; +export class TextInput extends BaseInput implements IonicFormInput { + _autoFocusAssist: string; _clearInput: boolean = false; _clearOnEdit: boolean; - _coord: PointerCoordinates; _didBlurAfterEdit: boolean; - _disabled: boolean = false; _readonly: boolean = false; - _isTouch: boolean; _keyboardHeight: number; - _min: any; - _max: any; - _step: any; - _native: NativeInput; - _nav: NavControllerBase; - _scrollStart: any; - _scrollEnd: any; _type: string = 'text'; - _useAssist: boolean; - _usePadding: boolean; - _value: any = ''; - - /** @hidden */ - inputControl: NgControl; - - constructor( - config: Config, - private _plt: Platform, - private _form: Form, - private _app: App, - elementRef: ElementRef, - renderer: Renderer, - @Optional() private _content: Content, - @Optional() private _item: Item, - @Optional() nav: NavController, - @Optional() public ngControl: NgControl, - private _dom: DomController - ) { - super(config, elementRef, renderer, 'input'); - - this._nav = nav; - - this._autoFocusAssist = config.get('autoFocusAssist', 'delay'); - this._autoComplete = config.get('autocomplete', 'off'); - this._autoCorrect = config.get('autocorrect', 'off'); - this._keyboardHeight = config.getNumber('keyboardHeight'); - this._useAssist = config.getBoolean('scrollAssist', false); - this._usePadding = config.getBoolean('scrollPadding', this._useAssist); - - if (elementRef.nativeElement.tagName === 'ION-TEXTAREA') { - this._type = TEXTAREA; - } - - if (ngControl) { - ngControl.valueAccessor = this; - this.inputControl = ngControl; - } - - _form.register(this); - - // only listen to content scroll events if there is content - if (_content) { - this._scrollStart = _content.ionScrollStart.subscribe((ev: ScrollEvent) => { - this.scrollHideFocus(ev, true); - }); - this._scrollEnd = _content.ionScrollEnd.subscribe((ev: ScrollEvent) => { - this.scrollHideFocus(ev, false); - }); - } - - this.mode = config.get('mode'); - } - - /** - * @input {string} Instructional text that shows before the input has a value. - */ - @Input() placeholder: string = ''; + _scrollData: ScrollData; + _isTextarea: boolean = false; + _onDestroy = new Subject(); + _coord: any; + _isTouch: boolean; + _useAssist = false; + _relocated: boolean = false; /** * @input {boolean} If true, a clear icon will appear in the input when there is a value. Clicking it clears the input. @@ -179,67 +154,21 @@ export class TextInput extends Ion implements IonicFormInput { return this._clearInput; } set clearInput(val: any) { - this._clearInput = (this._type !== TEXTAREA && isTrueProperty(val)); - } - - /** - * @input {string} The text value of the input. - */ - @Input() - get value() { - return this._value; - } - set value(val: any) { - this._value = val; - this.checkHasValue(val); + this._clearInput = (!this._isTextarea && isTrueProperty(val)); } /** - * @input {string} The type of control to display. The default type is text. Possible values are: `"text"`, `"password"`, `"email"`, `"number"`, `"search"`, `"tel"`, or `"url"`. + * @input {string} The type of control to display. The default type is text. + * Possible values are: `"text"`, `"password"`, `"email"`, `"number"`, `"search"`, `"tel"`, or `"url"`. */ @Input() get type() { - return this._type; + return (this._isTextarea) + ? 'text' + : this._type; } set type(val: any) { - if (this._type !== TEXTAREA) { - this._type = 'text'; - - if (isString(val)) { - val = val.toLowerCase(); - - if (TEXT_TYPE_REGEX.test(val)) { - this._type = val; - } - } - } - } - - /** - * @input {boolean} If true, the user cannot interact with this element. - */ - @Input() - get disabled(): boolean { - return this._disabled; - } - set disabled(val: boolean) { - this.setDisabled(this._disabled = isTrueProperty(val)); - } - - /** - * @hidden - */ - setDisabled(val: boolean) { - this._renderer.setElementAttribute(this._elementRef.nativeElement, 'disabled', val ? '' : null); - this._item && this._item.setElementClass('item-input-disabled', val); - this._native && this._native.isDisabled(val); - } - - /** - * @hidden - */ - setDisabledState(isDisabled: boolean) { - this.disabled = isDisabled; + this._type = val; } /** @@ -254,7 +183,8 @@ export class TextInput extends Ion implements IonicFormInput { } /** - * @input {boolean} If true, the value will be cleared after focus upon edit. Defaults to `true` when `type` is `"password"`, `false` for all other types. + * @input {boolean} If true, the value will be cleared after focus upon edit. + * Defaults to `true` when `type` is `"password"`, `false` for all other types. */ @Input() get clearOnEdit() { @@ -265,375 +195,382 @@ export class TextInput extends Ion implements IonicFormInput { } /** - * @input {any} The minimum value, which must not be greater than its maximum (max attribute) value. + * @hidden */ - @Input() - get min() { - return this._min; - } - set min(val: any) { - this.setMin(this._min = val); - } + @ViewChild('textInput', { read: ElementRef }) _native: ElementRef; /** - * @hidden + * @input {string} Instructional text that shows before the input has a value. */ - setMin(val: any) { - this._native && this._native.setMin(val); - } + @Input() autocomplete: string = ''; /** - * @input {any} The maximum value, which must not be less than its minimum (min attribute) value. + * @input {string} Instructional text that shows before the input has a value. */ - @Input() - get max() { - return this._max; - } - set max(val: any) { - this.setMax(this._max = val); - } + @Input() autocorrect: string = ''; /** - * @hidden + * @input {string} Specifies whether the element is to have its spelling + * and grammar checked or not. */ - setMax(val: any) { - this._native && this._native.setMax(val); - } + @Input() spellcheck: string = null; /** - * @input {any} Works with the min and max attributes to limit the increments at which a value can be set. + * @input {string} controls whether and how the text value for textual form control descendants should be automatically capitalized as it is entered/edited by the user. */ - @Input() - get step() { - return this._step; - } - set step(val: any) { - this.setStep(this._step = val); - } + @Input() autocapitalize: string = null; /** - * @hidden + * @input {string} Instructional text that shows before the input has a value. */ - setStep(val: any) { - this._native && this._native.setStep(val); - } + @Input() placeholder: string = ''; /** - * @hidden + * @input {string} The name attribute is used to reference elements in a JavaScript, + * or to reference form data after a form is submitted. */ - @ViewChild('input', { read: NativeInput }) - set _nativeInput(nativeInput: NativeInput) { - if (this.type !== TEXTAREA) { - this.setNativeInput(nativeInput); - } - } + @Input() name: string = null; /** - * @hidden + * @input {any} The minimum value, which must not be greater than its maximum (max attribute) value. */ - @ViewChild('textarea', { read: NativeInput }) - set _nativeTextarea(nativeInput: NativeInput) { - if (this.type === TEXTAREA) { - this.setNativeInput(nativeInput); - } - } + @Input() min: number | string = null; /** - * @hidden + * @input {any} The maximum value, which must not be less than its minimum (min attribute) value. */ - @ViewChild(NextInput) - set _nextInput(nextInput: NextInput) { - if (nextInput) { - nextInput.focused.subscribe(() => { - this._form.tabFocus(this); - }); - } - } + @Input() max: number | string = null; + + /** + * @input {any} Works with the min and max attributes to limit the increments at which a value can be set. + */ + @Input() step: number | string = null; /** - * @output {event} Emitted when the input no longer has focus. + * @input {any} Specifies the maximum number of characters allowed in the element. */ - @Output() blur: EventEmitter = new EventEmitter(); + @Input() maxlength: number | string = null; /** - * @output {event} Emitted when the input has focus. + * @hidden */ - @Output() focus: EventEmitter = new EventEmitter(); + @Output() input = new EventEmitter(); /** * @hidden */ - setNativeInput(nativeInput: NativeInput) { - this._native = nativeInput; - nativeInput.setValue(this._value); - nativeInput.setMin(this._min); - nativeInput.setMax(this._max); - nativeInput.setStep(this._step); - nativeInput.isDisabled(this.disabled); - - if (this._item && this._item.labelId !== null) { - nativeInput.labelledBy(this._item.labelId); + @Output() blur = new EventEmitter(); + + /** + * @hidden + */ + @Output() focus = new EventEmitter(); + + constructor( + config: Config, + private _plt: Platform, + private form: Form, + private _app: App, + elementRef: ElementRef, + renderer: Renderer, + @Optional() private _content: Content, + @Optional() private item: Item, + @Optional() public ngControl: NgControl, + private _dom: DomController + ) { + super(config, elementRef, renderer, + elementRef.nativeElement.tagName === 'ION-TEXTAREA' ? 'textarea' : 'input', '', form, item, ngControl); + + this.autocomplete = config.get('autocomplete', 'off'); + this.autocorrect = config.get('autocorrect', 'off'); + this._autoFocusAssist = config.get('autoFocusAssist', 'delay'); + this._keyboardHeight = config.getNumber('keyboardHeight'); + this._isTextarea = !!(elementRef.nativeElement.tagName === 'ION-TEXTAREA'); + + // If not inside content, let's disable all the hacks + if (!_content) { + return; } - nativeInput.valueChange.subscribe((inputValue: any) => { - this.onChange(inputValue); - this.checkHasValue(inputValue); - }); + const blurOnScroll = config.getBoolean('hideCaretOnScroll', false); + if (blurOnScroll) { + this._enableHideCaretOnScroll(); + } - nativeInput.keydown.subscribe((inputValue: any) => { - this.onKeydown(inputValue); - }); + const resizeAssist = config.getBoolean('resizeAssist', false); + if (resizeAssist) { + this._keyboardHeight = 60; + this._enableResizeAssist(); - this.focusChange(this.hasFocus()); - nativeInput.focusChange.subscribe((textInputHasFocus: any) => { - this.focusChange(textInputHasFocus); - this.checkHasValue(nativeInput.getValue()); - if (!textInputHasFocus) { - this.onTouched(textInputHasFocus); - } - }); + } else { + this._useAssist = config.getBoolean('scrollAssist', false); - this.checkHasValue(nativeInput.getValue()); + const usePadding = config.getBoolean('scrollPadding', this._useAssist); + if (usePadding) { + this._enableScrollPadding(); + } + } + } - var ionInputEle: HTMLElement = this._elementRef.nativeElement; - var nativeInputEle: HTMLElement = nativeInput.element(); + ngAfterContentInit() { } - // copy ion-input attributes to the native input element - copyInputAttributes(ionInputEle, nativeInputEle); + /** + * @hidden + */ + ngAfterViewInit() { + assert(this._native && this._native.nativeElement, 'input element must be valid'); + // By default, password inputs clear after focus when they have content + if (this.clearOnEdit !== false && this.type === 'password') { + this.clearOnEdit = true; + } + const ionInputEle: HTMLElement = this._elementRef.nativeElement; if (ionInputEle.hasAttribute('autofocus')) { // the ion-input element has the autofocus attributes + const nativeInputEle: HTMLElement = this._native.nativeElement; ionInputEle.removeAttribute('autofocus'); - - if (this._autoFocusAssist === 'immediate') { - // config says to immediate focus on the input - // works best on android devices - nativeInputEle.focus(); - - } else if (this._autoFocusAssist === 'delay') { - // config says to chill out a bit and focus on the input after transitions - // works best on desktop - this._plt.timeout(() => { + switch (this._autoFocusAssist) { + case 'immediate': + // config says to immediate focus on the input + // works best on android devices nativeInputEle.focus(); - }, 650); + break; + case 'delay': + // config says to chill out a bit and focus on the input after transitions + // works best on desktop + this._plt.timeout(() => nativeInputEle.focus(), 650); + break; } - // traditionally iOS has big issues with autofocus on actual devices // autoFocus is disabled by default with the iOS mode config } + this._initialize(); - // by default set autocomplete="off" unless specified by the input - if (ionInputEle.hasAttribute('autocomplete')) { - this._autoComplete = ionInputEle.getAttribute('autocomplete'); + if (this.focus.observers.length > 0) { + console.warn('(focus) is deprecated in ion-input, use (ionFocus) instead'); } - nativeInputEle.setAttribute('autocomplete', this._autoComplete); - - // by default set autocorrect="off" unless specified by the input - if (ionInputEle.hasAttribute('autocorrect')) { - this._autoCorrect = ionInputEle.getAttribute('autocorrect'); + if (this.blur.observers.length > 0) { + console.warn('(blur) is deprecated in ion-input, use (ionBlur) instead'); } - nativeInputEle.setAttribute('autocorrect', this._autoCorrect); } /** * @hidden */ - initFocus() { - // begin the process of setting focus to the inner input element - const app = this._app; - const content = this._content; - const nav = this._nav; - const nativeInput = this._native; - - console.debug(`input-base, initFocus(), scrollView: ${!!content}`); - - if (content) { - // this input is inside of a scroll view - // find out if text input should be manually scrolled into view - - // get container of this input, probably an ion-item a few nodes up - var ele: HTMLElement = this._elementRef.nativeElement; - ele = ele.closest('ion-item,[ion-item]') || ele; - - var scrollData = getScrollData(ele.offsetTop, ele.offsetHeight, content.getContentDimensions(), this._keyboardHeight, this._plt.height()); - if (Math.abs(scrollData.scrollAmount) < 4) { - // the text input is in a safe position that doesn't - // require it to be scrolled into view, just set focus now - this.setFocus(); - - // all good, allow clicks again - app.setEnabled(true); - nav && nav.setTransitioning(false); - - if (this._usePadding) { - content.clearScrollPaddingFocusOut(); - } - return; - } - - if (this._usePadding) { - // add padding to the bottom of the scroll view (if needed) - content.addScrollPadding(scrollData.scrollPadding); - } - - // manually scroll the text input to the top - // do not allow any clicks while it's scrolling - var scrollDuration = getScrollAssistDuration(scrollData.scrollAmount); - app.setEnabled(false, scrollDuration); - nav && nav.setTransitioning(true); - - // temporarily move the focus to the focus holder so the browser - // doesn't freak out while it's trying to get the input in place - // at this point the native text input still does not have focus - nativeInput.beginFocus(true, scrollData.inputSafeY); - - // scroll the input into place - content.scrollTo(0, scrollData.scrollTo, scrollDuration, () => { - console.debug(`input-base, scrollTo completed, scrollTo: ${scrollData.scrollTo}, scrollDuration: ${scrollDuration}`); - // the scroll view is in the correct position now - // give the native text input focus - nativeInput.beginFocus(false, 0); - - // ensure this is the focused input - this.setFocus(); - - // all good, allow clicks again - app.setEnabled(true); - nav && nav.setTransitioning(false); - - if (this._usePadding) { - content.clearScrollPaddingFocusOut(); - } - }); - - } else { - // not inside of a scroll view, just focus it - this.setFocus(); - } + ngOnDestroy() { + super.ngOnDestroy(); + this._onDestroy.next(); + this._onDestroy = null; } /** * @hidden */ - setFocus() { - // immediately set focus - this._form.setAsFocused(this); - - // set focus on the actual input element - console.debug(`input-base, setFocus ${this._native.element().value}`); - this._native.setFocus(); - - // ensure the body hasn't scrolled down - this._dom.write(() => { - this._plt.doc().body.scrollTop = 0; - }); + initFocus() { + this.setFocus(); } /** * @hidden */ - scrollHideFocus(ev: ScrollEvent, shouldHideFocus: boolean) { - // do not continue if there's no nav, or it's transitioning - if (this._nav && this.hasFocus()) { - // if it does have focus, then do the dom write - this._dom.write(() => { - this._native.hideFocus(shouldHideFocus); - }); + setFocus() { + // let's set focus to the element + // but only if it does not already have focus + if (!this.isFocus()) { + this._native.nativeElement.focus(); } } /** * @hidden */ - inputBlurred(ev: UIEvent) { - this.blur.emit(ev); + setBlur() { + if (this.isFocus()) { + this._native.nativeElement.blur(); + } } /** * @hidden */ - inputFocused(ev: UIEvent) { - this.focus.emit(ev); + onInput(ev: any) { + this.value = ev.target.value; + + // TODO: deprecate this + this.input.emit(ev); } /** * @hidden */ - writeValue(val: any) { - this._value = val; - this.checkHasValue(val); + onBlur(ev: UIEvent) { + this._fireBlur(); + + // TODO: deprecate this (06/07/2017) + this.blur.emit(ev); + + this._scrollData = null; + if (this._clearOnEdit && this.hasValue()) { + this._didBlurAfterEdit = true; + } } /** * @hidden */ - onChange(val: any) { - this.checkHasValue(val); + onFocus(ev: UIEvent) { + this._fireFocus(); + + // TODO: deprecate this (06/07/2017) + this.focus.emit(ev); } /** * @hidden */ - onKeydown(val: any) { - if (this._clearOnEdit) { - this.checkClearOnEdit(val); + onKeydown(ev: any) { + if (ev && this._clearOnEdit) { + this.checkClearOnEdit(ev.target.value); } } /** * @hidden */ - onTouched(val: any) {} + _inputUpdated() { + super._inputUpdated(); + const inputEle = this._native.nativeElement; + const value = this._value; + if (inputEle.value !== value) { + inputEle.value = value; + } + } /** * @hidden */ - hasFocus(): boolean { - // check if an input has focus or not - return this._plt.hasFocus(this._native.element()); + clearTextInput() { + this.value = ''; } /** - * @hidden - */ - hasValue(): boolean { - const inputValue = this._value; - return (inputValue !== null && inputValue !== undefined && inputValue !== ''); + * Check if we need to clear the text input if clearOnEdit is enabled + * @hidden + */ + checkClearOnEdit(inputValue: string) { + if (!this._clearOnEdit) { + return; + } + + // Did the input value change after it was blurred and edited? + if (this._didBlurAfterEdit && this.hasValue()) { + // Clear the input + this.clearTextInput(); + } + + // Reset the flag + this._didBlurAfterEdit = false; } - /** - * @hidden - */ - checkHasValue(inputValue: any) { - if (this._item) { - var hasValue = (inputValue !== null && inputValue !== undefined && inputValue !== ''); - // TODO remove all uses of input-has-value in v4 - this._item.setElementClass('input-has-value', hasValue); - this._item.setElementClass('item-input-has-value', hasValue); + _getScrollData(): ScrollData { + if (!this._content) { + return newScrollData(); + } + + // get container of this input, probably an ion-item a few nodes up + if (this._scrollData) { + return this._scrollData; } + let ele: HTMLElement = this._elementRef.nativeElement; + ele = ele.closest('ion-item,[ion-item]') || ele; + return this._scrollData = getScrollData( + ele.offsetTop, ele.offsetHeight, + this._content.getContentDimensions(), this._keyboardHeight, this._plt.height()); } - /** - * @hidden - */ - focusChange(inputHasFocus: boolean) { - if (this._item) { - console.debug(`input-base, focusChange, inputHasFocus: ${inputHasFocus}, ${this._item.getNativeElement().nodeName}.${this._item.getNativeElement().className}`); - // TODO remove input-has-focus for v4 - this._item.setElementClass('input-has-focus', inputHasFocus); - this._item.setElementClass('item-input-has-focus', inputHasFocus); + _relocateInput(shouldRelocate: boolean) { + if (this._relocated === shouldRelocate) { + return; } + const platform = this._plt; + const componentEle = this.getNativeElement(); + const focusedInputEle = this._native.nativeElement; + console.debug(`native-input, hideCaret, shouldHideCaret: ${shouldRelocate}, input value: ${focusedInputEle.value}`); + if (shouldRelocate) { + // this allows for the actual input to receive the focus from + // the user's touch event, but before it receives focus, it + // moves the actual input to a location that will not screw + // up the app's layout, and does not allow the native browser + // to attempt to scroll the input into place (messing up headers/footers) + // the cloned input fills the area of where native input should be + // while the native input fakes out the browser by relocating itself + // before it receives the actual focus event + // We hide the focused input (with the visible caret) invisiable by making it scale(0), + cloneInputComponent(platform, componentEle, focusedInputEle); + const inputRelativeY = this._getScrollData().inputSafeY; + focusedInputEle.style[platform.Css.transform] = `translate3d(-9999px,${inputRelativeY}px,0)`; + focusedInputEle.style.opacity = '0'; - // If clearOnEdit is enabled and the input blurred but has a value, set a flag - if (this._clearOnEdit && !inputHasFocus && this.hasValue()) { - this._didBlurAfterEdit = true; + } else { + removeClone(platform, componentEle, focusedInputEle); } + this._relocated = shouldRelocate; } - /** - * @hidden - */ - pointerStart(ev: UIEvent) { + _enableScrollPadding() { + assert(this._content, 'content is undefined'); + + console.debug('Input: enableScrollPadding'); + + this.ionFocus.subscribe(() => { + const content = this._content; + + // add padding to the bottom of the scroll view (if needed) + content.addScrollPadding(this._getScrollData().scrollPadding); + content.clearScrollPaddingFocusOut(); + }); + } + + _enableHideCaretOnScroll() { + assert(this._content, 'content is undefined'); + + const content = this._content; + console.debug('Input: enableHideCaretOnScroll'); + + content.ionScrollStart + .takeUntil(this._onDestroy) + .subscribe(() => scrollHideCaret(true)); + + content.ionScrollEnd + .takeUntil(this._onDestroy) + .subscribe(() => scrollHideCaret(false)); + + this.ionBlur.subscribe(() => this._relocateInput(false)); + + const self = this; + function scrollHideCaret(shouldHideCaret: boolean) { + // if it does have focus, then do the dom write + if (self.isFocus()) { + self._dom.write(() => self._relocateInput(shouldHideCaret)); + } + } + } + + _enableResizeAssist() { + assert(this._content, 'content is undefined'); + + console.debug('Input: enableAutoScroll'); + this.ionFocus.subscribe(() => { + const scrollData = this._getScrollData(); + if (Math.abs(scrollData.scrollAmount) > 100) { + this._content.scrollTo(0, scrollData.scrollTo, scrollData.scrollDuration); + } + }); + } + + _pointerStart(ev: UIEvent) { + assert(this._content, 'content is undefined'); + // input cover touchstart if (ev.type === 'touchstart') { this._isTouch = true; @@ -647,10 +584,9 @@ export class TextInput extends Ion implements IonicFormInput { console.debug(`input-base, pointerStart, type: ${ev.type}`); } - /** - * @hidden - */ - pointerEnd(ev: UIEvent) { + _pointerEnd(ev: UIEvent) { + assert(this._content, 'content is undefined'); + // input cover touchend/mouseup console.debug(`input-base, pointerEnd, type: ${ev.type}`); @@ -662,130 +598,56 @@ export class TextInput extends Ion implements IonicFormInput { } else if (this._coord) { // get where the touchend/mouseup ended - let endCoord = pointerCoord(ev); + var endCoord = pointerCoord(ev); // focus this input if the pointer hasn't moved XX pixels // and the input doesn't already have focus - if (!hasPointerMoved(8, this._coord, endCoord) && !this.hasFocus()) { + if (!hasPointerMoved(8, this._coord, endCoord) && !this.isFocus()) { ev.preventDefault(); ev.stopPropagation(); // begin the input focus process - this.initFocus(); + this._jsSetFocus(); } } this._coord = null; } - /** - * @hidden - */ - setItemInputControlCss() { - let item = this._item; - let nativeInput = this._native; - let inputControl = this.inputControl; - - // Set the control classes on the item - if (item && inputControl) { - setControlCss(item, inputControl); - } - // Set the control classes on the native input - if (nativeInput && inputControl) { - setControlCss(nativeInput, inputControl); - } - } + _jsSetFocus() { + assert(this._content, 'content is undefined'); - /** - * @hidden - */ - ngOnInit() { - const item = this._item; - if (item) { - if (this.type === TEXTAREA) { - item.setElementClass('item-textarea', true); - } - item.setElementClass('item-input', true); - item.registerInput(this.type); - } - - // By default, password inputs clear after focus when they have content - if (this.type === 'password' && this.clearOnEdit !== false) { - this.clearOnEdit = true; - } - } - - /** - * @hidden - */ - ngAfterContentChecked() { - this.setItemInputControlCss(); - } - - /** - * @hidden - */ - ngOnDestroy() { - this._form.deregister(this); - - // only stop listening to content scroll events if there is content - if (this._content) { - this._scrollStart.unsubscribe(); - this._scrollEnd.unsubscribe(); + // begin the process of setting focus to the inner input element + const content = this._content; + console.debug(`input-base, initFocus(), scrollView: ${!!content}`); + if (!content) { + // not inside of a scroll view, just focus it + this.setFocus(); } - } - /** - * @hidden - */ - clearTextInput() { - console.debug('Should clear input'); - this._value = ''; - this.onChange(this._value); - this.writeValue(this._value); - } - - /** - * Check if we need to clear the text input if clearOnEdit is enabled - * @hidden - */ - checkClearOnEdit(inputValue: string) { - if (!this._clearOnEdit) { + var scrollData = this._getScrollData(); + if (Math.abs(scrollData.scrollAmount) < 4) { + // the text input is in a safe position that doesn't + // require it to be scrolled into view, just set focus now + this.setFocus(); return; } - // Did the input value change after it was blurred and edited? - if (this._didBlurAfterEdit && this.hasValue()) { - // Clear the input - this.clearTextInput(); - } - - // Reset the flag - this._didBlurAfterEdit = false; - } - - /** - * @hidden - * Angular2 Forms API method called by the view (formControlName) to register the - * onChange event handler that updates the model (Control). - * @param {Function} fn the onChange event handler. - */ - registerOnChange(fn: any) { this.onChange = fn; } - - /** - * @hidden - * Angular2 Forms API method called by the view (formControlName) to register - * the onTouched event handler that marks model (Control) as touched. - * @param {Function} fn onTouched event handler. - */ - registerOnTouched(fn: any) { this.onTouched = fn; } + // temporarily move the focus to the focus holder so the browser + // doesn't freak out while it's trying to get the input in place + // at this point the native text input still does not have focus + this._relocateInput(true); + this.setFocus(); + // scroll the input into place + content.scrollTo(0, scrollData.scrollTo, scrollData.scrollDuration, () => { + // the scroll view is in the correct position now + // give the native text input focus + this._relocateInput(false); - /** - * @hidden - */ - focusNext() { - this._form.tabFocus(this); + // ensure this is the focused input + this.setFocus(); + }); } } @@ -837,29 +699,41 @@ export class TextInput extends Ion implements IonicFormInput { const SCROLL_ASSIST_SPEED = 0.3; -const TEXTAREA = 'textarea'; -const TEXT_TYPE_REGEX = /password|email|number|search|tel|url|date|month|time|week/; +function newScrollData(): ScrollData { + return { + scrollAmount: 0, + scrollTo: 0, + scrollPadding: 0, + scrollDuration: 0, + inputSafeY: 0 + }; +} /** * @hidden */ -export function getScrollData(inputOffsetTop: number, inputOffsetHeight: number, scrollViewDimensions: ContentDimensions, keyboardHeight: number, plaformHeight: number) { +export function getScrollData( + inputOffsetTop: number, + inputOffsetHeight: number, + scrollViewDimensions: ContentDimensions, + keyboardHeight: number, + plaformHeight: number): ScrollData { // compute input's Y values relative to the body - let inputTop = (inputOffsetTop + scrollViewDimensions.contentTop - scrollViewDimensions.scrollTop); - let inputBottom = (inputTop + inputOffsetHeight); + const inputTop = (inputOffsetTop + scrollViewDimensions.contentTop - scrollViewDimensions.scrollTop); + const inputBottom = (inputTop + inputOffsetHeight); // compute the safe area which is the viewable content area when the soft keyboard is up - let safeAreaTop = scrollViewDimensions.contentTop; - let safeAreaHeight = (plaformHeight - keyboardHeight - safeAreaTop) / 2; - let safeAreaBottom = safeAreaTop + safeAreaHeight; + const safeAreaTop = scrollViewDimensions.contentTop; + const safeAreaHeight = (plaformHeight - keyboardHeight - safeAreaTop) / 2; + const safeAreaBottom = safeAreaTop + safeAreaHeight; // figure out if each edge of teh input is within the safe area - let inputTopWithinSafeArea = (inputTop >= safeAreaTop && inputTop <= safeAreaBottom); - let inputTopAboveSafeArea = (inputTop < safeAreaTop); - let inputTopBelowSafeArea = (inputTop > safeAreaBottom); - let inputBottomWithinSafeArea = (inputBottom >= safeAreaTop && inputBottom <= safeAreaBottom); - let inputBottomBelowSafeArea = (inputBottom > safeAreaBottom); + const inputTopWithinSafeArea = (inputTop >= safeAreaTop && inputTop <= safeAreaBottom); + const inputTopAboveSafeArea = (inputTop < safeAreaTop); + const inputTopBelowSafeArea = (inputTop > safeAreaBottom); + const inputBottomWithinSafeArea = (inputBottom >= safeAreaTop && inputBottom <= safeAreaBottom); + const inputBottomBelowSafeArea = (inputBottom > safeAreaBottom); /* Text Input Scroll To Scenarios @@ -873,12 +747,7 @@ export function getScrollData(inputOffsetTop: number, inputOffsetHeight: number, 7) Input top below safe area, no room to scroll, input larger than safe area */ - const scrollData: ScrollData = { - scrollAmount: 0, - scrollTo: 0, - scrollPadding: 0, - inputSafeY: 0 - }; + const scrollData: ScrollData = newScrollData(); if (inputTopWithinSafeArea && inputBottomWithinSafeArea) { // Input top within safe area, bottom within safe area @@ -924,46 +793,73 @@ export function getScrollData(inputOffsetTop: number, inputOffsetHeight: number, // to have the padding already rendered so there's no jank scrollData.scrollPadding = keyboardHeight; - // var safeAreaEle: HTMLElement = (window).safeAreaEle; - // if (!safeAreaEle) { - // safeAreaEle = (window).safeAreaEle = document.createElement('div'); - // safeAreaEle.style.cssText = 'position:absolute; padding:1px 5px; left:0; right:0; font-weight:bold; font-size:10px; font-family:Courier; text-align:right; background:rgba(0, 128, 0, 0.8); text-shadow:1px 1px white; pointer-events:none;'; - // document.body.appendChild(safeAreaEle); - // } - // safeAreaEle.style.top = safeAreaTop + 'px'; - // safeAreaEle.style.height = safeAreaHeight + 'px'; - // safeAreaEle.innerHTML = ` - //
scrollTo: ${scrollData.scrollTo}
- //
scrollAmount: ${scrollData.scrollAmount}
- //
scrollPadding: ${scrollData.scrollPadding}
- //
inputSafeY: ${scrollData.inputSafeY}
- //
scrollHeight: ${scrollViewDimensions.scrollHeight}
- //
scrollTop: ${scrollViewDimensions.scrollTop}
- //
contentHeight: ${scrollViewDimensions.contentHeight}
- //
plaformHeight: ${plaformHeight}
- // `; + // calculate animation duration + const distance = Math.abs(scrollData.scrollAmount); + const duration = distance / SCROLL_ASSIST_SPEED; + scrollData.scrollDuration = Math.min(400, Math.max(150, duration)); return scrollData; } -function setControlCss(element: any, control: NgControl) { - element.setElementClass('ng-untouched', control.untouched); - element.setElementClass('ng-touched', control.touched); - element.setElementClass('ng-pristine', control.pristine); - element.setElementClass('ng-dirty', control.dirty); - element.setElementClass('ng-valid', control.valid); - element.setElementClass('ng-invalid', !control.valid); +function cloneInputComponent(plt: Platform, srcComponentEle: HTMLInputElement, srcNativeInputEle: HTMLInputElement) { + // Make sure we kill all the clones before creating new ones + // It is a defensive, removeClone() should do nothing + // removeClone(plt, srcComponentEle, srcNativeInputEle); + assert(srcComponentEle.parentElement.querySelector('.cloned-input') === null, 'leaked cloned input'); + // given a native or ' + From 9f86e10f46d87441de170e114bc876cf8157d57b Mon Sep 17 00:00:00 2001 From: Manuel Mtz-Almeida Date: Tue, 13 Jun 2017 14:18:03 +0200 Subject: [PATCH 15/72] fix(input): better handling of attributes --- src/components/input/input.ts | 44 ++++---------- .../test/attributes/app/app.component.ts | 10 ++++ .../input/test/attributes/app/app.module.ts | 19 ++++++ .../input/test/attributes/app/main.ts | 5 ++ src/components/input/test/attributes/e2e.ts | 0 .../attributes/pages/root-page/root-page.html | 39 ++++++++++++ .../pages/root-page/root-page.module.ts | 14 +++++ .../attributes/pages/root-page/root-page.ts | 59 +++++++++++++++++++ src/components/item/item.ts | 3 +- src/util/base-input.ts | 3 +- src/util/dom.ts | 6 +- 11 files changed, 164 insertions(+), 38 deletions(-) create mode 100644 src/components/input/test/attributes/app/app.component.ts create mode 100644 src/components/input/test/attributes/app/app.module.ts create mode 100644 src/components/input/test/attributes/app/main.ts create mode 100644 src/components/input/test/attributes/e2e.ts create mode 100644 src/components/input/test/attributes/pages/root-page/root-page.html create mode 100644 src/components/input/test/attributes/pages/root-page/root-page.module.ts create mode 100644 src/components/input/test/attributes/pages/root-page/root-page.ts diff --git a/src/components/input/input.ts b/src/components/input/input.ts index ca6f3e92143..36bad87447c 100644 --- a/src/components/input/input.ts +++ b/src/components/input/input.ts @@ -10,7 +10,7 @@ import 'rxjs/add/operator/takeUntil'; import { App } from '../app/app'; import { Config } from '../../config/config'; import { Content, ContentDimensions } from '../content/content'; -import { hasPointerMoved, pointerCoord } from '../../util/dom'; +import { copyInputAttributes, hasPointerMoved, pointerCoord } from '../../util/dom'; import { DomController } from '../../platform/dom-controller'; import { Form, IonicFormInput } from '../../util/form'; import { BaseInput } from '../../util/base-input'; @@ -93,15 +93,12 @@ import { Platform } from '../../platform/platform'; '(focus)="onFocus($event)" ' + '(keydown)="onKeydown($event)" ' + '[type]="_type" ' + - '[attr.name]="name" ' + + '[attr.aria-labelledby]="_labelId" ' + '[attr.min]="min" ' + '[attr.max]="max" ' + '[attr.step]="step" ' + - '[attr.maxlength]="maxlength" ' + '[attr.autocomplete]="autocomplete" ' + '[attr.autocorrect]="autocorrect" ' + - '[attr.spellcheck]="spellcheck" ' + - '[attr.autocapitalize]="autocapitalize" ' + '[placeholder]="placeholder" ' + '[disabled]="_disabled" ' + '[readonly]="_readonly">' + @@ -111,12 +108,9 @@ import { Platform } from '../../platform/platform'; '(blur)="onBlur($event)" ' + '(focus)="onFocus($event)" ' + '(keydown)="onKeydown($event)" ' + - '[attr.name]="name" ' + - '[attr.maxlength]="maxlength" ' + + '[attr.aria-labelledby]="_labelId" ' + '[attr.autocomplete]="autocomplete" ' + '[attr.autocorrect]="autocorrect" ' + - '[attr.spellcheck]="spellcheck" ' + - '[attr.autocapitalize]="autocapitalize" ' + '[placeholder]="placeholder" ' + '[disabled]="_disabled" ' + '[readonly]="_readonly">' + @@ -134,7 +128,8 @@ import { Platform } from '../../platform/platform'; '(mouseup)="_pointerEnd($event)">', encapsulation: ViewEncapsulation.None, - changeDetection: ChangeDetectionStrategy.OnPush + changeDetection: ChangeDetectionStrategy.OnPush, + inputs: ['value'] }) export class TextInput extends BaseInput implements IonicFormInput { @@ -216,28 +211,11 @@ export class TextInput extends BaseInput implements IonicFormInput { */ @Input() autocorrect: string = ''; - /** - * @input {string} Specifies whether the element is to have its spelling - * and grammar checked or not. - */ - @Input() spellcheck: string = null; - - /** - * @input {string} controls whether and how the text value for textual form control descendants should be automatically capitalized as it is entered/edited by the user. - */ - @Input() autocapitalize: string = null; - /** * @input {string} Instructional text that shows before the input has a value. */ @Input() placeholder: string = ''; - /** - * @input {string} The name attribute is used to reference elements in a JavaScript, - * or to reference form data after a form is submitted. - */ - @Input() name: string = null; - /** * @input {any} The minimum value, which must not be greater than its maximum (max attribute) value. */ @@ -253,11 +231,6 @@ export class TextInput extends BaseInput implements IonicFormInput { */ @Input() step: number | string = null; - /** - * @input {any} Specifies the maximum number of characters allowed in the element. - */ - @Input() maxlength: number | string = null; - /** * @hidden */ @@ -331,10 +304,13 @@ export class TextInput extends BaseInput implements IonicFormInput { this.clearOnEdit = true; } const ionInputEle: HTMLElement = this._elementRef.nativeElement; + const nativeInputEle: HTMLElement = this._native.nativeElement; + + // Copy remaining attributes, not handled by ionic/angular + copyInputAttributes(ionInputEle, nativeInputEle); if (ionInputEle.hasAttribute('autofocus')) { // the ion-input element has the autofocus attributes - const nativeInputEle: HTMLElement = this._native.nativeElement; ionInputEle.removeAttribute('autofocus'); switch (this._autoFocusAssist) { case 'immediate': @@ -351,6 +327,8 @@ export class TextInput extends BaseInput implements IonicFormInput { // traditionally iOS has big issues with autofocus on actual devices // autoFocus is disabled by default with the iOS mode config } + + // Initialize the input (can start emitting events) this._initialize(); if (this.focus.observers.length > 0) { diff --git a/src/components/input/test/attributes/app/app.component.ts b/src/components/input/test/attributes/app/app.component.ts new file mode 100644 index 00000000000..dcf2f9e7e73 --- /dev/null +++ b/src/components/input/test/attributes/app/app.component.ts @@ -0,0 +1,10 @@ +import { Component } from '@angular/core'; + +import { RootPage } from '../pages/root-page/root-page'; + +@Component({ + template: '' +}) +export class AppComponent { + root = RootPage; +} diff --git a/src/components/input/test/attributes/app/app.module.ts b/src/components/input/test/attributes/app/app.module.ts new file mode 100644 index 00000000000..7f76975a263 --- /dev/null +++ b/src/components/input/test/attributes/app/app.module.ts @@ -0,0 +1,19 @@ +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; +import { IonicApp, IonicModule } from '../../../../..'; + +import { AppComponent } from './app.component'; +import { RootPageModule } from '../pages/root-page/root-page.module'; + +@NgModule({ + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + IonicModule.forRoot(AppComponent), + RootPageModule + ], + bootstrap: [IonicApp] +}) +export class AppModule {} diff --git a/src/components/input/test/attributes/app/main.ts b/src/components/input/test/attributes/app/main.ts new file mode 100644 index 00000000000..6af7a5b2ae8 --- /dev/null +++ b/src/components/input/test/attributes/app/main.ts @@ -0,0 +1,5 @@ +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app.module'; + +platformBrowserDynamic().bootstrapModule(AppModule); diff --git a/src/components/input/test/attributes/e2e.ts b/src/components/input/test/attributes/e2e.ts new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/components/input/test/attributes/pages/root-page/root-page.html b/src/components/input/test/attributes/pages/root-page/root-page.html new file mode 100644 index 00000000000..c7e03960992 --- /dev/null +++ b/src/components/input/test/attributes/pages/root-page/root-page.html @@ -0,0 +1,39 @@ + + + + Input attributes + + + + + + + + Stacked + + + + + Test passed + Test FAILED + + + + + diff --git a/src/components/input/test/attributes/pages/root-page/root-page.module.ts b/src/components/input/test/attributes/pages/root-page/root-page.module.ts new file mode 100644 index 00000000000..85fba79181b --- /dev/null +++ b/src/components/input/test/attributes/pages/root-page/root-page.module.ts @@ -0,0 +1,14 @@ +import { NgModule } from '@angular/core'; +import { IonicPageModule } from '../../../../../..'; + +import { RootPage } from './root-page'; + +@NgModule({ + declarations: [ + RootPage, + ], + imports: [ + IonicPageModule.forChild(RootPage) + ] +}) +export class RootPageModule {} diff --git a/src/components/input/test/attributes/pages/root-page/root-page.ts b/src/components/input/test/attributes/pages/root-page/root-page.ts new file mode 100644 index 00000000000..d33987eb606 --- /dev/null +++ b/src/components/input/test/attributes/pages/root-page/root-page.ts @@ -0,0 +1,59 @@ +import { Component, ViewChild } from '@angular/core'; +import { TextInput } from '../../../../../../'; + +@Component({ + templateUrl: 'root-page.html' +}) +export class RootPage { + + input1Valid: boolean; + input2Valid: boolean; + + @ViewChild('input1') input1: TextInput; + + ionViewDidEnter() { + this.input1Valid = this.checkInput1(); + } + + checkInput1(): boolean { + const nativeEle = this.input1._native.nativeElement; + + return testAttributes(nativeEle, { + id: null, + type: 'number', + placeholder: 'Placeholder', + name: 'holaa', + min: '0', + max: '10000', + step: '2', + autocomplete: 'on', + autocorrect: 'on', + autocapitalize: 'on', + spellcheck: 'true', + maxLength: '4', + 'aria-labelledby': 'lbl-0', + readOnly: true, + disabled: true + }); + } +} + +function testAttributes(ele: HTMLElement, attributes: any): boolean { + for (let attr in attributes) { + const expected = attributes[attr]; + const value = (ele)[attr]; + + if (expected === null) { + if (ele.hasAttribute(attr) || value !== '') { + console.error(`Element should NOT have "${attr}"`); + return false; + } + } else { + if (expected !== value && expected !== ele.getAttribute(attr)) { + console.error(`Value "${attr}" does not match: ${expected} != ${value}`); + return false; + } + } + } + return true; +} diff --git a/src/components/item/item.ts b/src/components/item/item.ts index 8f5bb710635..87152f022e9 100644 --- a/src/components/item/item.ts +++ b/src/components/item/item.ts @@ -323,6 +323,7 @@ export class Item extends Ion { this._setName(elementRef); this._hasReorder = !!reorder; this.id = form.nextId().toString(); + this.labelId = 'lbl-' + this.id; // auto add "tappable" attribute to ion-item components that have a click listener if (!(renderer).orgListen) { @@ -391,7 +392,7 @@ export class Item extends Ion { set contentLabel(label: Label) { if (label) { this._label = label; - this.labelId = label.id = ('lbl-' + this.id); + label.id = this.labelId; if (label.type) { this.setElementClass('item-label-' + label.type, true); } diff --git a/src/util/base-input.ts b/src/util/base-input.ts index e648e52ee34..1094915a792 100644 --- a/src/util/base-input.ts +++ b/src/util/base-input.ts @@ -84,8 +84,9 @@ export class BaseInput extends Ion implements CommonInput { this._value = deepCopy(this._defaultValue); if (_item) { + assert('lbl-' + _item.id === _item.labelId, 'labelId was not calculated correctly'); this.id = name + '-' + _item.registerInput(name); - this._labelId = 'lbl-' + _item.id; + this._labelId = _item.labelId; this._item.setElementClass('item-' + name, true); } diff --git a/src/util/dom.ts b/src/util/dom.ts index 37412e37cae..62d742a905f 100644 --- a/src/util/dom.ts +++ b/src/util/dom.ts @@ -94,15 +94,15 @@ export function isTextInput(ele: any) { export const NON_TEXT_INPUT_REGEX = /^(radio|checkbox|range|file|submit|reset|color|image|button)$/i; -const skipInputAttrsReg = /^(value|checked|disabled|type|class|style|id|autofocus|autocomplete|autocorrect)$/i; +const SKIP_INPUT_ATTR = ['value', 'checked', 'disabled', 'readonly', 'placeholder', 'type', 'class', 'style', 'id', 'autofocus', 'autocomplete', 'autocorrect']; export function copyInputAttributes(srcElement: HTMLElement, destElement: HTMLElement) { // copy attributes from one element to another // however, skip over a few of them as they're already // handled in the angular world - var attrs = srcElement.attributes; + const attrs = srcElement.attributes; for (var i = 0; i < attrs.length; i++) { var attr = attrs[i]; - if (!skipInputAttrsReg.test(attr.name)) { + if (SKIP_INPUT_ATTR.indexOf(attr.name) === -1) { destElement.setAttribute(attr.name, attr.value); } } From dc958c3e2cc3726339591a91cbe64e90c82d0238 Mon Sep 17 00:00:00 2001 From: Manuel Mtz-Almeida Date: Tue, 13 Jun 2017 17:26:04 +0200 Subject: [PATCH 16/72] fix(textarea): apply classes properly --- src/components/input/input.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/input/input.ts b/src/components/input/input.ts index 36bad87447c..39c822bf5e8 100644 --- a/src/components/input/input.ts +++ b/src/components/input/input.ts @@ -104,6 +104,7 @@ import { Platform } from '../../platform/platform'; '[readonly]="_readonly">' + '