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