nice-elements 1.12.2 → 1.12.3

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 (154) hide show
  1. package/.vscode/settings.json +18 -18
  2. package/CHANGELOG.md +537 -532
  3. package/LICENSE.md +21 -21
  4. package/README.md +98 -98
  5. package/bower.json +17 -17
  6. package/dist/nice.js +11318 -8897
  7. package/dist/nice.min.js +1 -1
  8. package/dist/style.css +17 -7
  9. package/dist/style.css.map +1 -1
  10. package/dist/style.min.css +8 -8
  11. package/eslint.config.mjs +32 -32
  12. package/package.json +44 -44
  13. package/src/components/nice-alert/nice-alert.html +6 -6
  14. package/src/components/nice-alert/nice-alert.js +28 -28
  15. package/src/components/nice-alert/nice-alert.scss +44 -44
  16. package/src/components/nice-button/nice-button.html +4 -4
  17. package/src/components/nice-button/nice-button.js +41 -41
  18. package/src/components/nice-button/nice-button.scss +16 -16
  19. package/src/components/nice-button-toggle/nice-button-toggle.html +9 -9
  20. package/src/components/nice-button-toggle/nice-button-toggle.js +23 -23
  21. package/src/components/nice-button-toggle/nice-button-toggle.scss +2 -2
  22. package/src/components/nice-calendar/nice-calendar.html +91 -91
  23. package/src/components/nice-calendar/nice-calendar.js +346 -346
  24. package/src/components/nice-calendar/nice-calendar.scss +288 -288
  25. package/src/components/nice-checkbox/nice-checkbox.html +5 -5
  26. package/src/components/nice-checkbox/nice-checkbox.js +34 -34
  27. package/src/components/nice-checkbox/nice-checkbox.scss +91 -91
  28. package/src/components/nice-choice/nice-choice.html +18 -18
  29. package/src/components/nice-choice/nice-choice.js +212 -212
  30. package/src/components/nice-choice/nice-choice.scss +110 -110
  31. package/src/components/nice-click-outside/nice-click-outside.js +105 -105
  32. package/src/components/nice-comment/nice-comment.html +10 -10
  33. package/src/components/nice-comment/nice-comment.js +71 -71
  34. package/src/components/nice-comment/nice-comment.scss +39 -39
  35. package/src/components/nice-date/nice-date.html +82 -79
  36. package/src/components/nice-date/nice-date.js +350 -333
  37. package/src/components/nice-date/nice-date.scss +305 -305
  38. package/src/components/nice-date-input/nice-date-input.html +49 -41
  39. package/src/components/nice-date-input/nice-date-input.js +128 -111
  40. package/src/components/nice-date-input/nice-date-input.scss +53 -46
  41. package/src/components/nice-date-range/nice-date-range.html +15 -15
  42. package/src/components/nice-date-range/nice-date-range.js +78 -78
  43. package/src/components/nice-date-range/nice-date-range.scss +390 -390
  44. package/src/components/nice-datetime-picker/nice-datetime-picker.html +29 -29
  45. package/src/components/nice-datetime-picker/nice-datetime-picker.js +89 -89
  46. package/src/components/nice-datetime-picker/nice-datetime-picker.scss +428 -428
  47. package/src/components/nice-datetimerange-picker/nice-datetimerange-picker.html +55 -55
  48. package/src/components/nice-datetimerange-picker/nice-datetimerange-picker.js +248 -248
  49. package/src/components/nice-datetimerange-picker/nice-datetimerange-picker.scss +73 -73
  50. package/src/components/nice-datetimerange-picker-2/nice-datetimerange-picker-2.html +44 -44
  51. package/src/components/nice-datetimerange-picker-2/nice-datetimerange-picker-2.js +175 -175
  52. package/src/components/nice-datetimerange-picker-2/nice-datetimerange-picker-2.scss +127 -127
  53. package/src/components/nice-dropdown/nice-dropdown.html +61 -61
  54. package/src/components/nice-dropdown/nice-dropdown.js +517 -517
  55. package/src/components/nice-dropdown/nice-dropdown.scss +262 -262
  56. package/src/components/nice-dropdown-date/nice-dropdown-date.html +28 -28
  57. package/src/components/nice-dropdown-date/nice-dropdown-date.js +132 -132
  58. package/src/components/nice-dropdown-date/nice-dropdown-date.scss +87 -87
  59. package/src/components/nice-dropdown-old/nice-dropdown-old.html +44 -44
  60. package/src/components/nice-dropdown-old/nice-dropdown-old.js +330 -330
  61. package/src/components/nice-dropdown-old/nice-dropdown-old.scss +129 -129
  62. package/src/components/nice-dtp/nice-dtp.html +91 -91
  63. package/src/components/nice-dtp/nice-dtp.js +835 -835
  64. package/src/components/nice-help/nice-help.html +6 -6
  65. package/src/components/nice-help/nice-help.js +58 -58
  66. package/src/components/nice-help/nice-help.scss +78 -78
  67. package/src/components/nice-input/nice-input.html +47 -47
  68. package/src/components/nice-input/nice-input.js +250 -250
  69. package/src/components/nice-input/nice-input.scss +130 -130
  70. package/src/components/nice-label/nice-label.html +10 -10
  71. package/src/components/nice-label/nice-label.js +32 -32
  72. package/src/components/nice-loader/nice-loader.html +6 -6
  73. package/src/components/nice-loader/nice-loader.js +28 -28
  74. package/src/components/nice-loader/nice-loader.scss +47 -47
  75. package/src/components/nice-no-data/nice-no-data.html +3 -3
  76. package/src/components/nice-no-data/nice-no-data.js +27 -27
  77. package/src/components/nice-no-data/nice-no-data.scss +26 -26
  78. package/src/components/nice-notification/nice-notification.html +3 -3
  79. package/src/components/nice-notification/nice-notification.js +124 -124
  80. package/src/components/nice-notification/nice-notification.scss +66 -66
  81. package/src/components/nice-number/nice-number.html +40 -40
  82. package/src/components/nice-number/nice-number.js +172 -172
  83. package/src/components/nice-number/nice-number.scss +18 -18
  84. package/src/components/nice-on-scroll/nice-on-scroll.js +17 -17
  85. package/src/components/nice-percent/nice-percent.html +24 -24
  86. package/src/components/nice-percent/nice-percent.js +95 -95
  87. package/src/components/nice-pill/nice-pill.html +3 -3
  88. package/src/components/nice-pill/nice-pill.js +29 -29
  89. package/src/components/nice-pill/nice-pill.scss +25 -25
  90. package/src/components/nice-popup/nice-popup.html +6 -6
  91. package/src/components/nice-popup/nice-popup.js +122 -122
  92. package/src/components/nice-popup/nice-popup.scss +43 -43
  93. package/src/components/nice-progress-bar/nice-progress-bar.html +15 -15
  94. package/src/components/nice-progress-bar/nice-progress-bar.js +55 -55
  95. package/src/components/nice-progress-bar/nice-progress-bar.scss +47 -47
  96. package/src/components/nice-quantity/nice-quantity.html +22 -22
  97. package/src/components/nice-quantity/nice-quantity.js +57 -57
  98. package/src/components/nice-quantity/nice-quantity.scss +51 -51
  99. package/src/components/nice-search/nice-search.html +44 -44
  100. package/src/components/nice-search/nice-search.js +243 -243
  101. package/src/components/nice-search/nice-search.scss +90 -90
  102. package/src/components/nice-search2/nice-search2.html +32 -32
  103. package/src/components/nice-search2/nice-search2.js +267 -267
  104. package/src/components/nice-search2/nice-search2.scss +80 -80
  105. package/src/components/nice-slot/nice-slot.html +10 -10
  106. package/src/components/nice-slot/nice-slot.js +31 -31
  107. package/src/components/nice-steps/nice-steps.scss +81 -81
  108. package/src/components/nice-svgs/nice-svgs.html +1275 -1275
  109. package/src/components/nice-svgs/nice-svgs.js +19 -19
  110. package/src/components/nice-table/nice-table.html +38 -38
  111. package/src/components/nice-table/nice-table.js +60 -60
  112. package/src/components/nice-table/nice-table.scss +112 -112
  113. package/src/components/nice-time-picker/nice-time-picker.html +43 -43
  114. package/src/components/nice-time-picker/nice-time-picker.js +85 -85
  115. package/src/components/nice-time-picker/nice-time-picker.scss +37 -37
  116. package/src/components/nice-title/nice-title.html +3 -3
  117. package/src/components/nice-title/nice-title.js +21 -21
  118. package/src/components/nice-toast/nice-toast.html +2 -2
  119. package/src/components/nice-toast/nice-toast.js +56 -56
  120. package/src/components/nice-toast/nice-toast.scss +74 -74
  121. package/src/components/nice-toggle/nice-toggle.html +12 -12
  122. package/src/components/nice-toggle/nice-toggle.js +56 -56
  123. package/src/components/nice-toggle/nice-toggle.scss +131 -131
  124. package/src/components/nice-tooltip/_nice-tooltip.scss +55 -55
  125. package/src/components/nice-tooltip/nice-tooltip.js +84 -84
  126. package/src/components/nice-upload/nice-upload.html +22 -22
  127. package/src/components/nice-upload/nice-upload.js +159 -159
  128. package/src/components/nice-upload/nice-upload.scss +85 -85
  129. package/src/components/nice-view/_nice-view.scss +62 -62
  130. package/src/components/nice-view/nice-view.html +12 -12
  131. package/src/components/nice-view/nice-view.js +30 -30
  132. package/src/components/nice-virtual-scroll/_nice-virtual-scroll.scss +17 -17
  133. package/src/components/nice-virtual-scroll/nice-virtual-scroll.html +12 -12
  134. package/src/components/nice-virtual-scroll/nice-virtual-scroll.js +70 -70
  135. package/src/components/nice-wrapper/_nice-wrapper.scss +87 -87
  136. package/src/components/nice-wrapper/nice-wrapper.html +12 -12
  137. package/src/components/nice-wrapper/nice-wrapper.js +44 -44
  138. package/src/components/nice-yesno/nice-yesno.html +14 -14
  139. package/src/components/nice-yesno/nice-yesno.js +111 -111
  140. package/src/components/nice-yesno/nice-yesno.scss +95 -95
  141. package/src/filters/nice-date.js +17 -17
  142. package/src/filters/nice-highlight.js +16 -16
  143. package/src/modals/nice-multilangual-modal/nice-multilangual-modal.html +30 -30
  144. package/src/modals/nice-multilangual-modal/nice-multilangual-modal.js +67 -67
  145. package/src/modals/nice-multilangual-modal/nice-multilangual-modal.scss +19 -19
  146. package/src/nice.js +2 -2
  147. package/src/services/nice-service.js +22 -22
  148. package/src/styles/_bootstrap-variables.scss +178 -178
  149. package/src/styles/_nice-buttons.scss +612 -612
  150. package/src/styles/_nice-common.scss +471 -472
  151. package/src/styles/_nice-ngdialog.scss +85 -85
  152. package/src/styles/_nice-spacing.scss +283 -283
  153. package/src/styles/_nice.scss +71 -71
  154. package/src/styles/_variables.scss +191 -191
@@ -1,518 +1,518 @@
1
- 'use strict';
2
-
3
- /**
4
- * @ngdoc directive
5
- * @name niceElements.directive:nice-dropdown
6
- * @description
7
- * # nice-dropdown
8
- */
9
- angular.module('niceElements')
10
- .directive('niceDropdown', function () {
11
- return {
12
- templateUrl: 'src/components/nice-dropdown/nice-dropdown.html',
13
- restrict: 'E',
14
- transclude: {
15
- 'button': '?niceDropdownButton',
16
- 'option': '?niceDropdownOption'
17
- },
18
- scope: {
19
- niceTitle: '@',
20
- title: '@', // Title of the field
21
- model: '=', // Aka model
22
- list: '=', // List of options
23
- onChange: '&?',
24
- onSelect: '&?', // Like onChange but always return objects
25
- onClose: '&?',
26
- onOpen: '&?',
27
- isDisabled: '=',
28
- fieldWidth: '@',
29
- labelWidth: '@',
30
- loading: '=?',
31
- addButtonFunction: '=?',
32
- objValue: '@', // Optional - default is 'value'
33
- objKey: '@?', // Optional - default is 'id'. Used only when returnOnlyKey=true.
34
- selectedIsKey: '@?',
35
- nullable: '@', // No selection is possible
36
- required: '=', // Model cannot be NULL
37
- noMargin: '@', // margin-bottom: 0px
38
- multiple: '@', // Can select multiple items
39
- help: '@',
40
- noOptionsText: '@',
41
- noDataText: '@',
42
- selectText: '@',
43
- searchText: '@',
44
- noSearch: '=',
45
- nullableText: '@',
46
- selectedText: '@',
47
- dropdownDistance: '@',
48
- searchFunction: '=?',
49
- filterFunction: '=?',
50
- isInline: '=',
51
- clearOnSelect: '@',
52
- enableLoadMore: '@', // Enable load more
53
- placement: '@', // Describes the preferred placement of the popper
54
- },
55
- controller: function ($scope, $http, $element, $timeout, gettextCatalog, NiceService) {
56
- $scope.id = Math.random().toString(36).substring(7);
57
- if (!$scope.dropdownDistance) { $scope.dropdownDistance = 5; }
58
- if (!$scope.objValue) { $scope.objValue = 'value'; }
59
- if (!$scope.objKey) { $scope.objKey = 'id'; }
60
- if (!$scope.noOptionsText) { $scope.noOptionsText = gettextCatalog.getString("No options", null, "Nice"); }
61
- if (!$scope.noDataText) { $scope.noDataText = gettextCatalog.getString("No options", null, "Nice"); }
62
- if (!$scope.searchText) { $scope.searchText = gettextCatalog.getString("Search...", null, "Nice"); }
63
- if (!$scope.nullableText) { $scope.nullableText = gettextCatalog.getString("None", null, "Nice"); }
64
- if (!$scope.selectText) { $scope.selectText = gettextCatalog.getString("None", null, "Nice"); }
65
- if (!$scope.selectedText) { $scope.selectedText = gettextCatalog.getString("selected", null, "Nice"); }
66
- if (!$scope.addButtonFunction) { $scope.addButtonFunction = null; }
67
- if (!$scope.placement) { $scope.placement = "bottom-start"; }
68
- $scope.nullable = $scope.nullable === 'true' || $scope.nullable === true;
69
- $scope.noMargin = $scope.noMargin === 'true' || $scope.noMargin === true;
70
- $scope.multiple = $scope.multiple === 'true' || $scope.multiple === true;
71
- $scope.clearOnSelect = $scope.clearOnSelect === 'true' || $scope.clearOnSelect === true;
72
- $scope.isOpen = false;
73
- $scope.selected = null;
74
- // $scope.selectedIndex = 0;
75
- $scope.popper = null;
76
-
77
- $scope.internal = {
78
- search: ""
79
- };
80
-
81
- if ($scope.list) {
82
- $scope.originalList = angular.copy($scope.list);
83
- }
84
-
85
- // Setup popper
86
- // https://popper.js.org/docs/v2/constructors/
87
- $scope.setupPopper = function () {
88
- const button = $element[0].getElementsByClassName('btn-dropdown')[0];
89
- const tooltip = $element[0].getElementsByClassName('nice-dropdown-menu-wrapper')[0];
90
- $scope.popper = Popper.createPopper(button, tooltip, {
91
- strategy: 'fixed',
92
- placement: $scope.placement,
93
- scroll: true,
94
- resize: true,
95
- modifiers: [
96
- {
97
- name: 'offset',
98
- options: {
99
- offset: [0, $scope.dropdownDistance],
100
- },
101
- },
102
- {
103
- name: "sameWidth",
104
- enabled: true,
105
- phase: "beforeWrite",
106
- requires: ["computeStyles"],
107
- fn: function (e) {
108
- const state = e.state;
109
- let width = state.rects.reference.width;
110
- if (width < 200) width = 200;
111
- state.styles.popper.width = width + "px";
112
- },
113
- effect: function (e) {
114
- const state = e.state;
115
- let width = state.elements.reference.width;
116
- if (width < 200) width = 200;
117
- state.elements.popper.style.width = width + "px";
118
- }
119
- }
120
- ],
121
- });
122
- };
123
-
124
- $timeout(function () {
125
- $scope.setupPopper();
126
- });
127
-
128
-
129
- // ----------------------------------- Open -----------------------------------
130
- $scope.toggle = function () {
131
- if ($scope.isOpen) {
132
- $scope.close();
133
- } else {
134
- $scope.open();
135
- }
136
- };
137
-
138
- $scope.close = function () {
139
- $scope.isOpen = false;
140
- if ($scope.onClose) $scope.onClose();
141
- };
142
-
143
- $scope.open = function () {
144
- $scope.popper.update();
145
- if ($scope.filterFunction) $scope.internalList = $scope.filterFunction($scope.originalList, $scope.model);
146
- $timeout(function () {
147
- $scope.isOpen = true;
148
- if ($scope.onOpen) $scope.onOpen();
149
- $timeout(function () {
150
- $scope.popper.update();
151
- });
152
- });
153
- $timeout(function () {
154
- $scope.focusInput();
155
- $scope.scrollToHover(true);
156
- if ($scope.enableLoadMore) $scope.handleScrollToBottom();
157
- }, 100);
158
- };
159
-
160
-
161
- // ----------------------------------- Handle scroll to bottom -----------------------------------
162
- let lastScrollPosition = Infinity;
163
- $scope.handleScrollToBottom = function () {
164
- lastScrollPosition = Infinity;
165
- const element = angular.element($element[0].getElementsByClassName("nice-dropdown-items")[0]);
166
- element.bind("scroll mousewheel", function (e) {
167
- const scrollPosition = element[0].scrollHeight - element[0].scrollTop;
168
- if (scrollPosition === element[0].clientHeight && lastScrollPosition != scrollPosition) {
169
- $scope.loadMore();
170
- }
171
- if (scrollPosition <= lastScrollPosition) lastScrollPosition = scrollPosition;
172
- });
173
- }
174
-
175
-
176
- // ----------------------------------- Load more -----------------------------------
177
- $scope.loadMore = function () {
178
- if (!$scope.internalList || !$scope.internalList._metadata || !$scope.internalList._metadata.next) return;
179
- $scope.loading = true;
180
- $http({
181
- method: 'GET',
182
- url: $scope.internalList._metadata.next,
183
- headers: NiceService.getHeader()
184
- }).then(function (response) {
185
- response = response.data;
186
- const metadata = {
187
- count: response.count,
188
- previous: response.previous,
189
- next: response.next,
190
- };
191
- $scope.internalList._metadata = metadata;
192
- angular.forEach(response.results, function (item) {
193
- $scope.internalList.push(item);
194
- });
195
- lastScrollPosition = Infinity;
196
- $scope.loading = false;
197
- return response;
198
- }, function (error) {
199
- $scope.loading = false;
200
- return error;
201
- });
202
- }
203
-
204
-
205
- // ----------------------------------- Focus input -----------------------------------
206
- $scope.focusInput = function () {
207
- $timeout(function () {
208
- const input = $element[0].getElementsByClassName("search-bar-input")[0];
209
- if (input) input.focus();
210
- });
211
- };
212
-
213
-
214
- // $scope.onInputFocus = function () {
215
- // $timeout(function () {
216
- // const input = $element[0].getElementsByClassName("btn-dropdown")[0];
217
- // if (input) input.focus();
218
- // });
219
- // }
220
-
221
-
222
- // ------------------- On blur -------------------
223
- $scope.onBlur = function () {
224
- $scope.close();
225
- };
226
-
227
-
228
- $scope.handleNewInternalList = function (items) {
229
- $scope.originalList = angular.copy(items);
230
- if ($scope.filterFunction) $scope.internalList = $scope.filterFunction($scope.originalList, $scope.model);
231
- else $scope.internalList = items;
232
- }
233
-
234
-
235
- // ----------------------------------- Scroll to hover -----------------------------------
236
- $scope.scrollToHover = function (notSmooth) {
237
- const dropdownMenu = $element[0].getElementsByClassName("nice-dropdown-menu")[0];
238
- if (!dropdownMenu) return;
239
- const dropdownList = dropdownMenu.getElementsByClassName("nice-dropdown-items")[0];
240
- const hoverItem = dropdownList.getElementsByClassName("hover")[0];
241
- if (!hoverItem) return
242
- const topPos = hoverItem.offsetTop;
243
- dropdownList.scroll({
244
- top: topPos - 120,
245
- left: 0,
246
- behavior: notSmooth ? 'auto' : 'smooth'
247
- });
248
- };
249
-
250
-
251
- // ----------------------------------- Search -----------------------------------
252
- $scope.handleSearch = function () {
253
- if (!$scope.searchFunction) return;
254
- $scope.loading = true;
255
- $scope.searchFunction($scope.internal.search).then(function (response) {
256
- $scope.handleNewInternalList(response);
257
- $scope.loading = false;
258
- $scope.handleDefault();
259
- }, function () {
260
- $scope.internalList = null;
261
- $scope.loading = false;
262
- });
263
- };
264
-
265
- $scope.clearSearch = function () {
266
- $scope.internal.search = "";
267
- $scope.handleSearch();
268
- }
269
-
270
-
271
- // ----------------------------------- Item clicked -----------------------------------
272
- $scope.handleSelected = function (item, index) {
273
- $scope.formDropdown.$setDirty();
274
-
275
- if (item != null) {
276
- if ($scope.multiple) {
277
- $scope.handleMultipleSelect(item, index);
278
- } else {
279
- $scope.handleSingleSelect(item, index);
280
- }
281
- } else {
282
- $scope.handleSingleSelect(undefined, -1);
283
- }
284
- };
285
-
286
-
287
- // ----------------------------------- Handle multiple select -----------------------------------
288
- $scope.handleMultipleSelect = function (item, index) {
289
- if (!$scope.selected) $scope.selected = [];
290
-
291
- if (item._selected) {
292
- $scope.selected = $scope.selected.filter(function (s) {
293
- return s[$scope.objKey] != item[$scope.objKey];
294
- });
295
- } else {
296
- $scope.selected.push(item);
297
- }
298
-
299
- $scope.handleSetModel();
300
- };
301
-
302
-
303
- // ----------------------------------- Select all -----------------------------------
304
- $scope.selectAll = function () {
305
- if (!$scope.multiple) return;
306
- $scope.selected = $scope.internalList;
307
- angular.forEach($scope.internalList, function (o) {
308
- o._selected = true;
309
- });
310
- $scope.handleSetModel();
311
- }
312
-
313
-
314
- // ----------------------------------- Select none -----------------------------------
315
- $scope.selectNone = function () {
316
- if (!$scope.multiple) return;
317
- $scope.selected = [];
318
- angular.forEach($scope.internalList, function (o) {
319
- o._selected = false;
320
- });
321
- $scope.handleSetModel();
322
- }
323
-
324
-
325
- // ----------------------------------- Handle single slect -----------------------------------
326
- $scope.handleSingleSelect = function (item, index) {
327
- $scope.selected = item;
328
- $scope.close();
329
- $scope.handleSetModel();
330
- };
331
-
332
-
333
- // ----------------------------------- Handle set model -----------------------------------
334
- $scope.handleSetModel = function () {
335
- let obj = angular.copy($scope.selected);
336
-
337
- if ($scope.selected != null) {
338
- // Remove selected flag
339
- if (!$scope.selectedIsKey) {
340
- if ($scope.multiple) {
341
- angular.forEach(obj, function (o) {
342
- o._selected = undefined;
343
- });
344
- } else {
345
- obj._selected = undefined;
346
- }
347
- }
348
-
349
- // Selected is object
350
- if ($scope.selectedIsKey) {
351
- if ($scope.multiple) {
352
- obj = obj.map(function (o) {
353
- return o[$scope.objKey];
354
- });
355
- } else {
356
- obj = obj[$scope.objKey];
357
- }
358
- }
359
- }
360
-
361
- if ($scope.clearOnSelect) {
362
- // Clear on select
363
- $scope.model = null;
364
- } else {
365
- // Set model
366
- $scope.model = obj;
367
- }
368
-
369
- // Trigger on change
370
- $timeout(function () {
371
- if ($scope.onChange) $scope.onChange({ model: obj });
372
- if ($scope.onSelect) $scope.onSelect({ model: angular.copy($scope.selected) });
373
- })
374
- };
375
-
376
-
377
- // ----------------------------------- Handle default -----------------------------------
378
- $scope.handleDefault = function () {
379
- if ($scope.model) $scope.handleModelChange();
380
- if (!$scope.nullable && !$scope.model && !$scope.clearOnSelect && $scope.internalList && $scope.internalList.length > 0) {
381
- $scope.handleSelected($scope.internalList[0], 0);
382
- }
383
- };
384
-
385
- // ----------------------------------- Watch for list change -----------------------------------
386
- $scope.$watchCollection('list', function (value_new, value_old) {
387
- $scope.handleNewInternalList(angular.copy($scope.list));
388
- $scope.handleDefault();
389
- });
390
-
391
-
392
- // ----------------------------------- Watch for model change -----------------------------------
393
- $scope.$watch('model', function (value_new, value_old) {
394
- $scope.handleModelChange();
395
- });
396
-
397
- $scope.handleModelChange = function () {
398
- $scope.selected = angular.copy($scope.model);
399
- angular.forEach($scope.internalList, function (i, index) {
400
- i._selected = false;
401
-
402
- if ($scope.selectedIsKey) {
403
- // Not object
404
- if ($scope.multiple) {
405
- // Multiple
406
- angular.forEach($scope.selected, function (s, index) {
407
- if (i[$scope.objKey] == s) {
408
- i._selected = true;
409
- // $scope.selected.push(i);
410
- $scope.selected[index] = i
411
- // $scope.selectedIndex = index;
412
- }
413
- });
414
- } else {
415
- // Single
416
- if ($scope.selected != null && i[$scope.objKey] == $scope.selected) {
417
- i._selected = true;
418
- $scope.selected = i;
419
- // $scope.selectedIndex = index;
420
- $scope.scrollToHover();
421
- }
422
- }
423
- } else {
424
- // Is object
425
- if ($scope.multiple) {
426
- // Multiple
427
- angular.forEach($scope.selected, function (s) {
428
- if (i[$scope.objKey] == s[$scope.objKey]) {
429
- i._selected = true;
430
- // $scope.selected.push(i);
431
- // $scope.selectedIndex = index;
432
- }
433
- });
434
- } else {
435
- // Single
436
- if ($scope.selected != null && i[$scope.objKey] == $scope.selected[$scope.objKey]) {
437
- i._selected = true;
438
- $scope.selected = i;
439
- // $scope.selectedIndex = index;
440
- $scope.scrollToHover();
441
- }
442
- }
443
- }
444
- });
445
-
446
- // Handle required
447
- $timeout(function () {
448
- if ($scope.formDropdown) $scope.formDropdown.$setValidity('required', !(!$scope.selected && $scope.required));
449
- });
450
- }
451
-
452
- // ----------------------------------- Watch for keydown and keypress -----------------------------------
453
- // $element.bind("keydown keypress", function (event) {
454
- // // Arrow Up
455
- // if (event.keyCode == 38) {
456
- // event.preventDefault();
457
- // $timeout(function () {
458
- // if ($scope.selectedIndex > 0) {
459
- // $scope.selectedIndex -= 1;
460
- // $timeout(function () {
461
- // $scope.scrollToHover();
462
- // });
463
- // }
464
- // });
465
- // }
466
-
467
- // // Arrow Down
468
- // if (event.keyCode == 40) {
469
- // event.preventDefault();
470
- // $timeout(function () {
471
- // if ($scope.internalList && $scope.selectedIndex < $scope.internalList.length - 1) {
472
- // $scope.selectedIndex += 1;
473
- // $timeout(function () {
474
- // $scope.scrollToHover();
475
- // });
476
- // }
477
- // });
478
- // }
479
-
480
- // // Enter
481
- // if (event.keyCode == 13) {
482
- // event.preventDefault();
483
- // $timeout(function () {
484
- // $scope.handleSelected($scope.internalList[$scope.selectedIndex], $scope.selectedIndex);
485
- // });
486
- // }
487
-
488
- // // Escape
489
- // if (event.keyCode == 27) {
490
- // $timeout(function () {
491
- // $scope.close();
492
- // });
493
- // }
494
- // });
495
-
496
- $scope.handleDefault();
497
-
498
- // ----------------------------------- Init search -----------------------------------
499
- if ($scope.searchFunction) {
500
- $scope.handleSearch();
501
- }
502
-
503
-
504
- // ----------------------------------- Handle add button function -----------------------------------
505
- $scope.handleAddButton = function ($event) {
506
- let element = $event.target;
507
- while (true) {
508
- if (element.nodeName == 'BUTTON') {
509
- element.blur();
510
- break;
511
- }
512
- element = element.parentElement;
513
- }
514
- $scope.addButtonFunction();
515
- }
516
- }
517
- };
1
+ 'use strict';
2
+
3
+ /**
4
+ * @ngdoc directive
5
+ * @name niceElements.directive:nice-dropdown
6
+ * @description
7
+ * # nice-dropdown
8
+ */
9
+ angular.module('niceElements')
10
+ .directive('niceDropdown', function () {
11
+ return {
12
+ templateUrl: 'src/components/nice-dropdown/nice-dropdown.html',
13
+ restrict: 'E',
14
+ transclude: {
15
+ 'button': '?niceDropdownButton',
16
+ 'option': '?niceDropdownOption'
17
+ },
18
+ scope: {
19
+ niceTitle: '@',
20
+ title: '@', // Title of the field
21
+ model: '=', // Aka model
22
+ list: '=', // List of options
23
+ onChange: '&?',
24
+ onSelect: '&?', // Like onChange but always return objects
25
+ onClose: '&?',
26
+ onOpen: '&?',
27
+ isDisabled: '=',
28
+ fieldWidth: '@',
29
+ labelWidth: '@',
30
+ loading: '=?',
31
+ addButtonFunction: '=?',
32
+ objValue: '@', // Optional - default is 'value'
33
+ objKey: '@?', // Optional - default is 'id'. Used only when returnOnlyKey=true.
34
+ selectedIsKey: '@?',
35
+ nullable: '@', // No selection is possible
36
+ required: '=', // Model cannot be NULL
37
+ noMargin: '@', // margin-bottom: 0px
38
+ multiple: '@', // Can select multiple items
39
+ help: '@',
40
+ noOptionsText: '@',
41
+ noDataText: '@',
42
+ selectText: '@',
43
+ searchText: '@',
44
+ noSearch: '=',
45
+ nullableText: '@',
46
+ selectedText: '@',
47
+ dropdownDistance: '@',
48
+ searchFunction: '=?',
49
+ filterFunction: '=?',
50
+ isInline: '=',
51
+ clearOnSelect: '@',
52
+ enableLoadMore: '@', // Enable load more
53
+ placement: '@', // Describes the preferred placement of the popper
54
+ },
55
+ controller: function ($scope, $http, $element, $timeout, gettextCatalog, NiceService) {
56
+ $scope.id = Math.random().toString(36).substring(7);
57
+ if (!$scope.dropdownDistance) { $scope.dropdownDistance = 5; }
58
+ if (!$scope.objValue) { $scope.objValue = 'value'; }
59
+ if (!$scope.objKey) { $scope.objKey = 'id'; }
60
+ if (!$scope.noOptionsText) { $scope.noOptionsText = gettextCatalog.getString("No options", null, "Nice"); }
61
+ if (!$scope.noDataText) { $scope.noDataText = gettextCatalog.getString("No options", null, "Nice"); }
62
+ if (!$scope.searchText) { $scope.searchText = gettextCatalog.getString("Search...", null, "Nice"); }
63
+ if (!$scope.nullableText) { $scope.nullableText = gettextCatalog.getString("None", null, "Nice"); }
64
+ if (!$scope.selectText) { $scope.selectText = gettextCatalog.getString("None", null, "Nice"); }
65
+ if (!$scope.selectedText) { $scope.selectedText = gettextCatalog.getString("selected", null, "Nice"); }
66
+ if (!$scope.addButtonFunction) { $scope.addButtonFunction = null; }
67
+ if (!$scope.placement) { $scope.placement = "bottom-start"; }
68
+ $scope.nullable = $scope.nullable === 'true' || $scope.nullable === true;
69
+ $scope.noMargin = $scope.noMargin === 'true' || $scope.noMargin === true;
70
+ $scope.multiple = $scope.multiple === 'true' || $scope.multiple === true;
71
+ $scope.clearOnSelect = $scope.clearOnSelect === 'true' || $scope.clearOnSelect === true;
72
+ $scope.isOpen = false;
73
+ $scope.selected = null;
74
+ // $scope.selectedIndex = 0;
75
+ $scope.popper = null;
76
+
77
+ $scope.internal = {
78
+ search: ""
79
+ };
80
+
81
+ if ($scope.list) {
82
+ $scope.originalList = angular.copy($scope.list);
83
+ }
84
+
85
+ // Setup popper
86
+ // https://popper.js.org/docs/v2/constructors/
87
+ $scope.setupPopper = function () {
88
+ const button = $element[0].getElementsByClassName('btn-dropdown')[0];
89
+ const tooltip = $element[0].getElementsByClassName('nice-dropdown-menu-wrapper')[0];
90
+ $scope.popper = Popper.createPopper(button, tooltip, {
91
+ strategy: 'fixed',
92
+ placement: $scope.placement,
93
+ scroll: true,
94
+ resize: true,
95
+ modifiers: [
96
+ {
97
+ name: 'offset',
98
+ options: {
99
+ offset: [0, $scope.dropdownDistance],
100
+ },
101
+ },
102
+ {
103
+ name: "sameWidth",
104
+ enabled: true,
105
+ phase: "beforeWrite",
106
+ requires: ["computeStyles"],
107
+ fn: function (e) {
108
+ const state = e.state;
109
+ let width = state.rects.reference.width;
110
+ if (width < 200) width = 200;
111
+ state.styles.popper.width = width + "px";
112
+ },
113
+ effect: function (e) {
114
+ const state = e.state;
115
+ let width = state.elements.reference.width;
116
+ if (width < 200) width = 200;
117
+ state.elements.popper.style.width = width + "px";
118
+ }
119
+ }
120
+ ],
121
+ });
122
+ };
123
+
124
+ $timeout(function () {
125
+ $scope.setupPopper();
126
+ });
127
+
128
+
129
+ // ----------------------------------- Open -----------------------------------
130
+ $scope.toggle = function () {
131
+ if ($scope.isOpen) {
132
+ $scope.close();
133
+ } else {
134
+ $scope.open();
135
+ }
136
+ };
137
+
138
+ $scope.close = function () {
139
+ $scope.isOpen = false;
140
+ if ($scope.onClose) $scope.onClose();
141
+ };
142
+
143
+ $scope.open = function () {
144
+ $scope.popper.update();
145
+ if ($scope.filterFunction) $scope.internalList = $scope.filterFunction($scope.originalList, $scope.model);
146
+ $timeout(function () {
147
+ $scope.isOpen = true;
148
+ if ($scope.onOpen) $scope.onOpen();
149
+ $timeout(function () {
150
+ $scope.popper.update();
151
+ });
152
+ });
153
+ $timeout(function () {
154
+ $scope.focusInput();
155
+ $scope.scrollToHover(true);
156
+ if ($scope.enableLoadMore) $scope.handleScrollToBottom();
157
+ }, 100);
158
+ };
159
+
160
+
161
+ // ----------------------------------- Handle scroll to bottom -----------------------------------
162
+ let lastScrollPosition = Infinity;
163
+ $scope.handleScrollToBottom = function () {
164
+ lastScrollPosition = Infinity;
165
+ const element = angular.element($element[0].getElementsByClassName("nice-dropdown-items")[0]);
166
+ element.bind("scroll mousewheel", function (e) {
167
+ const scrollPosition = element[0].scrollHeight - element[0].scrollTop;
168
+ if (scrollPosition === element[0].clientHeight && lastScrollPosition != scrollPosition) {
169
+ $scope.loadMore();
170
+ }
171
+ if (scrollPosition <= lastScrollPosition) lastScrollPosition = scrollPosition;
172
+ });
173
+ }
174
+
175
+
176
+ // ----------------------------------- Load more -----------------------------------
177
+ $scope.loadMore = function () {
178
+ if (!$scope.internalList || !$scope.internalList._metadata || !$scope.internalList._metadata.next) return;
179
+ $scope.loading = true;
180
+ $http({
181
+ method: 'GET',
182
+ url: $scope.internalList._metadata.next,
183
+ headers: NiceService.getHeader()
184
+ }).then(function (response) {
185
+ response = response.data;
186
+ const metadata = {
187
+ count: response.count,
188
+ previous: response.previous,
189
+ next: response.next,
190
+ };
191
+ $scope.internalList._metadata = metadata;
192
+ angular.forEach(response.results, function (item) {
193
+ $scope.internalList.push(item);
194
+ });
195
+ lastScrollPosition = Infinity;
196
+ $scope.loading = false;
197
+ return response;
198
+ }, function (error) {
199
+ $scope.loading = false;
200
+ return error;
201
+ });
202
+ }
203
+
204
+
205
+ // ----------------------------------- Focus input -----------------------------------
206
+ $scope.focusInput = function () {
207
+ $timeout(function () {
208
+ const input = $element[0].getElementsByClassName("search-bar-input")[0];
209
+ if (input) input.focus();
210
+ });
211
+ };
212
+
213
+
214
+ // $scope.onInputFocus = function () {
215
+ // $timeout(function () {
216
+ // const input = $element[0].getElementsByClassName("btn-dropdown")[0];
217
+ // if (input) input.focus();
218
+ // });
219
+ // }
220
+
221
+
222
+ // ------------------- On blur -------------------
223
+ $scope.onBlur = function () {
224
+ $scope.close();
225
+ };
226
+
227
+
228
+ $scope.handleNewInternalList = function (items) {
229
+ $scope.originalList = angular.copy(items);
230
+ if ($scope.filterFunction) $scope.internalList = $scope.filterFunction($scope.originalList, $scope.model);
231
+ else $scope.internalList = items;
232
+ }
233
+
234
+
235
+ // ----------------------------------- Scroll to hover -----------------------------------
236
+ $scope.scrollToHover = function (notSmooth) {
237
+ const dropdownMenu = $element[0].getElementsByClassName("nice-dropdown-menu")[0];
238
+ if (!dropdownMenu) return;
239
+ const dropdownList = dropdownMenu.getElementsByClassName("nice-dropdown-items")[0];
240
+ const hoverItem = dropdownList.getElementsByClassName("hover")[0];
241
+ if (!hoverItem) return
242
+ const topPos = hoverItem.offsetTop;
243
+ dropdownList.scroll({
244
+ top: topPos - 120,
245
+ left: 0,
246
+ behavior: notSmooth ? 'auto' : 'smooth'
247
+ });
248
+ };
249
+
250
+
251
+ // ----------------------------------- Search -----------------------------------
252
+ $scope.handleSearch = function () {
253
+ if (!$scope.searchFunction) return;
254
+ $scope.loading = true;
255
+ $scope.searchFunction($scope.internal.search).then(function (response) {
256
+ $scope.handleNewInternalList(response);
257
+ $scope.loading = false;
258
+ $scope.handleDefault();
259
+ }, function () {
260
+ $scope.internalList = null;
261
+ $scope.loading = false;
262
+ });
263
+ };
264
+
265
+ $scope.clearSearch = function () {
266
+ $scope.internal.search = "";
267
+ $scope.handleSearch();
268
+ }
269
+
270
+
271
+ // ----------------------------------- Item clicked -----------------------------------
272
+ $scope.handleSelected = function (item, index) {
273
+ $scope.formDropdown.$setDirty();
274
+
275
+ if (item != null) {
276
+ if ($scope.multiple) {
277
+ $scope.handleMultipleSelect(item, index);
278
+ } else {
279
+ $scope.handleSingleSelect(item, index);
280
+ }
281
+ } else {
282
+ $scope.handleSingleSelect(undefined, -1);
283
+ }
284
+ };
285
+
286
+
287
+ // ----------------------------------- Handle multiple select -----------------------------------
288
+ $scope.handleMultipleSelect = function (item, index) {
289
+ if (!$scope.selected) $scope.selected = [];
290
+
291
+ if (item._selected) {
292
+ $scope.selected = $scope.selected.filter(function (s) {
293
+ return s[$scope.objKey] != item[$scope.objKey];
294
+ });
295
+ } else {
296
+ $scope.selected.push(item);
297
+ }
298
+
299
+ $scope.handleSetModel();
300
+ };
301
+
302
+
303
+ // ----------------------------------- Select all -----------------------------------
304
+ $scope.selectAll = function () {
305
+ if (!$scope.multiple) return;
306
+ $scope.selected = $scope.internalList;
307
+ angular.forEach($scope.internalList, function (o) {
308
+ o._selected = true;
309
+ });
310
+ $scope.handleSetModel();
311
+ }
312
+
313
+
314
+ // ----------------------------------- Select none -----------------------------------
315
+ $scope.selectNone = function () {
316
+ if (!$scope.multiple) return;
317
+ $scope.selected = [];
318
+ angular.forEach($scope.internalList, function (o) {
319
+ o._selected = false;
320
+ });
321
+ $scope.handleSetModel();
322
+ }
323
+
324
+
325
+ // ----------------------------------- Handle single slect -----------------------------------
326
+ $scope.handleSingleSelect = function (item, index) {
327
+ $scope.selected = item;
328
+ $scope.close();
329
+ $scope.handleSetModel();
330
+ };
331
+
332
+
333
+ // ----------------------------------- Handle set model -----------------------------------
334
+ $scope.handleSetModel = function () {
335
+ let obj = angular.copy($scope.selected);
336
+
337
+ if ($scope.selected != null) {
338
+ // Remove selected flag
339
+ if (!$scope.selectedIsKey) {
340
+ if ($scope.multiple) {
341
+ angular.forEach(obj, function (o) {
342
+ o._selected = undefined;
343
+ });
344
+ } else {
345
+ obj._selected = undefined;
346
+ }
347
+ }
348
+
349
+ // Selected is object
350
+ if ($scope.selectedIsKey) {
351
+ if ($scope.multiple) {
352
+ obj = obj.map(function (o) {
353
+ return o[$scope.objKey];
354
+ });
355
+ } else {
356
+ obj = obj[$scope.objKey];
357
+ }
358
+ }
359
+ }
360
+
361
+ if ($scope.clearOnSelect) {
362
+ // Clear on select
363
+ $scope.model = null;
364
+ } else {
365
+ // Set model
366
+ $scope.model = obj;
367
+ }
368
+
369
+ // Trigger on change
370
+ $timeout(function () {
371
+ if ($scope.onChange) $scope.onChange({ model: obj });
372
+ if ($scope.onSelect) $scope.onSelect({ model: angular.copy($scope.selected) });
373
+ })
374
+ };
375
+
376
+
377
+ // ----------------------------------- Handle default -----------------------------------
378
+ $scope.handleDefault = function () {
379
+ if ($scope.model) $scope.handleModelChange();
380
+ if (!$scope.nullable && !$scope.model && !$scope.clearOnSelect && $scope.internalList && $scope.internalList.length > 0) {
381
+ $scope.handleSelected($scope.internalList[0], 0);
382
+ }
383
+ };
384
+
385
+ // ----------------------------------- Watch for list change -----------------------------------
386
+ $scope.$watchCollection('list', function (value_new, value_old) {
387
+ $scope.handleNewInternalList(angular.copy($scope.list));
388
+ $scope.handleDefault();
389
+ });
390
+
391
+
392
+ // ----------------------------------- Watch for model change -----------------------------------
393
+ $scope.$watch('model', function (value_new, value_old) {
394
+ $scope.handleModelChange();
395
+ });
396
+
397
+ $scope.handleModelChange = function () {
398
+ $scope.selected = angular.copy($scope.model);
399
+ angular.forEach($scope.internalList, function (i, index) {
400
+ i._selected = false;
401
+
402
+ if ($scope.selectedIsKey) {
403
+ // Not object
404
+ if ($scope.multiple) {
405
+ // Multiple
406
+ angular.forEach($scope.selected, function (s, index) {
407
+ if (i[$scope.objKey] == s) {
408
+ i._selected = true;
409
+ // $scope.selected.push(i);
410
+ $scope.selected[index] = i
411
+ // $scope.selectedIndex = index;
412
+ }
413
+ });
414
+ } else {
415
+ // Single
416
+ if ($scope.selected != null && i[$scope.objKey] == $scope.selected) {
417
+ i._selected = true;
418
+ $scope.selected = i;
419
+ // $scope.selectedIndex = index;
420
+ $scope.scrollToHover();
421
+ }
422
+ }
423
+ } else {
424
+ // Is object
425
+ if ($scope.multiple) {
426
+ // Multiple
427
+ angular.forEach($scope.selected, function (s) {
428
+ if (i[$scope.objKey] == s[$scope.objKey]) {
429
+ i._selected = true;
430
+ // $scope.selected.push(i);
431
+ // $scope.selectedIndex = index;
432
+ }
433
+ });
434
+ } else {
435
+ // Single
436
+ if ($scope.selected != null && i[$scope.objKey] == $scope.selected[$scope.objKey]) {
437
+ i._selected = true;
438
+ $scope.selected = i;
439
+ // $scope.selectedIndex = index;
440
+ $scope.scrollToHover();
441
+ }
442
+ }
443
+ }
444
+ });
445
+
446
+ // Handle required
447
+ $timeout(function () {
448
+ if ($scope.formDropdown) $scope.formDropdown.$setValidity('required', !(!$scope.selected && $scope.required));
449
+ });
450
+ }
451
+
452
+ // ----------------------------------- Watch for keydown and keypress -----------------------------------
453
+ // $element.bind("keydown keypress", function (event) {
454
+ // // Arrow Up
455
+ // if (event.keyCode == 38) {
456
+ // event.preventDefault();
457
+ // $timeout(function () {
458
+ // if ($scope.selectedIndex > 0) {
459
+ // $scope.selectedIndex -= 1;
460
+ // $timeout(function () {
461
+ // $scope.scrollToHover();
462
+ // });
463
+ // }
464
+ // });
465
+ // }
466
+
467
+ // // Arrow Down
468
+ // if (event.keyCode == 40) {
469
+ // event.preventDefault();
470
+ // $timeout(function () {
471
+ // if ($scope.internalList && $scope.selectedIndex < $scope.internalList.length - 1) {
472
+ // $scope.selectedIndex += 1;
473
+ // $timeout(function () {
474
+ // $scope.scrollToHover();
475
+ // });
476
+ // }
477
+ // });
478
+ // }
479
+
480
+ // // Enter
481
+ // if (event.keyCode == 13) {
482
+ // event.preventDefault();
483
+ // $timeout(function () {
484
+ // $scope.handleSelected($scope.internalList[$scope.selectedIndex], $scope.selectedIndex);
485
+ // });
486
+ // }
487
+
488
+ // // Escape
489
+ // if (event.keyCode == 27) {
490
+ // $timeout(function () {
491
+ // $scope.close();
492
+ // });
493
+ // }
494
+ // });
495
+
496
+ $scope.handleDefault();
497
+
498
+ // ----------------------------------- Init search -----------------------------------
499
+ if ($scope.searchFunction) {
500
+ $scope.handleSearch();
501
+ }
502
+
503
+
504
+ // ----------------------------------- Handle add button function -----------------------------------
505
+ $scope.handleAddButton = function ($event) {
506
+ let element = $event.target;
507
+ while (true) {
508
+ if (element.nodeName == 'BUTTON') {
509
+ element.blur();
510
+ break;
511
+ }
512
+ element = element.parentElement;
513
+ }
514
+ $scope.addButtonFunction();
515
+ }
516
+ }
517
+ };
518
518
  });