-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectByLabelModule.js
More file actions
140 lines (119 loc) · 4.46 KB
/
selectByLabelModule.js
File metadata and controls
140 lines (119 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
angular.module("SelectByLabelModule", [])
.directive('selectByLabel', ['$timeout', function($timeout) {
var _TEMPLATE = `
<span ng-repeat="obj in sbl.config.options track by $index">
<a
class="label label-clickable" href="#"
ng-class="{'label-success': sbl.isSelected(obj), 'label-default': !sbl.isSelected(obj), 'disabled': sbl.isDisabled(obj)}"
ng-click="sbl.toggleSelection(obj,sbl.isDisabled(obj));"
ng-attr-title="{{sbl.config.title ? obj[sbl.config.title] : ''}}">
{{sbl.getDisplayValue(obj)}}
</a>
</span>`
return {
restrict: "AE",
template: _TEMPLATE,
controllerAs: 'sbl',
bindToController: true,
scope: {
options: "=", // Array of options or object, each option a property
disabledOptions: "=?", // array of disabled options
selectedOptions: "=?", // Array of selected options, OR
displayProperty: "@?", // item property used for display value
sortProperty: "@?", // Sort labels based on this property value
titleProperty: "@?", // Display this property as the popover text
selectedProperty: "@?", // Property in the items/objects set to true if selected
disabledProperty: "@?", // Property (boolean) indicating the object is disabled
config: "=?", // object with property defs
onChange: "&", // Callback when the selection changes; passes back selection as 'selected'
disabled: "@?", // this component is disabled
//returnAsObject: "=?",
//autoCollapse: "=?"
},
controller: function($scope, $q, $timeout) {
var sbl = this;
sbl.getDisplayValue = getDisplayValue;
sbl.toggleSelection = toggleSelection;
sbl.isOptionVisible = isOptionVisible;
sbl.isDisabled = isDisabled;
sbl.isIn = isIn;
sbl.isSelected = isSelected;
if (!sbl.config) {
sbl.config = {};
if (sbl.disabledProperty) sbl.config.disabled = sbl.disabledProperty;
if (sbl.disabledOptions) sbl.config.disabled = sbl.disabledOptions;
if (sbl.displayProperty) sbl.config.display = sbl.displayProperty;
if (sbl.selectedProperty) sbl.config.selected = sbl.selectedProperty;
if (sbl.selectedOptions) sbl.config.selected = sbl.selectedOptions;
if (sbl.titleProperty) sbl.title = sbl.titleProperty;
if (sbl.sortProperty) sbl.config.sort = sbl.sortProperty;
if (!sbl.config.selected) sbl.config.selected = [];
}
setOptions(sbl.options);
$scope.$watch('sbl.options',function(newVal,oldVal) {
if (newVal == oldVal) return;
$timeout(function() {
setOptions(newVal);
})
});
function setOptions(options) {
if (angular.isArray(options)) {
sbl.config.options = options;
} else if (angular.isObject(options)) {
let newOptions = [];
for (let prop in options) {
let obj = options[prop];
newOptions.push(obj);
}
sbl.config.options = newOptions;
}
}
function getDisplayValue(obj) {
if (!obj) return "";
if (typeof obj === 'string') return obj;
if (!sbl.config.display || typeof sbl.config.display != 'string' || !obj[sbl.config.display] || typeof obj[sbl.config.display] !== 'string') {
return '!err';
}
return obj[sbl.config.display];
}
function isSelected(obj) {
if (angular.isArray(sbl.config.selected)) {
return isIn(obj,sbl.config.selected);
} else if (angular.isString(sbl.config.selected)) {
return obj[sbl.config.selected];
}
return false;
}
function isDisabled(obj) {
if (sbl.disabled) return true
let isDisabled = false;
if (angular.isArray(sbl.config.disabled)) {
return isIn(obj,sbl.config.disabled);
} else if (angular.isString(sbl.config.disabled)) {
return obj[sbl.config.disabled];
}
return isDisabled;
}
function isIn(obj, objs) {
if (!objs) return false;
let idx = objs.indexOf(obj);
return idx > -1;
}
function isOptionVisible(obj) {
return true;
}
function toggleSelection(obj, isDisabled) {
if (sbl.isDisabled(obj)) return;
if (angular.isArray(sbl.config.selected)) {
let idx = sbl.config.selected.indexOf(obj);
if (idx > -1) sbl.config.selected.splice(idx,1)
else sbl.config.selected.push(obj);
sbl.onChange({selected: sbl.config.selected});
} else if (angular.isString(sbl.config.selected)) {
obj[sbl.config.selected] = !obj[sbl.config.selected];
if (sbl.onChange) sbl.onChange({selected: obj});
}
}
}
}
}]);