nice-elements 1.14.1 → 1.15.1

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.
Files changed (56) hide show
  1. package/.claude/launch.json +11 -0
  2. package/CHANGELOG.md +28 -0
  3. package/CLAUDE.md +89 -0
  4. package/README.md +3 -1
  5. package/bower.json +1 -1
  6. package/dist/nice.js +707 -112
  7. package/dist/nice.min.js +1 -1
  8. package/dist/style.css +198 -11
  9. package/dist/style.css.map +1 -1
  10. package/dist/style.min.css +2 -2
  11. package/package.json +2 -2
  12. package/src/components/nice-badges-list/nice-badges-list.html +13 -0
  13. package/src/components/nice-badges-list/nice-badges-list.js +135 -0
  14. package/src/components/nice-badges-list/nice-badges-list.scss +75 -0
  15. package/src/components/nice-calendar/nice-calendar.html +4 -0
  16. package/src/components/nice-checkbox/nice-checkbox.html +11 -5
  17. package/src/components/nice-checkbox/nice-checkbox.js +1 -0
  18. package/src/components/nice-checkbox/nice-checkbox.scss +7 -0
  19. package/src/components/nice-choice/nice-choice.js +11 -0
  20. package/src/components/nice-date/nice-date.html +10 -1
  21. package/src/components/nice-date/nice-date.js +50 -1
  22. package/src/components/nice-date/nice-date.scss +1 -0
  23. package/src/components/nice-date-input/nice-date-input.js +1 -1
  24. package/src/components/nice-date-range/nice-date-range.html +1 -1
  25. package/src/components/nice-date-range/nice-date-range.js +7 -7
  26. package/src/components/nice-datetime-picker/nice-datetime-picker.html +6 -3
  27. package/src/components/nice-datetime-picker/nice-datetime-picker.js +7 -2
  28. package/src/components/nice-datetimerange-picker-2/nice-datetimerange-picker-2.html +2 -2
  29. package/src/components/nice-dialog/nice-dialog.html +16 -0
  30. package/src/components/nice-dialog/nice-dialog.js +47 -0
  31. package/src/components/nice-dialog/nice-dialog.scss +126 -0
  32. package/src/components/nice-dropdown/nice-dropdown.js +16 -4
  33. package/src/components/nice-dropdown/nice-dropdown.scss +0 -11
  34. package/src/components/nice-dropdown-date/nice-dropdown-date.js +3 -1
  35. package/src/components/nice-input/nice-input.js +12 -8
  36. package/src/components/nice-no-data/nice-no-data.js +3 -1
  37. package/src/components/nice-number/nice-number.html +23 -9
  38. package/src/components/nice-number/nice-number.js +81 -12
  39. package/src/components/nice-percent/nice-percent.js +15 -6
  40. package/src/components/nice-popup/nice-popup.js +2 -2
  41. package/src/components/nice-search/nice-search.html +2 -4
  42. package/src/components/nice-search/nice-search.js +40 -20
  43. package/src/components/nice-search2/nice-search2.html +1 -1
  44. package/src/components/nice-search2/nice-search2.js +1 -0
  45. package/src/components/nice-svgs/nice-svgs.html +165 -20
  46. package/src/components/nice-table/nice-table.js +9 -4
  47. package/src/components/nice-toggle/nice-toggle.html +3 -0
  48. package/src/components/nice-upload/nice-upload.html +1 -1
  49. package/src/components/nice-view/_nice-view.scss +10 -1
  50. package/src/components/nice-view/nice-view.html +1 -0
  51. package/src/components/nice-wrapper/nice-wrapper.js +3 -1
  52. package/src/components/nice-yesno/nice-yesno.html +3 -0
  53. package/src/components/nice-yesno/nice-yesno.js +4 -2
  54. package/src/services/nice-service.js +16 -1
  55. package/src/styles/_nice-common.scss +17 -0
  56. package/src/styles/_nice.scss +2 -0
@@ -0,0 +1,126 @@
1
+ // ngDialog wrapper, open with className: 'nice-ngdialog', showClose: false
2
+ .nice-ngdialog {
3
+ .ngdialog-content {
4
+ background: unset;
5
+ display: flex;
6
+ justify-content: center;
7
+ align-items: center;
8
+
9
+ .nice-dialog {
10
+ max-height: calc(100dvh - 4rem);
11
+ max-width: 95%;
12
+ width: 800px;
13
+ margin: 2rem;
14
+
15
+ background: var(--nice-card-bg);
16
+ border: 1px solid var(--nice-border-color);
17
+ position: relative;
18
+ border-radius: var(--nice-border-radius);
19
+ }
20
+ }
21
+ }
22
+
23
+
24
+ .nice-dialog {
25
+ display: flex;
26
+ flex-direction: column;
27
+ max-height: calc(100vh - 6rem);
28
+ --nice-dialog-padding: 2rem;
29
+
30
+ // Header / footer borders are driven by the body scroll position (same as nice-view)
31
+ timeline-scope: --nice-dialog-scroll;
32
+
33
+ // ---------------- Dialog Header ----------------
34
+ .nice-dialog-header {
35
+ font-size: 1.3em;
36
+ line-height: 1.3;
37
+ padding: var(--nice-dialog-padding);
38
+ padding-bottom: calc(var(--nice-dialog-padding) / 2);
39
+ flex-shrink: 0;
40
+ border-bottom: 1px solid transparent;
41
+ animation: nice-dialog-header-border linear both;
42
+ animation-timeline: --nice-dialog-scroll;
43
+ animation-range: 0 1rem;
44
+
45
+ display: flex;
46
+ align-items: center;
47
+ gap: 1rem;
48
+
49
+ .nice-icon {
50
+ height: 30px;
51
+ width: 30px;
52
+ }
53
+
54
+ .nice-dialog-sub-title {
55
+ font-size: 0.7em;
56
+ color: var(--nice-font-color);
57
+ opacity: 0.8;
58
+ }
59
+
60
+ .nice-dialog-title {
61
+ font-size: 1.2em;
62
+ color: var(--nice-primary-color);
63
+ text-transform: uppercase;
64
+ }
65
+ }
66
+
67
+ // ---------------- Dialog Body ----------------
68
+ .nice-dialog-body {
69
+ padding: calc(var(--nice-dialog-padding) / 2) var(--nice-dialog-padding);
70
+ flex-grow: 1;
71
+ overflow: auto;
72
+ scroll-timeline: --nice-dialog-scroll block;
73
+ }
74
+
75
+ // ---------------- Dialog Footer ----------------
76
+ .nice-dialog-footer {
77
+ position: relative;
78
+ display: block;
79
+ padding: var(--nice-dialog-padding);
80
+ padding-top: calc(var(--nice-dialog-padding) / 2);
81
+ flex-shrink: 0;
82
+ border-top: 1px solid transparent;
83
+ animation: nice-dialog-footer-border linear both;
84
+ animation-timeline: --nice-dialog-scroll;
85
+ animation-range: calc(100% - 1rem) 100%;
86
+
87
+ // Hide the footer when nothing was passed in
88
+ &:empty {
89
+ display: none;
90
+ }
91
+
92
+ .btn-navigation {
93
+ margin: 0;
94
+ }
95
+
96
+ .view-actions {
97
+ gap: 0.5rem;
98
+
99
+ @media (max-width: $screen-xs-max) {
100
+ .split {
101
+ display: none;
102
+ }
103
+ .btn {
104
+ flex-grow: 1;
105
+ }
106
+ }
107
+ }
108
+
109
+ nice-dialog-footer {
110
+ display: flex;
111
+ justify-content: flex-end;
112
+ flex-wrap: wrap;
113
+ gap: 0.5rem;
114
+ }
115
+ }
116
+ }
117
+
118
+ @keyframes nice-dialog-header-border {
119
+ from { border-bottom-color: transparent; }
120
+ to { border-bottom-color: var(--nice-border-color); }
121
+ }
122
+
123
+ @keyframes nice-dialog-footer-border {
124
+ from { border-top-color: var(--nice-border-color); }
125
+ to { border-top-color: transparent; }
126
+ }
@@ -56,7 +56,10 @@ angular.module('niceElements')
56
56
  enableLoadMore: '@', // Enable load more
57
57
  placement: '@', // Describes the preferred placement of the popper
58
58
  },
59
- controller: function ($scope, $http, $element, $timeout, gettextCatalog, NiceService) {
59
+ controller: function ($scope, $attrs, $http, $element, $timeout, gettextCatalog, NiceService) {
60
+ NiceService.bindFlag($scope, $attrs, 'selectedIsKey');
61
+ NiceService.bindFlag($scope, $attrs, 'enableLoadMore');
62
+
60
63
  $scope.id = Math.random().toString(36).substring(7);
61
64
  if (!$scope.dropdownDistance) { $scope.dropdownDistance = 5; }
62
65
  if (!$scope.objValue) { $scope.objValue = 'value'; }
@@ -455,11 +458,20 @@ angular.module('niceElements')
455
458
  });
456
459
 
457
460
  // Handle required
458
- $timeout(function () {
459
- if ($scope.formDropdown) $scope.formDropdown.$setValidity('required', !(!$scope.selected && $scope.required));
460
- });
461
+ $timeout($scope.checkRequired);
461
462
  }
462
463
 
464
+ // ----------------------------------- Required -----------------------------------
465
+ // Form is invalid when required and nothing is selected
466
+ $scope.checkRequired = function () {
467
+ if ($scope.formDropdown) $scope.formDropdown.$setValidity('required', !(!$scope.selected && $scope.required));
468
+ };
469
+
470
+ // Re-check when required is turned on/off
471
+ $scope.$watch('required', function () {
472
+ $scope.checkRequired();
473
+ });
474
+
463
475
  // ----------------------------------- Watch for keydown and keypress -----------------------------------
464
476
  // $element.bind("keydown keypress", function (event) {
465
477
  // // Arrow Up
@@ -7,17 +7,6 @@
7
7
  position: fixed !important;
8
8
  }
9
9
 
10
- .hidden-input {
11
- height: 0;
12
- width: 100%;
13
- position: absolute;
14
- bottom: 0;
15
- left: 0;
16
- padding: 0;
17
- background: transparent;
18
- opacity: 0;
19
- }
20
-
21
10
  .open {
22
11
  .btn-dropdown {
23
12
  background: $nice-focus-bg !important;
@@ -29,7 +29,9 @@ angular.module('niceElements')
29
29
  onChange: '&?',
30
30
  required: '='
31
31
  },
32
- controller: function ($scope, $timeout) {
32
+ controller: function ($scope, $attrs, $timeout, NiceService) {
33
+ NiceService.bindFlag($scope, $attrs, 'mature');
34
+
33
35
  $scope.days = [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];
34
36
  $scope.months = [
35
37
  { value: 0, name: gettextCatalog.getString('Jan', null, 'Nice') },
@@ -45,16 +45,14 @@ angular.module('niceElements')
45
45
  autocomplete: "@?",
46
46
  disableTrim: "@?"
47
47
  },
48
- controller: function ($scope, $attrs, $rootScope, $element, ngDialog, NiceService) {
48
+ controller: function ($scope, $attrs, $rootScope, $element, $timeout, ngDialog, NiceService) {
49
49
  if (!$attrs.type) { $attrs.type = 'text'; }
50
50
  if (!$attrs.title) { $attrs.title = ''; }
51
51
  if (!$attrs.regex) { $attrs.regex = null; }
52
52
  if (!$attrs.placeholder) { $attrs.placeholder = ''; }
53
- if (!$attrs.minlength) { $attrs.minlength = 1; }
54
- if (!$attrs.maxlength) { $attrs.maxlength = 100; }
55
53
  if (!$attrs.fieldWidth) { $attrs.fieldWidth = 'col-sm-8'; }
56
54
  if (!$attrs.labelWidth) { $attrs.labelWidth = 'col-sm-4'; }
57
- if (!$attrs.textAreaLines) { $attrs.textAreaLines = 3; }
55
+ if (!$scope.textAreaLines) { $scope.textAreaLines = 3; }
58
56
  if (!$attrs.symbol) { $attrs.symbol = ''; }
59
57
  if (!$attrs.help) { $attrs.help = ''; }
60
58
  if (!$attrs.name) { $attrs.name = ''; }
@@ -74,9 +72,12 @@ angular.module('niceElements')
74
72
  $scope.elementType = "textarea";
75
73
  }
76
74
 
75
+ // Focus after the input (rendered inside ng-if) is in the DOM
77
76
  if ($scope.isFocused) {
78
- const input = $element[0].getElementsByTagName($scope.elementType)[0];
79
- if (input) input.focus();
77
+ $timeout(function () {
78
+ const input = $element[0].getElementsByTagName($scope.elementType)[0];
79
+ if (input) input.focus();
80
+ });
80
81
  }
81
82
 
82
83
  if ($scope.numbersOnly) {
@@ -111,8 +112,11 @@ angular.module('niceElements')
111
112
  $scope.regexexp = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
112
113
  }
113
114
 
114
- if (angular.isDefined($scope.valid)) {
115
- $scope.valid = $scope.forma;
115
+ // Link form object with valid object (the form only exists after the template is linked)
116
+ if ($attrs.valid) {
117
+ $scope.$watch('forma', function (form) {
118
+ if (form) $scope.valid = form;
119
+ });
116
120
  }
117
121
 
118
122
  if ($scope.model && !$scope.decimalConstructor && angular.isDefined($scope.minDecimalsCutZeros) && $scope.type == 'number') {
@@ -18,7 +18,9 @@ angular.module('niceElements')
18
18
  message: '@?',
19
19
  fulldiv: '@?'
20
20
  },
21
- controller: function ($scope, gettextCatalog) {
21
+ controller: function ($scope, $attrs, gettextCatalog, NiceService) {
22
+ NiceService.bindFlag($scope, $attrs, 'fulldiv');
23
+
22
24
  if ($scope.icon == "" || !$scope.icon) $scope.innerIcon = "icon-alert-circle";
23
25
  else $scope.innerIcon = $scope.icon;
24
26
  if ($scope.message == "" || !$scope.message) $scope.innerMessage = gettextCatalog.getString("No data");
@@ -26,15 +26,29 @@
26
26
  </button>
27
27
  </span>
28
28
 
29
- <!-- Value -->
30
- <input
31
- type="number"
32
- step="{{ step }}"
33
- ng-change="inputChanged()"
34
- class="form-control"
35
- max="{{ max }}"
36
- min="{{ min }}"
37
- ng-disabled="isDisabled"
29
+ <!-- Value. Two inputs, because Angular picks the input type before {{ }} is evaluated -->
30
+ <input
31
+ ng-if="!decimalMode"
32
+ type="number"
33
+ inputmode="numeric"
34
+ step="{{ step }}"
35
+ ng-change="inputChanged()"
36
+ class="form-control"
37
+ max="{{ max }}"
38
+ min="{{ min }}"
39
+ ng-disabled="isDisabled"
40
+ ng-model="internal.value"
41
+ />
42
+
43
+ <!-- Value in decimal mode: text input, so "1,5" can be typed -->
44
+ <input
45
+ ng-if="decimalMode"
46
+ type="text"
47
+ inputmode="decimal"
48
+ ng-change="inputChanged()"
49
+ ng-blur="inputBlurred()"
50
+ class="form-control"
51
+ ng-disabled="isDisabled"
38
52
  ng-model="internal.value"
39
53
  />
40
54
 
@@ -29,6 +29,7 @@ angular.module('niceElements')
29
29
  noMargin: '@',
30
30
  step: '@',
31
31
  decimals: '@',
32
+ lang: '@',
32
33
  allowNegative: '@',
33
34
  floatingError: '@',
34
35
  help: '@',
@@ -36,14 +37,20 @@ angular.module('niceElements')
36
37
  onChange: '&?'
37
38
  },
38
39
 
39
- controller: function ($scope, $timeout, NiceService) {
40
+ controller: function ($scope, $attrs, $timeout, NiceService, gettextCatalog) {
41
+ NiceService.bindFlag($scope, $attrs, 'allowNegative');
42
+ NiceService.bindFlag($scope, $attrs, 'hideError');
43
+ NiceService.bindFlag($scope, $attrs, 'floatingError');
44
+
40
45
  $scope.canAdd = true;
41
46
  $scope.canSubtract = true;
42
47
  $scope.preventZero = $scope.preventZero == "true";
43
48
 
44
49
  // Link form object with valid object
45
- if ($scope.valid) {
46
- $scope.valid = $scope.forma;
50
+ if ($attrs.valid) {
51
+ $scope.$watch('niceNumberForm', function (form) {
52
+ if (form) $scope.valid = form;
53
+ });
47
54
  }
48
55
 
49
56
  // Fix min
@@ -61,12 +68,22 @@ angular.module('niceElements')
61
68
  if ($scope.min != 0 && $scope.min != -Infinity) $scope.defaultValue = $scope.min;
62
69
  else $scope.defaultValue = 0;
63
70
  } else {
64
- $scope.defaultValue = parseInt($scope.defaultValue);
71
+ $scope.defaultValue = parseFloat($scope.defaultValue);
65
72
  }
66
73
 
67
- // Fix decimals
68
- if (!$scope.decimals) $scope.decimals = 0;
69
- else $scope.decimals = parseInt($scope.decimals);
74
+ // Decimal mode: when `decimals` is set, the input is a text field so "1.5" and "1,5" can be typed,
75
+ // and values are rounded to `decimals` places. Kept in a local variable, because Angular
76
+ // writes the "@" attribute back to $scope.decimals as a string.
77
+ const decimalPlaces = parseInt($attrs.decimals);
78
+ $scope.decimalMode = !Number.isNaN(decimalPlaces);
79
+
80
+ // Decimal separator of the locale, e.g. "." for "en" and "," for "sl".
81
+ // Uses the `lang` attribute, or the app language from angular-gettext ("sl_SI" -> "sl-SI").
82
+ const lang = ($attrs.lang || gettextCatalog.getCurrentLanguage()).replace("_", "-");
83
+ const decimalSeparator = new Intl.NumberFormat(lang)
84
+ .formatToParts(1.5)
85
+ .find(function (part) { return part.type === "decimal"; })
86
+ .value;
70
87
 
71
88
  // Fix step
72
89
  if (!$scope.step) $scope.step = 1;
@@ -122,6 +139,15 @@ angular.module('niceElements')
122
139
  $scope.niceNumberForm.$setValidity("zero", null);
123
140
  }
124
141
  }
142
+
143
+ // Text input has no built-in min/max validation, so decimal mode checks it here
144
+ if ($scope.decimalMode && $scope.niceNumberForm) {
145
+ const value = parseFloat($scope.model);
146
+ const tooSmall = !Number.isNaN(value) && value < $scope.min;
147
+ const tooBig = !Number.isNaN(value) && $scope.max && value > $scope.max;
148
+ $scope.niceNumberForm.$setValidity("min", tooSmall ? false : null);
149
+ $scope.niceNumberForm.$setValidity("max", tooBig ? false : null);
150
+ }
125
151
  };
126
152
 
127
153
 
@@ -131,11 +157,35 @@ angular.module('niceElements')
131
157
  });
132
158
 
133
159
 
134
- // Input model. <input type="number"> needs a number, so Decimal model is converted.
160
+ // Returns a plain number, rounded to `decimals` places in decimal mode
161
+ const round = function (value) {
162
+ if (value == null) return null;
163
+ if (!$scope.decimalMode) return Number(value);
164
+ return new Decimal(value).toDecimalPlaces(decimalPlaces).toNumber();
165
+ };
166
+
167
+ // Decimal mode: typed text -> number ("1,5" -> 1.5).
168
+ // Returns null for empty text and NaN for text that is not a number.
169
+ const parseText = function (text) {
170
+ const cleaned = (text || "").trim().replace(",", ".");
171
+ if (cleaned === "") return null;
172
+ if (!/^-?(\d+\.?\d*|\.\d+)$/.test(cleaned)) return NaN;
173
+ return parseFloat(cleaned);
174
+ };
175
+
176
+ // Model -> input value. Number input needs a number, decimal mode needs text
177
+ // with the locale separator. Decimal.toFixed() avoids exponent notation (1e-7).
135
178
  const toInternal = function (value) {
179
+ if ($scope.decimalMode) return value == null ? "" : new Decimal(value).toFixed().replace(".", decimalSeparator);
136
180
  return NiceService.isDecimal(value) ? value.toNumber() : value;
137
181
  };
138
182
 
183
+ // Does the input already show the model value? (In decimal mode "1,50" shows 1.5)
184
+ const isInputInSync = function () {
185
+ if (!$scope.decimalMode) return toInternal($scope.model) === $scope.internal.value;
186
+ return round($scope.model) === round(parseText($scope.internal.value));
187
+ };
188
+
139
189
  const setModel = function (value) {
140
190
  if ($scope.decimalConstructor && value != null && !NiceService.isDecimal(value)) {
141
191
  value = new $scope.decimalConstructor(value);
@@ -150,11 +200,28 @@ angular.module('niceElements')
150
200
 
151
201
  // On input change
152
202
  $scope.inputChanged = function () {
153
- setModel($scope.internal.value);
203
+ if ($scope.decimalMode) {
204
+ // Invalid text keeps the last valid model and marks the field invalid
205
+ const value = parseText($scope.internal.value);
206
+ const isNumber = !Number.isNaN(value);
207
+ $scope.niceNumberForm.$setValidity("number", isNumber ? null : false);
208
+ if (isNumber) setModel(round(value));
209
+ } else {
210
+ setModel($scope.internal.value);
211
+ }
154
212
  $scope.check();
155
213
  };
156
214
 
157
215
 
216
+ // Decimal mode: on blur show the cleaned up value ("1,239" -> "1.24" with decimals="2")
217
+ $scope.inputBlurred = function () {
218
+ if (!$scope.decimalMode) return;
219
+ if (!Number.isNaN(parseText($scope.internal.value))) {
220
+ $scope.internal.value = toInternal($scope.model);
221
+ }
222
+ };
223
+
224
+
158
225
  // Watch for model change. Decimal is watched by value, because parent can replace it with new equal object.
159
226
  $scope.$watch(function () {
160
227
  return NiceService.isDecimal($scope.model) ? $scope.model.toString() : $scope.model;
@@ -164,8 +231,10 @@ angular.module('niceElements')
164
231
  } else if ($scope.model != null) {
165
232
  $scope.decimalConstructor = null; // Parent switched to plain value
166
233
  }
167
- if (toInternal($scope.model) !== $scope.internal.value) {
234
+ if (!isInputInSync()) {
168
235
  $scope.internal.value = toInternal($scope.model);
236
+ // Input shows a valid value again (e.g. after plus/minus)
237
+ if ($scope.decimalMode && $scope.niceNumberForm) $scope.niceNumberForm.$setValidity("number", null);
169
238
  }
170
239
  $scope.check();
171
240
  if ($scope.onChange && oldModel != newModel) {
@@ -178,7 +247,7 @@ angular.module('niceElements')
178
247
 
179
248
  // Add to the value
180
249
  $scope.add = function () {
181
- const result = new Decimal($scope.model != undefined ? $scope.model : $scope.defaultValue).plus($scope.innerStep).toNumber(); //.toFixed($scope.decimals);
250
+ const result = round(new Decimal($scope.model != undefined ? $scope.model : $scope.defaultValue).plus($scope.innerStep));
182
251
  if ($scope.max) {
183
252
  if (result <= parseFloat($scope.max)) {
184
253
  setModel(result);
@@ -194,7 +263,7 @@ angular.module('niceElements')
194
263
 
195
264
  // Subtract to the value
196
265
  $scope.subtract = function () {
197
- const result = new Decimal($scope.model != undefined ? $scope.model : $scope.defaultValue).minus($scope.innerStep).toNumber();
266
+ const result = round(new Decimal($scope.model != undefined ? $scope.model : $scope.defaultValue).minus($scope.innerStep));
198
267
  if (result >= Number($scope.min)) {
199
268
  setModel(result);
200
269
  $scope.niceNumberForm.$setDirty();
@@ -38,17 +38,22 @@ angular.module('niceElements')
38
38
  attrs.noMargin = attrs.noMargin === 'true' || attrs.noMargin === true;
39
39
  },
40
40
 
41
- controller: function ($rootScope, $scope) {
41
+ controller: function ($rootScope, $scope, $attrs) {
42
42
  // Link form object with valid object
43
- if (angular.isDefined($scope.valid)) {
44
- $scope.valid = $scope.form;
43
+ if ($attrs.valid) {
44
+ $scope.$watch('form', function (form) {
45
+ if (form) $scope.valid = form;
46
+ });
45
47
  }
46
48
 
47
49
  const roundN = function (number, decimals) {
48
50
  return Number(new Decimal(String(number)).toFixed(decimals, 4));
49
51
  };
50
52
 
51
- if (angular.isDefined($scope.model)) {
53
+ if ($scope.model === null && $scope.required) {
54
+ // Required and empty: keep the field empty so ng-required marks it invalid
55
+ $scope.internalModel = "";
56
+ } else if (angular.isDefined($scope.model)) {
52
57
  $scope.internalModel = roundN((angular.copy($scope.model) * 100), 6);
53
58
  } else {
54
59
  $scope.internalModel = "0";
@@ -61,7 +66,8 @@ angular.module('niceElements')
61
66
  if (parseFloat($scope.internalModel) > 100) $scope.internalModel = 100;
62
67
  $scope.model = roundN(parseFloat($scope.internalModel) / 100, 6);
63
68
  } else {
64
- $scope.model = 0;
69
+ // Cleared field: null when required (so the form is invalid), otherwise 0
70
+ $scope.model = $scope.required ? null : 0;
65
71
  }
66
72
  if ($scope.onChange) $scope.onChange({ model: $scope.model });
67
73
  };
@@ -83,11 +89,14 @@ angular.module('niceElements')
83
89
  $scope.$watch('model', function (value_new, value_old) {
84
90
  if (value_new) {
85
91
  $scope.internalModel = roundN(angular.copy($scope.model) * 100, 6);
92
+ } else if (value_new === null && $scope.required) {
93
+ $scope.internalModel = "";
86
94
  }
87
95
  });
88
96
 
89
97
  $scope.$watch('internalModel', function (value_new, value_old) {
90
- if (!$scope.internalModel) {
98
+ // Empty field falls back to 0, unless required (then it stays empty and invalid)
99
+ if (!$scope.internalModel && !$scope.required) {
91
100
  $scope.internalModel = "0";
92
101
  }
93
102
  });
@@ -112,13 +112,13 @@ angular.module('niceElements')
112
112
 
113
113
  $scope.close = function () {
114
114
  $scope.isOpen = false;
115
- if ($scope.onChange) $scope.onChange($scope.isOpen);
115
+ if ($scope.onChange) $scope.onChange({ isOpen: $scope.isOpen });
116
116
  };
117
117
 
118
118
  $scope.open = function () {
119
119
  if ($scope.popper) $scope.popper.update();
120
120
  $scope.isOpen = true;
121
- if ($scope.onChange) $scope.onChange($scope.isOpen);
121
+ if ($scope.onChange) $scope.onChange({ isOpen: $scope.isOpen });
122
122
  $timeout(function () {
123
123
  if ($scope.popper) $scope.popper.update();
124
124
  });
@@ -33,13 +33,11 @@
33
33
 
34
34
  <div class="nice-dropdown" ng-if="results.length">
35
35
  <div ng-repeat="result in results" class="nice-search-row" ng-class="{'active': selectedIndex == $index}" ng-click="selectRow(result)">
36
- <span class="text-bold">{{ result[keyForInputLabel] }}</span>
37
- <!--<div ng-transclude></div>-->
36
+ <span class="text-bold" ng-if="!hasRowTemplate">{{ result[keyForInputLabel] }}</span>
37
+ <div ng-if="hasRowTemplate" nice-search-row-transclude></div>
38
38
  </div>
39
39
  </div>
40
40
  </div>
41
41
  </div>
42
42
 
43
- <!--Here is injected dropdown html if passed and results present and open.-->
44
- <!--<div ng-transclude></div>-->
45
43
  </ng-form>
@@ -38,12 +38,11 @@ angular.module('niceElements')
38
38
  isInline: '=',
39
39
  onChange: '&?'
40
40
  },
41
- link: function (scope, element, attrs) {
41
+ link: function (scope, element, attrs, ctrl, transcludeFn) {
42
42
  if (!attrs.title) { attrs.title = ''; }
43
43
  if (!attrs.placeholder) { attrs.placeholder = ''; }
44
44
  if (!attrs.fieldWidth) { attrs.fieldWidth = 'col-sm-8'; }
45
45
  if (!attrs.labelWidth) { attrs.labelWidth = 'col-sm-4'; }
46
- if (!attrs.setText) { attrs.setText = ''; }
47
46
 
48
47
  attrs.required = attrs.required === 'true' || attrs.required === true;
49
48
  attrs.hideValid = attrs.hideValid === 'true' || attrs.hideValid === true;
@@ -53,15 +52,25 @@ angular.module('niceElements')
53
52
  attrs.disableRow = attrs.disableRow === 'true' || attrs.disableRow === true;
54
53
  attrs.noMargin = attrs.noMargin === 'true' || attrs.noMargin === true;
55
54
 
56
- // This is used for connecting directive's scope to transcluded html.
57
- // transcludeFn(scope, function(clone, scope) {
58
- // var el = element.find('.nice-search');
59
- // el.append(clone);
60
- // });
55
+ // Custom row content: markup placed inside <nice-search> is rendered for every
56
+ // result row (with `result` in scope) instead of the default label.
57
+ scope.rowTransclude = transcludeFn;
58
+ scope.hasRowTemplate = false;
59
+
60
+ // Make one throwaway copy of the content just to check if there is any
61
+ transcludeFn(function (clone, cloneScope) {
62
+ for (let i = 0; i < clone.length; i++) {
63
+ const isElement = clone[i].nodeType === 1;
64
+ const isText = clone[i].nodeType === 3 && clone[i].textContent.trim() !== '';
65
+ if (isElement || isText) scope.hasRowTemplate = true;
66
+ }
67
+ cloneScope.$destroy();
68
+ });
61
69
 
62
70
  // Set default text
71
+ // (skip when set-text is not used, so the label of a preset model below is kept)
63
72
  scope.$watch("setText", function () {
64
- scope.modelString = scope.setText;
73
+ if (angular.isDefined(scope.setText)) scope.modelString = scope.setText;
65
74
  });
66
75
 
67
76
  // Check if object is defined
@@ -72,18 +81,12 @@ angular.module('niceElements')
72
81
  scope.modelString = scope.model;
73
82
  }
74
83
 
75
- const setValid = function (isValid) {
76
- if (scope.required && scope.form) {
77
- scope.form.$setValidity('objectSelected', isValid);
78
- }
79
- };
80
-
81
- scope.$watch('model', function (newValue) {
82
- if (scope.model && scope.model.id) {
83
- setValid(true);
84
- } else {
85
- setValid(false);
86
- }
84
+ // When required, an object (with id) has to be selected. Also re-check when
85
+ // `required` changes, so turning it off clears the error.
86
+ scope.$watchGroup(['model', 'required'], function () {
87
+ if (!scope.form) return;
88
+ const hasObject = !!(scope.model && scope.model.id);
89
+ scope.form.$setValidity('objectSelected', !scope.required || hasObject);
87
90
  });
88
91
 
89
92
  scope.selectRow = function (obj) {
@@ -241,4 +244,21 @@ angular.module('niceElements')
241
244
  };
242
245
  }
243
246
  };
247
+ })
248
+
249
+ // Renders nice-search's transcluded content for one result row, with `result` in scope
250
+ .directive('niceSearchRowTransclude', function () {
251
+ return {
252
+ restrict: 'A',
253
+ link: function (scope, element) {
254
+ // Copy the content into this row; the copy gets its own scope (rowScope)
255
+ scope.rowTransclude(function (clone, rowScope) {
256
+ // Give the copy access to this row's result, and keep it up to date
257
+ scope.$watch('result', function (result) {
258
+ rowScope.result = result;
259
+ });
260
+ element.append(clone);
261
+ });
262
+ }
263
+ };
244
264
  });
@@ -6,7 +6,7 @@
6
6
  <!------------------------- Field ------------------------->
7
7
  <div class="nice-field col-xs-12" ng-class="[fieldWidth ? fieldWidth : 'col-sm-8', { 'nice-disabled': isDisabled }]" click-outside="close()">
8
8
  <div class="nice-search-button input-group" ng-class="{ 'disabled': isDisabled }">
9
- <input class="form-control" type="text" id="{{ id }}" ng-model="model" placeholder="{{ placeholder }}" ng-disabled="isDisabled" ng-change="updateSearch()" ng-focus="onFocus()" tabindex="{{ tabIndex }}">
9
+ <input class="form-control" type="text" id="{{ id }}" ng-model="model" placeholder="{{ placeholder }}" ng-disabled="isDisabled" ng-change="updateSearch()" ng-focus="onFocus()" ng-required="required" tabindex="{{ tabIndex }}">
10
10
 
11
11
  <button type="button" class="btn input-group-addon" ng-disabled="isDisabled" ng-click="buttonClicked()">
12
12
  <nice-icon ng-show="!loading && model != ''" icon="icon-x"></nice-icon>
@@ -32,6 +32,7 @@ angular.module('niceElements')
32
32
  debounceTime: '@',
33
33
  isInline: '=',
34
34
  help: '@',
35
+ required: '=',
35
36
  placement: '@', // Describes the preferred placement of the popper
36
37
  },
37
38
  controller: function ($scope, $timeout, $element) {