Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 16 | import { Component, OnInit, OnChanges, Input } from '@angular/core'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 17 | import { IconService, glyphMapping } from '../icon.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { LogService } from '../../log.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Icon Component |
| 22 | * |
| 23 | * Note: This is an alternative to the Icon Directive from ONOS 1.0.0 |
| 24 | * It has been implemented as a Component because it was inadvertently adding |
| 25 | * in a template through d3 DOM manipulations - it's better to make it a Comp |
Sean Condon | 7d27516 | 2018-11-02 16:29:06 +0000 | [diff] [blame] | 26 | * and build a template the Angular 7 way |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 27 | * |
| 28 | * Remember: The CSS files applied here only apply to this component |
| 29 | */ |
| 30 | @Component({ |
| 31 | selector: 'onos-icon', |
| 32 | templateUrl: './icon.component.html', |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 33 | styleUrls: [ |
| 34 | './icon.component.css', './icon.theme.css', |
| 35 | './glyph.css', './glyph-theme.css', |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 36 | './tooltip.css', './button-theme.css' |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 37 | ] |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 38 | }) |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 39 | export class IconComponent implements OnInit, OnChanges { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 40 | @Input() iconId: string; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 41 | @Input() iconSize: number = 20; |
| 42 | @Input() toolTip: string = undefined; |
| 43 | @Input() classes: string = undefined; |
| 44 | |
| 45 | // The displayed tooltip - undefined until mouse hovers over, then equals toolTip |
| 46 | toolTipDisp: string = undefined; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 47 | |
| 48 | constructor( |
| 49 | private is: IconService, |
| 50 | private log: LogService |
| 51 | ) { |
| 52 | // Note: iconId is not available until initialization |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 55 | /** |
| 56 | * Icons are loaded in to the DOM under iconDefs |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 57 | */ |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 58 | ngOnInit() { |
| 59 | this.is.loadIconDef(this.iconId); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 63 | * This is needed in case the iconId changes while icon component |
| 64 | * is displayed on screen. |
| 65 | */ |
| 66 | ngOnChanges() { |
| 67 | this.is.loadIconDef(this.iconId); |
| 68 | } |
| 69 | |
| 70 | /** |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 71 | * Get the corresponding iconTag from the glyphMapping in the iconService |
| 72 | * @returns The iconTag corresponding to the iconId of this instance |
| 73 | */ |
| 74 | iconTag(): string { |
Sean Condon | 59d3137 | 2019-02-02 20:07:00 +0000 | [diff] [blame] | 75 | const iconIdStr: string = glyphMapping.get(this.iconId); |
| 76 | if (iconIdStr) { |
| 77 | return '#' + iconIdStr; |
| 78 | } else { |
| 79 | return '#' + this.iconId; |
| 80 | } |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 81 | } |
| 82 | } |