2017-11-24 07:50:27 +00:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-02 14:25:00 +00:00
|
|
|
import { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core';
|
2017-12-12 14:28:01 +00:00
|
|
|
import { NavController } from 'ionic-angular';
|
2018-03-01 15:55:49 +00:00
|
|
|
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
|
|
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
2017-11-24 07:50:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive to auto focus an element when a view is loaded.
|
|
|
|
*
|
2017-12-29 17:05:52 +00:00
|
|
|
* You can apply it conditionallity assigning it a boolean value: <ion-input [core-auto-focus]="{{showKeyboard}}">
|
2017-11-24 07:50:27 +00:00
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: '[core-auto-focus]'
|
|
|
|
})
|
2017-12-12 14:28:01 +00:00
|
|
|
export class CoreAutoFocusDirective implements OnInit {
|
2018-01-29 09:05:20 +00:00
|
|
|
@Input('core-auto-focus') coreAutoFocus: boolean | string = true;
|
2017-11-24 07:50:27 +00:00
|
|
|
|
|
|
|
protected element: HTMLElement;
|
|
|
|
|
2017-12-12 14:28:01 +00:00
|
|
|
constructor(element: ElementRef, private domUtils: CoreDomUtilsProvider, private utils: CoreUtilsProvider,
|
2018-03-02 14:25:00 +00:00
|
|
|
@Optional() private navCtrl: NavController) {
|
2017-11-24 07:50:27 +00:00
|
|
|
this.element = element.nativeElement || element;
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:28:01 +00:00
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
ngOnInit(): void {
|
2017-12-12 14:28:01 +00:00
|
|
|
if (this.navCtrl.isTransitioning()) {
|
|
|
|
// Navigating to a new page. Wait for the transition to be over.
|
2018-01-29 09:05:20 +00:00
|
|
|
const subscription = this.navCtrl.viewDidEnter.subscribe(() => {
|
2017-12-12 14:28:01 +00:00
|
|
|
this.autoFocus();
|
|
|
|
subscription.unsubscribe();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.autoFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 07:50:27 +00:00
|
|
|
/**
|
|
|
|
* Function after the view is initialized.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected autoFocus(): void {
|
2017-11-30 15:27:10 +00:00
|
|
|
const autoFocus = this.utils.isTrueOrOne(this.coreAutoFocus);
|
2017-11-29 13:36:29 +00:00
|
|
|
if (autoFocus) {
|
2017-11-24 07:50:27 +00:00
|
|
|
// If it's a ion-input or ion-textarea, search the right input to use.
|
|
|
|
let element = this.element;
|
|
|
|
if (this.element.tagName == 'ION-INPUT') {
|
|
|
|
element = this.element.querySelector('input') || element;
|
|
|
|
} else if (this.element.tagName == 'ION-TEXTAREA') {
|
|
|
|
element = this.element.querySelector('textarea') || element;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a bit to make sure the view is loaded.
|
|
|
|
setTimeout(() => {
|
|
|
|
this.domUtils.focusElement(element);
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|