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