fng-ui-select 0.12.0-beta.193 → 0.12.0-beta.194
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/fng-ui-select.js +125 -40
package/package.json
CHANGED
package/src/fng-ui-select.js
CHANGED
|
@@ -3,9 +3,43 @@
|
|
|
3
3
|
|
|
4
4
|
var uiSelectModule = angular.module('fng.uiSelect', ['ui.select']);
|
|
5
5
|
|
|
6
|
-
uiSelectModule.factory('uiSelectHelper', ['$rootScope', '$q', 'SubmissionsService', function ($rootScope, $q, SubmissionsService) {
|
|
6
|
+
uiSelectModule.factory('uiSelectHelper', ['$rootScope', '$q', 'recordHandler', 'SubmissionsService', function ($rootScope, $q, recordHandler, SubmissionsService) {
|
|
7
7
|
var lastW, lastH;
|
|
8
|
-
|
|
8
|
+
const localLookups = {};
|
|
9
|
+
// this ultra-simple cache stores { [id: string]: stringValue } as a means of avoiding unnecessary network round-trips, and the UI refresh
|
|
10
|
+
// lagging caused by the promises used to make them.
|
|
11
|
+
// there is no cache life-span because all we're storing here is the concatenation of records' list fields, and the chances of those
|
|
12
|
+
// changing while present in this cache is miniscule. even if they did change, displaying a stale record description in a picklist
|
|
13
|
+
// would surely not cause any great concern.
|
|
14
|
+
// there is no cache size limit because we're storing tiny amounts of data.
|
|
15
|
+
// NB: this cache assumes that record ids are unique across all resources (as is the case with a standard MongoDB back-end).
|
|
16
|
+
// TODO: provide an alternative implementation that includes the resourceName in the cache ids to avoid this limitation.
|
|
17
|
+
let valueCache = {};
|
|
18
|
+
// we're going to populate the select with this value whenever a SubmissionsService.getListAttributes(...) call is rejected. because we're
|
|
19
|
+
// only asking for list fields, permissions should not be the cause of failures, making "record not found" (where the foreign key
|
|
20
|
+
// reference has been broken) by far the most likely explanation.
|
|
21
|
+
// TODO: interpret the actual error condition and translate this into a user-friendly message
|
|
22
|
+
const RECORD_NOT_FOUND = "record not found";
|
|
23
|
+
|
|
24
|
+
function useCacheOrLookItUp(resourceName, id, cb) {
|
|
25
|
+
if (valueCache[id]) {
|
|
26
|
+
cb(valueCache[id]);
|
|
27
|
+
} else {
|
|
28
|
+
var text;
|
|
29
|
+
SubmissionsService.getListAttributes(resourceName, id)
|
|
30
|
+
.then((response) => {
|
|
31
|
+
text = response.data.list;
|
|
32
|
+
})
|
|
33
|
+
.catch(() => {
|
|
34
|
+
text = RECORD_NOT_FOUND; // cache this as well as there's no point in repeatedly performing a failing lookup
|
|
35
|
+
})
|
|
36
|
+
.finally(() => {
|
|
37
|
+
cb(text);
|
|
38
|
+
valueCache[id] = text;
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
9
43
|
return {
|
|
10
44
|
windowChanged: function (w, h) {
|
|
11
45
|
var result = false;
|
|
@@ -19,39 +53,50 @@
|
|
|
19
53
|
addClientLookup: function (lkpName, lkpData) {
|
|
20
54
|
localLookups[lkpName] = lkpData;
|
|
21
55
|
},
|
|
56
|
+
clearCache: function () {
|
|
57
|
+
valueCache = {};
|
|
58
|
+
},
|
|
22
59
|
lookupFunc: function (value, formSchema, cb) {
|
|
23
60
|
if (formSchema.array) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
61
|
+
const promises = value.map((value) => {
|
|
62
|
+
const id = value.x?.id || value.x; // if it's already been converted, throw away the result of the previous conversion
|
|
63
|
+
if (!id) {
|
|
64
|
+
return $q.resolve({ data: { list: ""} }); // nothing to convert
|
|
65
|
+
} else if (valueCache[id]) {
|
|
66
|
+
return $q.resolve({ data: { list: valueCache[id] }}); // already cached
|
|
67
|
+
} else {
|
|
68
|
+
return SubmissionsService.getListAttributes(formSchema.ref, id); // need to look it up
|
|
69
|
+
}
|
|
30
70
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
71
|
+
// TODO: no error handling here
|
|
72
|
+
// TODO: perform lookups of cache misses using a single round-trip
|
|
73
|
+
$q.all(promises).then((responses) => {
|
|
74
|
+
const results = responses.map((response) => {
|
|
75
|
+
let id = value.shift().x;
|
|
76
|
+
id = id.id || id; // in case it's already been converted
|
|
77
|
+
const text = response.data.list
|
|
78
|
+
valueCache[id] = text; // cache it for next time
|
|
79
|
+
return { id, text }
|
|
34
80
|
});
|
|
35
81
|
cb(formSchema, results);
|
|
36
82
|
setTimeout(function () {
|
|
37
83
|
$rootScope.$digest();
|
|
38
84
|
});
|
|
39
85
|
});
|
|
86
|
+
} else if (typeof value !== "string") {
|
|
87
|
+
cb(formSchema, value); // already converted
|
|
40
88
|
} else if (formSchema.fngUiSelect.deriveOptions) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var obj = localLookups[formSchema.fngUiSelect.deriveOptions].find(function (test) {
|
|
44
|
-
return test.id == value
|
|
45
|
-
});
|
|
46
|
-
retVal = {id: value, text: obj ? obj.text : ''}
|
|
47
|
-
} else {
|
|
48
|
-
retVal = value;
|
|
49
|
-
}
|
|
50
|
-
cb(formSchema, retVal);
|
|
89
|
+
const obj = localLookups[formSchema.fngUiSelect.deriveOptions].find((test) => test.id === value);
|
|
90
|
+
cb(formSchema, { id: value, text: obj?.text || "" });
|
|
51
91
|
} else {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
92
|
+
useCacheOrLookItUp(formSchema.ref, value, (text) => { cb(formSchema, { id: value, text }) });
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
doOwnConversion: function(scope, processedAttrs, ref) {
|
|
96
|
+
var id = recordHandler.getData(scope.record, processedAttrs.info.name, scope.$index);
|
|
97
|
+
if (id) {
|
|
98
|
+
id = id.id || id; // in case it's already been converted
|
|
99
|
+
useCacheOrLookItUp(ref, id, (text) => { recordHandler.setData(scope.record, processedAttrs.info.name, scope.$index, { id, text }) });
|
|
55
100
|
}
|
|
56
101
|
}
|
|
57
102
|
};
|
|
@@ -95,7 +140,10 @@
|
|
|
95
140
|
$scope[id + '_options'] = [];
|
|
96
141
|
} else {
|
|
97
142
|
var promise;
|
|
98
|
-
if (elem.filter && elem.filter[0] ===
|
|
143
|
+
if (elem.filter && typeof elem.filter === "string" && elem.filter[0] === "/") {
|
|
144
|
+
if (elem.filter.startsWith("/api/")) {
|
|
145
|
+
console.log("WARNING! fng-ui-select is using an /api/ endpoint to refresh options - this is likely to cause security authorisation problems");
|
|
146
|
+
}
|
|
99
147
|
// HACK We want to pass the existing record in e, but we can fall foul of 431 errors if it is large, so we test for that
|
|
100
148
|
// using an arbitrary number and if so just send the _id (on the assumption that large records won't be new so will have an _id).
|
|
101
149
|
// According to https://stackoverflow.com/questions/57431355/how-to-fix-431-request-header-fields-too-large-in-react-redux-app the limit is 8KB
|
|
@@ -152,8 +200,8 @@
|
|
|
152
200
|
$timeout($scope.windowResizeUiSelect);
|
|
153
201
|
}]);
|
|
154
202
|
|
|
155
|
-
uiSelectModule.directive('fngUiSelect', ['$compile', 'pluginHelper', 'cssFrameworkService', 'uiSelectHelper',
|
|
156
|
-
function ($compile, pluginHelper, cssFrameworkService, uiSelectHelper) {
|
|
203
|
+
uiSelectModule.directive('fngUiSelect', ['$compile', 'pluginHelper', 'cssFrameworkService', 'uiSelectHelper', '$timeout',
|
|
204
|
+
function ($compile, pluginHelper, cssFrameworkService, uiSelectHelper, $timeout) {
|
|
157
205
|
return {
|
|
158
206
|
restrict: 'E',
|
|
159
207
|
controller: 'FngUISelectCtrl',
|
|
@@ -177,7 +225,8 @@
|
|
|
177
225
|
|
|
178
226
|
const processedAttrs = pluginHelper.extractFromAttr(attrs, 'fngUiSelect');
|
|
179
227
|
const id = processedAttrs.info.id;
|
|
180
|
-
const
|
|
228
|
+
const uniqueId = scope.$index !== undefined ? processedAttrs.info.id + "_" + scope.$index : id;
|
|
229
|
+
const elemScope = angular.extend({selectId: uniqueId}, processedAttrs.directiveOptions);
|
|
181
230
|
const multi = processedAttrs.info.array;
|
|
182
231
|
let elementHtml;
|
|
183
232
|
let input = '';
|
|
@@ -217,15 +266,17 @@
|
|
|
217
266
|
var allowClearStr = '';
|
|
218
267
|
if (processedAttrs.info.required) {
|
|
219
268
|
requiredStr = ' ng-required="true"';
|
|
269
|
+
} else if (processedAttrs.directiveOptions.ngrequired) {
|
|
270
|
+
requiredStr = ` ng-required="${processedAttrs.directiveOptions.ngrequired}"`;
|
|
220
271
|
} else {
|
|
221
272
|
allowClearStr = ' allow-clear';
|
|
222
273
|
}
|
|
223
|
-
let disabledStr = pluginHelper.genIdAndDisabledStr(scope, processedAttrs, "", true);
|
|
274
|
+
let disabledStr = pluginHelper.genIdAndDisabledStr(scope, processedAttrs, "", { forceNg: true });
|
|
224
275
|
|
|
225
276
|
// First of all add a hidden input field which we will use to set the width of the select
|
|
226
|
-
if (!angular.element(`#${
|
|
277
|
+
if (!angular.element(`#${uniqueId}_width-helper`).length > 0) {
|
|
227
278
|
var hiddenInputInfo = {
|
|
228
|
-
id: `${
|
|
279
|
+
id: `${uniqueId}_width-helper`,
|
|
229
280
|
name: processedAttrs.info.name + '_width-helper',
|
|
230
281
|
label: ''
|
|
231
282
|
};
|
|
@@ -280,6 +331,17 @@
|
|
|
280
331
|
defaultPlaceholder = 'Start typing...'
|
|
281
332
|
}
|
|
282
333
|
// set up the ui-select directives
|
|
334
|
+
// hack here. buildingBlocks.common will almost certainly contain 'id="xxxx"', but we don't want this because the
|
|
335
|
+
// id will already be in disabledStr. remove it.
|
|
336
|
+
// the right solution here would be to include an option in the params that are passed to buildInputMarkup which
|
|
337
|
+
// specifies whether or not the buildingBlocks that it creates should include the id or not.
|
|
338
|
+
const idIdx = buildingBlocks.common.indexOf('id="');
|
|
339
|
+
if (idIdx > -1) {
|
|
340
|
+
const endIdIdx = buildingBlocks.common.indexOf('"', idIdx+4);
|
|
341
|
+
if (endIdIdx > -1) {
|
|
342
|
+
buildingBlocks.common = buildingBlocks.common.substring(0, idIdx) + buildingBlocks.common.substring(endIdIdx + 1);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
283
345
|
var select = '<ui-select ' + multiStr + buildingBlocks.common + requiredStr + disabledStr + ' theme="' + theme + '" style="min-width:18em;">'
|
|
284
346
|
select += '<ui-select-match' + allowClearStr + ' placeholder="' + (processedAttrs.info.placeholder || defaultPlaceholder) + '">';
|
|
285
347
|
|
|
@@ -288,11 +350,34 @@
|
|
|
288
350
|
if (processedAttrs.directiveOptions.fngajax !== true) {
|
|
289
351
|
elemScope.filter = processedAttrs.directiveOptions.fngajax;
|
|
290
352
|
}
|
|
291
|
-
//
|
|
292
|
-
|
|
293
|
-
//
|
|
294
|
-
|
|
295
|
-
|
|
353
|
+
// if we have a hard-coded ref (processedAttrs.info.ref), forms-angular can perform the lookup conversion for us.
|
|
354
|
+
// where processedAttrs.directiveOptions.refprop is being used instead, the ref is 'variable' - it comes from a property of scope.record, and
|
|
355
|
+
// in this case, we'll need to handle the conversions ourselves.
|
|
356
|
+
addToConversions(processedAttrs.info.name, {fngajax: uiSelectHelper.lookupFunc, noconvert: !!processedAttrs.directiveOptions.refprop});
|
|
357
|
+
if (processedAttrs.directiveOptions.refprop) {
|
|
358
|
+
// the property that we'll be getting the ref from may not be populated yet, and might conceivably change at any time, so we
|
|
359
|
+
// need to $watch it. buildingBlocks.modelString will be something like "record.<array>[$index].<field>". replacing <field>
|
|
360
|
+
// with the value of refprop will give us a suitable watch expression.
|
|
361
|
+
const watchStr = buildingBlocks.modelString.substring(0, buildingBlocks.modelString.lastIndexOf(".") + 1) + processedAttrs.directiveOptions.refprop;
|
|
362
|
+
scope.$watch(watchStr, (newValue) => {
|
|
363
|
+
if (newValue && elemScope.ref !== newValue) {
|
|
364
|
+
elemScope.ref = newValue;
|
|
365
|
+
uiSelectHelper.doOwnConversion(scope, processedAttrs, elemScope.ref);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
// we also need to re-do the conversion if the user cancels changes. this event does fire on more than just user cancellation,
|
|
369
|
+
// so this is slightly wasteful. but with the caching implemented by uiSelectHelper, this is no big deal.
|
|
370
|
+
scope.$on("fngCancel", () => {
|
|
371
|
+
$timeout(() => {
|
|
372
|
+
if (elemScope.ref) {
|
|
373
|
+
uiSelectHelper.doOwnConversion(scope, processedAttrs, elemScope.ref);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
} else {
|
|
378
|
+
elemScope.ref = processedAttrs.info.ref;
|
|
379
|
+
}
|
|
380
|
+
scope[`${uniqueId}_options`] = [];
|
|
296
381
|
if (multiControl) {
|
|
297
382
|
select += '{{$select.selected.text}}';
|
|
298
383
|
} else if (processedAttrs.options.subschema) {
|
|
@@ -301,10 +386,10 @@
|
|
|
301
386
|
select += '{{' + buildingBlocks.modelString + '.text}}';
|
|
302
387
|
}
|
|
303
388
|
select += '</ui-select-match>';
|
|
304
|
-
select += `<ui-select-choices repeat="option in (${
|
|
305
|
-
select += `refresh="refreshOptions($select.search, '${
|
|
306
|
-
select += 'refresh-delay="
|
|
307
|
-
select += '<div ng-bind-html="option.text"></div>';
|
|
389
|
+
select += `<ui-select-choices repeat="option in (${uniqueId}_options) track by $index" `;
|
|
390
|
+
select += `refresh="refreshOptions($select.search, '${uniqueId}')" `;
|
|
391
|
+
select += 'refresh-delay="400"> ';
|
|
392
|
+
select += '<div ng-bind-html="option.text" ng-attr-title="{{ option.text }}"></div>';
|
|
308
393
|
} else if (processedAttrs.directiveOptions.deriveoptions) {
|
|
309
394
|
if (typeof scope[processedAttrs.directiveOptions.deriveoptions] === "function") {
|
|
310
395
|
select += optionsFromArray(multiControl, multi, scope[processedAttrs.directiveOptions.deriveoptions](), processedAttrs.directiveOptions.deriveoptions);
|