* <!-- see also <eg-combobox-entry> -->
* </eg-combobox>
*/
-import {Component, OnInit, Input, Output, ViewChild,
+import {Component, OnInit, Input, Output, ViewChild,
TemplateRef, EventEmitter, ElementRef, forwardRef} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {Observable, of, Subject} from 'rxjs';
}
}
+ hasEntry(entryId: any): boolean {
+ return this.entrylist.filter(e => e.id === entryId)[0] !== undefined;
+ }
+
onBlur() {
// When the selected value is a string it means we have either
// no value (user cleared the input) or a free-text value.
}
getOwnerName(id: number): string {
- return this.org.get(this.templateCache[id].owner()).shortname();
+ if (this.templateCache[id]) {
+ return this.org.get(this.templateCache[id].owner()).shortname();
+ }
+ return '';
}
- selectTemplate(id: number) {
+ // If the selected template changes through means other than the
+ // template selecdtor, setting updateSelector=true will force the
+ // template to appear in the selector and get selected, regardless
+ // of whether it would have been fetched with current filters.
+ selectTemplate(id: number, updateSelector?: boolean) {
if (id === null) {
this.template = null;
}
this.pcrud.retrieve('cpt', id).subscribe(t => {
- this.template = t;
+ this.template = this.templateCache[id] = t;
+
+ if (updateSelector) {
+ if (!this.templateSelector.hasEntry(id)) {
+ this.templateSelector.addEntry({id: id, label: t.label()});
+ }
+ this.templateSelector.applyEntryId(id);
+ }
+
const data = this.sampleData[t.name()];
if (data) {
this.sampleJson = JSON.stringify(data, null, 2);
});
}
+ // Allow the template editor textarea to expand vertically as
+ // content is added, with a sane minimum row count
+ templateRowCount(): number {
+ const def = 25;
+ if (this.template && this.template.template()) {
+ return Math.max(def,
+ this.template.template().split(/\n/).length + 2);
+ }
+ return def;
+ }
+
refreshPreview() {
if (!this.sampleJson) { return; }
this.compiledContent = '';
}).then(response => {
this.compiledContent = response.content;
- if (response.contentType === 'text/html') {
- this.container().innerHTML = response.content;
- } else {
- // Assumes text/plain or similar
- this.container().innerHTML = '<pre>' + response.content + '</pre>';
+ if (this.container()) { // null if on alternate tab
+ if (response.contentType === 'text/html') {
+ this.container().innerHTML = response.content;
+ } else {
+ // Assumes text/plain or similar
+ this.container().innerHTML = '<pre>' + response.content + '</pre>';
+ }
}
});
}
cloneTemplate() {
const tmpl = this.idl.clone(this.template);
tmpl.id(null);
+ tmpl.active(false); // Cloning requires manual activation
+ tmpl.owner(null);
this.editDialog.setRecord(tmpl);
this.editDialog.mode = 'create';
this.editDialog.open({size: 'lg'}).toPromise().then(newTmpl => {
if (newTmpl !== undefined) {
this.setTemplateInfo().toPromise()
- .then(_ => this.selectTemplate(newTmpl.id()));
+ .then(_ => this.selectTemplate(newTmpl.id(), true));
}
});
}