forked from OptimalBPM/angular-schema-form-dynamic-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
370 lines (349 loc) · 14.1 KB
/
app.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*global angular */
"use strict";
/**
* The main app module
* @name testApp
* @type {angular.Module}
*/
var testApp = angular.module("testApp", ["schemaForm", "mgcrea.ngStrap", "mgcrea.ngStrap.modal",
"pascalprecht.translate", "ui.select", "mgcrea.ngStrap.select"
]);
testApp.controller("appController", ["$scope", "$http", function ($scope, $http) {
$scope.callBackSD = function (options, search) {
if (search) {
console.log("Here the select lis could be narrowed using the search value: " + search.toString());
return [
{value: "value1", name: "text1"},
{value: "value2", name: "text2"},
{value: "value3", name: "Select dynamic!"}
].filter(function (item) {
return (item.name.search(search) > -1)
});
}
else {
return [
{value: "value1", name: "text1"},
{value: "value2", name: "text2"},
{value: "value3", name: "Select dynamic!"}
];
}
// Note: Options is a reference to the original instance, if you change a value,
// that change will persist when you use this form instance again.
};
$scope.callBackUI = function (options) {
return [
{"value": "value1", "name": "text1", "category": "value1"},
{"value": "value2", "name": "text2", "category": "value1"},
{"value": "value3", "name": "So this is the next item", "category": "value2"},
{"value": "value4", "name": "The last item", "category": "value1"}
];
// Note: Options is a reference to the original instance, if you change a value,
// that change will persist when you use this form instance again.
};
$scope.callBackMSD = function (options) {
return [
{value: "value1", name: "text1"},
{value: "value2", name: "text2"},
{value: "value3", name: "Multiple select dynamic!"}
];
// Note: Options is a reference to the original instance, if you change a value,
// that change will persist when you use this form instance again.
};
$scope.callBackMSDAsync = function (options) {
// Note that we got the url from the options. Not necessary, but then the same callback function can be used
// by different selects with different parameters.
// The asynchronous function must always return a httpPromise
return $http.get(options.urlOrWhateverOptionIWant);
};
$scope.stringOptionsCallback = function (options) {
// Here you can manipulate the form options used in a http_post or http_get
// For example, you can use variables to build the URL or set the parameters, here we just set the url.
options.httpPost.url = "test/testdata.json";
// Note: This is a copy of the form options, edits here will not persist but are only used in this request.
return options;
};
$scope.schema = {
type: "object",
title: "Select",
properties: {
select: {
title: "Single select strap-select",
type: "string",
enum: ["value1", "value2", "value3"],
description: "Only single item is allowed. Based on schema enum and form default. Change here and observe how the select list below is filtered."
},
multiselect: {
title: "Multi select strap-select",
type: "array",
items: {type: "string"},
maxItems: 2,
description: "Multiple items are allowed, select three for maxItems validation error. Each item belongs to a \"category\", so the list is filtered depending on what you have selected in the \"Single select strap-select\" above."
},
uiselect: {
title: "Single select for UI-select",
type: "string",
description: "This one is using UI-select, single selection. Fetches lookup values(titleMap) from a callback."
},
uiselectmultiple: {
title: "Multi select for UI-select",
type: "array",
items: {type: "integer"},
description: "This one is using UI-select, allows multiple selection. From a callback."
},
selectDynamic: {
title: "Single Select Dynamic",
type: "string",
description: "This titleMap is loaded from the $scope.callBackSD function. (and laid out using css-options)"
},
multiselectDynamic: {
title: "Multi Select Dynamic",
type: "array",
items: {type: "string"},
description: "This titleMap is loaded from the $scope.callBackMSD function. (referenced by name)"
},
multiselectDynamicHttpPost: {
title: "Multi Select Dynamic HTTP Post",
type: "array",
items: {type: "string"},
description: "This titleMap is asynchronously loaded using a HTTP post. " +
"(specifies parameter in form, options.url in a named callback)"
},
multiselectDynamicHttpGet: {
title: "Multi Select Dynamic HTTP Get",
type: "array",
items: {type: "string"},
description: "This titleMap is asynchronously loaded using a HTTP get. " +
"(Set the URL at options.url)"
},
multiselectDynamicHttpGetMapped: {
title: "Multi Select Dynamic HTTP Get Mapped data",
type: "array",
items: {type: "string"},
description: "This titleMap is as above, but remapped from a nodeId/nodeName array of objects. " +
"(See app.js: \"map\" : {valueProperty: \"nodeId\", textProperty: \"nodeName\"})"
},
multiselectDynamicHttpGetMappedArray: {
title: "Multi Select Dynamic HTTP Get Mapped data using array",
type: "array",
items: {type: "string"},
description: "This titleMap is as above, but remapped from a nodeId/nodeName/category array of objects with optional separator." +
"(See app.js: \"map\" : {valueProperty: \"nodeId\", nameProperty: [\"nodeName\",\"category\"], separatorValue: \" | \"})"
},
multiselectDynamicAsync: {
title: "Multi Select Dynamic Async",
type: "array",
items: {type: "string"},
description: "This titleMap is asynchrously loaded using options.async.call and implements the onChange event. "
},
multiselect_overflow: {
title: "Strap select with overflow",
type: "array",
items: {type: "string"},
description: "If you select more than two items here, it will only show the first two and "
},
"priorities": {
"type": "object",
"properties": {
"priority": {
"type": "array",
"items": {
"type": "object",
"properties": {
"value": {
"type": "string",
"enum": ["DOG", "CAT", "FISH"]
}
}
}
}
}
}
},
required: ["select", "multiselect"]
};
$scope.form = [
{
"key": "select"
},
{
"key": "multiselect",
"type": "strapselect",
"placeholder": "My items feel unselected. Or you selected text3 in the selector above me.",
"options": {
"multiple": "true",
"filterTriggers": ["model.select"],
"filter": "item.category.indexOf(model.select) > -1"
},
"validationMessage": "Hey, you can only select three items or you'll see this!",
"titleMap": [
{"value": "value1", "name": "text1 (belongs to the value1-category)", "category": "value1"},
{"value": "value2", "name": "text2 (belongs to the value1-category)", "category": "value1"},
{
"value": "value3",
"name": "long very very long label3 (belongs to the value2-category)",
"category": "value2"
},
{
"value": "value4",
"name": "Select three and get a validation error! (belongs to the value1-category)",
"category": "value1"
}
]
},
{
"key": "uiselect",
"type": "uiselect",
"placeholder": "not set yet..",
"options": {
"callback": "callBackSD"
}
},
{
"key": "uiselectmultiple",
"type": "uiselectmultiple",
"placeholder": "not set yet..",
"options": {
"callback": "callBackUI"
}
},
{
"key": "selectDynamic",
"type": "strapselect",
"htmlClass": "col-lg-3 col-md-3",
"labelHtmlClass": "bigger",
"fieldHtmlClass": "tilted",
"options": {
"callback": $scope.callBackSD
}
},
{
"key": "multiselectDynamic",
"type": "strapselect",
placeholder: "not set yet(this text is defined using the placeholder option)",
"options": {
"multiple": "true",
"callback": "callBackMSD"
}
},
{
"key": "multiselectDynamicHttpPost",
"type": "strapselect",
"title": "Multi Select Dynamic HTTP Post (title is from form.options, overriding the schema.title)",
"options": {
"multiple": "true",
"httpPost": {
"optionsCallback": "stringOptionsCallback",
"parameter": {"myparam": "Hello"}
}
}
},
{
"key": "multiselectDynamicHttpGet",
"type": "strapselect",
"placeholder": "None selected here neither.",
"options": {
"multiple": "true",
"httpGet": {
"url": "test/testdata.json"
}
}
},
{
"key": "multiselectDynamicHttpGetMapped",
"type": "strapselect",
"placeholder": "And even less here...",
"options": {
"multiple": "true",
"httpGet": {
"url": "test/testdata_mapped.json"
},
"map": {valueProperty: "nodeId", nameProperty: "nodeName"}
}
},
{
"key": "multiselectDynamicHttpGetMappedArray",
"type": "strapselect",
"placeholder": "And even less here...",
"options": {
"multiple": "true",
"httpGet": {
"url": "test/testdata_mapped.json"
},
"map": {valueProperty: "nodeId", nameProperty: ["nodeName","category"], separatorValue: " | "}
}
},
{
"key": "multiselectDynamicAsync",
"type": "strapselect",
"onChange": function (modelValue, form) {
$scope.form.forEach(function (item) {
if (item.key == "multiselectDynamicHttpGet") {
item.options.scope.populateTitleMap(item);
}
});
alert("onChange happened!\nYou changed this value into " + modelValue + " !\nThen code in this event cause the multiselectDynamicHttpGet to reload. \nSee the ASF onChange event for info.");
},
"options": {
"multiple": "true",
"asyncCallback": $scope.callBackMSDAsync,
"urlOrWhateverOptionIWant": "test/testdata.json"
}
},
{
"key": "multiselect_overflow",
"type": "strapselect",
"placeholder": "Please select some items.",
"options": {
"multiple": "true",
"inlineMaxLength": "2",
"inlineMaxLengthHtml": "Too many items to show...."
},
"titleMap": [
{"value": "value1", "name": "text1"},
{"value": "value2", "name": "text2"},
{"value": "value3", "name": "text3"},
{"value": "value4", "name": "text4"}
]
},
{
"key": "priorities.priority",
"title": "Array inside an object, defaults ASF select only",
"description": "This is an example of how to use this in a complex structure. Note that the title and description is in the form, ASF only looks in the form for that.",
"type": "array",
"items": [
{
"key": "priorities.priority[].value",
"type": "strapselect"
}
]
},
{
type: "submit",
style: "btn-info",
title: "OK"
}
];
$scope.model = {};
$scope.model.select = "value1";
$scope.model.multiselect = ["value2", "value1"];
$scope.model.uiselect = "value1";
$scope.model.uiselectmultiple = ["value1", "value2"];
$scope.model.priorities = {
"priority": [
{
"value": "DOG"
},
{
"value": "DOG"
},
{
"value": "FISH"
}
]
};
$scope.submitted = function (form) {
$scope.$broadcast("schemaFormValidate");
console.log($scope.model);
};
}])
;