/
rxForm.js
232 lines (225 loc) · 7.08 KB
/
rxForm.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
angular.module('encore.ui.rxForm', ['ngSanitize'])
// used to wrap input fields and select boxes in the proper HTML
.directive('rxFormItem', function () {
return {
restrict: 'E',
templateUrl: 'templates/rxFormItem.html',
transclude: true,
scope: {
label: '@',
suffix: '@',
prefix: '@',
description: '@'
}
};
})
/**
*
* @ngdoc directive
* @name encore.ui.rxForm:rxFormInput
* @restrict E
* @description
* This directive is used to create an <input> tag.
* @scope
* @param {String} type - Value passed to input's 'type' attribute
* @param {String} required - Value passed to input's 'ng-required' attribute
* @param {String} fieldId - Used for label and input 'id' attribute
* @param {Object} model - Value to bind input to using ng-model
* @param {String} minLength - Value passed to input's 'minLength' attribute
* @param {String} maxLength - Value passed to input's 'maxLength' attribute
* @param {String} max - Value passed to input's 'max' attribute
* @param {String} min - Value passed to input's 'min' attribute
* @param {String} label - Text to use for <label>
* @param {String} suffix - Text to use after <label>
* @param {String} description - Text to place after input
*/
.directive('rxFormInput', function () {
return {
restrict: 'E',
templateUrl: 'templates/rxFormInput.html',
scope: {
type: '@',
required: '@',
fieldId: '@',
model: '=',
minLength: '@',
maxLength: '@',
max: '@',
min: '@',
name: '@',
value: '@',
label: '@',
suffix: '@',
description: '@'
}
};
})
/**
*
* @ngdoc directive
* @name encore.ui.rxForm:rxFormRadio
* @restrict E
* @description
* This directive is used to create a set of <input type="radio"> tags.
* @scope
* @param {Array} options - An array of radio inputs to generate. Example format:
* ```
* [{
* label: 'Staging',
* value: 'STAGING'
* }, {
* label: 'ORD (Chicago)',
* value: 'ORD'
* }];
* ```
* @param {String} fieldId - Used for label and input 'id' attribute
* @param {Object} model - Value to bind input to using ng-model
*/
.directive('rxFormRadio', function () {
return {
restrict: 'E',
templateUrl: 'templates/rxFormRadio.html',
scope: {
options: '=',
fieldId: '@',
model: '='
}
};
})
/**
*
* @ngdoc directive
* @name encore.ui.rxForm:rxFormSelect
* @restrict E
* @description
* This directive is used to create a <select> box w/options.
* @scope
* @param {Array} options - An array of options to generate. Example format:
* ```
* [{
* label: 'Staging',
* value: 'STAGING'
* }, {
* label: 'ORD (Chicago)',
* value: 'ORD'
* }];
* ```
* @param {String} fieldId - Used for label and input 'id' attribute
* @param {String} label - Text to use for <label>
* @param {Object} model - Value to bind input to using ng-model
* @param {String} required - Value passed to select's 'ng-required' attribute
*/
.directive('rxFormSelect', function () {
return {
restrict: 'E',
templateUrl: 'templates/rxFormSelect.html',
scope: {
options: '=',
fieldId: '@',
label: '@',
model: '=',
required: '@'
}
};
})
/**
*
* @ngdoc directive
* @name encore.ui.rxForm:rxFormOptionTable
* @restrict E
* @description
* This directive is used to build a table of radio/checkbox inputs
* @scope
* @param {Array} data - Array of objects used to populate table. Properties must match columns data
* key. Example:
* ```
* [
* {
* 'name': 'Option 1',
* 'value': 0
* }, {
* 'name': 'Option 2',
* 'value': 1
* }, {
* 'name': 'Option 3',
* 'value': 2
* }
* ]
* ```
* @param {array} columns - Array of objects with label/key values. Example:
* ```
* [{
* 'label': 'Name',
* 'key': 'name'
* }]
* ```
* @param {String=} selected - Key of item that's selected (and therefore will have input disabled)
* @param {String} type - Type of input to be used
* @param {Object} model - Value to bind input to using ng-model
* @param {String} fieldId - Used for label and input 'id' attribute
* @param {String} required - Value passed to input's 'ng-required' attribute
*/
.directive('rxFormOptionTable', function ($interpolate) {
return {
restrict: 'E',
templateUrl: 'templates/rxFormOptionTable.html',
scope: {
data: '=',
columns: '=',
selected: '@',
type: '@',
model: '=',
fieldId: '@',
required: '@'
},
controller: function ($scope) {
var determineMatch = function (val1, val2) {
if (_.isUndefined(val1) || _.isUndefined(val2)) {
return false;
}
return (val1 == val2);
};
// Determines whether the row is the initial choice
$scope.isCurrent = function (val) {
return determineMatch(val, $scope.selected);
};
// Determines whether the row is selected
$scope.isSelected = function (val, idx) {
// row can only be 'selected' if it's not the 'current'' value
if (!$scope.isCurrent(val)) {
if ($scope.type == 'radio') {
return (val == $scope.model);
} else if ($scope.type == 'checkbox') {
if (!_.isUndefined(val)) {
// if 'val' is defined, run it through our custom matcher
return determineMatch(val, $scope.model[idx]);
} else {
// otherwise, just return the value of the model and angular can decide
return $scope.model[idx];
}
}
}
return false;
};
/*
* Get the value out of a key from the row, or parse an expression
* @param {String} expr - Key or Angular Expression (or static text) to be compiled
* @param {Object} row - Data object with data to be used against the expression
*/
$scope.getContent = function (column, row) {
var expr = column.key;
// If no expression exit out;
if (!expr) {
return;
}
// if the expr is a property of row, then we expect the value of the key.
if (row.hasOwnProperty(expr)) {
return row[expr];
}
// Compile expression & Run output template
var outputHTML = $interpolate(expr)(row);
return outputHTML;
};
}
};
});