]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/translate/translate.component.ts
LP1823041 Angular dialogs return observables
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / translate / translate.component.ts
1 import {Component, OnInit, Input, Renderer2} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {ToastService} from '@eg/share/toast/toast.service';
4 import {LocaleService} from '@eg/core/locale.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {DialogComponent} from '@eg/share/dialog/dialog.component';
8 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
9
10 @Component({
11   selector: 'eg-translate',
12   templateUrl: 'translate.component.html'
13 })
14
15 export class TranslateComponent
16     extends DialogComponent implements OnInit {
17
18     idlClassDef: any;
19     locales: IdlObject[];
20     selectedLocale: string;
21     translatedValue: string;
22     existingTranslation: IdlObject;
23
24     // These actions should update the idlObject and/or fieldName values,
25     // forcing the dialog to load a new string to translate.  When set,
26     // applying a translation in the dialog will leave the dialog window open
27     // so the next/prev buttons can be used to fetch the next string.
28     nextString: () => void;
29     prevString: () => void;
30
31     idlObj: IdlObject;
32     @Input() set idlObject(o: IdlObject) {
33         if (o) {
34             this.idlObj = o;
35             this.idlClassDef = this.idl.classes[o.classname];
36             this.fetchTranslation();
37         }
38     }
39
40     field: string;
41     @Input() set fieldName(n: string) {
42         this.field = n;
43     }
44
45     constructor(
46         private modal: NgbModal, // required for passing to parent
47         private renderer: Renderer2,
48         private idl: IdlService,
49         private toast: ToastService,
50         private locale: LocaleService,
51         private pcrud: PcrudService,
52         private auth: AuthService) {
53         super(modal);
54     }
55
56     ngOnInit() {
57         // Default to the login locale
58         this.selectedLocale = this.locale.currentLocaleCode();
59         this.locales = [];
60         this.locale.supportedLocales().subscribe(l => this.locales.push(l));
61
62         this.onOpen$.subscribe(() => {
63             const elm = this.renderer.selectRootElement('#translation-input');
64             if (elm) {
65                 elm.focus();
66                 elm.select();
67             }
68         });
69     }
70
71     localeChanged(code: string) {
72         this.fetchTranslation();
73     }
74
75     fetchTranslation() {
76         const exist = this.existingTranslation;
77
78         if (exist
79             && exist.fq_field() === this.fqField()
80             && exist.identity_value() === this.identValue()) {
81             // Already have the current translation object.
82             return;
83         }
84
85         this.translatedValue = '';
86         this.existingTranslation = null;
87
88         this.pcrud.search('i18n', {
89             translation: this.selectedLocale,
90             fq_field : this.fqField(),
91             identity_value: this.identValue()
92         }).subscribe(tr => {
93             this.existingTranslation = tr;
94             this.translatedValue = tr.string();
95             console.debug('found existing translation ', tr);
96         });
97     }
98
99     fqField(): string {
100         return this.idlClassDef.classname + '.' + this.field;
101     }
102
103     identValue(): string {
104         return this.idlObj[this.idlClassDef.pkey || 'id']();
105     }
106
107     translate() {
108         if (!this.translatedValue) { return; }
109
110         let entry;
111
112         if (this.existingTranslation) {
113             entry = this.existingTranslation;
114             entry.string(this.translatedValue);
115
116             this.pcrud.update(entry).toPromise().then(
117                 ok => {
118                     if (!this.nextString) {
119                         this.close(this.translatedValue);
120                     }
121                 },
122                 err => console.error(err)
123             );
124
125             return;
126         }
127
128         entry = this.idl.create('i18n');
129         entry.fq_field(this.fqField());
130         entry.identity_value(this.identValue());
131         entry.translation(this.selectedLocale);
132         entry.string(this.translatedValue);
133
134         this.pcrud.create(entry).toPromise().then(
135             ok => {
136                 if (!this.nextString) {
137                     this.close(this.translatedValue);
138                 }
139             },
140             err => console.error('Translation creation failed')
141         );
142     }
143 }
144
145