]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/util/bool.component.ts
LP#1833080: have eg-bool recognize IDL bool string values
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / share / util / bool.component.ts
1 import {Component, Input} from '@angular/core';
2
3 /* Simple component to render a boolean value as human-friendly text */
4
5 @Component({
6     selector: 'eg-bool',
7     template: `
8       <ng-container>
9         <span *ngIf="value" class="badge badge-success" i18n>Yes</span>
10         <span *ngIf="value == false" class="badge badge-secondary" i18n>No</span>
11         <ng-container *ngIf="value === null">
12           <span *ngIf="ternary" class="badge badge-light" i18n>Unset</span>
13           <span *ngIf="!ternary"> </span>
14       </ng-container>`
15 })
16 export class BoolDisplayComponent {
17
18     _value: boolean;
19     @Input() set value(v: any) {
20         if (typeof v === 'string') {
21             if (v === 't') {
22                 this._value = true;
23             } else if (v === 'f') {
24                 this._value = false;
25             } else {
26                 this._value = null;
27             }
28         } else {
29             this._value = v;
30         }
31     }
32     get value(): any {
33         return this._value;
34     }
35
36     // If true, a null value displays as unset.
37     // If false, a null value displays as an empty string.
38     _ternary: boolean;
39     @Input() set ternary(t: boolean) {
40         this._ternary = t;
41     }
42     get ternary(): boolean {
43         return this._ternary;
44     }
45
46     constructor() {
47         this.value = null;
48     }
49 }
50