2020-10-28 14:59:36 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-04-27 10:09:12 +00:00
|
|
|
import { AfterViewInit, Directive, ElementRef, Input, OnChanges, SimpleChange } from '@angular/core';
|
2020-10-28 14:59:36 +00:00
|
|
|
import { CoreLogger } from '@singletons/logger';
|
2020-11-24 08:31:11 +00:00
|
|
|
import { Http } from '@singletons';
|
2020-11-19 11:40:18 +00:00
|
|
|
import { CoreConstants } from '@/core/constants';
|
2020-10-28 14:59:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive to enable font-awesome 5 as ionicons.
|
|
|
|
* Check available icons at https://fontawesome.com/icons?d=gallery&m=free
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
2020-11-19 12:47:32 +00:00
|
|
|
* <ion-icon name="fas-icon">
|
2020-10-28 14:59:36 +00:00
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: 'ion-icon[name]',
|
|
|
|
})
|
2021-04-27 10:09:12 +00:00
|
|
|
export class CoreFaIconDirective implements AfterViewInit, OnChanges {
|
2020-10-28 14:59:36 +00:00
|
|
|
|
2022-11-29 10:38:16 +00:00
|
|
|
/**
|
|
|
|
* Object used to store whether icons exist or not during development.
|
|
|
|
*/
|
|
|
|
private static readonly DEV_ICONS_STATUS: Record<string, Promise<boolean>> = {};
|
|
|
|
|
2020-10-28 14:59:36 +00:00
|
|
|
@Input() name = '';
|
|
|
|
|
|
|
|
protected element: HTMLElement;
|
|
|
|
|
|
|
|
protected logger: CoreLogger;
|
|
|
|
|
|
|
|
constructor(el: ElementRef) {
|
|
|
|
this.element = el.nativeElement;
|
|
|
|
this.logger = CoreLogger.getInstance('CoreFaIconDirective');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect icon name and use svg.
|
|
|
|
*/
|
2020-11-05 15:17:31 +00:00
|
|
|
async setIcon(): Promise<void> {
|
2022-02-21 10:31:00 +00:00
|
|
|
let library = '';
|
2020-10-28 14:59:36 +00:00
|
|
|
let iconName = this.name;
|
2022-02-21 10:31:00 +00:00
|
|
|
let font = 'ionicons';
|
2020-10-28 14:59:36 +00:00
|
|
|
const parts = iconName.split('-', 2);
|
2022-11-30 10:36:56 +00:00
|
|
|
if (parts.length === 2) {
|
2020-10-28 14:59:36 +00:00
|
|
|
switch (parts[0]) {
|
|
|
|
case 'far':
|
|
|
|
library = 'regular';
|
2022-02-21 10:31:00 +00:00
|
|
|
font = 'font-awesome';
|
2020-10-28 14:59:36 +00:00
|
|
|
break;
|
|
|
|
case 'fa':
|
|
|
|
case 'fas':
|
|
|
|
library = 'solid';
|
2022-02-21 10:31:00 +00:00
|
|
|
font = 'font-awesome';
|
2020-10-28 14:59:36 +00:00
|
|
|
break;
|
|
|
|
case 'fab':
|
|
|
|
library = 'brands';
|
2022-02-21 10:31:00 +00:00
|
|
|
font = 'font-awesome';
|
|
|
|
break;
|
|
|
|
case 'moodle':
|
|
|
|
library = 'moodle';
|
|
|
|
font = 'moodle';
|
|
|
|
break;
|
|
|
|
case 'fam':
|
|
|
|
library = 'font-awesome';
|
|
|
|
font = 'moodle';
|
2020-10-28 14:59:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:36:56 +00:00
|
|
|
if (font === 'ionicons') {
|
2020-10-28 14:59:36 +00:00
|
|
|
this.element.removeAttribute('src');
|
2021-04-27 10:09:12 +00:00
|
|
|
this.logger.warn(`Ionic icon ${this.name} detected`);
|
2022-02-21 10:31:00 +00:00
|
|
|
|
|
|
|
return;
|
2020-10-28 14:59:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 10:31:00 +00:00
|
|
|
iconName = iconName.substring(parts[0].length + 1);
|
|
|
|
|
|
|
|
const src = `assets/fonts/${font}/${library}/${iconName}.svg`;
|
|
|
|
this.element.setAttribute('src', src);
|
|
|
|
this.element.classList.add('faicon');
|
2022-11-29 10:38:16 +00:00
|
|
|
this.validateIcon(this.name, src);
|
2020-10-28 14:59:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 10:09:12 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ngAfterViewInit(): void {
|
|
|
|
if (!this.element.getAttribute('aria-label') &&
|
|
|
|
!this.element.getAttribute('aria-labelledby') &&
|
2022-11-30 10:36:56 +00:00
|
|
|
this.element.getAttribute('aria-hidden') !== 'true') {
|
2021-04-27 10:09:12 +00:00
|
|
|
this.logger.warn('Aria label not set on icon ' + this.name, this.element);
|
|
|
|
|
|
|
|
this.element.setAttribute('aria-hidden', 'true');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:59:36 +00:00
|
|
|
/**
|
2022-11-30 10:36:56 +00:00
|
|
|
* @inheritdoc
|
2020-10-28 14:59:36 +00:00
|
|
|
*/
|
|
|
|
ngOnChanges(changes: { [name: string]: SimpleChange }): void {
|
|
|
|
if (!changes.name || !this.name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setIcon();
|
|
|
|
}
|
|
|
|
|
2022-11-29 10:38:16 +00:00
|
|
|
/**
|
|
|
|
* Validate that an icon exists, or show warning otherwise (only in development and testing environments).
|
|
|
|
*
|
|
|
|
* @param name Icon name.
|
|
|
|
* @param src Icon source url.
|
|
|
|
*/
|
|
|
|
private validateIcon(name: string, src: string): void {
|
|
|
|
if (!CoreConstants.BUILD.isDevelopment && !CoreConstants.BUILD.isTesting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(src in CoreFaIconDirective.DEV_ICONS_STATUS)) {
|
|
|
|
CoreFaIconDirective.DEV_ICONS_STATUS[src] = Http.get(src, { responseType: 'text' })
|
|
|
|
.toPromise()
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line promise/catch-or-return
|
|
|
|
CoreFaIconDirective.DEV_ICONS_STATUS[src].then(exists => {
|
|
|
|
if (exists) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.logger.error(`Icon ${name} not found`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:59:36 +00:00
|
|
|
}
|