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
|
'use strict';
import {FormState, PROPERTY_TYPES, ValidationUtils, PROPERTY_VALUE_CONSTRAINTS} from "app/utils";
import {InputModel} from "app/models";
export interface IInputEditModel {
editInput:InputModel;
}
export interface IInputFormViewModelScope extends ng.IScope {
forms:any;
editForm:ng.IFormController;
footerButtons:Array<any>;
isService:boolean;
modalInstanceInput:ng.ui.bootstrap.IModalServiceInstance;
isLoading:boolean;
inputEditModel:IInputEditModel;
myValue:any;
maxLength:number;
save():void;
close():void;
validateIntRange(value:string):boolean;
validateJson(json:string):boolean;
getValidationPattern(type:string):RegExp;
showSchema():boolean;
}
export class InputFormViewModel {
static '$inject' = [
'$scope',
'$uibModalInstance',
'ValidationUtils',
'input'
];
private formState:FormState;
constructor(private $scope:IInputFormViewModelScope,
private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
private ValidationUtils:ValidationUtils,
private input:InputModel) {
this.initScope();
this.initMyValue();
}
private initMyValue = ():void => {
switch (this.$scope.inputEditModel.editInput.type) {
case PROPERTY_TYPES.MAP:
this.$scope.myValue = this.$scope.inputEditModel.editInput.defaultValue ? JSON.parse(this.$scope.inputEditModel.editInput.defaultValue) : {'': null};
break;
case PROPERTY_TYPES.LIST:
this.$scope.myValue = this.$scope.inputEditModel.editInput.defaultValue ? JSON.parse(this.$scope.inputEditModel.editInput.defaultValue) : [];
break;
}
};
private initDefaultValueMaxLength = ():void => {
switch (this.$scope.inputEditModel.editInput.type) {
case PROPERTY_TYPES.MAP:
case PROPERTY_TYPES.LIST:
this.$scope.maxLength = this.$scope.inputEditModel.editInput.schema.property.type == PROPERTY_TYPES.JSON ?
PROPERTY_VALUE_CONSTRAINTS.JSON_MAX_LENGTH :
PROPERTY_VALUE_CONSTRAINTS.MAX_LENGTH;
break;
case PROPERTY_TYPES.JSON:
this.$scope.maxLength = PROPERTY_VALUE_CONSTRAINTS.JSON_MAX_LENGTH;
break;
default:
this.$scope.maxLength = PROPERTY_VALUE_CONSTRAINTS.MAX_LENGTH;
}
};
private initScope = ():void => {
this.$scope.forms = {};
this.$scope.modalInstanceInput = this.$uibModalInstance;
this.$scope.inputEditModel = {
editInput: null
};
this.$scope.inputEditModel.editInput = this.input;
this.initDefaultValueMaxLength();
//scope methods
this.$scope.save = ():void => {
if (this.$scope.showSchema()) {
this.$scope.inputEditModel.editInput.defaultValue = JSON.stringify(this.$scope.myValue);
}
};
this.$scope.close = ():void => {
this.$uibModalInstance.close();
};
this.$scope.validateIntRange = (value:string):boolean => {
return !value || this.ValidationUtils.validateIntRange(value);
};
this.$scope.validateJson = (json:string):boolean => {
if (!json) {
return true;
}
return this.ValidationUtils.validateJson(json);
};
this.$scope.showSchema = ():boolean => {
return ['list', 'map'].indexOf(this.$scope.inputEditModel.editInput.type) > -1;
};
this.$scope.getValidationPattern = (type:string):RegExp => {
return this.ValidationUtils.getValidationPattern(type);
};
// Add the done button at the footer.
this.$scope.footerButtons = [
{'name': 'Done', 'css': 'blue', 'callback': this.$scope.save},
{'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
];
this.$scope.$watchCollection("forms.editForm.$invalid", (newVal, oldVal) => {
this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
});
};
}
|