xsnippet logotype

xsnippet

#362693, Untitled [ Text only ]

by Guest
  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
import { DynamicFormControlModel } from '../../ng2-dynamic-forms/core/src/model/dynamic-form-control.model';
import { DynamicFormComponent } from '../dynamic-form.component';
import { DynamicFormGroupModel } from '../../ng2-dynamic-forms/core/src/model/form-group/dynamic-form-group.model';
import {
    DYNAMIC_FORM_CONTROL_INPUT_NAME_CALLER, DYNAMIC_FORM_CONTROL_INPUT_NAME_NOTES, DYNAMIC_FORM_CONTROL_INPUT_TYPE_ADDRESS,
    DYNAMIC_FORM_CONTROL_INPUT_TYPE_DATETIME,
    DynamicInputModel
} from '../../ng2-dynamic-forms/core/src/model/input/dynamic-input.model';
import { DynamicTextAreaModel } from '../../ng2-dynamic-forms/core/src/model/textarea/dynamic-textarea.model';

export interface IDefaultValueCommand {
    execute(): void;
}

export interface IDefaultValueCommandFactoryMethod {
    generate(model: DynamicFormControlModel): IDefaultValueCommand;
}

export class DynamicFormControlDefaultValueCommand implements IDefaultValueCommand, IDefaultValueCommandFactoryMethod {
    constructor(protected dynamicFormComponent: DynamicFormComponent) {
    }

    public execute(): void {
    }

    public generate(model: DynamicFormControlModel): IDefaultValueCommand {
        if (model instanceof DynamicFormGroupModel) {
            return new DynamicFormGroupCommand(this.dynamicFormComponent).generate(model);
        } else if (model instanceof DynamicInputModel) {
            return new DynamicFormInputCommand(this.dynamicFormComponent).generate(model);
        } else if (model instanceof DynamicTextAreaModel) {
            return new DynamicFormTextareaCommand(this.dynamicFormComponent).generate(model);
        }

        return this;
    }
}

export class DynamicFormGroupCommand extends DynamicFormControlDefaultValueCommand {
    private inputModel: DynamicFormGroupModel;

    public execute(): void {
        this.dynamicFormComponent.fillDefaultValues(this.inputModel.group);
    }

    public generate(model: DynamicFormGroupModel): IDefaultValueCommand {
        this.inputModel = model;
        return this;
    }
}

export class DynamicFormInputCommand extends DynamicFormControlDefaultValueCommand {

    public execute(): void {
    }

    public generate(inputModel: DynamicInputModel): IDefaultValueCommand {
        if (inputModel.inputType === DYNAMIC_FORM_CONTROL_INPUT_TYPE_DATETIME) {
            return new DynamicFormDateTimeCommand(this.dynamicFormComponent, inputModel);
        } else if (inputModel.inputType === DYNAMIC_FORM_CONTROL_INPUT_TYPE_ADDRESS) {
            return new DynamicFormAddressCommand(this.dynamicFormComponent, inputModel);
        } else if (inputModel.name === DYNAMIC_FORM_CONTROL_INPUT_NAME_CALLER) {
            return new DynamicFormCallerCommand(this.dynamicFormComponent, inputModel);
        }

        return this;
    }
}

export class DynamicFormTextareaCommand extends DynamicFormControlDefaultValueCommand {

    public execute(): void {
    }

    public generate(model: DynamicTextAreaModel): IDefaultValueCommand {
        if (model.name === DYNAMIC_FORM_CONTROL_INPUT_NAME_NOTES) {
            return new NotesTextAreaCommand(this.dynamicFormComponent, model);
        }

        return this;
    }
}

export class DynamicFormDateTimeCommand extends DynamicFormControlDefaultValueCommand {
    constructor(protected dynamicFormComponent: DynamicFormComponent,
                private inputModel: DynamicInputModel) {
        super(dynamicFormComponent);
    }

    private formatDate: string = 'MM/DD/YYYY';

    public execute(): void {
        const valueDate = this.inputModel.dateFromAgo ? moment().set('year', moment().year()
            - this.inputModel.dateFromAgo) : moment();

        this.inputModel.valueUpdates.next(valueDate.format(this.formatDate));
    }
}

export class DynamicFormAddressCommand extends DynamicFormControlDefaultValueCommand {
    constructor(protected dynamicFormComponent: DynamicFormComponent,
                private inputModel: DynamicInputModel) {
        super(dynamicFormComponent);
    }

    public execute(): void {
        this.dynamicFormComponent.googleLookupManager.setGoogleLookup(this.inputModel);
        this.inputModel.inputType = 'text';
    }
}

export class DynamicFormCallerCommand extends DynamicFormControlDefaultValueCommand {
    constructor(protected dynamicFormComponent: DynamicFormComponent,
                private inputModel: DynamicInputModel) {
        super(dynamicFormComponent);
    }

    public execute(): void {
        this.dynamicFormComponent.talkingToService.talkingToPerson.takeWhile(() => this.dynamicFormComponent.isDynamicFormAlive)
            .subscribe((data: string) => {
                this.inputModel.valueUpdates.next(data);
            });
    }
}

export class NotesTextAreaCommand extends DynamicFormControlDefaultValueCommand {
    constructor(protected dynamicFormComponent: DynamicFormComponent,
                private inputModel: DynamicTextAreaModel) {
        super(dynamicFormComponent);
    }

    public execute(): void {
        const talkingToRegex: RegExp = /^Talking To: .*;/g;
        this.dynamicFormComponent.talkingToService.talkingToPerson
            .takeWhile(() => this.dynamicFormComponent.isDynamicFormAlive)
            .subscribe((data: string) => {
                let value: string = this.inputModel.value as string || '';
                const talkingToString: string = `Talking To: ${data}; `;

                if (!data) {
                    value = value.replace(talkingToRegex, '');
                } else if (talkingToRegex.test(value)) {
                    value = value.replace(talkingToRegex, talkingToString);
                } else {
                    value = `${talkingToString}${value}`;
                }

                this.inputModel.valueUpdates.next(value.trim());
            });
    }

}