Skip to content

Commit 298e620

Browse files
committed
Add option to persist labels to the URL search query
1 parent 631d3cb commit 298e620

File tree

2 files changed

+74
-10
lines changed

2 files changed

+74
-10
lines changed

labelFilter.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
try { angular.module("kubernetesUI") } catch(e) { angular.module("kubernetesUI", []) }
44

55
angular.module('kubernetesUI')
6-
.factory('LabelFilter', [function() {
6+
.factory('LabelFilter', function($location) {
77
function LabelFilter() {
88
this._existingLabels = {};
99
this._labelSelector = new LabelSelector(null, true);
@@ -34,6 +34,42 @@ angular.module('kubernetesUI')
3434
}
3535
};
3636

37+
LabelFilter.prototype.persistFilterState = function(persist) {
38+
this._shouldPersistState = !!persist;
39+
};
40+
41+
LabelFilter.prototype._persistState = function() {
42+
if (!this._shouldPersistState) {
43+
return;
44+
}
45+
if (this._labelSelector.isEmpty()) {
46+
var search = $location.search();
47+
search.labelFilter = null;
48+
$location.replace().search(search);
49+
}
50+
else {
51+
var search = $location.search();
52+
search.labelFilter = this._labelSelector.exportJSON();
53+
$location.replace().search(search);
54+
}
55+
};
56+
57+
LabelFilter.prototype.readPersistedState = function() {
58+
var labelFilterStr = $location.search().labelFilter;
59+
if (labelFilterStr) {
60+
try {
61+
this._labelSelector = new LabelSelector(JSON.parse(labelFilterStr), true);
62+
}
63+
catch(e) {
64+
// wasn't valid JSON so don't use the data
65+
this._labelSelector = new LabelSelector({}, true);
66+
}
67+
}
68+
else {
69+
this._labelSelector = new LabelSelector({}, true);
70+
}
71+
};
72+
3773
LabelFilter.prototype._extractLabelsFromItem = function(item, map) {
3874
var labels = item.metadata ? item.metadata.labels : {};
3975
var self = this;
@@ -71,6 +107,8 @@ angular.module('kubernetesUI')
71107
}
72108
}
73109

110+
this._persistState();
111+
74112
if (!dontFireCallbacks) {
75113
this._onActiveFiltersChangedCallbacks.fire(this._labelSelector);
76114
}
@@ -121,11 +159,11 @@ angular.module('kubernetesUI')
121159
$('<span>')
122160
.text(opts.addButtonText || "Add Filter")
123161
);
124-
162+
125163
this._labelFilterActiveFiltersElement = $('<span>')
126164
.addClass("label-filter-active-filters")
127165
.appendTo(activeFiltersElement);
128-
166+
129167
// Render active filters area
130168
this._labelFilterActiveElement = $('<span>')
131169
.addClass("label-filter-clear")
@@ -347,6 +385,7 @@ angular.module('kubernetesUI')
347385

348386
LabelFilter.prototype._addActiveFilter = function(key, operator, values) {
349387
var filter = this._labelSelector.addConjunct(key, operator, values);
388+
this._persistState();
350389
this._onActiveFiltersChangedCallbacks.fire(this._labelSelector);
351390
this._renderActiveFilter(filter);
352391
};
@@ -378,11 +417,13 @@ angular.module('kubernetesUI')
378417
}
379418

380419
this._labelSelector.removeConjunct(filter);
420+
this._persistState();
381421
this._onActiveFiltersChangedCallbacks.fire(this._labelSelector);
382422
};
383423

384424
LabelFilter.prototype._clearActiveFilters = function() {
385425
this._labelSelector.clearConjuncts();
426+
this._persistState();
386427
this._onActiveFiltersChangedCallbacks.fire(this._labelSelector);
387428
};
388429

@@ -406,4 +447,4 @@ angular.module('kubernetesUI')
406447
};
407448

408449
return new LabelFilter();
409-
}]);
450+
});

labelSelector.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ function LabelSelector(selector, emptySelectsAll) {
1313
// as well as the new matchLabel and matchExpression syntax on newer controllers like ReplicaSets
1414
// For now it will also handle key: null as key exists for backwards compatibility from before
1515
// the matchExpression support was added.
16-
var OPERATOR_MAP = {
16+
this._OPERATOR_MAP = {
1717
"In": "in",
1818
"NotIn": "not in",
1919
"Exists": "exists",
2020
"DoesNotExist": "does not exist"
2121
};
22+
this._REVERSE_OPERATOR_MAP = {
23+
"in": "In",
24+
"not in": "NotIn",
25+
"exists": "Exists",
26+
"does not exist": "DoesNotExist"
27+
};
2228

2329
if (selector) {
2430
if (selector.matchLabels || selector.matchExpressions) {
2531
angular.forEach(selector.matchLabels, function(details, key) {
2632
this.addConjunct(key, "in", [details]);
2733
}, this);
2834
angular.forEach(selector.matchExpressions, function(expression){
29-
this.addConjunct(expression.key, OPERATOR_MAP[expression.operator], expression.values);
35+
this.addConjunct(expression.key, this._OPERATOR_MAP[expression.operator], expression.values);
3036
}, this);
3137
}
3238
else {
@@ -35,7 +41,7 @@ function LabelSelector(selector, emptySelectsAll) {
3541
this.addConjunct(key, "in", [details]);
3642
}
3743
else {
38-
this.addConjunct(key, "exists", []);
44+
this.addConjunct(key, "exists", []);
3945
}
4046
}, this);
4147
}
@@ -59,7 +65,7 @@ LabelSelector.prototype.addConjunct = function(key, operator, values) {
5965
// object that was returned from a call to addConjunct
6066
LabelSelector.prototype.removeConjunct = function(conjunct) {
6167
if (conjunct.id) {
62-
delete this._conjuncts[conjunct.id];
68+
delete this._conjuncts[conjunct.id];
6369
}
6470
else {
6571
delete this._conjuncts[conjunct];
@@ -139,7 +145,7 @@ LabelSelector.prototype.matches = function(resource) {
139145
}
140146
}
141147
return true;
142-
};
148+
};
143149

144150
LabelSelector.prototype.hasConjunct = function(conjunct) {
145151
return this._conjuncts[this._getIdForConjunct(conjunct)] ? true : false;
@@ -164,6 +170,23 @@ LabelSelector.prototype.covers = function(selector) {
164170
return true;
165171
};
166172

173+
// Exports the labelSelector as a string in the API format, exports as matchExpressions
174+
LabelSelector.prototype.exportJSON = function() {
175+
var result = {
176+
matchExpressions: []
177+
};
178+
for (var id in this._conjuncts) {
179+
var conjunct = this._conjuncts[id];
180+
var expression = {
181+
key: conjunct.key,
182+
operator: this._REVERSE_OPERATOR_MAP[conjunct.operator],
183+
values: conjunct.values
184+
};
185+
result.matchExpressions.push(expression);
186+
}
187+
return JSON.stringify(result);
188+
};
189+
167190
// We assume label values have no whitespace, commas, parens, etc. based
168191
// on k8s def for label values
169192
LabelSelector.prototype._getStringForConjunct = function(conjunct) {
@@ -201,4 +224,4 @@ LabelSelector.prototype._getIdForConjunct = function(conjunct) {
201224
id += "-" + conjunct.values.join(",");
202225
}
203226
return id;
204-
};
227+
};

0 commit comments

Comments
 (0)