import {DateRangeSelectComponent} from '@eg/share/daterange-select/daterange-select.component';
import {DateTimeSelectComponent} from '@eg/share/datetime-select/datetime-select.component';
import {ContextMenuModule} from '@eg/share/context-menu/context-menu.module';
+import {FileReaderComponent} from '@eg/share/file-reader/file-reader.component';
@NgModule({
DateSelectComponent,
OrgSelectComponent,
DateRangeSelectComponent,
- DateTimeSelectComponent
+ DateTimeSelectComponent,
+ FileReaderComponent,
],
imports: [
CommonModule,
OrgSelectComponent,
DateRangeSelectComponent,
DateTimeSelectComponent,
- ContextMenuModule
- ]
+ ContextMenuModule,
+ FileReaderComponent,
+ ],
})
export class CommonWidgetsModule { }
--- /dev/null
+/**
+ * <eg-file-reader [(ngModel)]="fileContents">
+ * </eg-file-reader>
+ */
+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';
+import {map, tap, reduce, mergeMap, mapTo, debounceTime, distinctUntilChanged, merge, filter} from 'rxjs/operators';
+
+@Component({
+ selector: 'eg-file-reader',
+ templateUrl: './file-reader.component.html',
+ providers: [{
+ provide: NG_VALUE_ACCESSOR,
+ useExisting: forwardRef(() => FileReaderComponent),
+ multi: true
+ }]
+})
+export class FileReaderComponent implements ControlValueAccessor, OnInit {
+
+ // Stub functions required by ControlValueAccessor
+ propagateChange = (_: any) => {};
+ propagateTouch = () => {};
+
+ ngOnInit() {
+ }
+
+ changeListener($event): void {
+ const me = this;
+ if ($event.target.files.length < 1) {
+ return;
+ }
+ const reader = new FileReader();
+ reader.onloadend = function(e) {
+ me.propagateChange(me.parseFileContents(reader.result));
+ };
+ reader.readAsText($event.target.files[0]);
+ }
+
+ parseFileContents(contents): Array<string> {
+ const values = contents.split('\n');
+ values.forEach((val, idx) => {
+ val = val.replace(/\s+$/, '');
+ val = val.replace(/^\s+/, '');
+ values[idx] = val;
+ });
+ return values;
+ }
+
+ writeValue(value: any) {
+ // intentionally empty
+ }
+
+ registerOnChange(fn) {
+ this.propagateChange = fn;
+ }
+
+ registerOnTouched(fn) {
+ this.propagateTouch = fn;
+ }
+}
<div>
+ <h4>File reader component</h4>
+ <eg-file-reader [(ngModel)]="fileContents"></eg-file-reader>
+ <h5>Contents are:</h5>
+ <ol *ngIf="fileContents && fileContents.length > 0">
+ <li *ngFor="let val of fileContents">{{val}}</li>
+ </ol>
+</div>
+
+<div>
<h4>Cross-tab communications example</h4>
<p>To test, open this sandbox in a second browser tab. Enter something in the input box below, then switch to the other tab and click anywhere on the page.
You should see the message that you sent to the other browser tab.</p>