2017-11-29 14:36:29 +01: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.
|
|
|
|
|
2017-11-30 16:27:10 +01:00
|
|
|
import { Component, Input, OnInit, AfterViewInit, ElementRef } from '@angular/core';
|
2017-11-29 14:36:29 +01:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
|
|
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
2017-11-29 14:36:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive to add a red asterisk for required input fields.
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* For forms with required and not required fields, it is recommended to use this directive to mark the required ones.
|
|
|
|
*
|
|
|
|
* This directive should be applied in the label. Example:
|
|
|
|
*
|
2017-12-08 15:53:27 +01:00
|
|
|
* <ion-label core-mark-required="{{field.required}}">{{ 'core.login.username' | translate }}</ion-label>
|
2017-11-29 14:36:29 +01:00
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: '[core-mark-required]',
|
2018-06-12 08:41:17 +02:00
|
|
|
templateUrl: 'core-mark-required.html'
|
2017-11-29 14:36:29 +01:00
|
|
|
})
|
2017-11-30 16:27:10 +01:00
|
|
|
export class CoreMarkRequiredComponent implements OnInit, AfterViewInit {
|
2018-01-29 10:05:20 +01:00
|
|
|
@Input('core-mark-required') coreMarkRequired: boolean | string = true;
|
2017-11-29 14:36:29 +01:00
|
|
|
protected element: HTMLElement;
|
|
|
|
requiredLabel: string;
|
|
|
|
|
2017-11-30 16:27:10 +01:00
|
|
|
constructor(element: ElementRef, private translate: TranslateService, private textUtils: CoreTextUtilsProvider,
|
|
|
|
private utils: CoreUtilsProvider) {
|
2017-11-29 14:36:29 +01:00
|
|
|
this.element = element.nativeElement;
|
2017-12-08 15:53:27 +01:00
|
|
|
this.requiredLabel = this.translate.instant('core.required');
|
2017-11-29 14:36:29 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 16:27:10 +01:00
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
ngOnInit(): void {
|
2017-11-30 16:27:10 +01:00
|
|
|
this.coreMarkRequired = this.utils.isTrueOrOne(this.coreMarkRequired);
|
|
|
|
}
|
|
|
|
|
2017-11-29 14:36:29 +01:00
|
|
|
/**
|
|
|
|
* Called after the view is initialized.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
ngAfterViewInit(): void {
|
2017-11-29 14:36:29 +01:00
|
|
|
if (this.coreMarkRequired) {
|
|
|
|
// Add the "required" to the aria-label.
|
|
|
|
const ariaLabel = this.element.getAttribute('aria-label') || this.textUtils.cleanTags(this.element.innerHTML, true);
|
|
|
|
if (ariaLabel) {
|
|
|
|
this.element.setAttribute('aria-label', ariaLabel + ' ' + this.requiredLabel);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove the "required" from the aria-label.
|
|
|
|
const ariaLabel = this.element.getAttribute('aria-label');
|
|
|
|
if (ariaLabel) {
|
|
|
|
this.element.setAttribute('aria-label', ariaLabel.replace(' ' + this.requiredLabel, ''));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|