-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput-switcher.directive.ts
174 lines (149 loc) · 6.23 KB
/
input-switcher.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { ComponentFactoryResolver, Directive, ElementRef, Input, Renderer, Output, EventEmitter, SimpleChange, ViewContainerRef, OnInit, OnDestroy, OnChanges } from '@angular/core';
import { NgbDatepicker, NgbInputDatepicker } from '@ng-bootstrap/ng-bootstrap';
import { DatepickerComponent } from '../components/datepicker/datepicker.component';
import { Observable } from 'rxjs/Rx';
import * as moment from 'moment';
import * as _ from 'lodash';
@Directive({
selector: '[safartiInputSwitcher]',
exportAs: 'InputSwitcherDirective'
})
export class InputSwitcherDirective implements OnChanges {
@Input() edit: boolean;
@Input() type = 'text';
@Input() subsetKey: string;
@Input() id = '';
@Output() valueOutput: EventEmitter<any> = new EventEmitter();
private nativeElement: Node;
private previousValue: string;
private currentValue: string;
private elementName: string;
private model;
private component;
constructor(public el: ElementRef, public renderer: Renderer, private vc: ViewContainerRef, private resolver: ComponentFactoryResolver) {
this.previousValue = el.nativeElement.textContent;
}
handleChange(value, firstChange) {
// Ensure this isn't the first time the variable was set
if (!firstChange) {
if (value === 'true') {
this.editMode();
}
else if (value === 'saved') {
this.newValues();
}
else {
this.el.nativeElement.textContent = this.previousValue;
}
}
}
ngOnChanges(changes: { [propName: string]: SimpleChange }) {
// if the datepicker is on, will remove the datepicker when edit is closed ( or else the datepicker just hangs around after the edit is closed )
if (this.component) {
this.component.destroy();
}
this.handleChange(changes['edit'].currentValue, changes['edit'].isFirstChange());
}
editMode() {
// Store current value
this.currentValue = _.trim(this.el.nativeElement.textContent);
// Set previousValue to current value
this.previousValue = this.currentValue;
// Wipe out text node
this.el.nativeElement.textContent = '';
//Create and render element
switch (this.type) {
case 'text':
this.generateElement('input', { 'value': this.currentValue, 'name': this.el.nativeElement.getAttribute('name'), 'type': 'text' });
break;
case 'ta':
this.generateTextarea({ 'value': this.currentValue, 'name': this.el.nativeElement.getAttribute('name') });
break;
case 'datepicker':
this.generateDatepicker({ 'value': this.el.nativeElement.getAttribute('data-value'), 'name': this.el.nativeElement.getAttribute('name'), 'type': 'hidden', 'parent': this.el.nativeElement });
break;
case 'select':
this.generateSelectBox({ 'name': this.el.nativeElement.getAttribute('name'), 'ele': this.el });
break;
default:
this.generateElement('input', { 'value': this.currentValue, 'name': this.el.nativeElement.getAttribute('name'), 'type': 'text' });
break;
}
}
generateElement(type, attrs) {
let inputElement = this.renderer.createElement(this.el.nativeElement, type);
this.renderer.setElementAttribute(inputElement, 'type', 'text');
this.renderer.setElementAttribute(inputElement, 'value', _.trim(attrs.value));
this.renderer.setElementAttribute(inputElement, 'name', attrs.name);
this.elementName = attrs.name;
this.renderer.listen(inputElement, 'change', (event) => {
this.currentValue = event.target.value;
this.valueOutput.emit(this.currentValue);
});
}
generateTextarea(attrs) {
let ele = this.renderer.createElement(this.el.nativeElement, 'textarea');
this.renderer.createText(ele, _.trim(attrs.value));
this.renderer.setElementAttribute(ele, 'name', attrs.name);
this.renderer.setElementAttribute(ele, 'rows', '6');
this.elementName = attrs.name;
this.renderer.listen(ele, 'change', (event) => {
this.currentValue = event.target.value;
this.valueOutput.emit(this.currentValue)
});
}
// add attrs.values
generateSelectBox(attrs) {
const name = attrs.name;
const rend = this.renderer;
const curValue = this.currentValue;
const opts = attrs.ele.nativeElement.getAttribute('options');
const values = attrs.ele.nativeElement.getAttribute('values');
var arr = opts.split('/');
let options;
if (values) {
const valuesSplit = values.split('/');
options = _.zipWith(arr, valuesSplit, function(opt, val) {
return { option: opt, value: val };
});
} else {
options = opts.split('/');
}
const select = this.renderer.createElement(this.el.nativeElement, 'select');
this.renderer.setElementAttribute(select, 'name', attrs.name);
this.elementName = attrs.name;
_.forEach(options, function (value, key) {
const readableValue = typeof value === 'object' ? value.option : value;
const writableValue = typeof value === 'object' ? value.value : value;
const option = rend.createElement(select, 'option');
rend.setElementAttribute(option, 'value', writableValue);
rend.createText(option, readableValue);
if (writableValue == curValue) {
rend.setElementAttribute(option, 'selected', 'selected');
}
});
this.renderer.listen(select, 'change', (event) => {
this.currentValue = event.target.value;
this.valueOutput.emit(this.currentValue);
})
}
generateDatepicker(attrs) {
let datePicker = this.resolver.resolveComponentFactory(DatepickerComponent);
let initDate = moment(attrs.value, ["x", "yyyy-mm-dd", "mm/dd/yyyy"]);
this.component = this.vc.createComponent(datePicker);
this.component.changeDetectorRef.detectChanges();
// Initializing Datepicker component with the input value;
this.component.instance.model = { year: initDate.year(), month: initDate.month() + 1, day: initDate.date() };
this.elementName = attrs.name;
this.type = 'datepicker';
this.currentValue = attrs.value;
this.component.instance.change.subscribe(date => {
this.currentValue = date;
this.valueOutput.emit(this.currentValue);
});
}
newValues() {
// Taking value from input/datepicker/select and placing in on the page as text
this.el.nativeElement.textContent = this.currentValue;
}
}